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

new_audit: has HSTS #16257

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

sebastian9er
Copy link

Summary

Adding a new audit to Ligththouse, which detects deviations from an optimal HSTS header deployment.

Part of a larger change to introduce more similar header deployments.

Link to documentation is pending internal discussions (@adamraine FYI, either of us can update the Link here once we have the blog post done).

@sebastian9er sebastian9er requested a review from a team as a code owner November 19, 2024 09:32
@sebastian9er sebastian9er requested review from adamraine and removed request for a team November 19, 2024 09:32
@adamraine adamraine changed the title Adding the HSTS audit to Lighthouse new_audit: HSTS policy check Nov 19, 2024
*/
const expectations = {
lhr: {
requestedUrl: 'https://hstspreload.org/',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be worried about the HSTS policy changing on this site?

Not a huge deal, but it could cause the tests to start failing if something changes on this site.

static get meta() {
return {
id: 'has-hsts',
scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this as INFORMATIVE SGTM. It should have similar severity to the CSP XSS audit.

// Sanitize the header value / directives.
hstsHeaders = hstsHeaders.map(v => v.toLowerCase().replace(/\s/g, ''));

return {hstsHeaders};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can just return hstsHeaders. No need to return in a wrapper object.

// If there is a directive that's not an official HSTS directive.
if (!allowedDirectives.includes(actualDirective)) {
// max-age=<number> would always trigger.
if (actualDirective.includes('max-age')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we just add this to the condition above with an || operator

@@ -541,6 +542,7 @@ const defaultConfig = {
{id: 'geolocation-on-start', weight: 1, group: 'best-practices-trust-safety'},
{id: 'notification-on-start', weight: 1, group: 'best-practices-trust-safety'},
{id: 'csp-xss', weight: 0, group: 'best-practices-trust-safety'},
{id: 'has-hsts', weight: 0, group: 'best-practices-trust-safety'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this until documentation is published? This will keep the audit in the JSON result but hide it for anyone looking at the report UI.

Suggested change
{id: 'has-hsts', weight: 0, group: 'best-practices-trust-safety'},
{id: 'has-hsts', weight: 0},

severity: {
i18nId: "core/lib/i18n/i18n.js | itemSeverityMedium",
values: undefined,
formattedDefault: 'Medium'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pro tip: we have a custom assertion check for display strings (example)

So this could be expect(results.details.items[0].severity).toBeDisplayString('Medium'). Then you can drop the i18nId/values stuff from expectations.

f.directive, f.description,
str_(i18n.UIStrings.itemSeverityLow))),
];
return {score: violations.length ? 0 : 1, results};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also fail if syntax.length > 1 IMO

finalDisplayedUrl: 'https://developer.mozilla.org/en-US/',
audits: {
'has-hsts': {
score: 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the HSTS header issues? Could we add some assertions for that?

@adamraine adamraine changed the title new_audit: HSTS policy check new_audit: has HSTS Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants