|
| 1 | +/** |
| 2 | + * Step definitions for security.feature. These scenarios run direct HTTP |
| 3 | + * requests (no browser) because they assert on response headers, status |
| 4 | + * codes, and raw HTML — not user interaction. |
| 5 | + */ |
| 6 | + |
| 7 | +import { When, Then } from '@cucumber/cucumber' |
| 8 | +import type { DataTable } from '@cucumber/cucumber' |
| 9 | +import type { EpdsWorld } from '../support/world.js' |
| 10 | +import { testEnv } from '../support/env.js' |
| 11 | + |
| 12 | +/** Response captured by the most recent direct-fetch step. */ |
| 13 | +interface CapturedResponse { |
| 14 | + status: number |
| 15 | + headers: Headers |
| 16 | + body: string |
| 17 | +} |
| 18 | + |
| 19 | +const capturedBySymbol = new WeakMap<EpdsWorld, CapturedResponse>() |
| 20 | + |
| 21 | +function setCapturedResponse( |
| 22 | + world: EpdsWorld, |
| 23 | + response: CapturedResponse, |
| 24 | +): void { |
| 25 | + capturedBySymbol.set(world, response) |
| 26 | + world.lastHttpStatus = response.status |
| 27 | +} |
| 28 | + |
| 29 | +function getCapturedResponse(world: EpdsWorld): CapturedResponse { |
| 30 | + const captured = capturedBySymbol.get(world) |
| 31 | + if (!captured) { |
| 32 | + throw new Error('No response has been captured by a prior step') |
| 33 | + } |
| 34 | + return captured |
| 35 | +} |
| 36 | + |
| 37 | +async function captureGet( |
| 38 | + world: EpdsWorld, |
| 39 | + url: string, |
| 40 | + init?: RequestInit, |
| 41 | +): Promise<void> { |
| 42 | + const res = await fetch(url, { redirect: 'manual', ...init }) |
| 43 | + const body = await res.text() |
| 44 | + setCapturedResponse(world, { status: res.status, headers: res.headers, body }) |
| 45 | +} |
| 46 | + |
| 47 | +// --------------------------------------------------------------------------- |
| 48 | +// CSRF scenarios |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | + |
| 51 | +When('the recovery page is loaded', async function (this: EpdsWorld) { |
| 52 | + const recoveryUrl = `${testEnv.authUrl}/auth/recover?request_uri=urn:ietf:params:oauth:request_uri:req-security-probe` |
| 53 | + await captureGet(this, recoveryUrl) |
| 54 | +}) |
| 55 | + |
| 56 | +Then('the response sets a CSRF cookie', function (this: EpdsWorld) { |
| 57 | + const { headers } = getCapturedResponse(this) |
| 58 | + const setCookie = headers.get('set-cookie') ?? '' |
| 59 | + if (!/epds_csrf=/.test(setCookie)) { |
| 60 | + throw new Error( |
| 61 | + `Expected Set-Cookie to include epds_csrf=..., got: ${setCookie || '(none)'}`, |
| 62 | + ) |
| 63 | + } |
| 64 | +}) |
| 65 | + |
| 66 | +Then( |
| 67 | + 'the HTML form contains a hidden CSRF token field', |
| 68 | + function (this: EpdsWorld) { |
| 69 | + const { body } = getCapturedResponse(this) |
| 70 | + if (!/<input[^>]*type="hidden"[^>]*name="csrf"[^>]*>/.test(body)) { |
| 71 | + throw new Error('HTML response has no hidden CSRF input field') |
| 72 | + } |
| 73 | + }, |
| 74 | +) |
| 75 | + |
| 76 | +When( |
| 77 | + 'a POST request is sent to the recovery endpoint without a CSRF token', |
| 78 | + async function (this: EpdsWorld) { |
| 79 | + const res = await fetch(`${testEnv.authUrl}/auth/recover`, { |
| 80 | + method: 'POST', |
| 81 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 82 | + body: new URLSearchParams({ |
| 83 | + request_uri: 'urn:ietf:params:oauth:request_uri:req-security-probe', |
| 84 | + email: 'noone@example.com', |
| 85 | + }).toString(), |
| 86 | + redirect: 'manual', |
| 87 | + }) |
| 88 | + const body = await res.text() |
| 89 | + setCapturedResponse(this, { |
| 90 | + status: res.status, |
| 91 | + headers: res.headers, |
| 92 | + body, |
| 93 | + }) |
| 94 | + }, |
| 95 | +) |
| 96 | + |
| 97 | +Then('the response status is {int}', function (this: EpdsWorld, code: number) { |
| 98 | + const { status } = getCapturedResponse(this) |
| 99 | + if (status !== code) { |
| 100 | + throw new Error(`Expected status ${code}, got ${status}`) |
| 101 | + } |
| 102 | +}) |
| 103 | + |
| 104 | +// --------------------------------------------------------------------------- |
| 105 | +// Security headers scenario |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | + |
| 108 | +When( |
| 109 | + 'any page is loaded from the auth service', |
| 110 | + async function (this: EpdsWorld) { |
| 111 | + await captureGet(this, `${testEnv.authUrl}/health`) |
| 112 | + }, |
| 113 | +) |
| 114 | + |
| 115 | +Then( |
| 116 | + 'the response includes the following security headers:', |
| 117 | + function (this: EpdsWorld, table: DataTable) { |
| 118 | + const { headers } = getCapturedResponse(this) |
| 119 | + const missing: string[] = [] |
| 120 | + for (const row of table.hashes()) { |
| 121 | + const expected = row.value |
| 122 | + const actual = headers.get(row.header) |
| 123 | + if (actual !== expected) { |
| 124 | + missing.push( |
| 125 | + `${row.header}: expected "${expected}", got "${actual ?? '(missing)'}"`, |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + if (missing.length) { |
| 130 | + throw new Error(`Security header mismatch:\n ${missing.join('\n ')}`) |
| 131 | + } |
| 132 | + }, |
| 133 | +) |
| 134 | + |
| 135 | +// --------------------------------------------------------------------------- |
| 136 | +// CSP scenario |
| 137 | +// --------------------------------------------------------------------------- |
| 138 | + |
| 139 | +When('the login page is loaded', async function (this: EpdsWorld) { |
| 140 | + // /oauth/authorize on the PDS renders the auth-service login page via |
| 141 | + // the epds-callback redirect chain, but hitting it without a valid |
| 142 | + // request_uri triggers an error response before headers are set the |
| 143 | + // way we want. The auth service exposes a preview route that renders |
| 144 | + // the same login template, guarded by AUTH_PREVIEW_ROUTES. If preview |
| 145 | + // is off, fall back to a probe of any auth-service page — the CSP |
| 146 | + // header is applied globally by the security-headers middleware, so |
| 147 | + // an auth-service /health response carries the same header. |
| 148 | + const previewUrl = `${testEnv.authUrl}/preview/login` |
| 149 | + let res = await fetch(previewUrl, { redirect: 'manual' }) |
| 150 | + if (res.status === 404) { |
| 151 | + res = await fetch(`${testEnv.authUrl}/health`, { redirect: 'manual' }) |
| 152 | + } |
| 153 | + const body = await res.text() |
| 154 | + setCapturedResponse(this, { status: res.status, headers: res.headers, body }) |
| 155 | +}) |
| 156 | + |
| 157 | +Then( |
| 158 | + 'the Content-Security-Policy header is present', |
| 159 | + function (this: EpdsWorld) { |
| 160 | + const { headers } = getCapturedResponse(this) |
| 161 | + if (!headers.get('content-security-policy')) { |
| 162 | + throw new Error('Content-Security-Policy header is not set') |
| 163 | + } |
| 164 | + }, |
| 165 | +) |
| 166 | + |
| 167 | +function getScriptSrcDirective(csp: string): string { |
| 168 | + const match = /(?:^|;\s*)script-src\s+([^;]+)/.exec(csp) |
| 169 | + if (!match) { |
| 170 | + throw new Error(`CSP is missing a script-src directive: "${csp}"`) |
| 171 | + } |
| 172 | + return match[1].trim() |
| 173 | +} |
| 174 | + |
| 175 | +Then( |
| 176 | + 'the script-src directive does not allow unsafe-inline', |
| 177 | + function (this: EpdsWorld) { |
| 178 | + const { headers } = getCapturedResponse(this) |
| 179 | + const csp = headers.get('content-security-policy') ?? '' |
| 180 | + const scriptSrc = getScriptSrcDirective(csp) |
| 181 | + if (/'unsafe-inline'/.test(scriptSrc)) { |
| 182 | + throw new Error( |
| 183 | + `script-src directive allows 'unsafe-inline': "${scriptSrc}"`, |
| 184 | + ) |
| 185 | + } |
| 186 | + }, |
| 187 | +) |
| 188 | + |
| 189 | +Then( |
| 190 | + 'the script-src directive carries a per-response nonce', |
| 191 | + function (this: EpdsWorld) { |
| 192 | + const { headers } = getCapturedResponse(this) |
| 193 | + const csp = headers.get('content-security-policy') ?? '' |
| 194 | + const scriptSrc = getScriptSrcDirective(csp) |
| 195 | + if (!/'nonce-[A-Za-z0-9_-]+'/.test(scriptSrc)) { |
| 196 | + throw new Error(`script-src directive has no 'nonce-...': "${scriptSrc}"`) |
| 197 | + } |
| 198 | + }, |
| 199 | +) |
| 200 | + |
| 201 | +// --------------------------------------------------------------------------- |
| 202 | +// Metrics scenario |
| 203 | +// --------------------------------------------------------------------------- |
| 204 | + |
| 205 | +When( |
| 206 | + 'GET \\/metrics is called on the auth service without credentials', |
| 207 | + async function (this: EpdsWorld) { |
| 208 | + await captureGet(this, `${testEnv.authUrl}/metrics`) |
| 209 | + }, |
| 210 | +) |
0 commit comments