Skip to content

Commit a9da10e

Browse files
authored
fix(gateway): bind response history to verified proxy identity (#118607)
1 parent 9c22a43 commit a9da10e

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎src/gateway/http-auth-utils.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function getBearerToken(req: IncomingMessage): string | undefined {
5252
type SharedSecretGatewayAuth = Pick<ResolvedGatewayAuth, "mode">;
5353
export type AuthorizedGatewayHttpRequest = {
5454
authMethod?: GatewayAuthResult["method"];
55+
user?: string;
5556
trustDeclaredOperatorScopes: boolean;
5657
controlUiPluginGrants?: ControlUiPluginTabAuthGrant[];
5758
controlUiPluginGrant?: ControlUiPluginTabAuthGrant;
@@ -258,6 +259,7 @@ async function checkGatewayHttpRequestAuthWith(
258259
ok: true,
259260
requestAuth: {
260261
authMethod: authResult.method,
262+
...(authResult.user ? { user: authResult.user } : {}),
261263
// Shared-secret bearer auth proves possession of the gateway secret, but it
262264
// does not prove a narrower per-request operator identity. HTTP endpoints
263265
// must opt in explicitly if they want to treat that shared-secret path as a

‎src/gateway/http-utils.authorize-request.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe("authorizeGatewayHttpRequestOrReply", () => {
8888
}),
8989
).resolves.toEqual({
9090
authMethod: "trusted-proxy",
91+
user: "operator",
9192
trustDeclaredOperatorScopes: true,
9293
});
9394
});

‎src/gateway/openresponses-http.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,56 @@ describe("OpenResponses HTTP API (e2e)", () => {
18651865
}
18661866
}
18671867

1868+
agentCommand.mockClear();
1869+
agentCommand.mockResolvedValue({ payloads: [{ text: "hello" }] } as never);
1870+
const forwardedHeaders = {
1871+
"x-forwarded-proto": "https",
1872+
authorization: "Bearer forwarded-untrusted",
1873+
};
1874+
const aliceResponse = await postResponses(
1875+
port,
1876+
{ model: "openclaw", user: "alice", input: "private alice history" },
1877+
{ ...forwardedHeaders, "x-forwarded-user": "[email protected]" },
1878+
);
1879+
expect(aliceResponse.status).toBe(200);
1880+
const aliceResponseId = ((await aliceResponse.json()) as { id: string }).id;
1881+
const aliceSessionKey = requireSessionKey(
1882+
firstAgentOpts().sessionKey as string | undefined,
1883+
"Alice trusted-proxy response",
1884+
);
1885+
1886+
const aliceContinuation = await postResponses(
1887+
port,
1888+
{
1889+
model: "openclaw",
1890+
user: "alice",
1891+
previous_response_id: aliceResponseId,
1892+
input: "continue alice history",
1893+
},
1894+
{
1895+
...forwardedHeaders,
1896+
authorization: "Bearer different-forwarded-untrusted",
1897+
"x-forwarded-user": "[email protected]",
1898+
},
1899+
);
1900+
expect(aliceContinuation.status).toBe(200);
1901+
await ensureResponseConsumed(aliceContinuation);
1902+
1903+
const bobContinuation = await postResponses(
1904+
port,
1905+
{
1906+
model: "openclaw",
1907+
user: "bob",
1908+
previous_response_id: aliceResponseId,
1909+
input: "attempt alice history",
1910+
},
1911+
{ ...forwardedHeaders, "x-forwarded-user": "[email protected]" },
1912+
);
1913+
expect(bobContinuation.status).toBe(200);
1914+
await ensureResponseConsumed(bobContinuation);
1915+
expect(firstAgentOpts(2).sessionKey).not.toBe(aliceSessionKey);
1916+
expect(firstAgentOpts(1).sessionKey).toBe(aliceSessionKey);
1917+
18681918
agentCommand.mockClear();
18691919
const unauthorized = await postResponses(
18701920
port,

‎src/gateway/openresponses-http.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
} from "./http-common.js";
5353
import { handleGatewayPostJsonEndpoint } from "./http-endpoint-helpers.js";
5454
import {
55+
type AuthorizedGatewayHttpRequest,
5556
authorizeOpenAiCompatibleHttpModelOverride,
5657
getBearerToken,
5758
getHeader,
@@ -126,27 +127,27 @@ function normalizeResponseSessionScope(scope: ResponseSessionScope): ResponseSes
126127
function resolveResponseSessionAuthSubject(params: {
127128
req: IncomingMessage;
128129
auth: ResolvedGatewayAuth;
130+
requestAuth: AuthorizedGatewayHttpRequest;
129131
}): string {
132+
// Proxy-verified identity owns continuation; forwarded bearers are unverified.
133+
if (params.requestAuth.authMethod === "trusted-proxy") {
134+
return `trusted-proxy:${params.requestAuth.user}`;
135+
}
130136
const bearer = getBearerToken(params.req);
131137
if (bearer) {
132138
return `bearer:${createHash("sha256").update(bearer).digest("hex")}`;
133139
}
134-
if (params.auth.mode === "trusted-proxy" && params.auth.trustedProxy?.userHeader) {
135-
const user = getHeader(params.req, params.auth.trustedProxy.userHeader)?.trim();
136-
if (user) {
137-
return `trusted-proxy:${user}`;
138-
}
139-
}
140140
return `gateway-auth:${params.auth.mode}`;
141141
}
142142

143143
function createResponseSessionScope(params: {
144144
req: IncomingMessage;
145145
auth: ResolvedGatewayAuth;
146+
requestAuth: AuthorizedGatewayHttpRequest;
146147
agentId: string;
147148
}): ResponseSessionScope {
148149
return normalizeResponseSessionScope({
149-
authSubject: resolveResponseSessionAuthSubject({ req: params.req, auth: params.auth }),
150+
authSubject: resolveResponseSessionAuthSubject(params),
150151
agentId: params.agentId,
151152
requestedSessionKey: getHeader(params.req, "x-openclaw-session-key"),
152153
});
@@ -647,6 +648,7 @@ export async function handleOpenResponsesHttpRequest(
647648
const responseSessionScope = createResponseSessionScope({
648649
req,
649650
auth: opts.auth,
651+
requestAuth: handled.requestAuth,
650652
agentId: resolved.agentId,
651653
});
652654
// Resolve session key: reuse previous_response_id only when it matches the

0 commit comments

Comments
 (0)