Skip to content

fix: enrich session_end lifecycle hooks - #59715

Merged
jalehman merged 2 commits into
openclaw:mainfrom
jalehman:feat/session-end-lifecycle-hook
Apr 3, 2026
Merged

jalehman merged 2 commits into
openclaw:mainfrom
jalehman:feat/session-end-lifecycle-hook

Conversation

@jalehman

@jalehman jalehman commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

What

Make session_end the generic old-session lifecycle hook by adding optional reason, transcript, archive, and replacement metadata, then emit it consistently for reset, rollover, delete, and compaction-driven session replacement flows.

Why

Transcript-aware plugins need one stable hook whenever an old session is ending or being replaced. This keeps before_reset command-specific while giving plugins a post-archive path they can safely read.

Changes

  • Expand session_end event metadata
  • Emit enriched hooks for /new and /reset
  • Cover automatic daily and idle rollovers
  • Add gateway reset and delete parity
  • Emit lifecycle hooks for compaction session rotation

Testing

  • node scripts/test-parallel.mjs src/plugins/wired-hooks-session.test.ts src/auto-reply/reply/session-hooks-context.test.ts src/auto-reply/reply/session-updates.lifecycle.test.ts
  • node scripts/test-parallel.mjs src/gateway/server.sessions.gateway-server-sessions-a.test.ts -- -t "sessions.reset emits enriched session_end and session_start hooks|sessions.delete emits session_end with deleted reason and no replacement"
  • ./node_modules/.bin/oxlint src/plugins/types.ts src/auto-reply/reply/session-hooks.ts src/gateway/session-transcript-files.fs.ts src/gateway/session-archive.fs.ts src/gateway/session-archive.runtime.ts src/auto-reply/reply/session.ts src/gateway/session-reset-service.ts src/gateway/server-methods/sessions.ts src/auto-reply/reply/session-updates.ts src/auto-reply/reply/session-run-accounting.ts src/auto-reply/reply/agent-runner.ts src/auto-reply/reply/followup-runner.ts src/auto-reply/reply/agent-runner-memory.ts src/auto-reply/reply/commands-compact.ts src/plugins/wired-hooks-session.test.ts src/auto-reply/reply/session-hooks-context.test.ts src/auto-reply/reply/session-updates.lifecycle.test.ts src/gateway/server.sessions.gateway-server-sessions-a.test.ts
  • ./node_modules/.bin/oxfmt --check src/plugins/types.ts src/auto-reply/reply/session-hooks.ts src/gateway/session-transcript-files.fs.ts src/gateway/session-archive.fs.ts src/gateway/session-archive.runtime.ts src/auto-reply/reply/session.ts src/gateway/session-reset-service.ts src/gateway/server-methods/sessions.ts src/auto-reply/reply/session-updates.ts src/auto-reply/reply/session-run-accounting.ts src/auto-reply/reply/agent-runner.ts src/auto-reply/reply/followup-runner.ts src/auto-reply/reply/agent-runner-memory.ts src/auto-reply/reply/commands-compact.ts src/plugins/wired-hooks-session.test.ts src/auto-reply/reply/session-hooks-context.test.ts src/auto-reply/reply/session-updates.lifecycle.test.ts src/gateway/server.sessions.gateway-server-sessions-a.test.ts
  • pnpm check is currently blocked by unrelated existing pnpm tsgo failures in extensions/msteams/src/sdk.ts, src/agents/skills.test-helpers.ts, and src/agents/skills/local-loader.ts

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Apr 2, 2026
@jalehman jalehman self-assigned this Apr 2, 2026
@aisle-research-bot

aisle-research-bot Bot commented Apr 2, 2026 •

Copy link
Copy Markdown

🔒 Aisle Security Analysis

We found 2 potential security issue(s) in this PR:

# Severity Title
1 🟠 High Symlink traversal allows arbitrary file rename during session transcript archiving
2 🟡 Medium Filesystem path disclosure to plugins via session_end hook payload (sessionFile)
1. 🟠 Symlink traversal allows arbitrary file rename during session transcript archiving
Property Value
Severity High
CWE CWE-59
Location src/gateway/session-transcript-files.fs.ts:59-129

Description

archiveSessionTranscriptsDetailed() canonicalizes candidate paths using fs.realpathSync(), which resolves symlinks to their targets. It then archives (renames) the resolved path.

This is dangerous because:

  • A transcript path inside the sessions directory can be replaced with a symlink pointing to an arbitrary file outside the sessions directory (e.g., /etc/passwd).
  • canonicalizePathForComparison() resolves that symlink to the target path.
  • archiveFileOnDisk() then calls fs.renameSync() on the resolved (target) path, effectively moving/renaming the external file.
  • restrictToStoreDir is optional and defaults to false at call sites shown in the diff, so there is no containment check by default.

Vulnerable code path:

  • input: candidate transcript path under sessions dir (potentially attacker-controlled via filesystem symlink)
  • transformation: fs.realpathSync() resolves symlink to external target
  • sink: fs.renameSync() mutates (moves) the external target file

Vulnerable code:

function canonicalizePathForComparison(filePath: string): string {
  const resolved = path.resolve(filePath);
  try {
    return fs.realpathSync(resolved);
  } catch {
    return resolved;
  }
}
...
const candidatePath = canonicalizePathForComparison(candidate);
...
archivedPath: archiveFileOnDisk(candidatePath, opts.reason),
...
fs.renameSync(filePath, archived);

Impact: arbitrary file move/rename (and effectively deletion from original location) on the host filesystem when a reset/delete triggers transcript archiving.

Recommendation

Avoid resolving symlinks for the path you later mutate, and explicitly reject symlinks before archiving.

Suggested approach:

  1. Use path.resolve() / normalization for comparisons, but do not convert the actual file operation path into its realpath.
  2. Before archiving, lstat the candidate and reject if it is a symlink; also ensure it is a regular file.
  3. Strongly consider defaulting restrictToStoreDir: true for maintenance operations.

Example hardening:

function resolveForComparison(p: string): string {
  return path.resolve(p);
}

function assertSafeRegularFile(p: string): void {
  const st = fs.lstatSync(p);
  if (st.isSymbolicLink()) throw new Error("refusing to archive symlink");
  if (!st.isFile()) throw new Error("refusing to archive non-file");
}

​// In archive loop:
const opPath = path.resolve(candidate);           // path used for rename
const comparePath = resolveForComparison(candidate);
if (storeDir) {
  const rel = path.relative(storeDir, comparePath);
  if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) continue;
}
if (!fs.existsSync(opPath)) continue;
assertSafeRegularFile(opPath);
archivedPath: archiveFileOnDisk(opPath, opts.reason);

This prevents symlink targets outside the sessions directory from being renamed.

2. 🟡 Filesystem path disclosure to plugins via session_end hook payload (sessionFile)
Property Value
Severity Medium
CWE CWE-200
Location src/gateway/session-reset-service.ts:96-134

Description

The session_end plugin hook payload now includes sessionFile, which is derived from on-disk transcript paths and canonicalized with path.resolve()/fs.realpathSync(). This can expose sensitive internal filesystem information (absolute paths, usernames/home directories, container mount points) to plugins.

Why this matters:

  • Hook handlers are plugin-provided code and may forward events to third-party services (e.g., via HTTP/webhook integrations).
  • The emitted sessionFile can be an archived path (e.g., *.jsonl.reset.* / *.jsonl.deleted.*) or an existing transcript path, and is not redacted.

Vulnerable flow:

  • Source: transcript path derived from session store / filesystem (resolveStableSessionEndTranscript() uses realpathSync)
  • Sink: delivered to plugins as event.sessionFile via runSessionEnd()

Recommendation

Avoid exposing raw filesystem paths across plugin boundaries.

Options (pick based on product needs):

  1. Redact: omit sessionFile from hook payloads entirely.
  2. Minimize: provide only a safe identifier (e.g., transcriptId or basename) and a boolean transcriptArchived.
  3. Make relative: if consumers need a path, return a path relative to the session store directory (never absolute), and/or gate it behind an explicit config capability.

Example (minimize to basename + relative path):

​// When building hook payload:
const safeSessionFile = transcript.sessionFile
  ? path.basename(transcript.sessionFile)
  : undefined;

const payload = buildSessionEndHookPayload({
  ...,
  sessionFile: safeSessionFile,
});

Also consider adding a plugin capability/permission like expose_filesystem_paths: true and default it to false.


Analyzed PR: #59715 at commit e1ca661

Last updated on: 2026-04-02T14:27:21Z

@greptile-apps

greptile-apps Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR enriches session_end hook events with reason, sessionFile, transcriptArchived, nextSessionId, and nextSessionKey metadata, then consistently emits these enriched hooks across /new, /reset, automatic daily/idle rollovers, gateway reset/delete, and compaction-driven session rotation. The implementation is well-structured, with proper early-return guards before hook emission and a clean fallback chain in resolveStableSessionEndTranscript that handles both archived and live-transcript scenarios. Test coverage spans all the new code paths.

Confidence Score: 5/5

  • Safe to merge — all changes are additive (new optional fields, new fire-and-forget hook calls), existing behavior is preserved, and the changed code paths are well-tested.
  • No P0 or P1 defects found. The early guard in incrementCompactionCount (if (!sessionStore || !sessionKey) return) correctly narrows both optionals before the hook emission, eliminating the main runtime risk. Logic for stale-reason disambiguation and archive-path resolution is correct. All remaining observations are P2 style/hardening suggestions.
  • No files require special attention.

Reviews (1): Last reviewed commit: "Hooks: enrich session_end lifecycle hook" | Re-trigger Greptile

@jalehman
jalehman force-pushed the feat/session-end-lifecycle-hook branch 4 times, most recently from 791192b to 052c773 Compare April 3, 2026 06:29
@jalehman
jalehman force-pushed the feat/session-end-lifecycle-hook branch from 052c773 to b3ef62b Compare April 3, 2026 07:15
@jalehman
jalehman merged commit 2b28e75 into openclaw:main Apr 3, 2026
25 checks passed
@jalehman

jalehman commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Thanks @jalehman!

@jalehman
jalehman deleted the feat/session-end-lifecycle-hook branch April 3, 2026 07:16
ngutman pushed a commit that referenced this pull request Apr 3, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
steipete pushed a commit to duncanita/openclaw that referenced this pull request Apr 4, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
Merged via squash.

Prepared head SHA: b3ef62b
Co-authored-by: jalehman <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant