Skip to content

Commit 75a93a9

Browse files
Require authentication for the VS Code E2E mock server (#2474)
* fix(vscode-e2e): require authentication for mock server requests <!-- coderabbit-ai-generated:v1 --> * refactor(vscode-e2e): simplify mock authentication and SDD coverage * test(vscode-e2e): run mock authentication tests with Node in CI * test(vscode-e2e): build mock auth dependencies and cover authenticated reset --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Elliott de Launay <edelauna@gmail.com>
1 parent 66b07e0 commit 75a93a9

12 files changed

Lines changed: 322 additions & 4 deletions

‎.github/workflows/code-qa.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ jobs:
114114
run: pnpm lint
115115
- name: Check types
116116
run: pnpm check-types
117+
- name: Test mock authentication
118+
run: pnpm --dir apps/vscode-e2e test:mock-auth
117119
- name: Validate Code QA workflow
118120
run: pnpm test:code-qa-ci
119121
- name: Validate Solheim provider smoke workflow

‎apps/vscode-e2e/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"lint": "eslint src --ext=ts --max-warnings=0",
66
"check-types": "tsc -p tsconfig.esm.json --noEmit",
77
"format": "prettier --write src",
8+
"test:mock-auth": "pnpm --filter @roo-code/types build && tsc -p tsconfig.json && node --test out/mock-auth.spec.js out/mock-auth.property.spec.js",
89
"test:ci": "pnpm -w bundle && pnpm test:run",
910
"test:ci:mock": "pnpm -w bundle && USE_MOCK=true pnpm test:run",
1011
"test:record": "AIMOCK_RECORD=true pnpm test:ci",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import assert from "node:assert/strict"
2+
import { test } from "node:test"
3+
4+
import { authenticatedMockFetch } from "./mock-auth"
5+
6+
const mockOrigin = "http://127.0.0.1:8123"
7+
const credentialNames = ["authorization", "x-api-key", "x-goog-api-key", "api-key", "xi-api-key"]
8+
9+
function headerEntries(headers: Headers): [string, string][] {
10+
const entries: [string, string][] = []
11+
headers.forEach((value, name) => entries.push([name, value]))
12+
return entries
13+
}
14+
15+
async function captureRequest(input: RequestInfo | URL, init?: RequestInit) {
16+
const calls: { input: RequestInfo | URL; init: RequestInit | undefined; headers: Headers }[] = []
17+
const sink: typeof fetch = async (actualInput, actualInit) => {
18+
calls.push({
19+
input: actualInput,
20+
init: actualInit,
21+
headers: new Headers(
22+
actualInit?.headers ?? (actualInput instanceof Request ? actualInput.headers : undefined),
23+
),
24+
})
25+
return new Response()
26+
}
27+
await authenticatedMockFetch(sink, mockOrigin, "per-run-key")(input, init)
28+
assert.equal(calls.length, 1)
29+
const call = calls[0]!
30+
assert.equal(call.input, input)
31+
return call
32+
}
33+
34+
// Exhaust the credential subsets in MockAuthentication.tla for each fetch input form.
35+
void test("mock requests replace all credential subsets and preserve other headers", async () => {
36+
for (let mask = 0; mask < 2 ** credentialNames.length; mask++) {
37+
const headers = new Headers({ "x-preserved": "yes" })
38+
credentialNames.forEach((name, index) => {
39+
if (mask & (1 << index)) headers.set(name, "original")
40+
})
41+
const url = `${mockOrigin}/__aimock/fixtures`
42+
for (const input of [url, new URL(url), new Request(url, { headers })]) {
43+
const call = await captureRequest(input, input instanceof Request ? undefined : { headers })
44+
assert.deepEqual(headerEntries(call.headers), [
45+
["authorization", "Bearer per-run-key"],
46+
["x-preserved", "yes"],
47+
])
48+
}
49+
}
50+
})
51+
52+
void test("other origins keep the original request options and credentials", async () => {
53+
for (const origin of ["http://127.0.0.1:8124", "http://localhost:8123", "https://127.0.0.1:8123"]) {
54+
const headers = new Headers(credentialNames.map((name): [string, string] => [name, "original"]))
55+
const url = `${origin}/__aimock/fixtures`
56+
for (const input of [url, new URL(url), new Request(url, { headers })]) {
57+
const init: RequestInit = { redirect: "manual", ...(input instanceof Request ? {} : { headers }) }
58+
const call = await captureRequest(input, init)
59+
assert.equal(call.init, init)
60+
assert.deepEqual(headerEntries(call.headers), headerEntries(headers))
61+
}
62+
}
63+
})
64+
65+
void test("mock requests reject redirects regardless of the requested policy", async () => {
66+
for (const redirect of [undefined, "follow", "manual", "error"] as const) {
67+
const call = await captureRequest(mockOrigin, { redirect })
68+
assert.equal(call.init?.redirect, "error")
69+
}
70+
})
71+
72+
void test("init headers replace Request headers before mock authentication is applied", async () => {
73+
const input = new Request(mockOrigin, {
74+
headers: { "x-source": "request", "x-request-only": "discard", authorization: "Bearer request" },
75+
})
76+
for (const headers of [new Headers(), new Headers({ "x-source": "init", "x-api-key": "original" })]) {
77+
const call = await captureRequest(input, { headers })
78+
assert.equal(call.headers.get("authorization"), "Bearer per-run-key")
79+
assert.equal(call.headers.get("x-source"), headers.get("x-source"))
80+
assert.equal(call.headers.get("x-request-only"), null)
81+
assert.equal(call.headers.get("x-api-key"), null)
82+
}
83+
})
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import assert from "node:assert/strict"
2+
import { randomBytes } from "node:crypto"
3+
import { createServer } from "node:http"
4+
import { test } from "node:test"
5+
import { LLMock } from "@copilotkit/aimock"
6+
7+
import { authenticatedMockFetch } from "./mock-auth"
8+
9+
void test("mock authentication rejects fixture tampering and permits replay", async () => {
10+
const key = randomBytes(32).toString("hex")
11+
const mock = new LLMock({ port: 0, auth: { apiKeys: [key] } })
12+
mock.addFixture({ match: { userMessage: "smoke" }, response: { content: "trusted" } })
13+
await mock.start()
14+
try {
15+
for (const path of ["/__aimock/fixtures", "/__aimock/reset"]) {
16+
for (const credential of [undefined, "mock-key"]) {
17+
const response = await fetch(`${mock.url}${path}`, {
18+
method: "POST",
19+
headers: credential ? { authorization: `Bearer ${credential}` } : {},
20+
body: JSON.stringify({ fixtures: [] }),
21+
})
22+
assert.equal(response.status, 401)
23+
}
24+
}
25+
const client = authenticatedMockFetch(fetch, mock.url, key)
26+
assert.equal(
27+
(
28+
await client(`${mock.url}/__aimock/fixtures`, {
29+
method: "POST",
30+
headers: { "content-type": "application/json" },
31+
body: JSON.stringify({
32+
fixtures: [{ match: { userMessage: "other" }, response: { content: "safe" } }],
33+
}),
34+
})
35+
).status,
36+
200,
37+
)
38+
const providerHeaders: Record<string, string>[] = [
39+
{},
40+
{ "x-api-key": "original" },
41+
{ "x-goog-api-key": "original" },
42+
]
43+
for (const headers of providerHeaders) {
44+
const response = await client(
45+
new Request(`${mock.url}/v1/chat/completions`, {
46+
method: "POST",
47+
headers: { ...headers, "content-type": "application/json" },
48+
body: JSON.stringify({ model: "test", messages: [{ role: "user", content: "smoke" }] }),
49+
}),
50+
)
51+
assert.equal(response.status, 200)
52+
assert.equal((await response.json()).choices[0].message.content, "trusted")
53+
}
54+
assert.equal((await client(`${mock.url}/__aimock/fixtures`)).status, 200)
55+
assert.equal((await client(`${mock.url}/__aimock/reset`, { method: "POST" })).status, 200)
56+
} finally {
57+
await mock.stop()
58+
}
59+
})
60+
61+
void test("recording uses the upstream key and never forwards the mock credential", async () => {
62+
let authorization: string | undefined
63+
const upstream = createServer((req, res) => {
64+
authorization = req.headers.authorization
65+
res.setHeader("content-type", "application/json")
66+
res.end(
67+
JSON.stringify({
68+
choices: [{ message: { role: "assistant", content: "recorded" }, finish_reason: "stop" }],
69+
}),
70+
)
71+
})
72+
await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve))
73+
const address = upstream.address()
74+
assert.ok(address && typeof address !== "string")
75+
const upstreamUrl = `http://127.0.0.1:${address.port}`
76+
const key = randomBytes(32).toString("hex")
77+
const mock = new LLMock({
78+
port: 0,
79+
auth: { apiKeys: [key] },
80+
record: {
81+
providers: { openai: upstreamUrl },
82+
providerKeys: { openai: "synthetic-upstream-key" },
83+
proxyOnly: true,
84+
},
85+
})
86+
try {
87+
await mock.start()
88+
const client = authenticatedMockFetch(fetch, mock.url, key)
89+
const response = await client(`${mock.url}/v1/chat/completions`, {
90+
method: "POST",
91+
headers: { "content-type": "application/json", authorization: "Bearer original" },
92+
body: JSON.stringify({ model: "test", messages: [{ role: "user", content: "record" }] }),
93+
})
94+
assert.equal(response.status, 200)
95+
await response.text()
96+
assert.equal(authorization, "Bearer synthetic-upstream-key")
97+
await client(upstreamUrl, { headers: { authorization: "Bearer unrelated" } })
98+
assert.equal(authorization, "Bearer unrelated")
99+
} finally {
100+
await mock.stop()
101+
await new Promise<void>((resolve, reject) => upstream.close((error) => (error ? reject(error) : resolve())))
102+
}
103+
})

‎apps/vscode-e2e/src/mock-auth.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Keep the per-run credential on requests to this mock only, including SDKs
2+
// that use different credential headers (or no credentials, like LM Studio).
3+
export function authenticatedMockFetch(fetch: typeof globalThis.fetch, mockUrl: string, apiKey: string): typeof fetch {
4+
const origin = new URL(mockUrl).origin
5+
return (input, init) => {
6+
const url = input instanceof Request ? input.url : input.toString()
7+
if (new URL(url).origin !== origin) {
8+
return fetch(input, init)
9+
}
10+
11+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))
12+
// aimock rejects conflicting credential headers, and recording uses the
13+
// parent's providerKeys rather than any of these inbound credentials.
14+
for (const name of ["x-api-key", "x-goog-api-key", "api-key", "xi-api-key"]) {
15+
headers.delete(name)
16+
}
17+
headers.set("authorization", `Bearer ${apiKey}`)
18+
return fetch(input, { ...init, headers, redirect: "error" })
19+
}
20+
}

‎apps/vscode-e2e/src/runTest.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from "path"
22
import * as os from "os"
33
import * as fs from "fs/promises"
44
import { readFileSync } from "fs"
5+
import { randomBytes } from "crypto"
56

67
import { downloadAndUnzipVSCode, runTests } from "@vscode/test-electron"
78
import { LLMock } from "@copilotkit/aimock"
@@ -88,6 +89,7 @@ async function main() {
8889
const useMock = isRecord || !hasRealApiKey || process.env.USE_MOCK === "true"
8990

9091
let mock: InstanceType<typeof LLMock> | undefined
92+
const mockApiKey = randomBytes(32).toString("hex")
9193

9294
// The folder containing the Extension Manifest package.json
9395
// Passed to `--extensionDevelopmentPath`
@@ -113,18 +115,25 @@ async function main() {
113115

114116
mock = new LLMock({
115117
port: 0, // random free port
118+
auth: { apiKeys: [mockApiKey] },
116119
...(isRecord && {
117120
record: {
118121
// OpenRouter is OpenAI-compatible; aimock proxies using the openai provider key.
119122
// Use /api (not /api/v1) — aimock appends the request path (/v1/chat/completions)
120123
// so including /v1 here would produce a doubled /v1/v1 upstream URL.
121124
providers: {
122125
openai: isDeepSeekTest ? "https://api.deepseek.com" : "https://openrouter.ai/api",
123-
// aimock forwards the x-api-key header from the Anthropic SDK to the real API.
126+
// Recording credentials are supplied separately through providerKeys.
124127
anthropic: "https://api.anthropic.com",
125-
// aimock forwards the x-goog-api-key header from the Google AI SDK.
128+
// Google AI recording is enabled only for its targeted suite.
126129
...(isGeminiTest && { gemini: "https://generativelanguage.googleapis.com" }),
127130
},
131+
// Authenticated aimock requests never forward inbound credentials.
132+
providerKeys: {
133+
openai: isDeepSeekTest ? process.env.DEEPSEEK_API_KEY : process.env.OPENROUTER_API_KEY,
134+
anthropic: process.env.ANTHROPIC_API_KEY,
135+
gemini: process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY,
136+
},
128137
fixturePath: fixturesDir,
129138
},
130139
}),
@@ -183,7 +192,7 @@ async function main() {
183192
...process.env,
184193
...(testGrep && { TEST_GREP: testGrep }),
185194
...(testFile && { TEST_FILE: testFile }),
186-
...(mock && { AIMOCK_URL: mock.url }),
195+
...(mock && { AIMOCK_URL: mock.url, E2E_AIMOCK_API_KEY: mockApiKey }),
187196
...(mock && { E2E_MOCK_MODEL_LIST_FALLBACK: "true" }),
188197
}
189198

‎apps/vscode-e2e/src/suite/index.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ import * as vscode from "vscode"
77
import { RooCodeEventName, type RooCodeAPI } from "@roo-code/types"
88

99
import { isCompletedAsk, waitFor } from "./utils"
10+
import { authenticatedMockFetch } from "../mock-auth"
1011

1112
export async function run() {
13+
// Install before extension activation so SDK clients capture the authenticated fetch.
14+
if (process.env.AIMOCK_URL && process.env.E2E_AIMOCK_API_KEY) {
15+
globalThis.fetch = authenticatedMockFetch(
16+
globalThis.fetch,
17+
process.env.AIMOCK_URL,
18+
process.env.E2E_AIMOCK_API_KEY,
19+
)
20+
}
21+
1222
const extension = vscode.extensions.getExtension<RooCodeAPI>("ZooCodeOrganization.zoo-code")
1323

1424
if (!extension) {
@@ -22,7 +32,7 @@ export async function run() {
2232

2333
await api.setConfiguration({
2434
apiProvider: providerIdentifiers.openrouter,
25-
// In record mode, forward the real key so aimock can proxy it to OpenRouter.
35+
// The fetch wrapper supplies mock authentication; the parent owns recording keys.
2636
// In replay mode, "mock-key" is sufficient — aimock never contacts the real API.
2737
openRouterApiKey: aimockUrl && !isRecord ? "mock-key" : process.env.OPENROUTER_API_KEY!,
2838
openRouterModelId: "openai/gpt-4.1",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
ScopeGuard = TRUE
4+
StripConflicts = TRUE
5+
BlockRedirects = FALSE
6+
INVARIANTS TypeOK MockAccepted OtherOriginUnchanged NoRedirectFollow
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
ScopeGuard = FALSE
4+
StripConflicts = TRUE
5+
BlockRedirects = TRUE
6+
INVARIANTS TypeOK MockAccepted OtherOriginUnchanged NoRedirectFollow
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SPECIFICATION Spec
2+
CONSTANTS
3+
ScopeGuard = TRUE
4+
StripConflicts = FALSE
5+
BlockRedirects = TRUE
6+
INVARIANTS TypeOK MockAccepted OtherOriginUnchanged NoRedirectFollow

0 commit comments

Comments
 (0)