import { Expectation } from "../types.js"; /** * @type {Map} */ export const GRADE_CHART = new Map([ [100, "A+"], [95, "A"], [90, "A"], [85, "A-"], [80, "B+"], [75, "B"], [70, "B"], [65, "B-"], [60, "C+"], [55, "C"], [50, "C"], [45, "C-"], [40, "D+"], [35, "D"], [30, "D"], [25, "D-"], [20, "F"], [15, "F"], [10, "F"], [5, "F"], [0, "F"], ]); export const MINIMUM_SCORE_FOR_EXTRA_CREDIT = 90; /** * @type {Set} */ export const GRADES = new Set(GRADE_CHART.values()); /** @type {import("../types.js").StringMap} */ export const TEST_TITLES = { "cross-origin-resource-sharing": "Cross Origin Resource Sharing (CORS)", "cross-origin-opener-policy": "Cross Origin Opener Policy", "cross-origin-embedder-policy": "Cross Origin Embedder Policy", "cross-origin-resource-policy": "Cross Origin Resource Policy", "content-security-policy": "Content Security Policy (CSP)", redirection: "Redirection", "referrer-policy": "Referrer Policy", "strict-transport-security": "Strict Transport Security (HSTS)", "subresource-integrity": "Subresource Integrity", "x-content-type-options": "X-Content-Type-Options", "x-frame-options": "X-Frame-Options", cookies: "Cookies", }; export const TEST_TOPIC_LINKS = new Map([ [ "content-security-policy", "/en-US/docs/Web/Security/Practical_implementation_guides/CSP", ], [ "cookies", "/en-US/docs/Web/Security/Practical_implementation_guides/Cookies", ], [ "cross-origin-embedder-policy", "/en-US/docs/Web/HTTP/Reference/Headers/Cross-Origin-Embedder-Policy", ], [ "cross-origin-opener-policy", "/en-US/docs/Web/HTTP/Reference/Headers/Cross-Origin-Opener-Policy", ], [ "cross-origin-resource-policy", "/en-US/docs/Web/Security/Practical_implementation_guides/CORP", ], [ "cross-origin-resource-sharing", "/en-US/docs/Web/Security/Practical_implementation_guides/CORS", ], [ "redirection", "/en-US/docs/Web/Security/Practical_implementation_guides/TLS#http_redirection", ], [ "referrer-policy", "/en-US/docs/Web/Security/Practical_implementation_guides/Referrer_policy", ], [ "strict-transport-security", "/en-US/docs/Web/Security/Practical_implementation_guides/TLS#http_strict_transport_security_implementation", ], ["subresource-integrity", "/en-US/docs/Web/Security/Subresource_Integrity"], [ "x-content-type-options", "/en-US/docs/Web/Security/Practical_implementation_guides/MIME_types", ], ["x-frame-options", "/en-US/docs/Web/Security/Attacks/Clickjacking"], // inactive, for historic records ["contribute", "/en-US/"], [ "x-xss-protection", "/en-US/docs/Web/HTTP/Reference/Headers/X-XSS-Protection", ], ]); /** * @type {Map} */ export const SCORE_TABLE = new Map([ // CSP [ Expectation.CspImplementedWithNoUnsafeDefaultSrcNone, { description: `

Content Security Policy (CSP) implemented with default-src 'none', no 'unsafe' and form-action is set to 'none' or 'self'

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CspImplementedWithNoUnsafe, { description: `

Content Security Policy (CSP) implemented without 'unsafe-inline' or 'unsafe-eval'

`, modifier: 5, recommendation: `

Set default-src to 'none'

`, }, ], [ Expectation.CspImplementedWithUnsafeInlineInStyleSrcOnly, { description: `

Content Security Policy (CSP) implemented with unsafe sources inside style-src. This includes 'unsafe-inline', data: or overly broad sources such as https. 'form-action' is set to 'self', 'none' or 'specific source'

`, modifier: 0, recommendation: `

Lock down style-src directive, removing 'unsafe-inline', data: and broad sources.

`, }, ], [ Expectation.CspImplementedWithInsecureSchemeInPassiveContentOnly, { description: `

Content Security Policy (CSP) implemented, but secure site allows images or media to be loaded over HTTP

`, modifier: -10, recommendation: `

Load images and media over HTTPS and prevent their loading over HTTP with the img-src and media-src or default-src directives

`, }, ], [ Expectation.CspImplementedWithUnsafeEval, { description: `

Content Security Policy (CSP) implemented, but allows 'unsafe-eval' and allows forms to be submitted to any source.

`, modifier: -10, recommendation: `

First remove the use of eval() from the codebase and once completed, remove 'unsafe-eval' from the CSP header.

`, }, ], [ Expectation.CspImplementedWithUnsafeInline, { description: `

Content Security Policy (CSP) implemented unsafely. This includes 'unsafe-inline' or data: inside script-src, overly broad sources such as https: inside object-src or script-src, or not restricting the sources for object-src or script-src.

`, modifier: -20, recommendation: `

Remove unsafe-inline and data: from script-src, overly broad sources from object-src and script-src, and ensure object-src and script-src are set.

`, }, ], [ Expectation.CspImplementedWithInsecureScheme, { description: `

Content Security Policy (CSP) implemented, but secure site allows resources to be loaded over HTTP

`, modifier: -20, recommendation: `

Load resources over HTTPS and remove any HTTP sources from your CSP.

`, }, ], [ Expectation.CspHeaderInvalid, { description: `

Content Security Policy (CSP) header cannot be parsed successfully

`, modifier: -25, recommendation: `

Check your CSP syntax with the CSP Evaluator

.`, }, ], [ Expectation.CspNotImplemented, { description: `

Content Security Policy (CSP) header not implemented

`, modifier: -25, recommendation: `

Implement one, see MDN's Content Security Policy (CSP) documentation.

`, }, ], [ Expectation.CspNotImplementedButReportingEnabled, { description: `

Content Security Policy (CSP) reporting implemented only, with Content-Security-Policy-Report-Only header.

`, modifier: -25, recommendation: `

Implement an enforced policy, see MDN's Content Security Policy (CSP) documentation.

`, }, ], [ Expectation.CspImplementedButDuplicateDirectives, { description: `

Content Security Policy (CSP) implemented, but contains duplicate directives.

`, modifier: 0, recommendation: `

Remove duplicate directives from the CSP

`, }, ], // Cookies [ Expectation.CookiesSecureWithHttponlySessionsAndSamesite, { description: `

All cookies use the Secure flag, session cookies use the HttpOnly flag, and cross-origin restrictions are in place via the SameSite flag.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.CookiesSecureWithHttponlySessions, { description: `

All cookies use the Secure flag and all session cookies use the HttpOnly flag.

`, modifier: 0, recommendation: `

Use SameSite.

`, }, ], [ Expectation.CookiesNotFound, { description: `

No cookies detected

`, modifier: 0, recommendation: ``, }, ], [ Expectation.CookiesWithoutSecureFlagButProtectedByHsts, { description: `

Cookies set without using the Secure flag, but transmission over HTTP prevented by HSTS.

`, modifier: -5, recommendation: `

Use Secure flag.

`, }, ], [ Expectation.CookiesSessionWithoutSecureFlagButProtectedByHsts, { description: `

Session cookie set without the Secure flag, but transmission over HTTP prevented by HSTS.

`, modifier: -10, recommendation: `

Use Secure flag.

`, }, ], [ Expectation.CookiesWithoutSecureFlag, { description: `

Cookies set without using the Secure flag or set over HTTP.

`, modifier: -20, recommendation: `

Use Secure flag and set up HSTS.

`, }, ], [ Expectation.CookiesSamesiteFlagInvalid, { description: `

Cookies use SameSite flag, but set to something other than Strict or Lax

`, modifier: -20, recommendation: `

Use SameSite Strict or Lax.

`, }, ], [ Expectation.CookiesAnticsrfWithoutSamesiteFlag, { description: `

Anti-CSRF tokens set without using the SameSite flag.

`, modifier: -20, recommendation: `

Use SameSite Strict or Lax.

`, }, ], [ Expectation.CookiesSessionWithoutHttponlyFlag, { description: `

Session cookie set without using the HttpOnly flag.

`, modifier: -30, recommendation: `

Use HttpOnly

`, }, ], [ Expectation.CookiesSessionWithoutSecureFlag, { description: "Session cookie set without using the Secure flag or set over HTTP.", modifier: -40, recommendation: `

Use Secure flag and set up HSTS.

`, }, ], // CORS [ Expectation.CrossOriginResourceSharingNotImplemented, { description: `

Content is not visible via cross-origin resource sharing (CORS) files or headers.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.CrossOriginResourceSharingImplementedWithPublicAccess, { description: `

Public content is visible via cross-origin resource sharing (CORS) Access-Control-Allow-Origin header.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.CrossOriginResourceSharingImplementedWithRestrictedAccess, { description: `

Content is visible via cross-origin resource sharing (CORS) files or headers, but is restricted to specific domains.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.CrossOriginResourceSharingImplementedWithUniversalAccess, { description: `

Content is visible via cross-origin resource sharing (CORS) file or headers, and credentials can be sent. Your site could be vulnerable to CSRF attacks.

`, modifier: -50, recommendation: `

If credentialed access is required from specific origins, ensure Access-Control-Allow-Origin can only be set to those origins, rather than reflecting the Origin header. If public non-credentialed access is required, set Access-Control-Allow-Origin to * and omit the Access-Control-Allow-Credentials header. Otherwise, omit both headers.

`, }, ], // Redirection [ Expectation.RedirectionAllRedirectsPreloaded, { description: `

All hosts redirected to are in the HTTP Strict Transport Security (HSTS) preload list.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.RedirectionToHttps, { description: `

Initial redirection is to HTTPS on same host, final destination is HTTPS

`, modifier: 0, recommendation: ``, }, ], [ Expectation.RedirectionNotNeededNoHttp, { description: `

Not able to connect via HTTP, so no redirection necessary.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.RedirectionOffHostFromHttp, { description: `

Initial redirection from HTTP to HTTPS is to a different host, preventing HSTS.

`, modifier: -5, recommendation: `

HSTS headers aren't recognized when set over HTTP, so redirect to the same host on HTTPS first, then redirect to the final host.

`, }, ], [ Expectation.RedirectionNotToHttpsOnInitialRedirection, { description: `

Redirects to HTTPS eventually, but initial redirection is to another HTTP URL.

`, modifier: -10, recommendation: `

Redirect to the same host on HTTPS first, then redirect to the final host on HTTPS.

`, }, ], [ Expectation.RedirectionNotToHttps, { description: `

Redirects, but final destination is not an HTTPS URL.

`, modifier: -20, recommendation: `

Redirect to the same host on HTTPS first, then redirect to the final host on HTTPS.

`, }, ], [ Expectation.RedirectionMissing, { description: `

Does not redirect to an HTTPS site.

`, modifier: -20, recommendation: `

Redirect to the same host on HTTPS first, then redirect to the final host on HTTPS.

`, }, ], [ Expectation.RedirectionInvalidCert, { description: `

Invalid certificate chain encountered during redirection.

`, modifier: -20, recommendation: `

Install a valid TLS certificate on the server. Let's Encrypt is a good choice, as are certificates managed by your cloud provider or commercially sold ones.

`, }, ], // Referrer Policy [ Expectation.ReferrerPolicyPrivate, { description: `

Referrer-Policy header set to no-referrer, same-origin, strict-origin or strict-origin-when-cross-origin.

`, modifier: 5, recommendation: `

None

`, }, ], [ Expectation.ReferrerPolicyNotImplemented, { description: `

Referrer-Policy header not implemented.

`, modifier: 0, recommendation: `

Set to strict-origin-when-cross-origin at a minimum.

`, }, ], [ Expectation.ReferrerPolicyUnsafe, { description: `

Referrer-Policy header set unsafely to origin, origin-when-cross-origin, unsafe-url or no-referrer-when-downgrade.

`, modifier: -5, recommendation: `

Set to strict-origin-when-cross-origin at a minimum

`, }, ], [ Expectation.ReferrerPolicyHeaderInvalid, { description: `

Referrer-Policy header cannot be recognized.

`, modifier: -5, recommendation: `

Set to strict-origin-when-cross-origin at a minimum

`, }, ], // HSTS [ Expectation.HstsPreloaded, { description: `

Preloaded via the HTTP Strict Transport Security (HSTS) preloading process.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.HstsImplementedMaxAgeAtLeastSixMonths, { description: `

Strict-Transport-Security header set to a minimum of six months (15768000).

`, modifier: 0, recommendation: `

Consider preloading: this requires adding the preload and includeSubDomains directives and setting max-age to at least 31536000 (1 year), and submitting your site to https://hstspreload.org/.

`, }, ], [ Expectation.HstsImplementedMaxAgeLessThanSixMonths, { description: `

Strict-Transport-Security header set to less than six months (15768000).

`, modifier: -10, recommendation: `

Increase HSTS period.

`, }, ], [ Expectation.HstsNotImplemented, { description: `

Strict-Transport-Security header not implemented.

`, modifier: -20, recommendation: `

Add HSTS. Consider rolling out with shorter periods first (as suggested on https://hstspreload.org/).

`, }, ], [ Expectation.HstsHeaderInvalid, { description: `

Strict-Transport-Security header cannot be recognized

`, modifier: -20, recommendation: `

Read MDN's documentation on HTTP Strict Transport Security.

`, }, ], [ Expectation.HstsNotImplementedNoHttps, { description: `

Strict-Transport-Security header cannot be set for sites not available over HTTPS.

`, modifier: -20, recommendation: `

Make your site available over HTTPS. Let's Encrypt docs are a good starting point.

`, }, ], [ Expectation.HstsInvalidCert, { description: `

Strict-Transport-Security header cannot be set, as site contains an invalid certificate chain.

`, modifier: -20, recommendation: `

HSTS can only work with a valid TLS certificate on the server. Let's Encrypt is a good choice, as are certificates managed by your cloud provider or commercially sold ones.

`, }, ], // SRI [ Expectation.SriImplementedAndAllScriptsLoadedSecurely, { description: `

Subresource Integrity (SRI) is implemented and all scripts are loaded from a similar origin.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.SriImplementedAndExternalScriptsLoadedSecurely, { description: `

Subresource Integrity (SRI) is implemented and all scripts are loaded securely.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.SriNotImplementedResponseNotHtml, { description: `

Subresource Integrity (SRI) is only needed for HTML resources.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.SriNotImplementedButNoScriptsLoaded, { description: `

Subresource Integrity (SRI) is not needed since site contains no script tags.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin, { description: `

Subresource Integrity (SRI) not implemented, but all scripts are loaded from a similar origin.

`, modifier: 0, recommendation: `

Add SRI for bonus points.

`, }, ], [ Expectation.SriNotImplementedButExternalScriptsLoadedSecurely, { description: `

Subresource Integrity (SRI) not implemented, but all external scripts are loaded over HTTPS.

`, modifier: -5, recommendation: `

Add SRI to external scripts.

`, }, ], [ Expectation.SriImplementedButExternalScriptsNotLoadedSecurely, { description: `

Subresource Integrity (SRI) implemented, but external scripts are loaded over HTTP or they use protocol-relative URLs via src="//...".

`, modifier: -20, recommendation: `

Load external scripts over HTTPS.

`, }, ], [ Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely, { description: `

Subresource Integrity (SRI) not implemented, and external scripts are loaded over HTTP or they use protocol-relative URLs via src="//...".

`, modifier: -50, recommendation: `

Load external scripts over HTTPS, and add SRI to them.

`, }, ], // X-Content-Type-Options [ Expectation.XContentTypeOptionsNosniff, { description: `

X-Content-Type-Options header set to nosniff.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.XContentTypeOptionsNotImplemented, { description: `

X-Content-Type-Options header not implemented.

`, modifier: -5, recommendation: `

Set to nosniff.

`, }, ], [ Expectation.XContentTypeOptionsHeaderInvalid, { description: `

X-Content-Type-Options header cannot be recognized.

`, modifier: -5, recommendation: `

Set to nosniff.

`, }, ], // X-Frame-Options [ Expectation.XFrameOptionsImplementedViaCsp, { description: `

X-Frame-Options (XFO) implemented via the CSP frame-ancestors directive.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.XFrameOptionsSameoriginOrDeny, { description: `

X-Frame-Options (XFO) header set to SAMEORIGIN or DENY.

`, modifier: 5, recommendation: ``, }, ], [ Expectation.XFrameOptionsAllowFromOrigin, { description: `

X-Frame-Options (XFO) header uses ALLOW-FROM uri directive.

`, modifier: 0, recommendation: `

Implement frame-ancestors CSP.

`, }, ], [ Expectation.XFrameOptionsNotImplemented, { description: `

X-Frame-Options (XFO) header not implemented.

`, modifier: -20, recommendation: `

Implement frame-ancestors CSP.

`, }, ], [ Expectation.XFrameOptionsHeaderInvalid, { description: `

X-Frame-Options (XFO) header cannot be recognized.

`, modifier: -20, recommendation: `

Implement frame-ancestors CSP.

`, }, ], // Cross Origin Resource Policy [ Expectation.CrossOriginResourcePolicyHeaderInvalid, { description: `

Cross-Origin-Resource-Policy (CORP) header cannot be recognized.

`, modifier: -5, recommendation: `

Implement Cross Origin Resource Policy.

`, }, ], [ Expectation.CrossOriginResourcePolicyImplementedWithCrossOrigin, { description: `

Cross Origin Resource Policy (CORP) implemented, but allows cross-origin resource sharing by default.

`, modifier: 0, recommendation: ``, }, ], [ Expectation.CrossOriginResourcePolicyImplementedWithSameOrigin, { description: `

Cross Origin Resource Policy (CORP) implemented, prevents leaks into cross-origin contexts.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CrossOriginResourcePolicyImplementedWithSameSite, { description: `

Cross Origin Resource Policy (CORP) implemented, prevents leaks into cross-site contexts.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CrossOriginResourcePolicyNotImplemented, { description: `

Cross Origin Resource Policy (CORP) is not implemented (defaults to cross-origin).

`, modifier: 0, recommendation: ``, }, ], // Cross Origin Embedder Policy [ Expectation.CoepImplementedWithRequireCorp, { description: `

Cross-Origin-Embedder-Policy header set to require-corp, restricting loading cross-origin resources.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CoepImplementedWithCredentialless, { description: `

Cross-Origin-Embedder-Policy header set to credentialless, allowing loading cross-origin resources but without sending credentials in the requests.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CoepImplementedWithUnsafeNone, { description: `

Cross-Origin-Embedder-Policy header set to unsafe-none, allowing loading cross-origin resources, which is the default value.

`, modifier: 0, recommendation: `

Set to require-corp or credentialless.

`, }, ], [ Expectation.CoepNotImplemented, { description: `

Cross-Origin-Embedder-Policy header not implemented.

`, modifier: 0, recommendation: `

Set to require-corp or credentialless.

`, }, ], [ Expectation.CoepHeaderInvalid, { description: `

Cross-Origin-Embedder-Policy header cannot be recognized.

`, modifier: -5, recommendation: `

Set to require-corp, credentialless, or unsafe-none.

`, }, ], // Cross Origin Opener Policy [ Expectation.CoopImplementedWithSameOrigin, { description: `

Cross-Origin-Opener-Policy header set to same-origin, enforcing site isolation from the opener.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CoopImplementedWithSameOriginAllowPopups, { description: `

Cross-Origin-Opener-Policy header set to same-origin-allow-popups, enforcing site isolation from the opener but allowing trusted resources such as for OAuth or payments.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CoopImplementedWithNoopenerAllowPopups, { description: `

Cross-Origin-Opener-Policy header set to noopener-allow-popups, enforcing site isolation from the opener even if they are same origin.

`, modifier: 10, recommendation: ``, }, ], [ Expectation.CoopImplementedWithUnsafeNone, { description: `

Cross-Origin-Opener-Policy header set to unsafe-none, the document permits sharing its browsing context group with any other document, and may therefore be unsafe.

`, modifier: 0, recommendation: `

Set to same-origin, same-origin-allow-popups, or noopener-allow-popups.

`, }, ], [ Expectation.CoopNotImplemented, { description: `

Cross-Origin-Opener-Policy header not implemented.

`, modifier: 0, recommendation: `

Set to same-origin, same-origin-allow-popups, or noopener-allow-popups.

`, }, ], [ Expectation.CoopHeaderInvalid, { description: `

Cross-Origin-Opener-Policy header cannot be recognized.

`, modifier: -5, recommendation: `

Set to same-origin, same-origin-allow-popups, noopener-allow-popups, or unsafe-none.

`, }, ], // GENERIC [ Expectation.HtmlNotParseable, { description: `

Claims to be html, but cannot be parsed.

`, modifier: -20, recommendation: `

Make sure the server returns correct HTML.

`, }, ], ]);