Skip to content

Commit b1f9599

Browse files
committed
Fix missing comma and add more tests
PiperOrigin-RevId: 467236522
1 parent 9a4b315 commit b1f9599

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

csp.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ export enum Keyword {
213213
STRICT_DYNAMIC = '\'strict-dynamic\'',
214214
UNSAFE_HASHED_ATTRIBUTES = '\'unsafe-hashed-attributes\'',
215215
UNSAFE_HASHES = '\'unsafe-hashes\'',
216-
REPORT_SAMPLE = '\'report-sample\''
217-
BLOCK = '\'block\''
218-
ALLOW = '\'allow\''
216+
REPORT_SAMPLE = '\'report-sample\'',
217+
BLOCK = '\'block\'',
218+
ALLOW = '\'allow\'',
219219
}
220220

221221

222222
/**
223223
* CSP directive source keywords.
224224
*/
225225
export enum TrustedTypesSink {
226-
SCRIPT = '\'script\''
226+
SCRIPT = '\'script\'',
227227
}
228228

229229

@@ -278,7 +278,7 @@ export enum Directive {
278278
REQUIRE_SRI_FOR = 'require-sri-for',
279279
TRUSTED_TYPES = 'trusted-types',
280280
// https://github.com/WICG/trusted-types
281-
REQUIRE_TRUSTED_TYPES_FOR = 'require-trusted-types-for'
281+
REQUIRE_TRUSTED_TYPES_FOR = 'require-trusted-types-for',
282282
WEBRTC = 'webrtc',
283283
}
284284

csp_test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,20 @@ describe('Test Csp', () => {
221221
expect(isHash('\'asdfASDF=\'', true)).toBeFalse();
222222
expect(isHash('example.com', true)).toBeFalse();
223223
});
224+
225+
it('ParseNavigateTo', () => {
226+
const testCsp = 'navigate-to \'self\'; script-src \'nonce-foo\'';
227+
const parsed = (new CspParser(testCsp)).csp;
228+
229+
expect(parsed.policyHasStrictDynamic()).toBeFalse();
230+
expect(parsed.policyHasScriptNonces()).toBeTrue();
231+
});
232+
233+
it('ParseWebRtc', () => {
234+
const testCsp = 'web-rtc \'allow\'; script-src \'nonce-foo\'';
235+
const parsed = (new CspParser(testCsp)).csp;
236+
237+
expect(parsed.policyHasStrictDynamic()).toBeFalse();
238+
expect(parsed.policyHasScriptNonces()).toBeTrue();
239+
});
224240
});

utils.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ export function getSchemeFreeUrl(url: string): string {
3939
* URLs and wildcards (aka `*`) in hostnames
4040
*/
4141
export function getHostname(url: string): string {
42-
const hostname =
43-
new URL(
44-
'https://' +
45-
getSchemeFreeUrl(url)
46-
.replace(':*', '') // Remove wildcard port
47-
.replace('*', 'wildcard_placeholder'))
48-
.hostname.replace('wildcard_placeholder', '*');
42+
const hostname = new URL(
43+
'https://' +
44+
getSchemeFreeUrl(url)
45+
.replace(':*', '') // Remove wildcard port
46+
.replace('*', 'wildcard_placeholder'))
47+
.hostname.replace('wildcard_placeholder', '*');
4948

5049
// Some browsers strip the brackets from IPv6 addresses when you access the
5150
// hostname. If the scheme free url starts with something that vaguely looks
@@ -81,8 +80,8 @@ export function matchWildcardUrls(
8180
// have to worry about this detail.
8281
const cspUrl =
8382
new URL(setScheme(cspUrlString
84-
.replace(':*', '') // Remove wildcard port
85-
.replace('*', 'wildcard_placeholder')));
83+
.replace(':*', '') // Remove wildcard port
84+
.replace('*', 'wildcard_placeholder')));
8685
const listOfUrls = listOfUrlStrings.map(u => new URL(setScheme(u)));
8786
const host = cspUrl.hostname.toLowerCase();
8887
const hostHasWildcard = host.startsWith('wildcard_placeholder.');

0 commit comments

Comments
 (0)