Skip to content

Commit

Permalink
Add workerSrc to CSP
Browse files Browse the repository at this point in the history
Fixes #11035

Since the child-src directive is deprecated (we should kill it at some
point) we need to have the proper worker-src available

Signed-off-by: Roeland Jago Douma <[email protected]>
  • Loading branch information
rullzer committed Sep 4, 2018
1 parent 12a2a75 commit c8fe4b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/private/Security/CSP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,12 @@ public function setAllowedFrameAncestors($allowedFrameAncestors) {
$this->allowedFrameAncestors = $allowedFrameAncestors;
}

public function getAllowedWorkerSrcDomains(): array {
return $this->allowedWorkerSrcDomains;
}

public function setAllowedWorkerSrcDomains(array $allowedWorkerSrcDomains) {
$this->allowedWorkerSrcDomains = $allowedWorkerSrcDomains;
}

}
3 changes: 3 additions & 0 deletions lib/public/AppFramework/Http/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ class ContentSecurityPolicy extends EmptyContentSecurityPolicy {

/** @var array Domains which can embed this Nextcloud instance */
protected $allowedFrameAncestors = [];

/** @var array Domains from which web-workers can be loaded */
protected $allowedWorkerSrcDomains = [];
}
31 changes: 31 additions & 0 deletions lib/public/AppFramework/Http/EmptyContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class EmptyContentSecurityPolicy {
protected $allowedChildSrcDomains = null;
/** @var array Domains which can embed this Nextcloud instance */
protected $allowedFrameAncestors = null;
/** @var array Domains from which web-workers can be loaded */
protected $allowedWorkerSrcDomains = null;

/**
* Whether inline JavaScript snippets are allowed or forbidden
Expand Down Expand Up @@ -355,6 +357,30 @@ public function disallowFrameAncestorDomain($domain) {
return $this;
}

/**
* Domain from which workers can be loaded
*
* @param string $domain
* @return $this
* @since 15.0.0
*/
public function addAllowedWorkerSrcDomain(string $domain) {
$this->allowedWorkerSrcDomains[] = $domain;
return $this;
}

/**
* Remove domain from which workers can be loaded
*
* @param string $domain
* @return $this
* @since 15.0.0
*/
public function disallowWorkerSrcDomain(string $domain) {
$this->allowedWorkerSrcDomains = array_diff($this->allowedWorkerSrcDomains, [$domain]);
return $this;
}

/**
* Get the generated Content-Security-Policy as a string
* @return string
Expand Down Expand Up @@ -439,6 +465,11 @@ public function buildPolicy() {
$policy .= ';';
}

if (!empty($this->allowedWorkerSrcDomains)) {
$policy .= 'worker-src ' . implode(' ', $this->allowedWorkerSrcDomains);
$policy .= ';';
}

return rtrim($policy, ';');
}
}

0 comments on commit c8fe4b4

Please sign in to comment.