mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-20 18:28:43 +00:00
Compare commits
3 Commits
dependabot
...
fix/api-br
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6475dbfa0f | ||
|
|
bd49e1bb34 | ||
|
|
fa084e324f |
1
changelog.d/fixes/20793-api-port-dot-segments.md
Normal file
1
changelog.d/fixes/20793-api-port-dot-segments.md
Normal file
@@ -0,0 +1 @@
|
||||
- **fix(api):** The API-only port (`API_PORT`) refuses a request path that contains a dot segment (`.` or `..`, literal or percent-encoded) or a fragment marker before it applies its route allowlist, for plain requests and WebSocket upgrades. The port relays a path exactly as it validated it ([#20793](https://github.com/cheaper-inference/OmniRoute/pull/20793))
|
||||
@@ -32,6 +32,43 @@ function isOpenAiCompatiblePath(pathname: string): boolean {
|
||||
return OPENAI_COMPAT_PATHS.some((pattern) => pattern.test(pathname));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the path carries a `.` or `..` segment, literal or percent-encoded.
|
||||
*
|
||||
* The allowlist is matched against the path as the client sent it, and that same
|
||||
* path is relayed untouched, so a dot segment would let the server behind the
|
||||
* bridge resolve the request to a route the allowlist never saw. Only the
|
||||
* encodings that can hide a separator or a dot are unwrapped; the loop is bounded
|
||||
* and cannot throw on malformed escapes.
|
||||
*/
|
||||
export function hasDotSegment(pathname: string): boolean {
|
||||
let current = pathname;
|
||||
for (let pass = 0; pass < 4; pass += 1) {
|
||||
const next = current
|
||||
.replace(/%25/gi, "%")
|
||||
.replace(/%2e/gi, ".")
|
||||
.replace(/%2f/gi, "/")
|
||||
.replace(/%5c/gi, "\\");
|
||||
if (next === current) break;
|
||||
current = next;
|
||||
}
|
||||
return current.split(/[/\\]/).some((segment) => segment === "." || segment === "..");
|
||||
}
|
||||
|
||||
/**
|
||||
* A request is relayed only when its path is free of dot segments and on the allowlist.
|
||||
*
|
||||
* A path that carries a fragment marker is refused as well. A request target has no
|
||||
* fragment, and parsers disagree on where such a path ends, so the path this function
|
||||
* validated would not be the path the server behind the bridge resolves. A marker after
|
||||
* the `?` belongs to the query for every parser and is left alone.
|
||||
*/
|
||||
export function isRelayablePath(rawUrl: string): boolean {
|
||||
const pathname = rawUrl.split("?")[0] || "/";
|
||||
if (pathname.includes("#")) return false;
|
||||
return !hasDotSegment(pathname) && isOpenAiCompatiblePath(pathname);
|
||||
}
|
||||
|
||||
function requestWantsStreaming(req: IncomingMessage): boolean {
|
||||
const accept = String(req.headers.accept || "").toLowerCase();
|
||||
if (accept.includes("text/event-stream")) return true;
|
||||
@@ -260,10 +297,8 @@ export function createApiBridgeServer(dashboardPort: number): http.Server {
|
||||
// HMR/bfcache) on the request/response streams so they never surface as an
|
||||
// uncaughtException that kills the server (#fix-dev-server-aborted).
|
||||
attachRequestStreamGuards(req, res);
|
||||
const rawUrl = req.url || "/";
|
||||
const pathname = rawUrl.split("?")[0] || "/";
|
||||
|
||||
if (!isOpenAiCompatiblePath(pathname)) {
|
||||
if (!isRelayablePath(req.url || "/")) {
|
||||
res.writeHead(404, { "content-type": "application/json" });
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
@@ -281,10 +316,7 @@ export function createApiBridgeServer(dashboardPort: number): http.Server {
|
||||
server.keepAliveTimeout = API_BRIDGE_TIMEOUTS.serverKeepAliveTimeoutMs;
|
||||
server.setTimeout(API_BRIDGE_TIMEOUTS.serverSocketTimeoutMs);
|
||||
server.on("upgrade", (req, socket, head) => {
|
||||
const rawUrl = req.url || "/";
|
||||
const pathname = rawUrl.split("?")[0] || "/";
|
||||
|
||||
if (!isOpenAiCompatiblePath(pathname)) {
|
||||
if (!isRelayablePath(req.url || "/")) {
|
||||
writeUpgradeProxyError(
|
||||
socket,
|
||||
404,
|
||||
|
||||
@@ -96,3 +96,129 @@ test("dashboard and management routes stay blocked on the API port", async () =>
|
||||
assert.deepEqual(relayed, []);
|
||||
});
|
||||
});
|
||||
|
||||
test("paths with dot segments never reach the dashboard port", async () => {
|
||||
await withBridge(async (apiPort, relayed) => {
|
||||
const refused = [
|
||||
"/v1/../api/keys",
|
||||
"/v1/./../api/keys",
|
||||
"/v1/%2e%2e/api/keys",
|
||||
"/v1/%2E%2E/api/keys",
|
||||
"/v1/..%2fapi/keys",
|
||||
"/v1/..%5capi/keys",
|
||||
"/v1/..\\api/keys",
|
||||
"/v1/%252e%252e/api/keys",
|
||||
"/v1beta/../api/keys",
|
||||
"/codex/../api/keys",
|
||||
"/api/oauth/../keys",
|
||||
"/v1/.",
|
||||
];
|
||||
for (const path of refused) {
|
||||
const res = await get(apiPort, path);
|
||||
assert.equal(res.status, 404, `${path} should be refused`);
|
||||
assert.equal(JSON.parse(res.body).error, "not_found");
|
||||
}
|
||||
assert.deepEqual(relayed, []);
|
||||
});
|
||||
});
|
||||
|
||||
test("paths that only look like dot segments are still relayed", async () => {
|
||||
await withBridge(async (apiPort, relayed) => {
|
||||
const paths = [
|
||||
"/v1/models/a..b",
|
||||
"/v1/files/.hidden",
|
||||
"/v1/models/openai%2Fgpt-4o",
|
||||
"/v1/chat/completions?next=../y",
|
||||
];
|
||||
for (const path of paths) {
|
||||
const res = await get(apiPort, path);
|
||||
assert.equal(res.status, 200, `${path} should be relayed`);
|
||||
}
|
||||
assert.deepEqual(relayed, paths);
|
||||
});
|
||||
});
|
||||
|
||||
test("a path with a fragment marker is refused", async () => {
|
||||
await withBridge(async (apiPort, relayed) => {
|
||||
// A request target has no fragment, and parsers disagree on where a path with "#" ends:
|
||||
// one reads "/v1/..#" as "/v1/..", another reads "/v1/x#/../../api/keys" as "/api/keys".
|
||||
const refused = [
|
||||
"/v1/..#",
|
||||
"/v1/..#x",
|
||||
"/v1/%2e%2e#",
|
||||
"/v1beta/.#",
|
||||
"/codex/..#",
|
||||
"/api/oauth/..#/keys",
|
||||
"/v1/models/..#?x=1",
|
||||
"/v1/x#/../../api/keys",
|
||||
"/v1/models#section",
|
||||
];
|
||||
for (const path of refused) {
|
||||
const res = await get(apiPort, path);
|
||||
assert.equal(res.status, 404, `${path} should be refused`);
|
||||
}
|
||||
assert.deepEqual(relayed, []);
|
||||
|
||||
// After the "?" the marker is part of the query for every parser, so it changes nothing.
|
||||
const allowed = ["/v1/chat/completions?next=1#../y"];
|
||||
for (const path of allowed) {
|
||||
const res = await get(apiPort, path);
|
||||
assert.equal(res.status, 200, `${path} should be relayed`);
|
||||
}
|
||||
assert.deepEqual(relayed, allowed);
|
||||
});
|
||||
});
|
||||
|
||||
test("isRelayablePath refuses a fragment marker in the path and ignores one in the query", () => {
|
||||
assert.equal(bridge.isRelayablePath("/v1/..#"), false);
|
||||
assert.equal(bridge.isRelayablePath("/v1/models#"), false);
|
||||
assert.equal(bridge.isRelayablePath("/v1/x#/../../api/keys"), false);
|
||||
assert.equal(bridge.isRelayablePath("/api/keys#/v1/"), false);
|
||||
assert.equal(bridge.isRelayablePath("/v1/..?x"), false);
|
||||
assert.equal(bridge.isRelayablePath("/v1/x?a=1#/../api/keys"), true);
|
||||
assert.equal(bridge.isRelayablePath("/v1/models"), true);
|
||||
});
|
||||
|
||||
test("hasDotSegment unwraps nested encodings and never throws", () => {
|
||||
assert.equal(bridge.hasDotSegment("/v1/%25252e%25252e/x"), true);
|
||||
assert.equal(bridge.hasDotSegment("/v1/%zz/x"), false);
|
||||
assert.equal(bridge.hasDotSegment("/v1/models"), false);
|
||||
assert.equal(bridge.hasDotSegment("/v1/models/a..b"), false);
|
||||
});
|
||||
|
||||
test("an upgrade request with a dot segment is refused before reaching the dashboard", async () => {
|
||||
const upgrades: string[] = [];
|
||||
const dashboard = http.createServer();
|
||||
dashboard.on("upgrade", (req, socket) => {
|
||||
upgrades.push(req.url || "");
|
||||
socket.destroy();
|
||||
});
|
||||
await new Promise<void>((resolve) => dashboard.listen(0, "127.0.0.1", resolve));
|
||||
const dashboardPort = (dashboard.address() as net.AddressInfo).port;
|
||||
const apiServer = bridge.createApiBridgeServer(dashboardPort);
|
||||
await new Promise<void>((resolve) => apiServer.listen(0, "127.0.0.1", resolve));
|
||||
const apiPort = (apiServer.address() as net.AddressInfo).port;
|
||||
try {
|
||||
const response = await new Promise<string>((resolve, reject) => {
|
||||
const socket = net.connect(apiPort, "127.0.0.1");
|
||||
let data = "";
|
||||
socket.setEncoding("utf8");
|
||||
socket.on("connect", () => {
|
||||
socket.write(
|
||||
"GET /v1/../api/keys HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n"
|
||||
);
|
||||
});
|
||||
socket.on("data", (chunk) => (data += chunk));
|
||||
socket.on("error", reject);
|
||||
socket.on("close", () => resolve(data));
|
||||
});
|
||||
assert.match(response, /^HTTP\/1\.1 404 /);
|
||||
assert.match(response, /"error":"not_found"/);
|
||||
assert.deepEqual(upgrades, []);
|
||||
} finally {
|
||||
apiServer.close();
|
||||
apiServer.closeAllConnections();
|
||||
dashboard.close();
|
||||
dashboard.closeAllConnections();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user