Skip to content

Commit

Permalink
Merge pull request #11040 from nextcloud/feature/11035/worker-src
Browse files Browse the repository at this point in the history
Add worker-src to CSP
  • Loading branch information
rullzer authored Sep 4, 2018
2 parents 373630c + 8354c50 commit 52012be
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/private/Security/CSP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ public function setAllowedFontDomains($allowedFontDomains) {

/**
* @return array
* @deprecated 15.0.0 use FrameDomains and WorkerSrcDomains
*/
public function getAllowedChildSrcDomains(): array {
return $this->allowedChildSrcDomains;
}

/**
* @param array $allowedChildSrcDomains
* @deprecated 15.0.0 use FrameDomains and WorkerSrcDomains
*/
public function setAllowedChildSrcDomains($allowedChildSrcDomains) {
$this->allowedChildSrcDomains = $allowedChildSrcDomains;
Expand All @@ -213,4 +215,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 = [];
}
33 changes: 33 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 @@ -313,6 +315,7 @@ public function disallowFrameDomain($domain) {
* @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
* @return $this
* @since 8.1.0
* @deprecated 15.0.0 use addAllowedWorkerSrcDomains or addAllowedFrameDomain
*/
public function addAllowedChildSrcDomain($domain) {
$this->allowedChildSrcDomains[] = $domain;
Expand All @@ -325,6 +328,7 @@ public function addAllowedChildSrcDomain($domain) {
* @param string $domain
* @return $this
* @since 8.1.0
* @deprecated 15.0.0 use the WorkerSrcDomains or FrameDomain
*/
public function disallowChildSrcDomain($domain) {
$this->allowedChildSrcDomains = array_diff($this->allowedChildSrcDomains, [$domain]);
Expand Down Expand Up @@ -355,6 +359,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 +467,11 @@ public function buildPolicy() {
$policy .= ';';
}

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

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

0 comments on commit 52012be

Please sign in to comment.