From c4aa2f2d14e83a3c9fea2e305186020fb9bfbf81 Mon Sep 17 00:00:00 2001 From: "aikido-autofix[bot]" <119856028+aikido-autofix[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 12:30:29 +0000 Subject: [PATCH 1/2] fix(security): autofix Express is not emitting security headers --- src/factories/web-app-factory.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/factories/web-app-factory.ts b/src/factories/web-app-factory.ts index 39b256217..e810f6534 100644 --- a/src/factories/web-app-factory.ts +++ b/src/factories/web-app-factory.ts @@ -1,5 +1,6 @@ import express, { Express } from 'express' import { randomBytes } from 'crypto' +import helmet from 'helmet' import { createSettings } from './settings-factory' import router from '../routes' @@ -11,6 +12,7 @@ export const getWebProtocolForRelay = (relayProtocol: string): string => (relayP export const createWebApp = (): Express => { const app = express() + app.use(helmet()) app .disable('x-powered-by') .use((req, res, next) => { From d6b98ab845d3f2e4e819ca39fa676876e502f87e Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sat, 7 Nov 2026 07:46:56 -0500 Subject: [PATCH 2/2] feat(web): set security headers on every web response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Helmet's remaining defaults ship as-is; its Content-Security-Policy is disabled, because the app already emits one carrying the per-request nonce and the CDN origins the pages load from, and a second CSP would intersect with it and block the nonce'd inline scripts. Verified against the composed app, not just the middleware: /favicon.ico returns 200 with nosniff, SAMEORIGIN, max-age=31536000, referrer-policy and exactly one CSP — the app's own, with frame-src intact. --- .changeset/web-security-headers.md | 9 +++++ package.json | 1 + pnpm-lock.yaml | 9 +++++ src/factories/web-app-factory.ts | 4 +- src/utils/security-headers.ts | 15 ++++++++ test/unit/utils/security-headers.spec.ts | 49 ++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 .changeset/web-security-headers.md create mode 100644 src/utils/security-headers.ts create mode 100644 test/unit/utils/security-headers.spec.ts diff --git a/.changeset/web-security-headers.md b/.changeset/web-security-headers.md new file mode 100644 index 000000000..f75c9efdb --- /dev/null +++ b/.changeset/web-security-headers.md @@ -0,0 +1,9 @@ +--- +"nostream": minor +--- + +feat(web): set security headers on every web response + +The web app now sends `Strict-Transport-Security`, `X-Content-Type-Options: nosniff`, `X-Frame-Options: SAMEORIGIN`, `Referrer-Policy`, `X-DNS-Prefetch-Control`, `X-Permitted-Cross-Domain-Policies` and the cross-origin isolation headers through Helmet. + +The app's own nonce-based `Content-Security-Policy` remains the single CSP on a response: Helmet's default policy is disabled, because a second CSP would intersect with the app's and block the nonce'd inline scripts the dashboard uses. Embedding the web UI in another origin is now refused by `X-Frame-Options`; the dashboard's own Grafana frame is unaffected. diff --git a/package.json b/package.json index 83045b866..61b3d4009 100644 --- a/package.json +++ b/package.json @@ -179,6 +179,7 @@ "cac": "^7.0.0", "colorette": "^2.0.20", "express": "4.22.1", + "helmet": "^8.3.0", "js-yaml": "4.3.2", "knex": "3.2.9", "ora": "^9.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e96d47cf2..bc9fcfbbe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,6 +49,9 @@ importers: express: specifier: 4.22.1 version: 4.22.1 + helmet: + specifier: ^8.3.0 + version: 8.3.0 js-yaml: specifier: 4.3.2 version: 4.3.2 @@ -2963,6 +2966,10 @@ packages: resolution: {integrity: sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==} hasBin: true + helmet@8.3.0: + resolution: {integrity: sha512-Qgpiaws3Sm30Av8Eah6sjMCZZwjlBu+E68rhpCWBshY1lb09HtLwj5GviX0OyQIn+ulUS0iX0AxN5n3tLZzz1w==} + engines: {node: '>=18.0.0'} + help-me@5.0.0: resolution: {integrity: sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg==} @@ -8717,6 +8724,8 @@ snapshots: he@1.2.0: {} + helmet@8.3.0: {} + help-me@5.0.0: {} hosted-git-info@2.8.9: {} diff --git a/src/factories/web-app-factory.ts b/src/factories/web-app-factory.ts index e810f6534..168f2f530 100644 --- a/src/factories/web-app-factory.ts +++ b/src/factories/web-app-factory.ts @@ -1,10 +1,10 @@ import express, { Express } from 'express' import { randomBytes } from 'crypto' -import helmet from 'helmet' import { createSettings } from './settings-factory' import router from '../routes' import { getGrafanaFrameOrigin } from '../utils/admin-grafana' +import { securityHeaders } from '../utils/security-headers' // Extracted so the ws:/wss: -> http:/https: mapping can be unit tested directly, // without needing to exercise the full Express middleware stack. @@ -12,7 +12,7 @@ export const getWebProtocolForRelay = (relayProtocol: string): string => (relayP export const createWebApp = (): Express => { const app = express() - app.use(helmet()) + app.use(securityHeaders()) app .disable('x-powered-by') .use((req, res, next) => { diff --git a/src/utils/security-headers.ts b/src/utils/security-headers.ts new file mode 100644 index 000000000..6030385b6 --- /dev/null +++ b/src/utils/security-headers.ts @@ -0,0 +1,15 @@ +import helmet from 'helmet' + +// Security headers for every web response. +// +// `contentSecurityPolicy` stays off here on purpose: `createWebApp` already emits +// its own CSP, with the per-request nonce the pages use and the CDN origins they +// load from. Helmet's default policy would be sent as a second CSP header, and a +// browser enforces the intersection of every CSP on a response — the nonce the +// app relies on is absent from Helmet's, so inline scripts would be blocked and +// the dashboard would stop rendering. One policy, owned by the app, is the rule. +// +// The rest of Helmet's defaults are wanted as-is: HSTS, `X-Content-Type-Options`, +// `X-Frame-Options` (clickjacking), `Referrer-Policy`, `X-DNS-Prefetch-Control`, +// `X-Permitted-Cross-Domain-Policies` and cross-origin isolation headers. +export const securityHeaders = () => helmet({ contentSecurityPolicy: false }) diff --git a/test/unit/utils/security-headers.spec.ts b/test/unit/utils/security-headers.spec.ts new file mode 100644 index 000000000..79966a0dc --- /dev/null +++ b/test/unit/utils/security-headers.spec.ts @@ -0,0 +1,49 @@ +import chai from 'chai' +import express, { Express } from 'express' +import { AddressInfo } from 'net' +import { Server } from 'http' +import { securityHeaders } from '../../../src/utils/security-headers' + +const { expect } = chai + +describe('securityHeaders', () => { + let server: Server + let origin: string + let response: Response + + before(async () => { + const app: Express = express() + app.use(securityHeaders()) + app.get('/', (_req, res) => { + res.send('ok') + }) + server = app.listen(0) + await new Promise((resolve) => server.once('listening', () => resolve())) + origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}` + response = await fetch(origin) + }) + + after(() => { + server.close() + }) + + it('sets the transport and sniffing headers', () => { + expect(response.headers.get('strict-transport-security')).to.contain('max-age=') + expect(response.headers.get('x-content-type-options')).to.equal('nosniff') + }) + + it('refuses framing by other origins', () => { + expect(response.headers.get('x-frame-options')).to.equal('SAMEORIGIN') + }) + + it('does not emit a Content-Security-Policy of its own', () => { + // The web app owns one CSP, with the page nonce and the CDN origins it + // loads from. A second policy from Helmet would intersect with it and block + // the nonce'd inline scripts. + expect(response.headers.get('content-security-policy')).to.equal(null) + }) + + it('still runs the whole middleware chain', () => { + expect(response.status).to.equal(200) + }) +})