|
| 1 | +import { test } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import http from 'node:http'; |
| 4 | +import { createDaemonProxyServer } from '../daemon-proxy.ts'; |
| 5 | +import { DAEMON_RPC_PROTOCOL_VERSION } from '../daemon/http-health.ts'; |
| 6 | +import { |
| 7 | + closeLoopbackServer, |
| 8 | + listenOnLoopback, |
| 9 | + skipWhenLoopbackUnavailable, |
| 10 | +} from './test-utils/index.ts'; |
| 11 | + |
| 12 | +test('daemon proxy forwards rpc requests with upstream daemon token', async (t) => { |
| 13 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 14 | + |
| 15 | + let upstreamAuth = ''; |
| 16 | + let upstreamTokenHeader = ''; |
| 17 | + let upstreamBody: Record<string, any> | undefined; |
| 18 | + const upstream = http.createServer((req, res) => { |
| 19 | + if (req.url === '/health') { |
| 20 | + res.setHeader('content-type', 'application/json'); |
| 21 | + res.end(JSON.stringify({ ok: true })); |
| 22 | + return; |
| 23 | + } |
| 24 | + assert.equal(req.url, '/rpc'); |
| 25 | + upstreamAuth = String(req.headers.authorization ?? ''); |
| 26 | + upstreamTokenHeader = String(req.headers['x-agent-device-token'] ?? ''); |
| 27 | + let body = ''; |
| 28 | + req.setEncoding('utf8'); |
| 29 | + req.on('data', (chunk) => { |
| 30 | + body += chunk; |
| 31 | + }); |
| 32 | + req.on('end', () => { |
| 33 | + upstreamBody = JSON.parse(body) as Record<string, any>; |
| 34 | + res.setHeader('content-type', 'application/json'); |
| 35 | + res.end( |
| 36 | + JSON.stringify({ |
| 37 | + jsonrpc: '2.0', |
| 38 | + id: upstreamBody.id, |
| 39 | + result: { ok: true, data: { via: 'proxy' } }, |
| 40 | + }), |
| 41 | + ); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + const proxy = createDaemonProxyServer({ |
| 46 | + upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`, |
| 47 | + upstreamToken: 'daemon-secret', |
| 48 | + clientToken: 'proxy-secret', |
| 49 | + }); |
| 50 | + |
| 51 | + try { |
| 52 | + const proxyPort = await listenOnLoopback(proxy); |
| 53 | + const response = await fetch(`http://127.0.0.1:${proxyPort}/agent-device/rpc`, { |
| 54 | + method: 'POST', |
| 55 | + headers: { |
| 56 | + 'content-type': 'application/json', |
| 57 | + authorization: 'Bearer proxy-secret', |
| 58 | + }, |
| 59 | + body: JSON.stringify({ |
| 60 | + jsonrpc: '2.0', |
| 61 | + id: 'req-1', |
| 62 | + method: 'agent_device.command', |
| 63 | + params: { |
| 64 | + token: 'proxy-secret', |
| 65 | + session: 'default', |
| 66 | + command: 'devices', |
| 67 | + positionals: [], |
| 68 | + flags: {}, |
| 69 | + }, |
| 70 | + }), |
| 71 | + }); |
| 72 | + |
| 73 | + assert.equal(response.status, 200); |
| 74 | + assert.deepEqual(await response.json(), { |
| 75 | + jsonrpc: '2.0', |
| 76 | + id: 'req-1', |
| 77 | + result: { ok: true, data: { via: 'proxy' } }, |
| 78 | + }); |
| 79 | + assert.equal(upstreamAuth, 'Bearer daemon-secret'); |
| 80 | + assert.equal(upstreamTokenHeader, 'daemon-secret'); |
| 81 | + assert.equal(upstreamBody?.params?.token, 'daemon-secret'); |
| 82 | + assert.equal(upstreamBody?.params?.command, 'devices'); |
| 83 | + } finally { |
| 84 | + await closeLoopbackServer(proxy); |
| 85 | + await closeLoopbackServer(upstream); |
| 86 | + } |
| 87 | +}); |
| 88 | + |
| 89 | +test('daemon proxy rejects unauthenticated rpc requests', async (t) => { |
| 90 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 91 | + |
| 92 | + let upstreamCalled = false; |
| 93 | + const upstream = http.createServer((_req, res) => { |
| 94 | + upstreamCalled = true; |
| 95 | + res.end('{}'); |
| 96 | + }); |
| 97 | + const proxy = createDaemonProxyServer({ |
| 98 | + upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`, |
| 99 | + upstreamToken: 'daemon-secret', |
| 100 | + clientToken: 'proxy-secret', |
| 101 | + }); |
| 102 | + |
| 103 | + try { |
| 104 | + const proxyPort = await listenOnLoopback(proxy); |
| 105 | + const response = await fetch(`http://127.0.0.1:${proxyPort}/rpc`, { |
| 106 | + method: 'POST', |
| 107 | + headers: { 'content-type': 'application/json' }, |
| 108 | + body: JSON.stringify({ |
| 109 | + jsonrpc: '2.0', |
| 110 | + id: 'req-unauthorized', |
| 111 | + method: 'agent_device.command', |
| 112 | + params: { command: 'devices' }, |
| 113 | + }), |
| 114 | + }); |
| 115 | + |
| 116 | + assert.equal(response.status, 401); |
| 117 | + const payload = (await response.json()) as { error?: { message?: string } }; |
| 118 | + assert.equal(payload.error?.message, 'Invalid proxy token'); |
| 119 | + assert.equal(upstreamCalled, false); |
| 120 | + } finally { |
| 121 | + await closeLoopbackServer(proxy); |
| 122 | + await closeLoopbackServer(upstream); |
| 123 | + } |
| 124 | +}); |
| 125 | + |
| 126 | +test('daemon proxy leaves health endpoint unauthenticated', async (t) => { |
| 127 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 128 | + |
| 129 | + let upstreamAuth = ''; |
| 130 | + let upstreamTokenHeader = ''; |
| 131 | + const upstream = http.createServer((req, res) => { |
| 132 | + assert.equal(req.url, '/health'); |
| 133 | + upstreamAuth = String(req.headers.authorization ?? ''); |
| 134 | + upstreamTokenHeader = String(req.headers['x-agent-device-token'] ?? ''); |
| 135 | + res.setHeader('content-type', 'application/json'); |
| 136 | + res.end(JSON.stringify({ ok: true })); |
| 137 | + }); |
| 138 | + const proxy = createDaemonProxyServer({ |
| 139 | + upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`, |
| 140 | + upstreamToken: 'daemon-secret', |
| 141 | + clientToken: 'proxy-secret', |
| 142 | + }); |
| 143 | + |
| 144 | + try { |
| 145 | + const proxyPort = await listenOnLoopback(proxy); |
| 146 | + const response = await fetch(`http://127.0.0.1:${proxyPort}/agent-device/health`); |
| 147 | + assert.equal(response.status, 200); |
| 148 | + const payload = (await response.json()) as Record<string, any>; |
| 149 | + assert.equal(payload.ok, true); |
| 150 | + assert.equal(payload.service, 'agent-device-proxy'); |
| 151 | + assert.equal(typeof payload.version, 'string'); |
| 152 | + assert.equal(payload.rpcProtocolVersion, DAEMON_RPC_PROTOCOL_VERSION); |
| 153 | + assert.deepEqual(payload.upstream, { ok: true }); |
| 154 | + assert.equal(upstreamAuth, 'Bearer daemon-secret'); |
| 155 | + assert.equal(upstreamTokenHeader, 'daemon-secret'); |
| 156 | + } finally { |
| 157 | + await closeLoopbackServer(proxy); |
| 158 | + await closeLoopbackServer(upstream); |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | +test('daemon proxy streams uploads and artifact downloads with upstream daemon token', async (t) => { |
| 163 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 164 | + |
| 165 | + let uploadAuth = ''; |
| 166 | + let uploadTokenHeader = ''; |
| 167 | + let uploadArtifactType = ''; |
| 168 | + let uploadArtifactFilename = ''; |
| 169 | + let uploadBody = ''; |
| 170 | + let artifactAuth = ''; |
| 171 | + let artifactTokenHeader = ''; |
| 172 | + const upstream = http.createServer((req, res) => { |
| 173 | + if (req.method === 'POST' && req.url === '/upload') { |
| 174 | + uploadAuth = String(req.headers.authorization ?? ''); |
| 175 | + uploadTokenHeader = String(req.headers['x-agent-device-token'] ?? ''); |
| 176 | + uploadArtifactType = String(req.headers['x-artifact-type'] ?? ''); |
| 177 | + uploadArtifactFilename = String(req.headers['x-artifact-filename'] ?? ''); |
| 178 | + req.setEncoding('utf8'); |
| 179 | + req.on('data', (chunk) => { |
| 180 | + uploadBody += chunk; |
| 181 | + }); |
| 182 | + req.on('end', () => { |
| 183 | + res.setHeader('content-type', 'application/json'); |
| 184 | + res.end(JSON.stringify({ ok: true, uploadId: 'upload-1' })); |
| 185 | + }); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + assert.equal(req.method, 'GET'); |
| 190 | + assert.equal(req.url, '/artifacts/shot-1?download=1'); |
| 191 | + artifactAuth = String(req.headers.authorization ?? ''); |
| 192 | + artifactTokenHeader = String(req.headers['x-agent-device-token'] ?? ''); |
| 193 | + res.setHeader('content-type', 'image/png'); |
| 194 | + res.setHeader('content-disposition', 'attachment; filename="shot.png"'); |
| 195 | + res.setHeader('x-request-id', 'upstream-request-1'); |
| 196 | + res.write('png-'); |
| 197 | + res.end('body'); |
| 198 | + }); |
| 199 | + const proxy = createDaemonProxyServer({ |
| 200 | + upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`, |
| 201 | + upstreamToken: 'daemon-secret', |
| 202 | + clientToken: 'proxy-secret', |
| 203 | + }); |
| 204 | + |
| 205 | + try { |
| 206 | + const proxyPort = await listenOnLoopback(proxy); |
| 207 | + const upload = await fetch(`http://127.0.0.1:${proxyPort}/agent-device/upload`, { |
| 208 | + method: 'POST', |
| 209 | + headers: { |
| 210 | + authorization: 'Bearer proxy-secret', |
| 211 | + 'x-artifact-type': 'file', |
| 212 | + 'x-artifact-filename': 'demo.apk', |
| 213 | + 'content-type': 'application/octet-stream', |
| 214 | + }, |
| 215 | + body: Buffer.from('fake-apk'), |
| 216 | + }); |
| 217 | + assert.equal(upload.status, 200); |
| 218 | + assert.deepEqual(await upload.json(), { ok: true, uploadId: 'upload-1' }); |
| 219 | + assert.equal(uploadAuth, 'Bearer daemon-secret'); |
| 220 | + assert.equal(uploadTokenHeader, 'daemon-secret'); |
| 221 | + assert.equal(uploadArtifactType, 'file'); |
| 222 | + assert.equal(uploadArtifactFilename, 'demo.apk'); |
| 223 | + assert.equal(uploadBody, 'fake-apk'); |
| 224 | + |
| 225 | + const artifact = await fetch( |
| 226 | + `http://127.0.0.1:${proxyPort}/agent-device/artifacts/shot-1?download=1`, |
| 227 | + { headers: { authorization: 'Bearer proxy-secret' } }, |
| 228 | + ); |
| 229 | + assert.equal(artifact.status, 200); |
| 230 | + assert.equal(await artifact.text(), 'png-body'); |
| 231 | + assert.equal(artifact.headers.get('content-type'), 'image/png'); |
| 232 | + assert.match(artifact.headers.get('content-disposition') ?? '', /shot\.png/); |
| 233 | + assert.equal(artifact.headers.get('x-request-id'), 'upstream-request-1'); |
| 234 | + assert.equal(artifactAuth, 'Bearer daemon-secret'); |
| 235 | + assert.equal(artifactTokenHeader, 'daemon-secret'); |
| 236 | + } finally { |
| 237 | + await closeLoopbackServer(proxy); |
| 238 | + await closeLoopbackServer(upstream); |
| 239 | + } |
| 240 | +}); |
0 commit comments