Skip to content

Commit

Permalink
fix wildcards being quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Apr 12, 2023
1 parent 3902b7a commit bdc813c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/csp.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type HttpDelineators = typeof httpDelineators[number];
type UriPath = `${HttpDelineators}${string}`

// Base Source Directives
export const baseSources = ['self', 'unsafe-eval', 'unsafe-hashes', 'unsafe-inline', 'none'] as const;
export const baseSources = ['self', 'unsafe-eval', 'unsafe-hashes', 'unsafe-inline', 'none', '*'] as const;
type BaseSources = typeof baseSources[number]

// Combined all source directives
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const PolicySet = new Set([
...sandboxDirectives,
]);
function isQuotedPolicy (policy: string): boolean {
if (policy === '*') return false;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (PolicySet.has(policy)) return true;
Expand Down
13 changes: 13 additions & 0 deletions tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,18 @@ describe('new CspDirectives()',() => {
}
expect(inst.getHeaders).toThrowError();
});

it('supports wildcards',() => {
const csp: Directives = {
'style-src': ['*', 'data:'],
};
const inst = new CspDirectives(csp, [], csp);
expect(inst.getHeaders()).toStrictEqual({
'Content-Security-Policy-Report-Only': 'style-src * data:;',
'Content-Security-Policy': "style-src * data:;",
'Report-To': '',
'Referrer-Policy': 'strict-origin-when-cross-origin',
});
});
});
});

0 comments on commit bdc813c

Please sign in to comment.