Skip to content

fix(sessions): escape terminal controls in cleanup store headers - #184747

Closed
SunnyShu0925 wants to merge 3 commits into
openclaw:mainfrom
SunnyShu0925:fix/sessions-cleanup-store-terminal-escape-238
Closed

SunnyShu0925 wants to merge 3 commits into
openclaw:mainfrom
SunnyShu0925:fix/sessions-cleanup-store-terminal-escape-238

Conversation

@SunnyShu0925

@SunnyShu0925 SunnyShu0925 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes terminal actions and forged header lines when a session store path (or agent id) passed to openclaw sessions cleanup contains control characters. The human-readable Session store: and Agent: headers were interpolated raw, so an ANSI escape or bell byte in the store selector could reach the terminal.

User Impact

openclaw sessions cleanup human output now safely renders the store path and agent id in Agent: / Session store: headers. JSON output and store selection are unchanged; the underlying path is not modified.

Why This Change Was Made

The file already imports the canonical terminal sanitizer and applies it to the Session label: header in the same renderer. This change applies that same policy to the Agent: and Session store: headers at their existing output owners, so both the gateway-delegated apply result and the local dry-run preview follow one consistent rule.

Evidence

Real CLI proof — both changed renderers (shipped binary, current head 81e5197). The dry-run preview and the apply result each render the Session store: header through a changed output owner; both are proven through the shipped CLI with the same control-char store path (saved as sess + ESC[31m ANSI red + ions + BEL + .sqlite), A/B: sanitizer wired (post-fix) vs unwrapped at the same owner (pre-fix). Both captures were recorded on the current head (81e5197); the production patch is byte-identical across 9d2c650 → 81e5197 (later commits only harden the regression test), so the post-fix output is the current head's behavior.

Dry-run preview (openclaw sessions cleanup --dry-run, control-char session.store config path that does not exist on disk, so it resolves as an in-memory preview):

lane Session store: header (raw terminal)
pre-fix /tmp/oc-proof-184747/agents/main/agent/sess^[[31mions^G.sqlite
post-fix /tmp/oc-proof-184747/agents/main/agent/sessions.sqlite

Apply result (openclaw sessions cleanup --store <real-init-store-with-control-chars> --enforce, stays local and renders renderAppliedResult):

lane Session store: header (raw terminal)
pre-fix /tmp/oc-proof-184747/agents/main/agent/sess^[[31mions^G.sqlite
post-fix /tmp/oc-proof-184747/agents/main/agent/sessions.sqlite

Both runs reach completion (dry-run: Maintenance mode: enforce / Entries: 0 -> 0; apply: Applied maintenance. Current entries: 0, exit 0). Pre-fix passes ^[[31m / ^G through verbatim; post-fix strips the control bytes and renders clean sessions.sqlite (cat -v confirms no ^[ / ^G). The Agent: header is the same expression shape (Agent: ${sanitizeTerminalText(summary.agentId)}) at the same output owners; a multi-store dry-run proves a control-bearing agent id is also sanitized in that header.

Regression (P3 + Agent-header coverage). Two stale/missing assertions were hardened. (1) The dry-run store assertion previously used vi.clearAllMocks() then a whole-array find — but clearAllMocks() does not clear the plain logs array, so the assertion could read the apply lane's own sanitized line. It now records appliedLength = logs.length before the dry-run invocation and searches only logs.slice(appliedLength). (2) The Agent: header was not asserted at all; a multi-store dry-run (previewResults.length > 1) now feeds a control-bearing agent id and asserts the rendered header is clean. Both verified bidirectional: with the respective dry-run sanitizer removed, the store-path assertion fails (expected "Session store: sessions-café\noperator-forged\tend\rrow" not to match /\p{Cc}/u) and the Agent-header assertion fails (expected 'Agent: worker\u001b[31m\u0007main' not to match /\p{Cc}/u); restored, all 14 tests pass.

  • Targeted oxlint, oxfmt, and git diff --check pass. Production change is net-zero LOC: four existing output expressions are wrapped in the already-imported sanitizer.
  • Open PR collision check: no open PR touches src/commands/sessions-cleanup.ts. The upstream family PR fix(sessions): escape terminal controls in store headers #184139 (merged) modified sessions.ts and secrets-store-cli.ts, not this file.

Human-readable sessions cleanup output inserted the store path and agent id
verbatim into runtime.log, so a store path carrying C0/C1 terminal controls
(ESC color sequences, BEL, newline/tab) could forge terminal output and break
single-line formatting. renderAppliedResult and the dry-run path now route both
the store path and agent id through sanitizeTerminalText, matching the existing
label handling in the same file and the store-header fix in sessions.ts.
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: XS labels Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Sanitize agent IDs and store paths in human-readable session-cleanup headers.

Example: Run cleanup with a SQLite filename containing ESC[31m and BEL between “sess” and “ions.sqlite”.

  • Before: The store header emits terminal color and bell controls.
  • After: The header displays “sessions.sqlite” without those controls while cleanup uses the original path.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused, correct repair with relevant CLI evidence and regression coverage; required CI remains pending in the supplied context.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): Current-head CLI output exercises control-bearing store paths through both dry-run preview and local apply, showing clean headers and successful completion; renderer regressions additionally cover delegated summaries and the multi-store Agent header.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Control characters in store paths can trigger terminal actions or forge header lines during cleanup.
Reason: The change extends the established terminal-output contract with four existing sanitizer calls and no new machinery.

Merge readiness

✅ Ready for maintainer review

Keep this PR: it fixes raw terminal-control output still present on current main, using the existing sanitizer without changing cleanup behavior; no actionable defects found.

Priority: P2
Reviewed head: 81e519713286dbb2a111ce060d8026ceb40df21f

Before merge

None.

Findings

None.

Agent review details

How this fits together

The sessions cleanup CLI receives local previews or applied cleanup summaries and renders human-readable headers or unchanged JSON results.

flowchart TD
  A[Cleanup CLI options] --> B[Cleanup command]
  B --> C[Local cleanup or Gateway]
  C --> D[Cleanup summaries]
  D --> E[Sanitized human headers]
  D --> F[JSON output]
Loading

Technical review

Best possible solution:

Merge the focused sanitizer fix when the required current-head checks pass.

Do we have a high-confidence way to reproduce the issue?

Current-main source directly emits raw header values, and the contributor supplies before/after CLI output; no target code was executed during this read-only review.

Is this the best way to solve the issue?

This is the smallest complete fix: sanitize at both existing presentation owners while preserving selection, persistence, and JSON output.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against a468f8dfa37b.

Provenance checked

  • Applied and preview cleanup headers keeps the original intent (ab7663d: Consolidate single-consumer presentation code while preserving public behavior and Gateway-owned path syntax.)
  • Dry-run Agent header keeps the original intent (98bb32b: Fold private display handoffs into their existing consumers without intended user-visible changes.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Introduced patch and current-main defect: The introduced patch wraps four existing header expressions in sanitizeTerminalText; fetched main still interpolates those values raw. (src/commands/sessions-cleanup.ts:121, 81e519713286)
  • Canonical terminal policy: The existing sanitizer strips ANSI sequences and escapes newline, tab, and carriage return; cleanup already uses it for label summaries. (packages/terminal-core/src/safe-text.ts:11, 81e519713286)
  • CLI behavior evidence: The PR body supplies current-head before/after CLI output for dry-run and local apply using control-bearing store paths, with successful completion and sanitized headers. (81e519713286)
  • Regression and earlier review: The regression now searches only dry-run-appended logs, resolving the earlier stale-output finding, and separately exercises the multi-store Agent header; the body records failing controls with sanitizers removed. (src/commands/sessions-cleanup.test.ts:309, 81e519713286)
  • Related merged fix covers a distinct command: fix(sessions): escape terminal controls in store headers #184139 is merged and sanitizes session-list output; its files do not include sessions-cleanup.ts, so it does not supersede this PR. (src/commands/sessions.ts:471, ed19242f9a27)
  • Feature history and original intent: History identifies the original cleanup feature, later label sanitization, and extensive owner-local refactors; the recent refactors intended to preserve user-visible contracts. Old-content traversal reaches a missing-object boundary, so no exact original raw-header introduction is asserted. (src/commands/sessions-cleanup.ts:86, a468f8dfa37b)

Likely related people:

  • Peter Steinberger: Raw commit ab7663d adds src/commands/sessions-cleanup.ts:116 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: ab7663d438f7; files: src/commands/sessions-cleanup.ts)
  • xiayu: Raw commit a89e6e0 adds src/commands/sessions-cleanup.ts:103 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: a89e6e05efab; files: src/commands/sessions-cleanup.ts)

PR surface

Source 0, Tests +48. Total +48 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 4 4 0
Tests 1 48 0 +48
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 52 4 +48

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.

Label justifications:

  • P2: Control-bearing store paths can alter terminal output, with impact limited to cleanup's human-readable headers.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (4 earlier review cycles)
  • reviewed 2026-10-26T15:31:35.743Z sha 9d2c650 :: needs real behavior proof before merge. :: [P3] Check only the output produced by the dry-run invocation
  • reviewed 2026-10-26T16:25:56.237Z sha 4c9f463 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-26T16:40:10.818Z sha 4c9f463 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-26T16:50:39.586Z sha 4c9f463 :: needs maintainer review before merge. :: none

Reviewed October 26, 2026, 7:16 PM ET / 23:16 UTC (Revision 5).

The dry-run assertion used vi.clearAllMocks() then a whole-array find, but
clearAllMocks does not clear the plain logs array, so the find could match the
apply lane's own sanitized Session store: line (stale output). Record the log
length before the dry-run invocation and search only the entries appended by it.
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 26, 2026
@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 26, 2026
@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Oct 26, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 26, 2026
@SunnyShu0925

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 26, 2026
@roboclaw-bot roboclaw-bot added the clawtributors PR from a clawtributor label Oct 28, 2026
obviyus added a commit that referenced this pull request Oct 28, 2026
…amilies (#186502)

fix(cli): route remaining CLI reports through the terminal-text owner

After #185927 added one owner for escaping untrusted values in CLI output, contributors kept opening one PR per command to escape the next report. This moves the remaining human-readable reports in the plugins, secrets, sessions, memory and memory-wiki, workboard, backup, pairing, oc-path, config and setup commands onto `terminalText` / `displayPath`, and extends the owner to neutralize bidirectional control characters. JSON output, persisted identities, raw exports and the CLI's own colors are unchanged, and the trusted styling of `renderTable` is untouched.

This absorbs the per-site fixes from @Leon-SK668 (#184454, #184379, #185374, #186449, #184396, #184788, #184813, #184394), @huwei-xydt (#185895, #185719), @wangmiao0668000666 (#175600, #175181, #175179), @SunnyShu0925 (#184747), @KrasimirKralev (#184597) and @arpe1618 (#150599), whose original commits are kept. Families no PR touched (update, Doctor, install-trust producers and others) are listed under "Not fixed here".

Evidence: 85 built-CLI invocations per ref with crafted control and bidi characters in the reported values, captured as raw stdout bytes. Main printed the raw sequences; this change printed them neutralized, with identical normal colors and unchanged JSON. Details: #186502 (comment).

Co-authored-by: 胡伟0668000622 <[email protected]>
Co-authored-by: Claude Code <[email protected]>
Co-authored-by: Leon-SK668 <[email protected]>
Co-authored-by: wangmiao0668000666 <[email protected]>
Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Krasimir Kralev <[email protected]>
Co-authored-by: arpe1618 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
@obviyus

obviyus commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks @SunnyShu0925. This is now fixed at the root in #186502 (3bcca4f), which routes the remaining CLI reports through the shared terminalText owner from terminal-core. Your commit is kept in that merge and you're credited as co-author, so I'm closing this one as covered. For new output sites, please pass untrusted values through terminalText (or displayPath for paths) rather than opening a PR per command.

@obviyus obviyus closed this Oct 28, 2026
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 29, 2026
…amilies (openclaw#186502)

fix(cli): route remaining CLI reports through the terminal-text owner

After openclaw#185927 added one owner for escaping untrusted values in CLI output, contributors kept opening one PR per command to escape the next report. This moves the remaining human-readable reports in the plugins, secrets, sessions, memory and memory-wiki, workboard, backup, pairing, oc-path, config and setup commands onto `terminalText` / `displayPath`, and extends the owner to neutralize bidirectional control characters. JSON output, persisted identities, raw exports and the CLI's own colors are unchanged, and the trusted styling of `renderTable` is untouched.

This absorbs the per-site fixes from @Leon-SK668 (openclaw#184454, openclaw#184379, openclaw#185374, openclaw#186449, openclaw#184396, openclaw#184788, openclaw#184813, openclaw#184394), @huwei-xydt (openclaw#185895, openclaw#185719), @wangmiao0668000666 (openclaw#175600, openclaw#175181, openclaw#175179), @SunnyShu0925 (openclaw#184747), @KrasimirKralev (openclaw#184597) and @arpe1618 (openclaw#150599), whose original commits are kept. Families no PR touched (update, Doctor, install-trust producers and others) are listed under "Not fixed here".

Evidence: 85 built-CLI invocations per ref with crafted control and bidi characters in the reported values, captured as raw stdout bytes. Main printed the raw sequences; this change printed them neutralized, with identical normal colors and unchanged JSON. Details: openclaw#186502 (comment).

Co-authored-by: 胡伟0668000622 <[email protected]>
Co-authored-by: Claude Code <[email protected]>
Co-authored-by: Leon-SK668 <[email protected]>
Co-authored-by: wangmiao0668000666 <[email protected]>
Co-authored-by: SunnyShu <0668000539@local>
Co-authored-by: Krasimir Kralev <[email protected]>
Co-authored-by: arpe1618 <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawtributors PR from a clawtributor commands Command implementations P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants