Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add worker-src to CSP #11040

Merged
merged 2 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, ';');
}
}