Skip to content
Myr

feat(secure-headers): Add CSP report-to and report-uri directive support (#4555)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(secure-headers): Add CSP report-to and report-uri directive support * ci: apply automated fixes * Removed the redundant reportUri special handling as suggested Remove 'reportUri' directive handling from Content Security Policy. ---------

authored by Aditya Chopra · committed by GitHub · parent b694129 · Browse files · 2 files+111 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

2 files+111 added−0 removed

  • tests:+110 added−0 removed
  • other:+1 added−0 removed

2 files+111 added−0 removed

modified:src/middleware/secure-headers/index.test.ts+110 added
478 unmodified lines
479 expect(csp).toMatch("style-src 'self' 'nonce-styleSrc'")480 expect(await res.text()).toEqual('script: scriptSrc, style: styleSrc')481 })482 })483484 // OUR NEW REPORT-URI TESTS485 describe('CSP report-uri directive', () => {486 it('should set report-uri with single endpoint', async () => {487 const app = new Hono()488 app.use(489 '/test',490 secureHeaders({491 contentSecurityPolicy: {492 defaultSrc: ["'self'"],493 reportUri: '/csp-report',494 },495 })496 )497498 const res = await app.request('/test')499 expect(res.headers.get('Content-Security-Policy')).toEqual(500 "default-src 'self'; report-uri /csp-report"501 )502 })503504 it('should set report-uri with multiple endpoints', async () => {505 const app = new Hono()506 app.use(507 '/test',508 secureHeaders({509 contentSecurityPolicy: {510 defaultSrc: ["'self'"],511 reportUri: ['/endpoint1', '/endpoint2'],512 },513 })514 )515516 const res = await app.request('/test')517 expect(res.headers.get('Content-Security-Policy')).toEqual(518 "default-src 'self'; report-uri /endpoint1 /endpoint2"519 )520 })521522 it('should work with report-to and report-uri together', async () => {523 const app = new Hono()524 app.use(525 '/test',526 secureHeaders({527 contentSecurityPolicy: {528 defaultSrc: ["'self'"],529 reportTo: 'endpoint-1',530 reportUri: '/legacy-report',531 },532 })533 )534535 const res = await app.request('/test')536 expect(res.headers.get('Content-Security-Policy')).toEqual(537 "default-src 'self'; report-to endpoint-1; report-uri /legacy-report"538 )539 })540541 it('should work with Content-Security-Policy-Report-Only', async () => {542 const app = new Hono()543 app.use(544 '/test',545 secureHeaders({546 contentSecurityPolicyReportOnly: {547 defaultSrc: ["'self'"],548 reportUri: '/csp-report-only',549 },550 })551 )552553 const res = await app.request('/test')554 expect(res.headers.get('Content-Security-Policy-Report-Only')).toEqual(555 "default-src 'self'; report-uri /csp-report-only"556 )557 })558559 // ADDED MISSING TEST CASE for Report-Only:560561 it('should not output report-uri when reportUri is not set', async () => {562 const app = new Hono()563 app.use(564 '/test',565 secureHeaders({566 contentSecurityPolicy: {567 defaultSrc: ["'self'"],568 },569 })570 )571572 const res = await app.request('/test')573 const header = res.headers.get('Content-Security-Policy') || ''574 expect(header.includes('report-uri')).toBe(false)575 })576577 it('should not output report-uri in Report-Only when reportUri is not set', async () => {578 const app = new Hono()579 app.use(580 '/test',581 secureHeaders({582 contentSecurityPolicyReportOnly: {583 defaultSrc: ["'self'"],584 },585 })586 )587588 const res = await app.request('/test')589 const header = res.headers.get('Content-Security-Policy-Report-Only') || ''590 expect(header.includes('report-uri')).toBe(false)591 })592 })593})
modified:src/middleware/secure-headers/secure-headers.ts+1 added
27 unmodified lines
28 manifestSrc?: ContentSecurityPolicyOptionValue29 mediaSrc?: ContentSecurityPolicyOptionValue30 objectSrc?: ContentSecurityPolicyOptionValue31 reportTo?: string32 reportUri?: string | string[]33 sandbox?: ContentSecurityPolicyOptionValue34 scriptSrc?: ContentSecurityPolicyOptionValue35 scriptSrcAttr?: ContentSecurityPolicyOptionValue36 scriptSrcElem?: ContentSecurityPolicyOptionValue
298 unmodified lines