T05 · Unauthorized Access and Privilege Escalation
- Location
hook/handler.ts:34- Finding
Path Traversal Through an Unvalidated Agent Identifier
- Content
View full analysis
:: function resolveAgentId(event: any): string { const ctxAgent = event?.context?.agentId; if (ctxAgent && String(ctxAgent).trim()) return String(ctxAgent).trim(); const sk = event?.sessionKey ? String(event.sessionKey) : ""; const m = sk.match(/^agent:([^:]+):/); if (m) return m[1]; const envAgent = process.env.OPENCLAW_AGENT_ID && process.env.OPENCLAW_AGENT_ID.trim(); if (envAgent) return envAgent; return "main"; } function sessionsFileFor(agentId: string): string { return path.join(openclawHome(), "agents", agentId, "sessions", "sessions.json"); } ``` The path is subsequently read: ```ts const agentId = resolveAgentId(event); const sessionsFile = sessionsFileFor(agentId); let sessions: any; try { sessions = JSON.parse(fs.readFileSync(sessionsFile, "utf8")); } catch { return; } ``` It is later written without validating that it remains under the expected agents directory: ```ts try { const fresh = JSON.parse(fs.readFileSync(sessionsFile, "utf8")); const target = fresh[sessionKey]; if (!target) return; if (target.label && String(target.label).trim()) return; target.label = label; fs.writeFileSync(sessionsFile, JSON.stringify(fresh, null, 2), "utf8"); } catch {} ``` ### Technical Analysis The hook treats `event.context.agentId` as a trusted filesystem path component. If that field is absent, it also accepts an agent identifier parsed from `event.sessionKey`. Neither source is restricted to a safe identifier syntax. An identifier containing parent-directory components or path separators can cause `path.join() ...[truncated 2579 chars]- Remediation
View remediation
