graphify icon indicating copy to clipboard operation
graphify copied to clipboard

skill.md: shell injection through `LABELS_DICT` and `INPUT_PATH` in `python -c "..."` blocks, plus three smaller robustness gaps (v0.9.84)

Open stubbam opened this issue 1 month ago • 4 comments

Found while vendoring graphify/skill.md from v0.9.84 into a project (an automated review flagged these; I then checked each against the file). Items 1, 2, and 5 are reproduced. Items 3 and 4 are from reading the text, not from running it.

1. Community labels are spliced into a double-quoted shell string (reproduced)

Step 5 has labels = LABELS_DICT inside python -c "...". The labels are chosen by the model from node labels, which come from the corpus (docs, comments, file names). In a double-quoted shell string, $(...) and backticks run before Python sees the text:

python3 -c "
labels = {0: 'Attention Mechanism', 1: 'Data $(touch LABEL_MARKER) Loading'}
print('python saw:', labels)
"
# python saw: {0: 'Attention Mechanism', 1: 'Data  Loading'}
# and LABEL_MARKER now exists

So a corpus that gets the model to emit such a label runs a command as the user. Step 1 already avoids this with a quoted heredoc (<<'GRAPHIFY_ROOT_EOF', with a comment explaining why); Step 5 doesn't.

2. INPUT_PATH has the same problem in Steps 2, 3, 4, 5, and 9 (reproduced)

Path('INPUT_PATH') is substituted into python -c "..." the same way:

python3 -c "from pathlib import Path; print('path:', Path('./proj$(touch PATH_MARKER)'))"
# PATH_MARKER now exists

The comment in Step 1 says a bare path in a command line "would let a malicious path execute as shell code"; the later steps don't follow it.

Suggested fix for 1 and 2: pass the values through a quoted heredoc (python3 - <<'EOF' ... EOF) or through environment variables or sys.argv, never by substituting text into a double-quoted script.

3. Step 5 carries on after the shrink guard refuses (from reading)

Step 4 exits with SystemExit(1) when to_json returns False. Step 5 prints the same error and continues, after it has already written GRAPH_REPORT.md and .graphify_labels.json, so they can describe a graph that graph.json doesn't contain. Step 4's own comment says to write the report only when the graph was written.

4. The chunk merge aborts on invalid JSON (from reading)

Step B3 says "If a subagent failed or returned invalid JSON, print a warning and skip that chunk - do not abort", but the merge script does d = json.loads(Path(c).read_text(...)) with no try, so one bad chunk raises and stops the merge.

5. The interpreter path is word-split (reproduced)

$(cat graphify-out/.graphify_python) -c ... is unquoted in every block, so an interpreter path with a space (a home directory like /Users/First Last/...) is split into two words:

no such file or directory: .../a

"$(cat graphify-out/.graphify_python)" fixes it.

Environment

graphifyy v0.9.84, the package's graphify/skill.md. I only read that file. The other skill-*.md variants (and v1.0.0's, which has a different layout) may have the same patterns; I haven't looked.

stubbam avatar Oct 10 '26 20:10 stubbam

Thanks for opening this issue, @stubbam. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Oct 10 '26 20:10 github-actions[bot]

Here's how I'm planning to fix this. Happy to change course if you'd prefer something else.

The core problem is that INPUT_PATH and LABELS_DICT get pasted into the Python scripts as text. Wrapping the scripts in heredocs would stop the shell from running $(...), but a label with a ' in it could still break out of the Python string. The Windows skill already uses here-strings and has the same hole, so I'd rather remove the pasting altogether.

What I'd change:

  • INPUT_PATH: Step 1 already saves the resolved root to graphify-out/.graphify_root. The later steps and the --update runbook would read it from there, using a small helper with the same in-repo check hooks.py does. No path gets pasted anywhere.
  • LABELS_DICT: the agent writes the labels to a JSON file through a quoted heredoc, and Step 5 loads it with json.loads. A weird label just fails to parse.
  • Item 5: add quotes around $(cat graphify-out/.graphify_python).
  • IS_DIRECTED stays as it is, since it's only True or False and comes from the user's own flag.

The earlier fixes (#3642, #3742, #3844) only covered Step 1 and --watch, and nothing caught the other steps. So I'd also make skillgen refuse to render a skill that pastes INPUT_PATH or LABELS_DICT into a script, and add tests that run the generated blocks with a hostile path and label.

Windows is generated from the bash fragments, so it gets the same fix.

I'll leave items 3 and 4, and converting the scripts to heredocs, for later PRs.

Deepjyoti-Sarmah avatar Oct 11 '26 13:10 Deepjyoti-Sarmah

A residual of item 2 survives the heredoc fix in v0.9.88 and v0.9.89 (graphify/skill.md Step 1, the GRAPHIFY_ROOT_EOF block at lines 104-106).

The quoted delimiter stops $() and backtick expansion inside the body, but the body is still raw path text, so a scan path that contains a newline followed by GRAPHIFY_ROOT_EOF terminates the heredoc early and whatever follows on the next lines runs as shell code. It needs a path the person types or a path handed over by a tool that builds it from untrusted text, so it is narrow, but the comment above the block promises that the path can never execute, and that promise does not hold for this input.

Suggested fix, in the same spirit as your suggestion for items 1 and 2: encode the path into a delimiter-safe single line before embedding it (for example base64 on the shell side, base64.b64decode(sys.stdin.read()) in the Python one-liner), or pass it through an environment variable (GRAPHIFY_INPUT_PATH="INPUT_PATH" "$PYTHON" -c 'import os; ...os.environ["GRAPHIFY_INPUT_PATH"]...'), so no raw path text sits inside a shell construct at all.

Found by an automated review while re-vendoring the 0.9.88 skill into a catalog; verified by reading the current graphify/skill.md, not by running it.

AlessandroLiscio avatar Oct 13 '26 11:10 AlessandroLiscio

Thanks @AlessandroLiscio , you're right. Step 1 still pastes the scan path into the heredoc as plain text. If the path contains a line that reads GRAPHIFY_ROOT_EOF, the heredoc ends there, and whatever comes after runs as a shell command. #4344 has the same gap, because it keeps Step 1 as the one place the path comes in.

I'd rather not use either of the two suggested encodings:

  • GRAPHIFY_INPUT_PATH="...": inside double quotes the shell still runs $(...) and backticks, which is the original bug again.
  • base64: the model would have to encode the path correctly by hand, and a wrong encoding breaks ordinary paths.

Any text the agent pastes into a shell command can end early in some way. So I'll make Step 1 refuse a path that contains a newline, which no real project path needs. The Python side will also reject input longer than one line, and the comment above the block will only promise what the code actually does.

I'll push this to #4344 together with a rebase onto 0.9.89.

Deepjyoti-Sarmah avatar Oct 13 '26 11:10 Deepjyoti-Sarmah