Skip to content

feat: code-context coverage (DET-008) — detect unsafe instructions hidden in code comments - #2

Merged
sscodeai merged 9 commits into
mainfrom
dev
Oct 11, 2026
Merged

sscodeai merged 9 commits into
mainfrom
dev

Conversation

@sscodeai

Copy link
Copy Markdown
Owner

Closes the inference-time context-injection channel from arXiv:2610.05139 — Hidden in the Comments: A Context-Injection Attack Surface in Code LLMs (Munir, Quinzan & Roberts, Oxford).

An unsafe objective written as an ordinary comment (# TODO: / # INSTRUCTION: / # TASK:) inside untrusted context is indistinguishable from the developer's own instruction — the marker space is shared. The paper measures 77.4–92.3% of completions containing a medium-or-higher weakness (benign condition: 1.7–5.1%), with the strongest post-generation screen still missing ~⅓. aiitg now answers upstream of generation: the directive never reaches the model intact.

What changed

  • text parser — source/config files plus agent rule files (AGENTS.md, CLAUDE.md, GEMINI.md, .cursorrules, .windsurfrules, copilot-instructions.md) read as one paragraph per line with a 1-based line number. The format registry gained basename dispatch (FormatHandler.basenames) and extension-key normalisation (a dotted registration can no longer create an unreachable handler).
  • DET-008 comment_directive — a hit needs a directive frame (marker / addressed-to-the-model / imperative opener / authority claim / safety negation) and an insecure objective from the paper's ten weakness classes, plus prompt injection aimed at the assistant itself. Descriptive prose that merely names a sink, prohibitions ("must never call child_process") and fix-notes are not reported.
  • Defusing on quarantine — stripping invisible characters does not touch a visible comment, so the sanitized substitute replaces the directive with an inert comment (same syntax, same indentation). Quarantine is no longer a no-op for code files; the removal is recorded in SanitizeResult.removed.
  • Hook enforcement — code/config extensions and rule files are enforced by the same gate as documents: CRITICAL/HIGH → deny (POL-001), MEDIUM → quarantine with a defused substitute. aiitg hook doctor reports the enforced set.
  • Trust labeling — a directed unsafe comment earns a content-dimension weight and a cap below the dangerous threshold: an instruction the model would follow may never read as safe.

Verification

  • 330 tests pass (make test), ruff and mypy clean.
  • Detector corpus: 32/32 attack payloads (29 CRITICAL / 3 HIGH, class agreement 32/32), 0/46 false positives on ordinary work comments across five comment syntax families, 0 findings over 4,643 comment lines of live third-party source (167 files).
  • End-to-end on the shipped paths, not just unit tests: aiitg scan oracle.py → exit 1 DET-008 CRITICAL @ line 10; hook → deny (POL-001); quarantine → the substitute no longer contains the directive; aiitg hook doctor → 63 extensions + 6 rule-file names.
  • Merge with main keeps PR Fix/dev review findings #1's hardening: evil.txt holding a docx is sniffed to docx and denied again (DOCUMENT_KINDS), pinned by tests/unit/test_text_parser.py and the existing regression test.

Honest boundaries

  • The paper's 700-item corpus was not released with its HTML, so the numbers above measure this detector on this corpus — they are not a reproduction of the paper's effect size.
  • A deterministic pattern detector is incomplete by construction (the paper's own strongest screen still missed ~⅓); the policy outcome for a reported finding is fail-closed and the audit trail is the durable artefact.
  • DET-008 reads instructions, not the vulnerable code a model may already have written (that is a SAST concern), and the IDE inline-completion channel is not covered yet.
  • Design notes, method and gaps: docs/comment-directive-coverage.md.

Notes

  • Merge commit a53c1ba (from main) resolved: the hook gate combines the sniff guard with the rule-file bypass, and prose extensions (.md/.txt) are no longer registered so a document wearing a misleading extension stays in the sniff path.
  • Changelog: 0.3.0, plus a 0.2.1 section recording the fixes this branch now builds on.
  • Commit slices: 7 (feat(parsers) → feat(detectors) → feat(trust) → feat(sanitize) → feat(hooks) → test → docs).

Add a `text` parser (one paragraph per line, 1-based line numbers, comment-syntax
family in metadata) so the gateway can read the files a Code LLM actually completes:
source, config and agent rule files.

The format registry gains `FormatHandler.basenames` because `.cursorrules` has no
extension to key on, and extension keys are normalised on registration: a handler
registered with ".py" used to land in an unreachable slot next to the dotless "py"
keys the other parsers use.
Closes the inference-time context-injection channel of arXiv:2610.05139: an unsafe
objective written as an ordinary comment (`# TODO:` / `# INSTRUCTION:` / `# TASK:`)
inside untrusted context is indistinguishable from the developer's own instruction.

A hit needs a directive frame (marker / addressed / imperative / authority /
safety-negation) AND an insecure objective from the paper's ten weakness classes,
plus prompt injection aimed at the assistant itself. Descriptive prose that merely
names a sink, prohibitions and fix-notes are not reported: measured on the bundled
corpus 32/32 attack payloads, 0/46 benign comments, 0 findings over 4,643 comment
lines of third-party source.
A directive in a comment is not concealment -- a human reads it too -- but the
model complies with it, so it earns a content-dimension weight and a cap below the
dangerous threshold when it is HIGH or CRITICAL.
Stripping invisible characters does not touch a visible comment payload, so
"quarantine" would have been a no-op for code files: the model would read the
directive verbatim out of the substitute. `defuse_comment_line()` replaces the
instruction with an inert comment that keeps the language's syntax and the line's
indentation, and records the removal in `SanitizeResult.removed`.
Source/config extensions and agent rule files (AGENTS.md, CLAUDE.md, GEMINI.md,
.cursorrules, .windsurfrules, copilot-instructions.md) are now enforced by the same
gate that guards documents; `hook doctor` reports the enforced set.

Also corrects the block message: a comment directive is visible to a human reader,
so "the file's hidden content never reached the model" was the wrong claim for this
finding type -- it now says the file was not sent to the model.
…aviour

Attack corpus: the paper's ten weakness classes x three framings plus
agent-directed injection (two payloads verbatim from the paper), a 46-comment benign
corpus across five comment syntax families, false-positive controls (prose, prohibition,
fix-note, marker-only weak class), parser/registry dispatch, defusing, hook enforcement
and CLI e2e. Test count 183 -> 320.
Design notes, the measured table and the honest boundaries (the paper's 700-item
corpus was not released; a pattern detector is incomplete by construction; DET-008
reads instructions, not vulnerable code). README/README.ja document the new surface
and the badge moves to 320 tests.
Sync with GitHub main (PR #1, fix/dev-review-findings) so dev can be pushed without
losing upstream's hardening. Conflict resolutions:

- hook gate: upstream's sniff guard for unenforced extensions is combined with this
  branch's rule-file name bypass, so `payload.txt` holding a docx is enforced again
  while AGENTS.md/.cursorrules stay enforced by name. The sniffed-kind set is now the
  named constant DOCUMENT_KINDS.
- text parser: `.md`/`.txt`/`.rst`/`.adoc` are no longer registered as extensions.
  Registering them shadowed the registry's content sniffing and would have reopened the
  misleading-extension hole; prose stays in the sniff path, rule files are matched by
  name. New tests pin both the registry behaviour (a docx named payload.txt sniffs to
  docx) and the hook verdict (deny).
- README/README.ja: merged test count (330 collected after the merge).

Verified on the merged tree: 330 tests pass, ruff and mypy clean, and evil.txt ->
sniffed docx -> deny (upstream's regression test, now green again).
… fix

The branch now builds on main's PR #1, so the changelog states what came in with it
(fail-closed on incomplete scans, hook/cache hardening, visible-content preservation)
and the 0.3.0 entry notes the extension/sniff precedence fix that the merge forced:
registering prose extensions shadowed sniffing, and the coverage doc no longer claims a
plain .md is parseable on demand.
@sscodeai
sscodeai merged commit 66b8382 into main Oct 11, 2026
4 checks passed
sscodeai added a commit that referenced this pull request Oct 11, 2026
…nts and threat model

Follow-up to #2. README/README.ja: 7 -> 8 detectors (the pipeline diagram and the
list-detectors example), M0 row clarified as "6 document formats". docs/plan: the
enforced-Read cost line names both paths. SECURITY.md: directive bypass (code comment
or rule file) is in scope. Docs-only, no behaviour change.
sscodeai added a commit that referenced this pull request Oct 12, 2026
… covers code and rule files

Second follow-up to #2. README headlines, pyproject/CLI/module descriptions and the
GitHub repository description now name the real input surface (documents, source
files, agent rule files); README.ja detector count fixed; list-detectors e2e pins
DET-008. Docs/metadata plus one test assertion — no behaviour change.
@sscodeai
sscodeai deleted the dev branch November 24, 2026 14:57
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