mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
Compare commits
3 Commits
chore/deri
...
fix/execut
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a24a7fcdd9 | ||
|
|
1aeb2ff392 | ||
|
|
708a344ed0 |
1
changelog.d/fixes/executor-dispatch-guard.md
Normal file
1
changelog.d/fixes/executor-dispatch-guard.md
Normal file
@@ -0,0 +1 @@
|
||||
- Route the GitLab, GHE Copilot, CLIProxyAPI, 9Router, Dario, GLM, GitHub Copilot auto, NLP Cloud and 1min.ai upstream calls through the provider dispatch guard, so their redirects are followed by hand and checked on every hop.
|
||||
@@ -466,12 +466,11 @@ export class CliproxyapiExecutor extends BaseExecutor {
|
||||
)
|
||||
: JSON.stringify(value);
|
||||
|
||||
let response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: serializeWire(transformedBody),
|
||||
signal: combinedSignal,
|
||||
});
|
||||
let response = await this.dispatchRequest(
|
||||
url,
|
||||
{ method: "POST", headers, body: serializeWire(transformedBody), signal: combinedSignal },
|
||||
input.credentials
|
||||
);
|
||||
|
||||
// #14629: this override never calls super.execute(). The retry must use
|
||||
// the same serializer so the in-memory tool maps never reach the wire.
|
||||
@@ -482,7 +481,8 @@ export class CliproxyapiExecutor extends BaseExecutor {
|
||||
model: input.model,
|
||||
body: transformedBody,
|
||||
fetchOptions: { method: "POST", headers, signal: combinedSignal },
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: (fetchUrl, fetchOpts) =>
|
||||
this.dispatchRequest(fetchUrl, fetchOpts, input.credentials),
|
||||
serializeBody: serializeWire,
|
||||
log: input.log,
|
||||
});
|
||||
@@ -496,7 +496,8 @@ export class CliproxyapiExecutor extends BaseExecutor {
|
||||
model: input.model,
|
||||
body: transformedBody,
|
||||
fetchOptions: { method: "POST", headers, signal: combinedSignal },
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: (fetchUrl, fetchOpts) =>
|
||||
this.dispatchRequest(fetchUrl, fetchOpts, input.credentials),
|
||||
serializeBody: serializeWire,
|
||||
strippedFields: new Set(),
|
||||
log: input.log,
|
||||
|
||||
@@ -244,12 +244,11 @@ export class DarioExecutor extends BaseExecutor {
|
||||
|
||||
input.log?.info?.("DARIO", `Dario → ${url} (model: ${input.model}, shape: ${shape})`);
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(transformedBody),
|
||||
signal: combinedSignal,
|
||||
});
|
||||
const response = await this.dispatchRequest(
|
||||
url,
|
||||
{ method: "POST", headers, body: JSON.stringify(transformedBody), signal: combinedSignal },
|
||||
input.credentials
|
||||
);
|
||||
|
||||
if (response.status === HTTP_STATUS.RATE_LIMITED) {
|
||||
input.log?.warn?.("DARIO", `Dario rate limited: ${response.status}`);
|
||||
|
||||
@@ -156,8 +156,10 @@ async function dispatchFollowingRedirects(
|
||||
let options: RequestInit = { ...init, redirect: "manual" };
|
||||
for (let hop = 0; ; hop++) {
|
||||
const response = await dispatchOnce(provider, currentUrl, options, credentials, hop > 0);
|
||||
// Status first: a non-redirect answer is returned as is, whatever its headers.
|
||||
if (!REDIRECT_STATUSES.has(response.status)) return response;
|
||||
const location = response.headers.get("location");
|
||||
if (!REDIRECT_STATUSES.has(response.status) || !location) return response;
|
||||
if (!location) return response;
|
||||
void response.body?.cancel().catch(() => {});
|
||||
if (hop >= MAX_REDIRECTS) {
|
||||
throw new Error(`Provider dispatch stopped after ${MAX_REDIRECTS} redirects`);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GithubExecutor } from "./github.ts";
|
||||
import type { ProviderCredentials, ExecuteInput, ExecutorLog } from "./base.ts";
|
||||
import { getModelTargetFormat } from "../config/providerModels.ts";
|
||||
import { dispatchGuarded } from "./dispatchPin.ts";
|
||||
|
||||
/** Result of a successful GHE Copilot internal token exchange. */
|
||||
type CopilotTokenResult = {
|
||||
@@ -193,12 +194,13 @@ export class GheCopilotExecutor extends GithubExecutor {
|
||||
const baseUrl = gheUrl.replace(/\/chat\/completions\/?$/, "").replace(/\/responses\/?$/, "");
|
||||
const tokenUrl = `${baseUrl}/api/v3/copilot_internal/v2/token`;
|
||||
|
||||
const response = await fetch(tokenUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${githubAccessToken}`,
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
this.assertOutboundUrlAllowed(tokenUrl);
|
||||
const response = await dispatchGuarded(
|
||||
this.provider,
|
||||
tokenUrl,
|
||||
{ headers: { Authorization: `Bearer ${githubAccessToken}`, Accept: "application/json" } },
|
||||
credentials
|
||||
);
|
||||
|
||||
if (!response.ok) return null;
|
||||
const data = await response.json();
|
||||
@@ -246,14 +248,20 @@ export class GheCopilotExecutor extends GithubExecutor {
|
||||
params.set("client_secret", this.config.clientSecret);
|
||||
}
|
||||
|
||||
const response = await fetch(tokenUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Accept: "application/json",
|
||||
this.assertOutboundUrlAllowed(tokenUrl);
|
||||
const response = await dispatchGuarded(
|
||||
this.provider,
|
||||
tokenUrl,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
Accept: "application/json",
|
||||
},
|
||||
body: params,
|
||||
},
|
||||
body: params,
|
||||
});
|
||||
credentials
|
||||
);
|
||||
|
||||
if (!response.ok) return null;
|
||||
const tokens = await response.json();
|
||||
|
||||
@@ -465,12 +465,16 @@ export class GithubExecutor extends BaseExecutor {
|
||||
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(hasImage ? { prompt, has_image: true } : { prompt }),
|
||||
signal: requestSignal,
|
||||
});
|
||||
response = await this.dispatchRequest(
|
||||
url,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(hasImage ? { prompt, has_image: true } : { prompt }),
|
||||
signal: requestSignal,
|
||||
},
|
||||
credentials
|
||||
);
|
||||
} catch (error) {
|
||||
const timedOut = timeout.aborted && !signal?.aborted;
|
||||
return {
|
||||
|
||||
@@ -461,7 +461,8 @@ export class GitlabExecutor extends BaseExecutor {
|
||||
private async fetchGitLabDirectAccess(
|
||||
root: string,
|
||||
accessToken: string,
|
||||
signal: AbortSignal | null | undefined
|
||||
signal: AbortSignal | null | undefined,
|
||||
credentials: ProviderCredentials
|
||||
): Promise<{
|
||||
directAccess: GitLabDirectAccessDetails | null;
|
||||
response: Response | null;
|
||||
@@ -470,14 +471,20 @@ export class GitlabExecutor extends BaseExecutor {
|
||||
const endpoints = buildGitLabOAuthEndpoints(root);
|
||||
const timeoutSignal = AbortSignal.timeout(FETCH_TIMEOUT_MS);
|
||||
const combinedSignal = signal ? mergeAbortSignals(signal, timeoutSignal) : timeoutSignal;
|
||||
const response = await fetch(endpoints.directAccessUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Accept: "application/json",
|
||||
// The host is operator config; follow redirects through the dispatch guard.
|
||||
this.assertOutboundUrlAllowed(endpoints.directAccessUrl);
|
||||
const response = await this.dispatchRequest(
|
||||
endpoints.directAccessUrl,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Accept: "application/json",
|
||||
},
|
||||
signal: combinedSignal,
|
||||
},
|
||||
signal: combinedSignal,
|
||||
});
|
||||
credentials
|
||||
);
|
||||
|
||||
const bodyText = await response.text();
|
||||
if (!response.ok) {
|
||||
@@ -540,7 +547,8 @@ export class GitlabExecutor extends BaseExecutor {
|
||||
const { directAccess, response, bodyText } = await this.fetchGitLabDirectAccess(
|
||||
root,
|
||||
credentials.accessToken,
|
||||
input.signal
|
||||
input.signal,
|
||||
credentials
|
||||
);
|
||||
|
||||
if (directAccess) {
|
||||
@@ -645,12 +653,13 @@ export class GitlabExecutor extends BaseExecutor {
|
||||
const combinedSignal = input.signal
|
||||
? mergeAbortSignals(input.signal, timeoutSignal)
|
||||
: timeoutSignal;
|
||||
const response = await fetch(target.url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(transformedBody),
|
||||
signal: combinedSignal,
|
||||
});
|
||||
// target.url is the stored GitLab host or the gateway its direct_access answer named.
|
||||
this.assertOutboundUrlAllowed(target.url);
|
||||
const response = await this.dispatchRequest(
|
||||
target.url,
|
||||
{ method: "POST", headers, body: JSON.stringify(transformedBody), signal: combinedSignal },
|
||||
input.credentials
|
||||
);
|
||||
return { response, headers };
|
||||
}
|
||||
|
||||
|
||||
@@ -540,12 +540,16 @@ export class GlmExecutor extends DefaultExecutor {
|
||||
let response: Response;
|
||||
try {
|
||||
this.assertOutboundUrlAllowed(url); // GHSA-4f49: glm has its own fetch path
|
||||
response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(transformedBody),
|
||||
signal: combinedSignal || undefined,
|
||||
});
|
||||
response = await this.dispatchRequest(
|
||||
url,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(transformedBody),
|
||||
signal: combinedSignal || undefined,
|
||||
},
|
||||
input.credentials
|
||||
);
|
||||
} finally {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
}
|
||||
@@ -560,7 +564,8 @@ export class GlmExecutor extends DefaultExecutor {
|
||||
model: input.model,
|
||||
body: transformedBody,
|
||||
fetchOptions: { method: "POST", headers, signal: combinedSignal || undefined },
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: (fetchUrl, fetchOpts) =>
|
||||
this.dispatchRequest(fetchUrl, fetchOpts, input.credentials),
|
||||
log: input.log,
|
||||
});
|
||||
response = recovery.response;
|
||||
@@ -572,7 +577,8 @@ export class GlmExecutor extends DefaultExecutor {
|
||||
model: input.model,
|
||||
body: transformedBody,
|
||||
fetchOptions: { method: "POST", headers, signal: combinedSignal || undefined },
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: (fetchUrl, fetchOpts) =>
|
||||
this.dispatchRequest(fetchUrl, fetchOpts, input.credentials),
|
||||
serializeBody: (value) => JSON.stringify(value),
|
||||
strippedFields: new Set<string>(),
|
||||
log: input.log,
|
||||
|
||||
@@ -198,7 +198,8 @@ export class NineRouterExecutor extends BaseExecutor {
|
||||
);
|
||||
|
||||
const fetchOptions = { method: "POST", headers, signal: combinedSignal };
|
||||
let response = await fetch(url, { ...fetchOptions, body: JSON.stringify(transformedBody) });
|
||||
const dispatch = (u: string, o: RequestInit) => this.dispatchRequest(u, o, input.credentials);
|
||||
let response = await dispatch(url, { ...fetchOptions, body: JSON.stringify(transformedBody) });
|
||||
|
||||
// #14629: this override never calls super.execute(), so wire the same
|
||||
// reactive reasoning_effort 400 clamp-and-retry BaseExecutor applies.
|
||||
@@ -209,7 +210,7 @@ export class NineRouterExecutor extends BaseExecutor {
|
||||
model: innerModel,
|
||||
body: transformedBody,
|
||||
fetchOptions,
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: dispatch,
|
||||
log: input.log,
|
||||
});
|
||||
|
||||
@@ -221,7 +222,7 @@ export class NineRouterExecutor extends BaseExecutor {
|
||||
model: innerModel,
|
||||
body: transformedBody,
|
||||
fetchOptions,
|
||||
fetchFn: (fetchUrl, fetchOpts) => fetch(fetchUrl, fetchOpts),
|
||||
fetchFn: dispatch,
|
||||
serializeBody: (body) => JSON.stringify(body),
|
||||
strippedFields: new Set<string>(),
|
||||
log: input.log,
|
||||
|
||||
@@ -415,12 +415,11 @@ export class NlpCloudExecutor extends BaseExecutor {
|
||||
|
||||
try {
|
||||
this.assertOutboundUrlAllowed(url); // GHSA-4f49: nlpcloud has its own fetch path
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(payload),
|
||||
signal,
|
||||
});
|
||||
const response = await this.dispatchRequest(
|
||||
url,
|
||||
{ method: "POST", headers, body: JSON.stringify(payload), signal },
|
||||
credentials
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
|
||||
@@ -380,12 +380,11 @@ export class OneMinAiExecutor extends BaseExecutor {
|
||||
|
||||
try {
|
||||
this.assertOutboundUrlAllowed(url);
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(payload),
|
||||
signal,
|
||||
});
|
||||
const response = await this.dispatchRequest(
|
||||
url,
|
||||
{ method: "POST", headers, body: JSON.stringify(payload), signal },
|
||||
credentials
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
|
||||
83
tests/unit/executor-bare-fetch-redirect-guard.test.ts
Normal file
83
tests/unit/executor-bare-fetch-redirect-guard.test.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
// Executors that build their upstream URL from stored or upstream-supplied data must send it
|
||||
// through the provider dispatch guard (dispatchGuarded), not the global fetch. A bare fetch
|
||||
// follows redirects on its own, so a 307 from the configured host sent the prompt body to a
|
||||
// second origin, including cloud metadata, without any guard check.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import http from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
// Production installs the patched global fetch; use the same path here.
|
||||
await import("../../open-sse/utils/proxyFetch.ts");
|
||||
const { GitlabExecutor } = await import("../../open-sse/executors/gitlab.ts");
|
||||
|
||||
// Nothing listens there in a test environment, so an unguarded fetch hangs until the
|
||||
// abort signal instead of failing fast with the guard error.
|
||||
const METADATA = "http://169.254.169.254/latest/meta-data/";
|
||||
|
||||
const server = http.createServer((_req, res) => {
|
||||
res.writeHead(307, { location: METADATA });
|
||||
res.end();
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
test.after(() => {
|
||||
server.close();
|
||||
server.closeAllConnections();
|
||||
});
|
||||
|
||||
test("a redirect from the GitLab host to cloud metadata is refused by the guard", async () => {
|
||||
const started = Date.now();
|
||||
const outcome = await new GitlabExecutor()
|
||||
.execute({
|
||||
model: "gitlab-duo-code-suggestions",
|
||||
body: { messages: [{ role: "user", content: "secret prompt" }] },
|
||||
stream: false,
|
||||
credentials: { apiKey: "glpat-test", providerSpecificData: { baseUrl: origin } },
|
||||
signal: AbortSignal.timeout(4000),
|
||||
log: null,
|
||||
} as never)
|
||||
.then(
|
||||
// The executor turns a guard refusal into an error response.
|
||||
async (result: { response?: Response }) =>
|
||||
`${result.response?.status} ${await result.response?.text()}`,
|
||||
(error: unknown) => String((error as Error)?.message || error)
|
||||
);
|
||||
assert.match(outcome, /metadata|blocked/i, `unexpected outcome: ${outcome}`);
|
||||
assert.ok(Date.now() - started < 3500, "the redirect was followed until the timeout");
|
||||
});
|
||||
|
||||
// Executors that dispatch to a stored, self-hosted or upstream-supplied host. Each one must
|
||||
// go through dispatchRequest()/dispatchGuarded(); the counts are the bare fetch() calls that
|
||||
// remain, all of them liveness probes or fixed public hosts.
|
||||
const GUARDED_EXECUTORS: Record<string, number> = {
|
||||
"cliproxyapi.ts": 1, // healthCheck GET on the operator's local CLIProxyAPI
|
||||
"dario.ts": 1, // healthCheck GET
|
||||
"ghe-copilot.ts": 0,
|
||||
"github.ts": 2, // fixed api.github.com and GitHub OAuth token endpoints
|
||||
"gitlab.ts": 0,
|
||||
"glm.ts": 0,
|
||||
"ninerouter.ts": 1, // healthCheck GET
|
||||
"nlpcloud.ts": 0,
|
||||
"oneminai.ts": 0,
|
||||
};
|
||||
|
||||
const BARE_FETCH = /(^|[^.\w])fetch\(/;
|
||||
|
||||
function bareFetchCount(file: string): number {
|
||||
return fs
|
||||
.readFileSync(file, "utf8")
|
||||
.split("\n")
|
||||
.filter((line) => !line.trim().startsWith("//") && !line.trim().startsWith("*"))
|
||||
.filter((line) => BARE_FETCH.test(line)).length;
|
||||
}
|
||||
|
||||
test("dispatching executors keep their upstream calls behind the dispatch guard", () => {
|
||||
const dir = path.join(process.cwd(), "open-sse/executors");
|
||||
for (const [file, allowed] of Object.entries(GUARDED_EXECUTORS)) {
|
||||
const count = bareFetchCount(path.join(dir, file));
|
||||
assert.ok(count <= allowed, `${file} has ${count} bare fetch() calls, ${allowed} allowed`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user