Skip to content

Commit c00dd28

Browse files
fix(agents): enforce strict session ownership (#123222)
Co-authored-by: RoboClaw <[email protected]>
1 parent 9877d6f commit c00dd28

2 files changed

Lines changed: 67 additions & 7 deletions

File tree

‎src/agents/session-agent-binding.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
33
import { resolveBoundAgentIdForSession } from "./session-agent-binding.js";
44

55
describe("resolveBoundAgentIdForSession", () => {
6+
const multiAgentConfig = {
7+
agents: { entries: { main: {}, research: {} } },
8+
} satisfies OpenClawConfig;
9+
const singleAgentConfig = {
10+
agents: { entries: { main: {} } },
11+
} satisfies OpenClawConfig;
12+
13+
it.each([
14+
{
15+
name: "an explicit agent id",
16+
params: { config: multiAgentConfig, agentId: "research", sessionKey: "legacy-session" },
17+
expected: "research",
18+
},
19+
{
20+
name: "an agent-scoped key",
21+
params: { config: multiAgentConfig, sessionKey: "agent:research:session:abc" },
22+
expected: "research",
23+
},
24+
{
25+
name: "the canonical main key",
26+
params: { config: singleAgentConfig, sessionKey: "main" },
27+
expected: "main",
28+
},
29+
{
30+
name: "the configured main alias",
31+
params: {
32+
config: { ...singleAgentConfig, session: { mainKey: "inbox" } },
33+
sessionKey: "inbox",
34+
},
35+
expected: "main",
36+
},
37+
])("binds $name", ({ params, expected }) => {
38+
expect(resolveBoundAgentIdForSession(params)).toBe(expected);
39+
});
40+
641
it("binds a bare global key to the persisted fixed-store owner", () => {
742
const config = {
843
agents: {
@@ -15,4 +50,13 @@ describe("resolveBoundAgentIdForSession", () => {
1550

1651
expect(resolveBoundAgentIdForSession({ config, sessionKey: "global" })).toBe("ops");
1752
});
53+
54+
it("does not grant default-agent authority to an arbitrary unscoped key", () => {
55+
expect(
56+
resolveBoundAgentIdForSession({ config: singleAgentConfig, sessionKey: "legacy-session" }),
57+
).toBeUndefined();
58+
expect(
59+
resolveBoundAgentIdForSession({ config: multiAgentConfig, sessionKey: "legacy-session" }),
60+
).toBeUndefined();
61+
});
1862
});

‎src/agents/session-agent-binding.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
*
44
* Derives the trusted active agent from explicit agent ids, agent session keys, or configured main-session aliases.
55
*/
6-
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
6+
import {
7+
normalizeLowercaseStringOrEmpty,
8+
normalizeOptionalString,
9+
} from "@openclaw/normalization-core/string-coerce";
10+
import { resolvePersistedSessionStoreOwnerForKey } from "../config/sessions/session-store-owner.js";
711
import type { OpenClawConfig } from "../config/types.openclaw.js";
12+
import { normalizeMainKey, parseAgentSessionKey } from "../routing/session-key.js";
813
import { resolveSessionAgentId } from "./agent-scope.js";
914

1015
/**
@@ -15,12 +20,23 @@ export function resolveBoundAgentIdForSession(params: {
1520
sessionKey?: string;
1621
agentId?: string;
1722
}): string | undefined {
18-
if (!normalizeOptionalString(params.agentId) && !normalizeOptionalString(params.sessionKey)) {
23+
const config = params.config ?? {};
24+
const agentId = normalizeOptionalString(params.agentId);
25+
const sessionKey = normalizeOptionalString(params.sessionKey);
26+
if (!agentId && !sessionKey) {
1927
return undefined;
2028
}
21-
return resolveSessionAgentId({
22-
config: params.config,
23-
sessionKey: params.sessionKey,
24-
agentId: params.agentId,
25-
});
29+
if (agentId) {
30+
return resolveSessionAgentId({ config, sessionKey, agentId });
31+
}
32+
33+
const persistedOwner = resolvePersistedSessionStoreOwnerForKey(config, sessionKey);
34+
const loweredSessionKey = normalizeLowercaseStringOrEmpty(sessionKey);
35+
const mainKey = normalizeMainKey(config.session?.mainKey);
36+
const hasTrustedBinding =
37+
Boolean(parseAgentSessionKey(sessionKey)?.agentId) ||
38+
persistedOwner.kind !== "none" ||
39+
loweredSessionKey === "main" ||
40+
loweredSessionKey === mainKey;
41+
return hasTrustedBinding ? resolveSessionAgentId({ config, sessionKey }) : undefined;
2642
}

0 commit comments

Comments
 (0)