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

Do not add signatures if unsafe-inline is enabled #335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ContentSecurityPolicy/DirectiveSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public function buildHeaderValue(Request $request, ?array $signatures = null): s
// this is the recommended way to deal with this.
if (false === strpos($value, '\'unsafe-inline\'') && $this->level1Fallback) {
$policy[] = $name.' '.$value.' \'unsafe-inline\' '.$signatures[$name];
// Do not add any signatures if 'unsafe-inline' is allowed anyway
} elseif (false !== strpos($value, '\'unsafe-inline\'')) {
$policy[] = $name.' '.$value;
Comment on lines +143 to +145
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is risky IMO, because if you use signatures then unsafe-inline will be ignored by modern browsers supporting CSP2. It's merely there as a fallback.

Now with your change suddenly having unsafe-inline in an existing config would silently break/remove signatures.

I think the more appropriate way is to disable the hash functionality if you are not interested in outputting hashes.

} else {
$policy[] = $name.' '.$value.' '.$signatures[$name];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ContentSecurityPolicy/DirectiveSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public function provideConfigAndSignatures(): array
{
return [
[
'default-src \'self\'; script-src \'self\' \'unsafe-inline\' \'sha-1\'; style-src \'self\' \'unsafe-inline\' \'sha2\'',
'default-src \'self\'; script-src \'self\' \'unsafe-inline\'; style-src \'self\' \'unsafe-inline\' \'sha2\'',
[
'enforce' => [
'level1_fallback' => true,
Expand Down
4 changes: 2 additions & 2 deletions tests/Listener/ContentSecurityPolicyListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public function testDefaultWithSignatures(): void
);
}

public function testEvenWithUnsafeInlineItAppliesSignature(): void
public function testDoesNotApplySignatureWithUnsafeInline(): void
{
$listener = $this->buildSimpleListener(['default-src' => "default.example.org 'self'", 'script-src' => "'self' 'unsafe-inline'"]);
$response = $this->callListener($listener, '/', true, 'text/html', ['signatures' => ['script-src' => ['sha-1']]]);

$this->assertSame(
"default-src default.example.org 'self'; script-src 'self' 'unsafe-inline' 'sha-1'",
"default-src default.example.org 'self'; script-src 'self' 'unsafe-inline'",
$response->headers->get('Content-Security-Policy')
);
}
Expand Down