Skip to content

Stop a crafted package.json script key becoming a memory - #371

Merged
abogartz merged 1 commit into
mainfrom
301-script-keys
Nov 9, 2026
Merged

abogartz merged 1 commit into
mainfrom
301-script-keys

Conversation

@abogartz

@abogartz abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner

What this does

/repo-init reads your package.json and remembers the notable scripts (test, lint, build, …) as memories, phrased as "run npm run <name> from the repo root". It took the script name from the file with no character check, so a checkout shipping a key like

{ "scripts": { "test && echo SYNTHETIC": "vitest run" } }

stored a memory whose command contained && echo SYNTHETIC. That row is tagged init/package.json, so it gets no [via ...] provenance marker, and it is injected into every later session of that repository — surviving long after the branch or clone that carried the crafted key is gone. Make and Just targets were already restricted to identifier characters; package.json was the one detector without a filter.

Keys that are not a plain identifier are now skipped: no record, no row. Everything else about init is unchanged.

One deliberate deviation from the ticket

#301 specified [A-Za-z0-9_-]+, the class the Make detector uses. That would also drop ordinary npm keys such as test:ci and lint.fix — the colon and the dot carry no shell meaning, and a CI test command is exactly the kind of thing worth remembering. This filter is /^[A-Za-z0-9_.:-]+$/: it rejects everything the ticket is about (whitespace, &, ;, |, $, quotes, paths) and keeps the npm convention. The test pins both halves — the crafted key is dropped, test:ci survives. Say the word if you want the narrower class instead.

Tests

  • RED first: with a fixture containing "test && echo SYNTHETIC", "lint --fix; rm -rf /", "test:ci" and lint, detectScripts returned the crafted keys.
  • GREEN: it returns lint and test:ci only, every returned key matches the identifier class, and no returned command contains the injected text.
  • Existing init tests (the npm run test / npm run lint rows, idempotency, force) still pass.

bun run gate green: 1130 unit tests + 30 terminal/BDD tests against a real host, 0 fail. bunx tsc --noEmit clean; lint and format identical to main's baseline.

Closes #301.

…301)

detectPackageScripts built "npm run ${name}" from any notable key, so a checkout
shipping a key like "test && echo SYNTHETIC" stored a command memory carrying the
injected text. The row is tagged init/package.json, so it gets no [via ...]
provenance marker and rides into every later session of the repo, outliving the
branch the crafted key came from. Make/Just targets were already restricted;
package.json was the detector without a filter.

Keys outside /^[A-Za-z0-9_.:-]+$/ are skipped. Deliberate widening of the
ticket's [A-Za-z0-9_-]+: the colon and dot carry no shell meaning and npm
convention uses them (test:ci, lint.fix), and dropping them would lose the CI
command a repo most wants remembered. The test pins both halves.
@abogartz

abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

Adversarial self-review (author-side, not independent)

Tried to route a crafted key around SCRIPT_KEY. Could not, and the combination is tighter than the PR itself argues.

Verified clean:

  • Flag injection is closed twice over. A key like -Ctest or --test-path passes the character class (hyphen is allowed), but isNotable requires the key to start with a notable word, so any key led by - is dropped before the filter even runs. Neither filter alone blocks it — together they do.
  • Filter placement: the skip happens before seen.add(name), so a crafted test && … key cannot shadow a legitimate test key that appears later in the same file.
  • Anchor semantics: JS $ without the m flag does not match before a trailing newline, and the class rejects every whitespace and control character anyway. Fullwidth look-alikes (:, .) fail the class.
  • detectLangManifests interpolates nothing — cargo test / pytest are fixed strings — so package.json really was the last detector with prose in the command.
  • The deviation from Filter package.json script keys before storing command memories #301's narrower class is the right call and is pinned on both sides (test:ci survives, crafted key dropped). The comment's claim about Make is also true: : is the rule separator there, so the narrower Make class loses nothing.
  • CI green on all four jobs; git merge-tree against current main is clean.

Findings:

  1. 🔵 Key length is uncapped. test + 10 000 alphanumerics passes both filters, and the only backstop is the write gate's content cap (the row is blocked, not truncated). Behavior is safe, but a one-line length bound in SCRIPT_KEY ({1,80} or similar) would make the defense local instead of borrowed. Optional.

Verdict: approve. Branch is 23 commits behind main; merge-tree clean, landing is mechanical.

@abogartz
abogartz merged commit 2c77fe1 into main Nov 9, 2026
7 of 8 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.22.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz

abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

Merged into main at 2c77fe1 with admin override (solo author). Landed in the order the self-review set: this PR before #368. The review's follow-ups are tracked separately (#375 for the re-tag scan; the optional nits were left as written).

@abogartz
abogartz deleted the 301-script-keys branch November 19, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter package.json script keys before storing command memories

1 participant