Skip to content

Commit

Permalink
Merge pull request #38082 from nextcloud/allow-wasm-unsafe-eval-in-csp
Browse files Browse the repository at this point in the history
Allow "wasm-unsafe-eval" in CSP
  • Loading branch information
nickvergessen committed Aug 11, 2023
2 parents 833489d + 41f2d91 commit 9fb08f5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/private/Security/CSP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function setEvalScriptAllowed(bool $evalScriptAllowed) {
$this->evalScriptAllowed = $evalScriptAllowed;
}

public function isEvalWasmAllowed(): ?bool {
return $this->evalWasmAllowed;
}

public function setEvalWasmAllowed(bool $evalWasmAllowed): void {
$this->evalWasmAllowed = $evalWasmAllowed;
}

/**
* @return array
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/public/AppFramework/Http/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ContentSecurityPolicy extends EmptyContentSecurityPolicy {
protected $inlineScriptAllowed = false;
/** @var bool Whether eval in JS scripts is allowed */
protected $evalScriptAllowed = false;
/** @var bool Whether WebAssembly compilation is allowed */
protected ?bool $evalWasmAllowed = false;
/** @var bool Whether strict-dynamic should be set */
protected $strictDynamicAllowed = false;
/** @var array Domains from which scripts can get loaded */
Expand Down
18 changes: 17 additions & 1 deletion lib/public/AppFramework/Http/EmptyContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class EmptyContentSecurityPolicy {
* @link https://github.com/owncloud/core/issues/11925
*/
protected $evalScriptAllowed = null;
/** @var bool Whether WebAssembly compilation is allowed */
protected ?bool $evalWasmAllowed = null;
/** @var array Domains from which scripts can get loaded */
protected $allowedScriptDomains = null;
/**
Expand Down Expand Up @@ -116,6 +118,17 @@ public function allowEvalScript($state = true) {
return $this;
}

/**
* Whether WebAssembly compilation is allowed or forbidden
* @param bool $state
* @return $this
* @since 28.0.0
*/
public function allowEvalWasm(bool $state = true) {
$this->evalWasmAllowed = $state;
return $this;
}

/**
* Allows to execute JavaScript files from a specific domain. Use * to
* allow JavaScript from all domains.
Expand Down Expand Up @@ -433,7 +446,7 @@ public function buildPolicy() {
$policy .= "base-uri 'none';";
$policy .= "manifest-src 'self';";

if (!empty($this->allowedScriptDomains) || $this->evalScriptAllowed) {
if (!empty($this->allowedScriptDomains) || $this->evalScriptAllowed || $this->evalWasmAllowed) {
$policy .= 'script-src ';
if (is_string($this->useJsNonce)) {
if ($this->strictDynamicAllowed) {
Expand All @@ -453,6 +466,9 @@ public function buildPolicy() {
if ($this->evalScriptAllowed) {
$policy .= ' \'unsafe-eval\'';
}
if ($this->evalWasmAllowed) {
$policy .= ' \'wasm-unsafe-eval\'';
}
$policy .= ';';
}

Expand Down
2 changes: 2 additions & 0 deletions lib/public/AppFramework/Http/StrictContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class StrictContentSecurityPolicy extends EmptyContentSecurityPolicy {
protected $inlineScriptAllowed = false;
/** @var bool Whether eval in JS scripts is allowed */
protected $evalScriptAllowed = false;
/** @var bool Whether WebAssembly compilation is allowed */
protected ?bool $evalWasmAllowed = false;
/** @var array Domains from which scripts can get loaded */
protected $allowedScriptDomains = [
'\'self\'',
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/AppFramework/Http/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ public function testGetPolicyUnsafeEval() {
$this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
}

public function testGetPolicyUnsafeWasmEval() {
$expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'wasm-unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' data:;connect-src 'self';media-src 'self';frame-ancestors 'self';form-action 'self'";

$this->contentSecurityPolicy->allowEvalWasm(true);
$this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
}

public function testGetPolicyNonce() {
$nonce = 'my-nonce';
$expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'nonce-".base64_encode($nonce) . "';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' data:;connect-src 'self';media-src 'self';frame-ancestors 'self';form-action 'self'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function testGetPolicyScriptAllowEval() {
$this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
}

public function testGetPolicyScriptAllowWasmEval() {
$expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'wasm-unsafe-eval';frame-ancestors 'none'";

$this->contentSecurityPolicy->allowEvalWasm(true);
$this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
}

public function testGetPolicyStyleDomainValid() {
$expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com;frame-ancestors 'none'";

Expand Down

0 comments on commit 9fb08f5

Please sign in to comment.