Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
246 lines
8.0 KiB
TypeScript
246 lines
8.0 KiB
TypeScript
// Copilot BYOK proxy tests verify SDK-local transport is guarded outbound fetch.
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { createCopilotByokProxy } from "./byok-proxy.js";
|
|
import { resolveCopilotProvider } from "./provider-bridge.js";
|
|
|
|
const ssrfRuntimeMock = vi.hoisted(() => ({
|
|
fetchWithSsrFGuard: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()),
|
|
fetchWithSsrFGuard: ssrfRuntimeMock.fetchWithSsrFGuard,
|
|
}));
|
|
|
|
describe("createCopilotByokProxy", () => {
|
|
afterEach(() => {
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockReset();
|
|
});
|
|
|
|
it("presents a loopback SDK endpoint and forwards through guarded fetch", async () => {
|
|
const release = vi.fn(async () => undefined);
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockResolvedValue({
|
|
response: new Response("ok", {
|
|
status: 201,
|
|
headers: {
|
|
"content-encoding": "gzip",
|
|
"content-length": "999",
|
|
"x-upstream": "yes",
|
|
},
|
|
}),
|
|
release,
|
|
});
|
|
const resolvedProvider = resolveCopilotProvider({
|
|
model: {
|
|
provider: "custom-proxy",
|
|
api: "openai-responses",
|
|
id: "proxy-model",
|
|
baseUrl: "https://proxy.example/v1?routing=blue",
|
|
},
|
|
resolvedApiKey: "secret-key",
|
|
});
|
|
|
|
const proxy = await createCopilotByokProxy(resolvedProvider);
|
|
expect(proxy?.provider.provider?.baseUrl).toMatch(
|
|
/^http:\/\/127\.0\.0\.1:\d+\/[a-f0-9]{24}\/v1$/,
|
|
);
|
|
|
|
try {
|
|
const response = await fetch(`${proxy?.provider.provider?.baseUrl}/responses?trace=request`, {
|
|
method: "POST",
|
|
headers: {
|
|
authorization: "Bearer secret-key",
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({ model: "proxy-model" }),
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
expect(response.headers.get("content-encoding")).toBeNull();
|
|
expect(response.headers.get("content-length")).toBeNull();
|
|
expect(response.headers.get("x-upstream")).toBe("yes");
|
|
expect(await response.text()).toBe("ok");
|
|
expect(ssrfRuntimeMock.fetchWithSsrFGuard).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
auditContext: "copilot-byok-provider",
|
|
requireHttps: true,
|
|
url: "https://proxy.example/v1/responses?routing=blue&trace=request",
|
|
init: expect.objectContaining({
|
|
method: "POST",
|
|
headers: expect.objectContaining({
|
|
"accept-encoding": "identity",
|
|
authorization: "Bearer secret-key",
|
|
"content-type": "application/json",
|
|
}),
|
|
signal: expect.any(AbortSignal),
|
|
}),
|
|
}),
|
|
);
|
|
expect(release).toHaveBeenCalledTimes(1);
|
|
} finally {
|
|
await proxy?.close();
|
|
}
|
|
});
|
|
|
|
it("injects resolved bearer auth when the SDK request omits Authorization", async () => {
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockResolvedValue({
|
|
response: new Response("ok", { status: 200 }),
|
|
release: vi.fn(async () => undefined),
|
|
});
|
|
const resolvedProvider = resolveCopilotProvider({
|
|
model: {
|
|
provider: "tencent-tokenplan",
|
|
api: "openai-completions",
|
|
id: "hy3",
|
|
baseUrl: "https://tokenplan.example/v1",
|
|
authHeader: true,
|
|
},
|
|
resolvedApiKey: "tokenplan-secret",
|
|
});
|
|
|
|
const proxy = await createCopilotByokProxy(resolvedProvider);
|
|
|
|
try {
|
|
const response = await fetch(`${proxy?.provider.provider?.baseUrl}/chat/completions`, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({ model: "hy3" }),
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(ssrfRuntimeMock.fetchWithSsrFGuard).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: "https://tokenplan.example/v1/chat/completions",
|
|
init: expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
authorization: "Bearer tokenplan-secret",
|
|
"content-type": "application/json",
|
|
}),
|
|
}),
|
|
}),
|
|
);
|
|
} finally {
|
|
await proxy?.close();
|
|
}
|
|
});
|
|
|
|
it("aborts in-flight upstream fetches when the proxy closes", async () => {
|
|
let upstreamSignal: AbortSignal | undefined;
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockImplementation(async ({ init }: any) => {
|
|
upstreamSignal = init.signal;
|
|
await new Promise((_, reject) => {
|
|
upstreamSignal?.addEventListener("abort", () => reject(new Error("upstream aborted")), {
|
|
once: true,
|
|
});
|
|
});
|
|
throw new Error("unreachable");
|
|
});
|
|
const resolvedProvider = resolveCopilotProvider({
|
|
model: {
|
|
provider: "custom-proxy",
|
|
api: "openai-responses",
|
|
id: "proxy-model",
|
|
baseUrl: "https://proxy.example/v1",
|
|
},
|
|
});
|
|
const proxy = await createCopilotByokProxy(resolvedProvider);
|
|
|
|
const responsePromise = fetch(`${proxy?.provider.provider?.baseUrl}/responses`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ model: "proxy-model" }),
|
|
}).catch((error: unknown) => error);
|
|
|
|
await vi.waitFor(() => {
|
|
expect(upstreamSignal).toBeDefined();
|
|
});
|
|
|
|
await proxy?.close();
|
|
|
|
expect(upstreamSignal?.aborted).toBe(true);
|
|
await responsePromise;
|
|
});
|
|
|
|
it("accepts Azure SDK paths that are rebuilt from the proxy origin", async () => {
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockResolvedValue({
|
|
response: new Response("azure-ok", { status: 200 }),
|
|
release: vi.fn(async () => undefined),
|
|
});
|
|
const resolvedProvider = resolveCopilotProvider({
|
|
model: {
|
|
provider: "custom-azure",
|
|
api: "azure-openai-responses",
|
|
id: "deployment-gpt",
|
|
baseUrl: "https://example.openai.azure.com/openai/v1",
|
|
},
|
|
resolvedApiKey: "azure-key",
|
|
});
|
|
|
|
const proxy = await createCopilotByokProxy(resolvedProvider);
|
|
expect(proxy?.provider.provider?.baseUrl).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/);
|
|
|
|
try {
|
|
const response = await fetch(
|
|
`${proxy?.provider.provider?.baseUrl}/openai/v1/responses?trace=request`,
|
|
{
|
|
method: "POST",
|
|
headers: { "api-key": "azure-key" },
|
|
body: JSON.stringify({ model: "deployment-gpt" }),
|
|
},
|
|
);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(await response.text()).toBe("azure-ok");
|
|
expect(ssrfRuntimeMock.fetchWithSsrFGuard).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
requireHttps: true,
|
|
url: "https://example.openai.azure.com/openai/v1/responses?trace=request",
|
|
init: expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
"accept-encoding": "identity",
|
|
"api-key": "azure-key",
|
|
}),
|
|
}),
|
|
}),
|
|
);
|
|
} finally {
|
|
await proxy?.close();
|
|
}
|
|
});
|
|
|
|
it("does not inject bearer auth on nonce-less Azure SDK paths", async () => {
|
|
ssrfRuntimeMock.fetchWithSsrFGuard.mockResolvedValue({
|
|
response: new Response("azure-ok", { status: 200 }),
|
|
release: vi.fn(async () => undefined),
|
|
});
|
|
const resolvedProvider = resolveCopilotProvider({
|
|
model: {
|
|
provider: "custom-azure",
|
|
api: "azure-openai-responses",
|
|
id: "deployment-gpt",
|
|
baseUrl: "https://example.openai.azure.com/openai/v1",
|
|
authHeader: true,
|
|
},
|
|
resolvedApiKey: "azure-bearer",
|
|
});
|
|
|
|
const proxy = await createCopilotByokProxy(resolvedProvider);
|
|
|
|
try {
|
|
const response = await fetch(`${proxy?.provider.provider?.baseUrl}/openai/v1/responses`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ model: "deployment-gpt" }),
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
const call = ssrfRuntimeMock.fetchWithSsrFGuard.mock.calls[0]?.[0] as
|
|
| { init?: { headers?: Record<string, string> } }
|
|
| undefined;
|
|
expect(call?.init?.headers).not.toHaveProperty("authorization");
|
|
} finally {
|
|
await proxy?.close();
|
|
}
|
|
});
|
|
});
|