Skip to content

Commit 2ac34b5

Browse files
CSP Evaluator Teamddworken
CSP Evaluator Team
authored andcommitted
No public description
PiperOrigin-RevId: 650314356
1 parent f72d89a commit 2ac34b5

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

checks/security_checks.ts

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export function checkScriptUnsafeInline(effectiveCsp: Csp): Finding[] {
7070
'and event handlers.',
7171
Severity.HIGH, directive, Keyword.UNSAFE_INLINE));
7272
}
73+
if (values.includes(Keyword.UNSAFE_HASHES)) {
74+
violations.push(new Finding(
75+
Type.SCRIPT_UNSAFE_HASHES,
76+
`'unsafe-hashes', while safer than 'unsafe-inline', allows the execution of unsafe in-page scripts and event handlers as long as their hashes appear in the CSP. Please refactor them to no longer use inline scripts if possible.`,
77+
Severity.MEDIUM_MAYBE, directive, Keyword.UNSAFE_HASHES));
78+
}
7379
}
7480

7581
return violations;

checks/security_checks_test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ describe('Test security checks', () => {
5454
expect(violations.length).toBe(1);
5555
});
5656

57+
it('CheckScriptUnsafeHashesInScriptSrc', () => {
58+
const test =
59+
'script-src \'unsafe-hashes\' \'sha256-1DCfk1NYWuHMfoobarfoobar=\'';
60+
61+
const violations = checkCsp(test, securityChecks.checkScriptUnsafeInline);
62+
expect(violations.length).toBe(1);
63+
expect(violations[0].severity).toBe(Severity.MEDIUM_MAYBE);
64+
});
65+
5766
it('CheckScriptUnsafeInlineInDefaultSrcAndNotInScriptSrc', () => {
5867
const test =
5968
'default-src \'unsafe-inline\'; script-src https:; script-src-attr https:; script-src-elem https:';

finding.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export enum Type {
8686
INVALID_KEYWORD,
8787
NONCE_CHARSET = 106,
8888

89-
// Security cheks
89+
// Security checks
9090
MISSING_DIRECTIVES = 300,
9191
SCRIPT_UNSAFE_INLINE,
9292
SCRIPT_UNSAFE_EVAL,
@@ -104,6 +104,7 @@ export enum Type {
104104
X_FRAME_OPTIONS_OBSOLETED,
105105
STYLE_UNSAFE_INLINE,
106106
STATIC_NONCE,
107+
SCRIPT_UNSAFE_HASHES,
107108

108109
// Strict dynamic and backward compatibility checks
109110
STRICT_DYNAMIC = 400,

parser_test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe('Test parser', () => {
2626
it('CspParser', () => {
2727
const validCsp = // Test policy with different features from CSP2.
2828
'default-src \'none\';' +
29-
'script-src \'nonce-unsafefoobar\' \'unsafe-eval\' \'unsafe-inline\' \n' +
30-
'https://example.com/foo.js foo.bar; ' +
29+
'script-src \'nonce-unsafefoobar\' \'unsafe-eval\' \'unsafe-hashes\' \'unsafe-inline\' \n' +
30+
'https://example.com/foo.js foo.bar \'sha256-1DCfk1NYWuHMfoobarfoobar=\';' +
3131
'script-src-elem \'self\' \'unsafe-inline\' https://apis.google.com https://www.googletagmanager.com https://www.google-analytics.com https://wchat.freshchat.com;' +
3232
'object-src \'none\';' +
3333
'img-src \'self\' https: data: blob:;' +
@@ -57,8 +57,9 @@ describe('Test parser', () => {
5757
parsedCsp.directives['default-src'] as string[]));
5858

5959
expect([
60-
'\'nonce-unsafefoobar\'', '\'unsafe-eval\'', '\'unsafe-inline\'',
61-
'https://example.com/foo.js', 'foo.bar'
60+
'\'nonce-unsafefoobar\'', '\'unsafe-eval\'', '\'unsafe-hashes\'',
61+
'\'unsafe-inline\'', 'https://example.com/foo.js', 'foo.bar',
62+
'\'sha256-1DCfk1NYWuHMfoobarfoobar=\''
6263
])
6364
.toEqual(jasmine.arrayWithExactContents(
6465
parsedCsp.directives['script-src'] as string[]));

0 commit comments

Comments
 (0)