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

core(csp-xss): prevent meta warning if header CSPs are secure #14490

Merged
merged 3 commits into from
Nov 4, 2022
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: 7 additions & 3 deletions core/audits/csp-xss.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const UIStrings = {
noCsp: 'No CSP found in enforcement mode',
/** Message shown when one or more CSPs are defined in a <meta> tag. Shown in a table with a list of other CSP bypasses and warnings. "CSP" stands for "Content Security Policy". "CSP" and "HTTP" do not need to be translated. */
metaTagMessage: 'The page contains a CSP defined in a <meta> tag. ' +
'Consider defining the CSP in an HTTP header if you can.',
'Consider moving the CSP to an HTTP header or ' +
'defining another strict CSP in an HTTP header.',
/** Label for a column in a data table; entries will be a directive of a CSP. "CSP" stands for "Content Security Policy". */
columnDirective: 'Directive',
/** Label for a column in a data table; entries will be the severity of an issue with the CSP. "CSP" stands for "Content Security Policy". */
Expand Down Expand Up @@ -139,8 +140,11 @@ class CspXss extends Audit {
...warnings.map(f => this.findingToTableItem(f, str_(i18n.UIStrings.itemSeverityMedium))),
];

// Add extra warning for a CSP defined in a meta tag.
if (cspMetaTags.length) {
const headerOnlyBypasses = evaluateRawCspsForXss(cspHeaders).bypasses;
const headerOnlyIsInsecure = headerOnlyBypasses.length > 0 || cspHeaders.length === 0;

// Warn if there is a meta tag CSP and the header CSPs are not strict enough on their own.
if (cspMetaTags.length > 0 && headerOnlyIsInsecure) {
adamraine marked this conversation as resolved.
Show resolved Hide resolved
results.push({
severity: str_(i18n.UIStrings.itemSeverityMedium),
description: str_(UIStrings.metaTagMessage),
Expand Down
17 changes: 15 additions & 2 deletions core/test/audits/csp-xss-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const STATIC_RESULTS = {
description: {
formattedDefault:
'The page contains a CSP defined in a <meta> tag. ' +
'Consider defining the CSP in an HTTP header if you can.',
'Consider moving the CSP to an HTTP header or ' +
'defining another strict CSP in an HTTP header.',
},
directive: undefined,
},
Expand Down Expand Up @@ -318,13 +319,25 @@ describe('constructResults', () => {
});

it('adds item for CSP in meta tag', () => {
const {score, results} = CspXss.constructResults([], [
const {score, results} = CspXss.constructResults([
`script-src https://example.com; object-src 'none'`,
], [
`script-src 'none'; object-src 'none'; report-uri https://example.com`,
]);
expect(score).toEqual(1);
expect(results).toMatchObject([STATIC_RESULTS.metaTag]);
});

it('does not add item for a meta CSP if header CSPs are secure', () => {
const {score, results} = CspXss.constructResults([
`script-src 'nonce-00000000' 'unsafe-inline'; object-src 'none'; base-uri 'none'`,
], [
`script-src 'none'; object-src 'none'; report-uri https://example.com`,
]);
expect(score).toEqual(1);
expect(results).toMatchObject([]);
});

it('single item for no CSP', () => {
const {score, results} = CspXss.constructResults([], []);
expect(score).toEqual(0);
Expand Down
2 changes: 1 addition & 1 deletion shared/localization/locales/en-US.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion shared/localization/locales/en-XL.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.