-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
new_audit: csp-inline #14878
Closed
Closed
new_audit: csp-inline #14878
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license Copyright 2023 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
import {Audit} from './audit.js'; | ||
import * as i18n from '../lib/i18n/i18n.js'; | ||
|
||
const UIStrings = { | ||
/** Title of a Lighthouse audit that advises users to avoid putting a CSP in an inline html meta tag. This descriptive title is shown to users when a CSP is not found in the page's html document. */ | ||
title: 'Does not define any CSPs in inline `<meta>` tags', | ||
/** Title of a Lighthouse audit that advises users to avoid putting a CSP in an inline html meta tag. This descriptive title is shown to users when a CSP is found in the page's html document. */ | ||
failureTitle: 'Defines a CSP in an inline `<meta>` tag', | ||
/** Description of a Lighthouse audit that advises users to avoid putting a CSP in an inline html meta tag. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */ | ||
description: 'A CSP defined in an inline `<meta>` tag will delay the preload scanner from ' + | ||
'loading resources early. Consider defining all CSPs in http headers if you can. ' + | ||
'[Learn more about defining a CSP in an inline meta tag]()', | ||
}; | ||
|
||
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings); | ||
|
||
const INLINE_CSP_REGEX = /http-equiv="content-security-policy"/i; | ||
|
||
class CSPInline extends Audit { | ||
/** | ||
* @return {LH.Audit.Meta} | ||
*/ | ||
static get meta() { | ||
return { | ||
id: 'csp-inline', | ||
title: str_(UIStrings.title), | ||
failureTitle: str_(UIStrings.failureTitle), | ||
description: str_(UIStrings.description), | ||
requiredArtifacts: ['MainDocumentContent', 'MetaElements'], | ||
supportedModes: ['navigation'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts} artifacts | ||
* @return {LH.Audit.Product} | ||
*/ | ||
static audit(artifacts) { | ||
const hasCspMetaTag = !!artifacts.MetaElements.find(m => { | ||
return m.httpEquiv && m.httpEquiv.toLowerCase() === 'content-security-policy'; | ||
}); | ||
|
||
// The page could add a CSP meta tag in a script. | ||
// This check doesn't cover all edge cases but should be good enough for our use case. | ||
const hasInlineCsp = INLINE_CSP_REGEX.test(artifacts.MainDocumentContent); | ||
const hasInlineCspMetaTag = hasCspMetaTag && hasInlineCsp; | ||
|
||
return { | ||
score: Number(!hasInlineCspMetaTag), | ||
}; | ||
} | ||
} | ||
|
||
export default CSPInline; | ||
export {UIStrings}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @license Copyright 2023 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
import CSPInline from '../../audits/csp-inline.js'; | ||
|
||
const CSP_HTML = ` | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'"/> | ||
</head> | ||
<body>hi</body> | ||
</html> | ||
`; | ||
|
||
const NO_CSP_HTML = ` | ||
<html> | ||
<body>hi</body> | ||
</html> | ||
`; | ||
|
||
const CSP_META_ELEMENT = { | ||
httpEquiv: 'Content-Security-Policy', | ||
}; | ||
|
||
describe('CSP inline audit', () => { | ||
it('fails when HTML contains a CSP in an inline meta tag', async () => { | ||
const auditResult = await CSPInline.audit({ | ||
MetaElements: [CSP_META_ELEMENT], | ||
MainDocumentContent: CSP_HTML, | ||
}); | ||
expect(auditResult.score).toEqual(0); | ||
}); | ||
|
||
it('passes if a CSP meta tag was added dynamically', async () => { | ||
const auditResult = await CSPInline.audit({ | ||
MetaElements: [CSP_META_ELEMENT], | ||
MainDocumentContent: NO_CSP_HTML, | ||
}); | ||
expect(auditResult.score).toEqual(1); | ||
}); | ||
|
||
it('passes if there was no CSP meta tag', async () => { | ||
const auditResult = await CSPInline.audit({ | ||
MetaElements: [], | ||
MainDocumentContent: NO_CSP_HTML, | ||
}); | ||
expect(auditResult.score).toEqual(1); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: url