Skip to content

[enhancement] che-apple-mail-mcp session-start hook: PID validation + started_at comparison hardening (follow-up from #76) #66

Description

@kiki830621

Problem

plugins/che-apple-mail-mcp/hooks/session-start.sh 對 stale-PID kill 的 PID validation 還可以更 defensive。Codex review of #61 提出兩個 hardening idea(見 verify master P3 #2 + P3 #3)。

Concern 1:PID 沒驗證是 positive integer

Hook line 42:

PID=$(jq -r '.pid // empty' "$RUNTIME_FILE" 2>/dev/null)
[ -z "$PID" ] && exit 0

雖然下游 ps -p "$PID" + command field guard 在實務上把惡意 PID(0/1/負數)都 fail-safe filter 掉,但 defense-in-depth 來看,讀完 PID 後先做 positive-integer sanity check 比較乾淨:

[[ "$PID" =~ ^[1-9][0-9]*$ ]] || exit 0

效果:-10abcnull、超大數值 等異常 input 第一時間直接 silent skip,不進到 ps/kill path。

Concern 2:started_at 沒被 hook 使用

Wrapper 寫 runtime state 時記錄 started_at(unix epoch seconds),但 hook 完全沒讀。如果加上 process start time 比對,可進一步排除 PID reuse 誤殺:

# After ps -p check
PROC_START=$(ps -p "$PID" -o lstart= 2>/dev/null | xargs -I {} date -j -f "%a %b %e %T %Y" {} +%s 2>/dev/null)
RECORDED_START=$(jq -r '.started_at // empty' "$RUNTIME_FILE" 2>/dev/null)

# If process started AFTER our recorded time, it's a different process — exit
if [ -n "$PROC_START" ] && [ -n "$RECORDED_START" ]; then
    [ "$PROC_START" -gt "$RECORDED_START" ] && exit 0
fi

效果:vim TOCTOU edge case 完全消除(若 vim 是 PID-reuse 後 reborn 的,start time 必然 > runtime.json 記的 started_at)。

Type

enhancement(defense-in-depth hardening;non-blocking)

Impact

低 — 現行版 (#61 merged) 已有 ps + command field 雙層 guard,實務上 PID 誤殺機率 ~0。本 issue 是為了 belt-and-suspenders 進一步收緊 attack/edge surface。

Strategy

  1. Hook 線 42 後加 PID positive-integer regex 檢查
  2. Hook line 50-53(command guard 後)加 started_at vs ps elapsed-time 比對
  3. 補 2 個 test cases:negative_pidpid_reused_after_started_at
  4. CHANGELOG entry as v2.18.1 patch release

Source

Surfaced during /idd-verify PsychQuant/che-apple-mail-mcp#76(PR #61 verify),P3 finding #2 + #3。Codex CLI(gpt-5.5 xhigh)flag。

Out-of-scope (parent issue 已決定不混進 #76 PR)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions