Skip to content
Myr

feat(secure-headers): remove "X-Powered-By" should be an option (#3177)

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

* fix: Secure-Headers delete "X-Powered-By" should be an option * fix: Secure-Headers delete "X-Powered-By" should be an option * Update index.test.ts * chore: format * Update secure-headers.ts * update tests * update middleware * chore * change prop name * chore * chore: format * revert the change of arg name * some fix * chore

authored by Ame_x · committed by GitHub · parent e8118e3 · Browse files · 2 files+36 added−1 removed
This commit

State

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

Diff stats

2 files+36 added−1 removed

  • tests:+29 added−0 removed
  • other:+7 added−1 removed

2 files+36 added−1 removed

modified:src/middleware/secure-headers/index.test.ts+29 added
403 unmodified lines
404 expect(csp).toMatch(`script-src 'self' 'nonce-scriptSrc'`)405 expect(csp).toMatch(`style-src 'self' 'nonce-styleSrc'`)406 expect(await res.text()).toEqual('script: scriptSrc, style: styleSrc')407 })408409 it('Remove X-Powered-By', async () => {410 const app = new Hono()411412 app.get('/test', secureHeaders(), poweredBy(), async (c) => {413 return c.text('Hono is cool')414 })415416 app.get(417 '/test2',418 secureHeaders({419 removePoweredBy: false,420 }),421 poweredBy(),422 async (c) => {423 return c.text('Hono is cool')424 }425 )426427 const res = await app.request('/test')428 const poweredby = res.headers.get('X-Powered-By')429 expect(poweredby).toEqual(null)430 expect(await res.text()).toEqual('Hono is cool')431432 const res2 = await app.request('/test2')433 const poweredby2 = res2.headers.get('X-Powered-By')434 expect(poweredby2).toEqual('Hono')435 expect(await res2.text()).toEqual('Hono is cool')436 })437})
modified:src/middleware/secure-headers/secure-headers.ts+7 added-1 removed
70 unmodified lines
71 xDownloadOptions?: overridableHeader72 xFrameOptions?: overridableHeader73 xPermittedCrossDomainPolicies?: overridableHeader74 xXssProtection?: overridableHeader75 removePoweredBy?: boolean76}7778type HeadersMap = {79 [key in keyof SecureHeadersOptions]: [string, string]
26 unmodified lines
106 xDownloadOptions: true,107 xFrameOptions: true,108 xPermittedCrossDomainPolicies: true,109 xXssProtection: true,110 removePoweredBy: true,111}112113type SecureHeadersCallback = (114 ctx: Context,
37 unmodified lines
152 * @param {overridableHeader} [customOptions.xDownloadOptions=true] - Settings for the X-Download-Options header.153 * @param {overridableHeader} [customOptions.xFrameOptions=true] - Settings for the X-Frame-Options header.154 * @param {overridableHeader} [customOptions.xPermittedCrossDomainPolicies=true] - Settings for the X-Permitted-Cross-Domain-Policies header.155 * @param {overridableHeader} [customOptions.xXssProtection=true] - Settings for the X-XSS-Protection header.156 * @param {boolean} [customOptions.removePoweredBy=true] - Settings for remove X-Powered-By header.157 * @returns {MiddlewareHandler} The middleware handler function.158 *159 * @example160 * ```ts
30 unmodified lines
191 ? headersToSet192 : callbacks.reduce((acc, cb) => cb(ctx, acc), headersToSet)193 await next()194 setHeaders(ctx, headersToSetForReq)195196 if (options?.removePoweredBy) {192 ctx.res.headers.delete('X-Powered-By')197 ctx.res.headers.delete('X-Powered-By')198 }199 }200}201202function getFilteredHeaders(options: SecureHeadersOptions): [string, string][] {
70 unmodified lines