Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/web-security-headers.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/factories/web-app-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { randomBytes } from 'crypto'
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.
export const getWebProtocolForRelay = (relayProtocol: string): string => (relayProtocol === 'wss:' ? 'https:' : 'http:')

export const createWebApp = (): Express => {
const app = express()
app.use(securityHeaders())
app
.disable('x-powered-by')
.use((req, res, next) => {
Expand Down
15 changes: 15 additions & 0 deletions src/utils/security-headers.ts
Original file line number Diff line number Diff line change
@@ -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 })
49 changes: 49 additions & 0 deletions test/unit/utils/security-headers.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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)
})
})
Loading