Skip to content

Fail CI on a new exec, eval or compile of a value that is not written out - #369

Merged
danielhanchen merged 2 commits into
mainfrom
exec-literal-gate
Aug 30, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
exec-literal-gate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

A small replacement for the checker in #365, which I closed. That one reached about ten thousand lines over roughly seventy review rounds, and measured against the same tree the extra machinery bought a smaller baseline, not a finding this one misses.

The rule

The first argument to the exec, eval or compile builtin must be a written-out string. A name, a call, or an f-string with a placeholder is reported.

Matching the bare name rather than an attribute is what makes it usable: counting re.compile(...) and model.eval() as sinks reported 69 false positives across the two library repositories.

Notebooks are read too, since they are the maintained source here. Magic and shell lines are blanked rather than dropped, so a reported line number still points at the right line of the cell.

What it finds here

Four call sites, each recorded with a reason:

File Why it is allowed
nb/AMD-Unsloth_Studio.ipynb Runs the chat module the notebook just downloaded from the Studio release
nb/Kaggle-Unsloth_Studio.ipynb Same
original_template/Unsloth_Studio.ipynb Same
original_template/gpt_oss_(20B)_GRPO_BF16.ipynb Runs a model-generated program, sandboxed by the helper added in unslothai/unsloth-zoo#1105

Both shapes are real, which is the point: this is not a gate over an empty set.

The baseline

An entry keys on the call's own text rather than its line number, so moving code does not churn it, and carries a count, so a new call cannot hide behind a removed one. The gate refuses to pass while any entry lacks a reason, and --update carries existing reasons across instead of resetting them to nothing.

Why this class needs a gate at all

CodeQL default setup runs on this org with Python enabled, and it works: unsloth has raised py/stack-trace-exposure, py/incomplete-url-substring-sanitization and five py/path-injection alerts. But there has never been a single py/code-injection alert on either library repository, in any state, while exec of an HTTPS response body sat in unsloth/models/loader_utils.py until unslothai/unsloth#9777. So this class is not covered by what already runs.

Checks

  • --self-test passes on 3.9 and 3.13, and runs in CI ahead of the gate
  • 25 tests: what fires, what does not, notebook parsing, line-number fidelity, the digest following the argument rather than the line, reason preservation across --update, and an unjustified entry failing the gate
  • Adds two steps to the existing Source lint job; the scan is under a second here

… out

A small replacement for the checker in the closed #365. That one grew to about
ten thousand lines over seventy review rounds, and measured against the same
tree the extra machinery bought a smaller baseline rather than a finding this
misses.

The rule: the first argument to the exec, eval or compile BUILTIN must be a
written-out string. A name, a call, or an f-string with a placeholder is
reported. Matching the bare name and not an attribute matters - counting
re.compile and model.eval as sinks reported 69 false positives across the two
library repositories.

Notebooks are read as well, since they are the maintained source here. Magic
and shell lines are blanked rather than dropped so reported line numbers still
point at the cell.

Four existing call sites are recorded with a justification each: three notebooks
run the Studio chat module they have just downloaded, and the GRPO notebook runs
a model-generated program through the sandbox added in unsloth-zoo#1105. An
entry keeps the call's own text rather than its line number, so moving code does
not churn it, and a count, so a new call cannot hide behind a removed one. The
gate refuses to pass while any entry lacks a reason, and --update carries
existing reasons across rather than resetting them.

Worth recording: CodeQL default setup runs here with Python enabled and raises
other injection findings on these repositories, py/path-injection among them,
but has never once raised py/code-injection - including while exec of an HTTPS
response body sat in unsloth. This class is not covered by what already runs.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-08-30T06:14:06.650228Z 9837bdd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9837bdda83

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# A notebook that does not parse as one module is ordinary: a cell may be
# mid-edit, or depend on an earlier `%%capture`. Nothing to check, and
# failing the build for it would be noise.
return []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Scan parseable cells instead of skipping the whole notebook

When concatenating any notebook's cells produces a syntax error, this returns no findings for the entire file, including otherwise valid cells containing a new exec(download()). In the reviewed tree this silently skips 347 of 445 notebooks under nb, 17 of 113 under original_template, and both kaggle notebooks, largely because independently valid cells do not form one valid module when joined. Scan cells independently (using the repository's existing notebook-magic stripping) so one problematic cell cannot disable the security gate for every other cell.

Useful? React with 👍 / 👎.

print("\nSay why the value is trusted in the entry's `reason` field.")
return 1

stale = sorted(k for k in allowed if k not in observed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject partially stale baseline counts

When a baseline key allows two identical calls and one is removed, the key remains present in observed, so this stale check passes while retaining an allowance of two. A later same-digest call can therefore be added without failing the gate, exactly the baseline-reuse scenario the count is intended to prevent. Treat observed.get(key, 0) < allowed[key] as stale rather than checking only key membership.

Useful? React with 👍 / 👎.

@danielhanchen
danielhanchen merged commit 6330458 into main Aug 30, 2026
12 of 13 checks passed
@danielhanchen
danielhanchen deleted the exec-literal-gate branch August 30, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant