#21662: fix(gateway): validate session key ownership against agent scope
gateway
size: XS
Cluster:
OpenClaw Plugin Enhancements
## Summary
- `resolveSessionKey()` accepted any value from the `x-openclaw-session-key` header without validating it belongs to the authenticated agent's scope, allowing cross-agent session injection.
- Added validation that the explicit session key starts with the expected `agent:<agentId>:` prefix using `normalizeAgentId()` for consistent comparison.
- If the explicit key does not match the agent's scope, the function falls through to default key generation instead of accepting the arbitrary value.
## Test plan
- [ ] Verify that a valid session key with the correct agent prefix is accepted (e.g., `agent:myagent:openai-user:alice`)
- [ ] Verify that a session key targeting a different agent (e.g., `agent:otheragent:openai-user:alice`) is rejected and the default key is generated instead
- [ ] Verify that when no `x-openclaw-session-key` header is present, default key generation works as before
- [ ] Verify both OpenAI and OpenResponses gateway endpoints correctly validate session keys
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes session key injection vulnerability by validating that explicit `x-openclaw-session-key` headers match the authenticated agent's scope.
**Key changes:**
- Added validation in `resolveSessionKey()` to check explicit session keys start with expected agent prefix
- Uses `normalizeAgentId()` for consistent comparison
- Falls through to default key generation when explicit key doesn't match agent scope
**Test coverage gap:**
The existing test (line 174-190 in `openai-http.e2e.test.ts`) only validates the happy path where the session key matches the agent. Missing test case: verify that a session key targeting a different agent is rejected and falls through to default key generation.
<h3>Confidence Score: 4/5</h3>
- Safe to merge - fixes a real security vulnerability with clean implementation
- The fix correctly validates session key ownership against agent scope, preventing cross-agent session injection. Import reordering is cosmetic. Only minor concern is missing test coverage for the negative case (mismatched agent rejection), but the implementation logic is sound and follows existing patterns.
- No files require special attention
<sub>Last reviewed commit: 1250701</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#11794: fix(gateway): validate session key ownership against authenticated ...
by coygeek · 2026-02-08
84.2%
#16171: fix: trust absolute sessionFile paths in multi-agent setups [AI-ass...
by iJaack · 2026-02-14
79.4%
#19200: fix(cli): validate --session-id is not a session key format
by Elarwei001 · 2026-02-17
78.9%
#22098: fix: isolate agent sessions by explicit --session-id
by AIflow-Labs · 2026-02-20
78.8%
#20431: fix(sessions): add session contamination guards and self-leak lock ...
by marcomarandiz · 2026-02-18
78.8%
#15941: fix(sessions): allow session file paths from other agents' sessions...
by LiJianLi128 · 2026-02-14
78.7%
#15176: fix(sessions): allow channel-routed session IDs and cross-agent paths
by cathrynlavery · 2026-02-13
78.4%
#20072: feat(sessions_spawn): add sessionKey param to reuse sub-agent sessions
by Be1Human · 2026-02-18
77.6%
#15744: fix: allow cross-agent session path validation
by scottgl9 · 2026-02-13
77.3%
#8774: Fix/frontend session key normalization
by zhaodageng · 2026-02-04
76.8%