Document safe parsing for preToolUse.toolArgs when it is a JSON-encoded string

未關閉 適合新手
#3,349 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
68/100
Issue 類型
文件
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
python, shell

研究方向

從 Hooks 參考文件及其 preToolUse payload 部分開始。記錄 toolArgs 可能是 JSON 編碼的字串,然後加入能處理無效值或非物件值的安全 Bash 和 Python 解析範例;當 hook 作者能可靠地檢查 command、path 或 url 等欄位時,即視為完成。

由索引模型根據 Issue 內容生成。

描述

area:plugins

Summary

The Hooks reference documents preToolUse.toolArgs as unknown, but does not show how hook authors should safely parse it.

In actual Copilot CLI hook invocations I tested, toolArgs arrived as a JSON-encoded string rather than a parsed object. That may be valid under the current unknown contract, but it is easy for hook authors to assume object-style access and accidentally write hooks that fail to inspect tool arguments.

Because hook failures are fail-open, this is a security footgun for policy-enforcing hooks.

Observed behavior

For preToolUse, the documentation shows the camelCase payload shape as:

{
    sessionId: string;
    timestamp: number;
    cwd: string;
    toolName: string;
    toolArgs: unknown;
}

In tested CLI/App-backed hook invocations, the payload effectively behaved like:

{
  "toolName": "bash",
  "toolArgs": "{\"command\":\"echo hello\"}"
}

rather than:

{
  "toolName": "bash",
  "toolArgs": {
    "command": "echo hello"
  }
}

I am not claiming the string form is invalid. Since the schema says unknown, this may be intentional or implementation-defined. The problem is that the docs do not tell hook authors how to handle it safely.

Why this matters

Security hooks commonly inspect fields like:

.toolArgs.command
.toolArgs.path
.toolArgs.url

If toolArgs is a JSON-encoded string, this kind of access does not work as expected. Depending on the script and shell settings, the hook may fail, emit invalid output, or skip the intended check.

Since hook failures are fail-open, a parsing mistake can silently bypass a security policy.

Request

Please document the expected handling for toolArgs and provide safe parsing examples.

At minimum, the docs should say something like:

toolArgs is unknown and may be a JSON-encoded string. Hook scripts should check its runtime type and parse it before inspecting tool arguments.

A Bash example would help:

INPUT="$(cat)"

TOOL_ARGS_JSON="$(
  jq -c '
    (.toolArgs // .tool_args // .tool_input // {}) as $args
    | if ($args | type) == "string" then ($args | fromjson? // {}) else $args end
  ' <<< "$INPUT"
)"

COMMAND="$(jq -r '.command // ""' <<< "$TOOL_ARGS_JSON")"

Python example:

import json
import sys

payload = json.load(sys.stdin)
tool_args = payload.get("toolArgs", payload.get("tool_input", {}))

if isinstance(tool_args, str):
    try:
        tool_args = json.loads(tool_args)
    except json.JSONDecodeError:
        tool_args = {}

if not isinstance(tool_args, dict):
    tool_args = {}

command = tool_args.get("command", "")

Expected improvement

This would make hook authoring safer, especially for security-focused preToolUse hooks, and reduce the chance of fail-open bypasses caused by incorrect assumptions about the runtime type of toolArgs.

主要語言
Shell
星號
11.2k
分支
1.9k
平均合併
14 小時 16 分鐘
30 天內合併 PR
6

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

github/copilot-cli 的其他 Issue

查看 github/copilot-cli 的全部 Issue

相似的 Issue

更多 Shell/Bash Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。