auth.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Security tests for the brainstorm server's per-session key.
  3. *
  4. * The companion server is reachable by any local browser tab (default loopback
  5. * bind) and by any host that can route to it (remote `--host 0.0.0.0` bind).
  6. * A per-session secret key gates every endpoint so that neither a browser
  7. * confused-deputy nor a direct remote client can read screens/files or inject
  8. * events into state/events (prompt injection into a live agent session).
  9. *
  10. * Auth = a valid `?key=<token>` query param OR a valid session cookie.
  11. *
  12. * Uses the `ws` npm package as a test client (test-only dependency).
  13. */
  14. const { spawn } = require('child_process');
  15. const http = require('http');
  16. const WebSocket = require('ws');
  17. const fs = require('fs');
  18. const path = require('path');
  19. const assert = require('assert');
  20. const SERVER_PATH = path.join(__dirname, '../../skills/brainstorming/scripts/server.cjs');
  21. const TEST_PORT = 3335;
  22. const TEST_DIR = '/tmp/brainstorm-auth-test';
  23. const CONTENT_DIR = path.join(TEST_DIR, 'content');
  24. const TOKEN = 'testtoken-0123456789abcdef0123456789abcdef';
  25. const COOKIE_NAME = `brainstorm-key-${TEST_PORT}`;
  26. const EXPECTED_SECURITY_HEADERS = {
  27. 'referrer-policy': 'no-referrer',
  28. 'cache-control': 'no-store',
  29. 'x-frame-options': 'DENY',
  30. 'content-security-policy': "frame-ancestors 'none'",
  31. 'cross-origin-resource-policy': 'same-origin'
  32. };
  33. function cleanup() {
  34. if (fs.existsSync(TEST_DIR)) fs.rmSync(TEST_DIR, { recursive: true });
  35. }
  36. async function sleep(ms) {
  37. return new Promise(resolve => setTimeout(resolve, ms));
  38. }
  39. // Raw HTTP GET with optional key query and Cookie header.
  40. function get(pathname, { key, cookie } = {}) {
  41. const url = `http://localhost:${TEST_PORT}${pathname}` + (key !== undefined ? `?key=${key}` : '');
  42. const headers = {};
  43. if (cookie) headers['Cookie'] = cookie;
  44. return new Promise((resolve, reject) => {
  45. http.get(url, { headers }, (res) => {
  46. let data = '';
  47. res.on('data', chunk => data += chunk);
  48. res.on('end', () => resolve({ status: res.statusCode, headers: res.headers, body: data }));
  49. }).on('error', reject);
  50. });
  51. }
  52. // Try to open a WebSocket; resolve 'opened' or 'rejected'.
  53. function wsConnect({ key, cookie, origin } = {}) {
  54. const url = `ws://localhost:${TEST_PORT}/` + (key !== undefined ? `?key=${key}` : '');
  55. const headers = {};
  56. if (cookie) headers['Cookie'] = cookie;
  57. if (origin) headers['Origin'] = origin;
  58. const opts = Object.keys(headers).length ? { headers } : {};
  59. const ws = new WebSocket(url, opts);
  60. return new Promise((resolve) => {
  61. let settled = false;
  62. const done = (outcome) => { if (!settled) { settled = true; resolve({ outcome, ws }); } };
  63. ws.on('open', () => done('opened'));
  64. ws.on('error', () => done('rejected'));
  65. ws.on('close', () => done('rejected'));
  66. setTimeout(() => done('rejected'), 1500);
  67. });
  68. }
  69. function startServer() {
  70. return spawn('node', [SERVER_PATH], {
  71. env: { ...process.env, BRAINSTORM_PORT: TEST_PORT, BRAINSTORM_DIR: TEST_DIR, BRAINSTORM_TOKEN: TOKEN }
  72. });
  73. }
  74. function assertSecurityHeaders(headers) {
  75. for (const [name, value] of Object.entries(EXPECTED_SECURITY_HEADERS)) {
  76. assert.strictEqual(headers[name], value, `${name} should be ${value}`);
  77. }
  78. }
  79. function runBootstrapScript(html, sessionStorage) {
  80. const match = html.match(/<script>\n([\s\S]*?)\n<\/script>/);
  81. assert(match, 'bootstrap response should contain a script block');
  82. const replacements = [];
  83. const location = { replace(url) { replacements.push(url); } };
  84. new Function('sessionStorage', 'location', match[1])(sessionStorage, location);
  85. return replacements;
  86. }
  87. async function waitForServer(server) {
  88. let stdout = '', stderr = '';
  89. return new Promise((resolve, reject) => {
  90. server.stdout.on('data', (d) => {
  91. stdout += d.toString();
  92. if (stdout.includes('server-started')) resolve({ stdout });
  93. });
  94. server.stderr.on('data', (d) => { stderr += d.toString(); });
  95. server.on('error', reject);
  96. setTimeout(() => reject(new Error(`Server didn't start. stderr: ${stderr}`)), 5000);
  97. });
  98. }
  99. async function runTests() {
  100. cleanup();
  101. fs.mkdirSync(CONTENT_DIR, { recursive: true });
  102. fs.writeFileSync(path.join(CONTENT_DIR, 'screen.html'), '<h2>Secret screen</h2>');
  103. fs.writeFileSync(path.join(CONTENT_DIR, 'asset.txt'), 'secret asset');
  104. const server = startServer();
  105. let stdoutAccum = '';
  106. server.stdout.on('data', (d) => { stdoutAccum += d.toString(); });
  107. const { stdout: initialStdout } = await waitForServer(server);
  108. let passed = 0, failed = 0;
  109. async function test(name, fn) {
  110. try { await fn(); console.log(` PASS: ${name}`); passed++; }
  111. catch (e) { console.log(` FAIL: ${name}`); console.log(` ${e.message}`); failed++; }
  112. }
  113. try {
  114. console.log('\n--- Startup URL ---');
  115. await test('server-started url includes the session key', () => {
  116. const msg = JSON.parse(initialStdout.trim());
  117. assert(msg.url.includes(`key=${TOKEN}`), `url should carry the key, got: ${msg.url}`);
  118. });
  119. console.log('\n--- HTTP / gate ---');
  120. await test('GET / without key is rejected with 403', async () => {
  121. const res = await get('/');
  122. assert.strictEqual(res.status, 403, 'no-key request must be 403');
  123. });
  124. await test('403 page names "coding agent" and the key', async () => {
  125. const res = await get('/');
  126. assert(/coding agent/i.test(res.body), '403 body should reference the coding agent');
  127. assert(/key/i.test(res.body), '403 body should mention the key');
  128. });
  129. await test('403 responses include leak-reduction and anti-framing headers', async () => {
  130. const res = await get('/');
  131. assert.strictEqual(res.status, 403);
  132. assertSecurityHeaders(res.headers);
  133. });
  134. await test('GET / with wrong key is rejected with 403', async () => {
  135. const res = await get('/', { key: 'wrong-token' });
  136. assert.strictEqual(res.status, 403);
  137. });
  138. await test('GET / with wrong key and valid cookie is rejected with 403', async () => {
  139. const res = await get('/', { key: 'wrong-token', cookie: `${COOKIE_NAME}=${TOKEN}` });
  140. assert.strictEqual(res.status, 403, 'explicit wrong query key must not fall back to cookie auth');
  141. });
  142. await test('GET / with valid query returns bootstrap instead of screen content', async () => {
  143. const res = await get('/', { key: TOKEN });
  144. assert.strictEqual(res.status, 200);
  145. assert(res.body.includes('sessionStorage'), 'bootstrap should store the session key in tab storage');
  146. assert(res.body.includes('location.replace'), 'bootstrap should navigate to the bare root URL');
  147. assert(!res.body.includes('Secret screen'), 'bootstrap must not serve screen HTML at the keyed URL');
  148. });
  149. await test('bootstrap strips the key URL even when sessionStorage write fails', async () => {
  150. const res = await get('/', { key: TOKEN });
  151. assert.strictEqual(res.status, 200);
  152. let replacements;
  153. assert.doesNotThrow(() => {
  154. replacements = runBootstrapScript(res.body, {
  155. setItem() { throw new Error('storage blocked'); }
  156. });
  157. });
  158. assert.deepStrictEqual(replacements, ['/']);
  159. });
  160. await test('HTML responses include leak-reduction and anti-framing headers', async () => {
  161. const res = await get('/', { key: TOKEN });
  162. assert.strictEqual(res.status, 200);
  163. assertSecurityHeaders(res.headers);
  164. });
  165. await test('valid key load sets an HttpOnly SameSite=Strict cookie', async () => {
  166. const res = await get('/', { key: TOKEN });
  167. const setCookie = (res.headers['set-cookie'] || []).join('; ');
  168. assert(setCookie.includes(`${COOKIE_NAME}=${TOKEN}`), `should set ${COOKIE_NAME}`);
  169. assert(/HttpOnly/i.test(setCookie), 'cookie should be HttpOnly');
  170. assert(/SameSite=Strict/i.test(setCookie), 'cookie should be SameSite=Strict');
  171. });
  172. await test('GET / with valid cookie (no query key) serves the screen', async () => {
  173. const res = await get('/', { cookie: `${COOKIE_NAME}=${TOKEN}` });
  174. assert.strictEqual(res.status, 200);
  175. assert(res.body.includes('Secret screen'), 'cookie-authenticated bare root should serve the screen');
  176. assert(!res.body.includes("location.replace('/');"), 'bare screen response should not be the bootstrap page');
  177. });
  178. console.log('\n--- HTTP /files gate ---');
  179. await test('GET /files without key is rejected with 403', async () => {
  180. const res = await get('/files/asset.txt');
  181. assert.strictEqual(res.status, 403);
  182. });
  183. await test('GET /files with valid key serves the file', async () => {
  184. const res = await get('/files/asset.txt', { key: TOKEN });
  185. assert.strictEqual(res.status, 200);
  186. assert(res.body.includes('secret asset'));
  187. });
  188. await test('/files responses include leak-reduction and anti-framing headers', async () => {
  189. const res = await get('/files/asset.txt', { key: TOKEN });
  190. assert.strictEqual(res.status, 200);
  191. assertSecurityHeaders(res.headers);
  192. });
  193. console.log('\n--- WebSocket gate ---');
  194. await test('WS upgrade without key is rejected', async () => {
  195. const { outcome, ws } = await wsConnect();
  196. ws.close();
  197. assert.strictEqual(outcome, 'rejected', 'unauthenticated WS must not open');
  198. });
  199. await test('WS upgrade with valid key opens', async () => {
  200. const { outcome, ws } = await wsConnect({ key: TOKEN });
  201. ws.close();
  202. assert.strictEqual(outcome, 'opened');
  203. });
  204. await test('WS upgrade with valid cookie opens', async () => {
  205. const { outcome, ws } = await wsConnect({ cookie: `${COOKIE_NAME}=${TOKEN}` });
  206. ws.close();
  207. assert.strictEqual(outcome, 'opened');
  208. });
  209. await test('WS upgrade with valid cookie and same-origin Origin opens', async () => {
  210. const { outcome, ws } = await wsConnect({
  211. cookie: `${COOKIE_NAME}=${TOKEN}`,
  212. origin: `http://localhost:${TEST_PORT}`
  213. });
  214. ws.close();
  215. assert.strictEqual(outcome, 'opened');
  216. });
  217. await test('WS upgrade with valid cookie but cross-origin Origin is rejected', async () => {
  218. const eventsFile = path.join(TEST_DIR, 'state', 'events');
  219. if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
  220. const { outcome, ws } = await wsConnect({
  221. cookie: `${COOKIE_NAME}=${TOKEN}`,
  222. origin: 'http://localhost:9999'
  223. });
  224. if (outcome === 'opened') {
  225. ws.send(JSON.stringify({ type: 'choice', choice: 'attacker-injected', text: 'local attacker probe' }));
  226. await sleep(300);
  227. }
  228. ws.close();
  229. assert.strictEqual(outcome, 'rejected', 'cross-origin browser WS must not open even with cookie');
  230. assert(!fs.existsSync(eventsFile), 'cross-origin WS must not write state/events');
  231. });
  232. console.log('\n--- Robustness (A3) ---');
  233. await test('null payload over an authed WS does not crash the server', async () => {
  234. const { ws } = await wsConnect({ key: TOKEN });
  235. ws.send('null');
  236. await sleep(300);
  237. const res = await get('/', { key: TOKEN });
  238. assert.strictEqual(res.status, 200, 'server must still respond after null payload');
  239. ws.close();
  240. });
  241. console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
  242. if (failed > 0) process.exit(1);
  243. } finally {
  244. server.kill();
  245. await sleep(100);
  246. cleanup();
  247. }
  248. }
  249. runTests().catch(err => { console.error('Test failed:', err); process.exit(1); });