@boyingliu01/xp-gate 0.37.3 → 0.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/adapter-common.sh +12 -2
  2. package/adapters/csharp.sh +73 -2
  3. package/adapters/dart.sh +13 -0
  4. package/adapters/go.sh +5 -0
  5. package/adapters/iac.sh +46 -3
  6. package/adapters/java.sh +6 -0
  7. package/adapters/kotlin.sh +6 -0
  8. package/adapters/shell.sh +119 -34
  9. package/adapters/swift.sh +6 -0
  10. package/adapters/typescript.sh +98 -34
  11. package/bin/xp-gate.js +48 -2
  12. package/gate-10.sh +99 -26
  13. package/gate-12-file-hygiene.sh +13 -1
  14. package/gate-registry.sh +197 -0
  15. package/gates/python-health.ts +67 -29
  16. package/hooks/adapter-common.sh +12 -2
  17. package/hooks/deploy-manifest.sh +100 -0
  18. package/hooks/gate-10.sh +99 -26
  19. package/hooks/gate-12-file-hygiene.sh +13 -1
  20. package/hooks/gate-driver.sh +120 -0
  21. package/hooks/gate-entries/gate-0-entry.sh +825 -0
  22. package/hooks/gate-entries/gate-1-entry.sh +767 -0
  23. package/hooks/gate-entries/gate-10-entry.sh +52 -0
  24. package/hooks/gate-entries/gate-11-entry.sh +120 -0
  25. package/hooks/gate-entries/gate-12-entry.sh +44 -0
  26. package/hooks/gate-entries/gate-12b-entry.sh +178 -0
  27. package/hooks/gate-entries/gate-12c-entry.sh +80 -0
  28. package/hooks/gate-entries/gate-13-entry.sh +584 -0
  29. package/hooks/gate-entries/gate-2-entry.sh +199 -0
  30. package/hooks/gate-entries/gate-3-entry.sh +53 -0
  31. package/hooks/gate-entries/gate-4-entry.sh +46 -0
  32. package/hooks/gate-entries/gate-5-entry.sh +1729 -0
  33. package/hooks/gate-entries/gate-6-entry.sh +658 -0
  34. package/hooks/gate-entries/gate-7-entry.sh +41 -0
  35. package/hooks/gate-entries/gate-8-entry.sh +33 -0
  36. package/hooks/gate-entries/gate-9-entry.sh +307 -0
  37. package/hooks/gate-entries/subdir-restore.sh +27 -0
  38. package/hooks/gate-registry.sh +51 -15
  39. package/hooks/hook-module-resolution.sh +73 -0
  40. package/hooks/install.sh +135 -0
  41. package/hooks/lib/now-ms.sh +55 -19
  42. package/hooks/lib/toolchain-path.sh +50 -6
  43. package/hooks/pre-commit +256 -5091
  44. package/hooks/pre-push +212 -25
  45. package/lib/__tests__/arch-config-template.test.js +190 -0
  46. package/lib/__tests__/baseline-shell-scope.test.js +60 -0
  47. package/lib/__tests__/crap-cli.test.js +14 -3
  48. package/lib/__tests__/deploy-manifest-check-wired.test.js +138 -0
  49. package/lib/__tests__/deploy-manifest-completeness.test.js +151 -0
  50. package/lib/__tests__/deploy-manifest-consumers-adapter-uninstall.test.js +40 -0
  51. package/lib/__tests__/deploy-manifest-consumers-no-private-lists.test.js +74 -0
  52. package/lib/__tests__/deploy-manifest-projection.test.js +66 -0
  53. package/lib/__tests__/deploy-manifest-shell-parity.test.js +108 -0
  54. package/lib/__tests__/deploy-manifest.test.js +97 -0
  55. package/lib/__tests__/doctor.test.js +51 -7
  56. package/lib/__tests__/gate-coverage.test.js +79 -12
  57. package/lib/__tests__/gate-numbering.test.js +28 -1
  58. package/lib/__tests__/install-sh-deploy-manifest.test.js +80 -0
  59. package/lib/__tests__/mirror-sync.test.js +45 -3
  60. package/lib/__tests__/npm-package.test.js +10 -7
  61. package/lib/__tests__/sastd-client.test.js +514 -0
  62. package/lib/__tests__/shared-utils-copyHooks-manifest.test.js +69 -0
  63. package/lib/__tests__/sprint-status-rework.test.js +1 -0
  64. package/lib/arch-config-template.js +87 -0
  65. package/lib/baseline.js +54 -2
  66. package/lib/deploy-manifest.js +174 -0
  67. package/lib/doctor.js +46 -14
  68. package/lib/gate-capability.js +291 -0
  69. package/lib/gate-coverage.js +194 -74
  70. package/lib/gate-identity.js +349 -0
  71. package/lib/gate-runner.js +11 -1
  72. package/lib/hook-module-resolution.js +27 -0
  73. package/lib/init.js +72 -5
  74. package/lib/mattpocock-license-verdict.json +1 -1
  75. package/lib/sastd-client.js +572 -0
  76. package/lib/shared-utils.js +31 -9
  77. package/lib/uninstall.js +10 -7
  78. package/lib/update-hooks.js +27 -18
  79. package/mock-policy/AGENTS.md +1 -1
  80. package/mutation/AGENTS.md +1 -1
  81. package/package.json +3 -1
  82. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  83. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  84. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  85. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  86. package/plugins/dsh/package.json +1 -1
  87. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  88. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  89. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  90. package/plugins/opencode/package.json +1 -1
  91. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  92. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  93. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  94. package/plugins/qoder/plugin.json +1 -1
  95. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  96. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  97. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  98. package/principles/AGENTS.md +1 -1
  99. package/principles/__tests__/analyzer.test.ts +2 -1
  100. package/principles/__tests__/config-enforcement.test.ts +1 -0
  101. package/principles/__tests__/types.test.ts +1 -0
  102. package/principles/adapters/base.ts +14 -1
  103. package/principles/adapters/duplication.ts +134 -0
  104. package/principles/rules/__tests__/clean-code/large-file.test.ts +2 -1
  105. package/principles/rules/clean-code/code-duplication.ts +75 -9
  106. package/principles/types.ts +39 -0
  107. package/scripts/__tests__/check-mattpocock-license.test.js +36 -3
  108. package/scripts/__tests__/sync-package-content.test.js +12 -2
  109. package/scripts/generate-mirrors.cjs +125 -0
  110. package/scripts/sync-package-content.js +64 -20
  111. package/skills/delphi-review/AGENTS.md +1 -1
  112. package/skills/sprint-flow/AGENTS.md +1 -1
  113. package/skills/test-specification-alignment/AGENTS.md +1 -1
  114. package/adapters/gate-9.sh +0 -245
  115. package/gate-9.sh +0 -245
  116. package/hooks/gate-9.sh +0 -245
package/gate-9.sh DELETED
@@ -1,245 +0,0 @@
1
- #!/usr/bin/env bash
2
- # ============================================================================
3
- # ⚠️ DEAD CODE — NOT EXECUTED. Read this before changing anything here.
4
- # ============================================================================
5
- # This file is NOT sourced by githooks/pre-commit or githooks/pre-push (both
6
- # source count = 0; verify with: grep -c "gate-9.sh" githooks/pre-commit).
7
- # It is shipped by githooks/install.sh but never runs.
8
- #
9
- # The gate that actually runs semgrep SAST is githooks/gate-10.sh, and it
10
- # reports as `gate10_sast`. Gate 9 in the report is `gate9_build_integrity`
11
- # (Build Integrity), a different gate entirely.
12
- #
13
- # This mislabelling is not cosmetic: in #717 a C# SAST fix was first written
14
- # into THIS file, where it changed nothing, while .cs stayed ungoverned. The
15
- # C# ruleset loading that works lives in gate-10.sh. Do not relocate it here.
16
- #
17
- # Kept rather than deleted because githooks/install.sh:GATE_SCRIPTS ships it;
18
- # removing it is a separate change. See issue #717 walkthrough MC-03.
19
- # ============================================================================
20
- # ============================================================================
21
- # GATE 9: Semgrep SAST Security Scan
22
- # Detects security vulnerabilities (SQL injection, XSS, etc.) in staged files
23
- # Tool: semgrep -- https://semgrep.dev
24
- # ============================================================================
25
- # #532: this is a legacy gate-9 fallback copy, kept alive by
26
- # resolveGateScript(9) in TS-less deployments (xp-gate gate sast without the
27
- # src/gates/gate-9.ts entry). The verdict semantics below are ported from
28
- # gate-10.sh (the #518 fix, v0.21.0) — the verdict block must stay byte
29
- # identical across githooks/gate-9.sh, githooks/gates/gate-9-sast-security.sh
30
- # and githooks/adapters/gate-9.sh; parity is asserted by
31
- # githooks/__tests__/issue-532-gate9-verdict.test.bats (AC-A-02-04).
32
-
33
- 2>&1 echo ""
34
- 2>&1 echo "→ Gate 9: Semgrep SAST Security Scan..."
35
- GATE_9_START=$(gate_start_ms)
36
-
37
- GATE_9_STATUS=""
38
-
39
- # Semgrep availability check
40
- SEMGREP_CMD=""
41
- if command -v semgrep >/dev/null 2>&1; then
42
- SEMGREP_CMD="semgrep"
43
- elif [ -f "$HOME/.local/bin/semgrep" ]; then
44
- SEMGREP_CMD="$HOME/.local/bin/semgrep"
45
- fi
46
-
47
- if [ -z "$SEMGREP_CMD" ]; then
48
- echo " ⚠️ WARN - semgrep not installed — SAST scanning unavailable"
49
- echo " Install: brew install semgrep (macOS) | pip install semgrep (Linux) | pip install semgrep (Windows)"
50
- echo " Pre-cache rules: semgrep --config=p/security-audit"
51
- echo " Gate 9: SAST Security (WARN, semgrep not installed)"
52
- GATE_9_STATUS="WARN"
53
- else
54
- # Get staged files filtered to Semgrep-supported languages.
55
- #
56
- # #616: under `--all` the index is usually empty, so the staged diff yielded no
57
- # files and the gate reported SKIPPED -- "no supported language files changed"
58
- # -- for a run whose whole purpose was to scan the tree. Same fail-open shape
59
- # #589 fixed in Gate 3, one layer down: the CLI says the tree was checked and
60
- # the fragment checks a diff that does not exist. The runner exports GATE_SCOPE;
61
- # read it and enumerate tracked files instead.
62
- if [ "${GATE_SCOPE:-diff}" = "all" ]; then
63
- echo " Scope: --all (scanning tracked files, not the index)"
64
- SEMGREP_FILES=$(git ls-files 2>/dev/null | grep -E '\.(ts|tsx|js|jsx|py|go|java|c|cpp|cs|rb|php|scala|swift)$' || true)
65
- else
66
- SEMGREP_FILES=$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null | grep -E '\.(ts|tsx|js|jsx|py|go|java|c|cpp|cs|rb|php|scala|swift)$' || true)
67
- fi
68
-
69
- if [ -z "$SEMGREP_FILES" ]; then
70
- echo " ⏭️ SKIPPED - SAST (no supported language files changed)"
71
- GATE_9_STATUS="SKIP"
72
- else
73
- # One path per line into an array. Quoting the newline-joined list would hand
74
- # semgrep ONE argument containing newlines -> "Invalid scanning root" once
75
- # >=2 files are staged (#490). Same defect class as the Gate 4 --files fix
76
- # (#457): an array is the only form that keeps spaces intact AND splits files.
77
- #
78
- # Each element gets a ./ prefix: a tracked path may legitimately start with
79
- # '-' (`git add -- ./-o.ts`), and bare in argv that element reads as an option
80
- # and rewrites the scan it was meant to constrain. ./ is a path to every
81
- # argument parser.
82
- #
83
- # The unquoted heredoc below expands $SEMGREP_FILES once, then feeds literal
84
- # lines to `read`; bash never rescans the result for separators. Its correct
85
- # reading rests on one property of the producer above, not of the loop:
86
- # `git diff --cached --name-only` terminates paths with LF and quotes control
87
- # characters (a CR in a name arrives as the four bytes \215, never as a bare
88
- # CR), so one line is exactly one path. Asserted for spaces and a leading '-'
89
- # by AC-490-04.
90
- SEMGREP_ARGS=()
91
- SEMGREP_FILE_COUNT=0
92
- SEMGREP_DIRS_SEEN=$'\n'
93
- while IFS= read -r _semgrep_file; do
94
- [ -n "$_semgrep_file" ] || continue
95
- SEMGREP_FILE_COUNT=$((SEMGREP_FILE_COUNT + 1))
96
- SEMGREP_ARGS+=("--include=${_semgrep_file##*/}")
97
- _semgrep_dir="./${_semgrep_file%/*}"
98
- # A repo-root file has no directory component ("./x.ts" strips to "./").
99
- [ "$_semgrep_dir" = "./" ] && _semgrep_dir="."
100
- case "$SEMGREP_DIRS_SEEN" in
101
- *"$_semgrep_dir"*) : ;;
102
- *) SEMGREP_ARGS+=("$_semgrep_dir"); SEMGREP_DIRS_SEEN="$SEMGREP_DIRS_SEEN$_semgrep_dir"$'\n' ;;
103
- esac
104
- done <<EOF
105
- $SEMGREP_FILES
106
- EOF
107
-
108
- # The verdict is parsed from the JSON with node (see below), so node is a
109
- # hard dependency of this gate — check it BEFORE spending a semgrep run
110
- # (#518 R2): without it we would silently SKIP every finding as
111
- # "unparseable" and lose the whole security net on minimal CI images.
112
- if ! command -v node >/dev/null 2>&1; then
113
- echo " ⚠️ WARN - node is not installed — Gate 9 needs it to parse semgrep's JSON verdict"
114
- echo " Install node (https://nodejs.org); without it SAST findings cannot be classified"
115
- echo " ⏭️ SKIPPED - SAST (node not installed)"
116
- GATE_9_STATUS="SKIP"
117
- else
118
-
119
- # Run semgrep with JSON output. stdout and stderr are captured SEPARATELY:
120
- # --json writes findings to stdout, progress/summary lines to stderr, and the
121
- # old `2>&1` merge poisoned the very JSON the verdict now depends on (#518).
122
- # SEMGREP_OUTPUT keeps its name and role — what the tool said, printed on the
123
- # SKIP path (AC-475-04).
124
- #
125
- # #518: the exit code does NOT carry the verdict. semgrep exits 0 even with
126
- # ERROR-severity findings, and its exit-code semantics have drifted across
127
- # versions and platforms (upstream currently reports findings WITH --error
128
- # as exit 1 and failures as 2, but that mapping is not something a gate
129
- # should bet on). The JSON carries what the exit code never could —
130
- # per-severity counts — so the verdict is read from the JSON and the exit
131
- # code is demoted to "did the tool itself run". --error stays OFF: it would
132
- # add nothing the JSON does not already provide, and keeping it off leaves
133
- # failure exit codes unambiguous for the #475 diagnostics path.
134
- # #717: p/security-audit carries NO C# rules. Measured: a .cs file with a
135
- # textbook SQL injection (string-concatenated SqlCommand) and a
136
- # Process.Start command injection produced 0 findings under
137
- # p/security-audit and 1 ERROR under p/csharp. Since gate-9.sh:45 lists
138
- # `cs` in the file filter, .cs was accepted and then scanned by a ruleset
139
- # that cannot see C# -- the identical "filter looks like coverage" defect
140
- # this issue removed from Gate 4. p/csharp is loaded IN ADDITION to
141
- # p/security-audit (not instead of: the security-audit rules still apply
142
- # to the other languages in a mixed commit).
143
- SEMGREP_RULES=(--config=p/security-audit)
144
- if printf "%s
145
- " "$SEMGREP_FILES" | grep -qE ".cs$"; then
146
- SEMGREP_RULES+=(--config=p/csharp)
147
- fi
148
- SEMGREP_STDOUT_FILE=$(mktemp)
149
- SEMGREP_OUTPUT=$("$SEMGREP_CMD" scan "${SEMGREP_RULES[@]}" --json --disable-version-check "${SEMGREP_ARGS[@]}" 2>&1 >"$SEMGREP_STDOUT_FILE")
150
- SEMGREP_EXIT=$?
151
-
152
- # One node pass over the JSON: first line = verdict counts, remaining lines =
153
- # finding details. The severity vocabulary is the UNION of the two scales
154
- # (#518): semgrep's own ERROR is its most severe tier (it maps to the old
155
- # CRITICAL/HIGH → BLOCK intent), its WARNING/INFO align with the old
156
- # MEDIUM/LOW → warnings-only tier. Anything OUTSIDE both lists (missing,
157
- # empty, or a future/new tier) counts as BLOCKING — a security gate must
158
- # fail closed on severities it does not recognize (#518 R2). Parsing
159
- # failure degrades to PARSE=FAIL — a fail-closed SKIP below, never a PASS.
160
- SEMGREP_VERDICT=$(node -e '
161
- let d="";process.stdin.on("data",c=>d+=c);process.stdin.on("end",()=>{
162
- let data;try{data=JSON.parse(d);}catch(e){console.log("PARSE=FAIL RESULTS=0 ERRORS=0 BLOCKING=0 WARNINGS=0");return;}
163
- const results=data.results||[];const errors=data.errors||[];
164
- const sev=r=>((r.extra&&r.extra.severity)||"").toUpperCase();
165
- const WARNV=["WARNING","INFO","MEDIUM","LOW"];
166
- let b=0,w=0;results.forEach(r=>{if(WARNV.indexOf(sev(r))>=0)w++;else b++;});
167
- console.log("PARSE=OK RESULTS="+results.length+" ERRORS="+errors.length+" BLOCKING="+b+" WARNINGS="+w);
168
- results.slice(0,5).forEach(r=>{const extra=r.extra||{};const s=sev(r)||"UNKNOWN";
169
- console.log(" ["+s+"] "+(r.check_id||"unknown"));
170
- console.log(" "+(r.path||"unknown")+":"+((r.start&&r.start.line)||"?")+" → "+(extra.message||"").slice(0,80));
171
- console.log("");});
172
- });' < "$SEMGREP_STDOUT_FILE") || SEMGREP_VERDICT="PARSE=FAIL RESULTS=0 ERRORS=0 BLOCKING=0 WARNINGS=0"
173
-
174
- PARSE_LINE=$(printf '%s\n' "$SEMGREP_VERDICT" | sed -n '1p')
175
- FINDING_DETAILS=$(printf '%s\n' "$SEMGREP_VERDICT" | sed -n '2,$p')
176
- V_PARSE=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/^PARSE=\([A-Z]*\) .*/\1/p')
177
- V_RESULTS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* RESULTS=\([0-9]*\) .*/\1/p')
178
- V_ERRORS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* ERRORS=\([0-9]*\) .*/\1/p')
179
- V_BLOCKING=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* BLOCKING=\([0-9]*\) .*/\1/p')
180
- V_WARNINGS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* WARNINGS=\([0-9]*\)$/\1/p')
181
- # Keep the raw stdout tail for the unparseable branch's diagnostics BEFORE
182
- # the temp file goes away (#518 R2: stderr alone often says nothing there).
183
- SEMGREP_STDOUT_TAIL=$(tail -5 "$SEMGREP_STDOUT_FILE" 2>/dev/null)
184
- rm -f "$SEMGREP_STDOUT_FILE"
185
-
186
- # Fail closed: an unparseable payload OR an OK parse with any missing count
187
- # field (verdict-line format drift) must SKIP, never silently degrade to
188
- # PASS (#518).
189
- if [ "$V_PARSE" != "OK" ] || [ -z "$V_RESULTS" ] || [ -z "$V_ERRORS" ] || [ -z "$V_BLOCKING" ] || [ -z "$V_WARNINGS" ]; then
190
- # Unparseable output: nothing the exit code claims can be trusted (#518).
191
- # Fail closed — SKIP with diagnostics, never a PASS.
192
- echo " ⚠️ semgrep output unparseable or verdict fields missing (exit code ${SEMGREP_EXIT}) — cannot determine verdict"
193
- if [ -n "$SEMGREP_OUTPUT" ]; then
194
- echo " ── what semgrep reported on stderr ──"
195
- printf '%s\n' "$SEMGREP_OUTPUT" | tail -10 | sed 's/^/ /'
196
- fi
197
- if [ -n "$SEMGREP_STDOUT_TAIL" ]; then
198
- echo " ── what semgrep wrote to stdout (tail) ──"
199
- printf '%s\n' "$SEMGREP_STDOUT_TAIL" | sed 's/^/ /'
200
- fi
201
- echo " ⏭️ SKIPPED - SAST (semgrep output unparseable)"
202
- GATE_9_STATUS="SKIP"
203
- elif [ "$V_RESULTS" -eq 0 ] && { [ "$V_ERRORS" -gt 0 ] || [ "$SEMGREP_EXIT" -ne 0 ]; }; then
204
- # Nothing was scanned to a verdict: semgrep either failed outright
205
- # (nonzero exit) or completed but reported scan errors for every input
206
- # (#518 R2: exit 0 + results:[] + errors:[{...}] used to fall through to
207
- # PASS). An operational failure must neither block nor claim a pass
208
- # (#475 kept).
209
- echo " ⚠️ semgrep exit=${SEMGREP_EXIT}, scan errors=${V_ERRORS}, findings=${V_RESULTS} — nothing to judge"
210
- if [ -n "$SEMGREP_OUTPUT" ]; then
211
- echo " ── what semgrep reported ──"
212
- printf '%s\n' "$SEMGREP_OUTPUT" | tail -10 | sed 's/^/ /'
213
- fi
214
- echo " ⏭️ SKIPPED - SAST (semgrep runtime error)"
215
- GATE_9_STATUS="SKIP"
216
- elif [ "$V_BLOCKING" -gt 0 ]; then
217
- echo ""
218
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
219
- echo " GATE 9: Semgrep Security Gate"
220
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
221
- echo " CRITICAL/HIGH/ERROR: ${V_BLOCKING} ❌ BLOCKED"
222
- echo " MEDIUM/LOW/WARNING/INFO: ${V_WARNINGS} ⚠️ warning"
223
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
224
- echo " ❌ BLOCKED — blocking-severity vulnerability found"
225
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
226
- echo "$FINDING_DETAILS"
227
- # #717: name BOTH rulesets. A C# finding comes from p/csharp, so
228
- # pointing the author at p/security-audit alone reproduces zero hits.
229
- echo " Run 'semgrep scan --config=p/security-audit --config=p/csharp' to review all findings."
230
- GATE_9_STATUS="FAIL"
231
- exit 1
232
- else
233
- echo ""
234
- echo " ✅ PASSED - No blocking vulnerabilities (files scanned: $SEMGREP_FILE_COUNT)"
235
- if [ "$V_WARNINGS" -gt 0 ]; then
236
- echo " ⚠️ ${V_WARNINGS} non-blocking findings (warnings only)"
237
- echo "$FINDING_DETAILS"
238
- fi
239
- GATE_9_STATUS="PASS"
240
- fi
241
-
242
- fi # node available
243
- fi
244
- fi
245
- record_gate_audit "gate-9" "sast-security" "$GATE_9_STATUS" "0" "$GATE_9_START"
package/hooks/gate-9.sh DELETED
@@ -1,245 +0,0 @@
1
- #!/usr/bin/env bash
2
- # ============================================================================
3
- # ⚠️ DEAD CODE — NOT EXECUTED. Read this before changing anything here.
4
- # ============================================================================
5
- # This file is NOT sourced by githooks/pre-commit or githooks/pre-push (both
6
- # source count = 0; verify with: grep -c "gate-9.sh" githooks/pre-commit).
7
- # It is shipped by githooks/install.sh but never runs.
8
- #
9
- # The gate that actually runs semgrep SAST is githooks/gate-10.sh, and it
10
- # reports as `gate10_sast`. Gate 9 in the report is `gate9_build_integrity`
11
- # (Build Integrity), a different gate entirely.
12
- #
13
- # This mislabelling is not cosmetic: in #717 a C# SAST fix was first written
14
- # into THIS file, where it changed nothing, while .cs stayed ungoverned. The
15
- # C# ruleset loading that works lives in gate-10.sh. Do not relocate it here.
16
- #
17
- # Kept rather than deleted because githooks/install.sh:GATE_SCRIPTS ships it;
18
- # removing it is a separate change. See issue #717 walkthrough MC-03.
19
- # ============================================================================
20
- # ============================================================================
21
- # GATE 9: Semgrep SAST Security Scan
22
- # Detects security vulnerabilities (SQL injection, XSS, etc.) in staged files
23
- # Tool: semgrep -- https://semgrep.dev
24
- # ============================================================================
25
- # #532: this is a legacy gate-9 fallback copy, kept alive by
26
- # resolveGateScript(9) in TS-less deployments (xp-gate gate sast without the
27
- # src/gates/gate-9.ts entry). The verdict semantics below are ported from
28
- # gate-10.sh (the #518 fix, v0.21.0) — the verdict block must stay byte
29
- # identical across githooks/gate-9.sh, githooks/gates/gate-9-sast-security.sh
30
- # and githooks/adapters/gate-9.sh; parity is asserted by
31
- # githooks/__tests__/issue-532-gate9-verdict.test.bats (AC-A-02-04).
32
-
33
- 2>&1 echo ""
34
- 2>&1 echo "→ Gate 9: Semgrep SAST Security Scan..."
35
- GATE_9_START=$(gate_start_ms)
36
-
37
- GATE_9_STATUS=""
38
-
39
- # Semgrep availability check
40
- SEMGREP_CMD=""
41
- if command -v semgrep >/dev/null 2>&1; then
42
- SEMGREP_CMD="semgrep"
43
- elif [ -f "$HOME/.local/bin/semgrep" ]; then
44
- SEMGREP_CMD="$HOME/.local/bin/semgrep"
45
- fi
46
-
47
- if [ -z "$SEMGREP_CMD" ]; then
48
- echo " ⚠️ WARN - semgrep not installed — SAST scanning unavailable"
49
- echo " Install: brew install semgrep (macOS) | pip install semgrep (Linux) | pip install semgrep (Windows)"
50
- echo " Pre-cache rules: semgrep --config=p/security-audit"
51
- echo " Gate 9: SAST Security (WARN, semgrep not installed)"
52
- GATE_9_STATUS="WARN"
53
- else
54
- # Get staged files filtered to Semgrep-supported languages.
55
- #
56
- # #616: under `--all` the index is usually empty, so the staged diff yielded no
57
- # files and the gate reported SKIPPED -- "no supported language files changed"
58
- # -- for a run whose whole purpose was to scan the tree. Same fail-open shape
59
- # #589 fixed in Gate 3, one layer down: the CLI says the tree was checked and
60
- # the fragment checks a diff that does not exist. The runner exports GATE_SCOPE;
61
- # read it and enumerate tracked files instead.
62
- if [ "${GATE_SCOPE:-diff}" = "all" ]; then
63
- echo " Scope: --all (scanning tracked files, not the index)"
64
- SEMGREP_FILES=$(git ls-files 2>/dev/null | grep -E '\.(ts|tsx|js|jsx|py|go|java|c|cpp|cs|rb|php|scala|swift)$' || true)
65
- else
66
- SEMGREP_FILES=$(git diff --cached --name-only --diff-filter=ACM 2>/dev/null | grep -E '\.(ts|tsx|js|jsx|py|go|java|c|cpp|cs|rb|php|scala|swift)$' || true)
67
- fi
68
-
69
- if [ -z "$SEMGREP_FILES" ]; then
70
- echo " ⏭️ SKIPPED - SAST (no supported language files changed)"
71
- GATE_9_STATUS="SKIP"
72
- else
73
- # One path per line into an array. Quoting the newline-joined list would hand
74
- # semgrep ONE argument containing newlines -> "Invalid scanning root" once
75
- # >=2 files are staged (#490). Same defect class as the Gate 4 --files fix
76
- # (#457): an array is the only form that keeps spaces intact AND splits files.
77
- #
78
- # Each element gets a ./ prefix: a tracked path may legitimately start with
79
- # '-' (`git add -- ./-o.ts`), and bare in argv that element reads as an option
80
- # and rewrites the scan it was meant to constrain. ./ is a path to every
81
- # argument parser.
82
- #
83
- # The unquoted heredoc below expands $SEMGREP_FILES once, then feeds literal
84
- # lines to `read`; bash never rescans the result for separators. Its correct
85
- # reading rests on one property of the producer above, not of the loop:
86
- # `git diff --cached --name-only` terminates paths with LF and quotes control
87
- # characters (a CR in a name arrives as the four bytes \215, never as a bare
88
- # CR), so one line is exactly one path. Asserted for spaces and a leading '-'
89
- # by AC-490-04.
90
- SEMGREP_ARGS=()
91
- SEMGREP_FILE_COUNT=0
92
- SEMGREP_DIRS_SEEN=$'\n'
93
- while IFS= read -r _semgrep_file; do
94
- [ -n "$_semgrep_file" ] || continue
95
- SEMGREP_FILE_COUNT=$((SEMGREP_FILE_COUNT + 1))
96
- SEMGREP_ARGS+=("--include=${_semgrep_file##*/}")
97
- _semgrep_dir="./${_semgrep_file%/*}"
98
- # A repo-root file has no directory component ("./x.ts" strips to "./").
99
- [ "$_semgrep_dir" = "./" ] && _semgrep_dir="."
100
- case "$SEMGREP_DIRS_SEEN" in
101
- *"$_semgrep_dir"*) : ;;
102
- *) SEMGREP_ARGS+=("$_semgrep_dir"); SEMGREP_DIRS_SEEN="$SEMGREP_DIRS_SEEN$_semgrep_dir"$'\n' ;;
103
- esac
104
- done <<EOF
105
- $SEMGREP_FILES
106
- EOF
107
-
108
- # The verdict is parsed from the JSON with node (see below), so node is a
109
- # hard dependency of this gate — check it BEFORE spending a semgrep run
110
- # (#518 R2): without it we would silently SKIP every finding as
111
- # "unparseable" and lose the whole security net on minimal CI images.
112
- if ! command -v node >/dev/null 2>&1; then
113
- echo " ⚠️ WARN - node is not installed — Gate 9 needs it to parse semgrep's JSON verdict"
114
- echo " Install node (https://nodejs.org); without it SAST findings cannot be classified"
115
- echo " ⏭️ SKIPPED - SAST (node not installed)"
116
- GATE_9_STATUS="SKIP"
117
- else
118
-
119
- # Run semgrep with JSON output. stdout and stderr are captured SEPARATELY:
120
- # --json writes findings to stdout, progress/summary lines to stderr, and the
121
- # old `2>&1` merge poisoned the very JSON the verdict now depends on (#518).
122
- # SEMGREP_OUTPUT keeps its name and role — what the tool said, printed on the
123
- # SKIP path (AC-475-04).
124
- #
125
- # #518: the exit code does NOT carry the verdict. semgrep exits 0 even with
126
- # ERROR-severity findings, and its exit-code semantics have drifted across
127
- # versions and platforms (upstream currently reports findings WITH --error
128
- # as exit 1 and failures as 2, but that mapping is not something a gate
129
- # should bet on). The JSON carries what the exit code never could —
130
- # per-severity counts — so the verdict is read from the JSON and the exit
131
- # code is demoted to "did the tool itself run". --error stays OFF: it would
132
- # add nothing the JSON does not already provide, and keeping it off leaves
133
- # failure exit codes unambiguous for the #475 diagnostics path.
134
- # #717: p/security-audit carries NO C# rules. Measured: a .cs file with a
135
- # textbook SQL injection (string-concatenated SqlCommand) and a
136
- # Process.Start command injection produced 0 findings under
137
- # p/security-audit and 1 ERROR under p/csharp. Since gate-9.sh:45 lists
138
- # `cs` in the file filter, .cs was accepted and then scanned by a ruleset
139
- # that cannot see C# -- the identical "filter looks like coverage" defect
140
- # this issue removed from Gate 4. p/csharp is loaded IN ADDITION to
141
- # p/security-audit (not instead of: the security-audit rules still apply
142
- # to the other languages in a mixed commit).
143
- SEMGREP_RULES=(--config=p/security-audit)
144
- if printf "%s
145
- " "$SEMGREP_FILES" | grep -qE ".cs$"; then
146
- SEMGREP_RULES+=(--config=p/csharp)
147
- fi
148
- SEMGREP_STDOUT_FILE=$(mktemp)
149
- SEMGREP_OUTPUT=$("$SEMGREP_CMD" scan "${SEMGREP_RULES[@]}" --json --disable-version-check "${SEMGREP_ARGS[@]}" 2>&1 >"$SEMGREP_STDOUT_FILE")
150
- SEMGREP_EXIT=$?
151
-
152
- # One node pass over the JSON: first line = verdict counts, remaining lines =
153
- # finding details. The severity vocabulary is the UNION of the two scales
154
- # (#518): semgrep's own ERROR is its most severe tier (it maps to the old
155
- # CRITICAL/HIGH → BLOCK intent), its WARNING/INFO align with the old
156
- # MEDIUM/LOW → warnings-only tier. Anything OUTSIDE both lists (missing,
157
- # empty, or a future/new tier) counts as BLOCKING — a security gate must
158
- # fail closed on severities it does not recognize (#518 R2). Parsing
159
- # failure degrades to PARSE=FAIL — a fail-closed SKIP below, never a PASS.
160
- SEMGREP_VERDICT=$(node -e '
161
- let d="";process.stdin.on("data",c=>d+=c);process.stdin.on("end",()=>{
162
- let data;try{data=JSON.parse(d);}catch(e){console.log("PARSE=FAIL RESULTS=0 ERRORS=0 BLOCKING=0 WARNINGS=0");return;}
163
- const results=data.results||[];const errors=data.errors||[];
164
- const sev=r=>((r.extra&&r.extra.severity)||"").toUpperCase();
165
- const WARNV=["WARNING","INFO","MEDIUM","LOW"];
166
- let b=0,w=0;results.forEach(r=>{if(WARNV.indexOf(sev(r))>=0)w++;else b++;});
167
- console.log("PARSE=OK RESULTS="+results.length+" ERRORS="+errors.length+" BLOCKING="+b+" WARNINGS="+w);
168
- results.slice(0,5).forEach(r=>{const extra=r.extra||{};const s=sev(r)||"UNKNOWN";
169
- console.log(" ["+s+"] "+(r.check_id||"unknown"));
170
- console.log(" "+(r.path||"unknown")+":"+((r.start&&r.start.line)||"?")+" → "+(extra.message||"").slice(0,80));
171
- console.log("");});
172
- });' < "$SEMGREP_STDOUT_FILE") || SEMGREP_VERDICT="PARSE=FAIL RESULTS=0 ERRORS=0 BLOCKING=0 WARNINGS=0"
173
-
174
- PARSE_LINE=$(printf '%s\n' "$SEMGREP_VERDICT" | sed -n '1p')
175
- FINDING_DETAILS=$(printf '%s\n' "$SEMGREP_VERDICT" | sed -n '2,$p')
176
- V_PARSE=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/^PARSE=\([A-Z]*\) .*/\1/p')
177
- V_RESULTS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* RESULTS=\([0-9]*\) .*/\1/p')
178
- V_ERRORS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* ERRORS=\([0-9]*\) .*/\1/p')
179
- V_BLOCKING=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* BLOCKING=\([0-9]*\) .*/\1/p')
180
- V_WARNINGS=$(printf '%s\n' "$PARSE_LINE" | sed -n 's/.* WARNINGS=\([0-9]*\)$/\1/p')
181
- # Keep the raw stdout tail for the unparseable branch's diagnostics BEFORE
182
- # the temp file goes away (#518 R2: stderr alone often says nothing there).
183
- SEMGREP_STDOUT_TAIL=$(tail -5 "$SEMGREP_STDOUT_FILE" 2>/dev/null)
184
- rm -f "$SEMGREP_STDOUT_FILE"
185
-
186
- # Fail closed: an unparseable payload OR an OK parse with any missing count
187
- # field (verdict-line format drift) must SKIP, never silently degrade to
188
- # PASS (#518).
189
- if [ "$V_PARSE" != "OK" ] || [ -z "$V_RESULTS" ] || [ -z "$V_ERRORS" ] || [ -z "$V_BLOCKING" ] || [ -z "$V_WARNINGS" ]; then
190
- # Unparseable output: nothing the exit code claims can be trusted (#518).
191
- # Fail closed — SKIP with diagnostics, never a PASS.
192
- echo " ⚠️ semgrep output unparseable or verdict fields missing (exit code ${SEMGREP_EXIT}) — cannot determine verdict"
193
- if [ -n "$SEMGREP_OUTPUT" ]; then
194
- echo " ── what semgrep reported on stderr ──"
195
- printf '%s\n' "$SEMGREP_OUTPUT" | tail -10 | sed 's/^/ /'
196
- fi
197
- if [ -n "$SEMGREP_STDOUT_TAIL" ]; then
198
- echo " ── what semgrep wrote to stdout (tail) ──"
199
- printf '%s\n' "$SEMGREP_STDOUT_TAIL" | sed 's/^/ /'
200
- fi
201
- echo " ⏭️ SKIPPED - SAST (semgrep output unparseable)"
202
- GATE_9_STATUS="SKIP"
203
- elif [ "$V_RESULTS" -eq 0 ] && { [ "$V_ERRORS" -gt 0 ] || [ "$SEMGREP_EXIT" -ne 0 ]; }; then
204
- # Nothing was scanned to a verdict: semgrep either failed outright
205
- # (nonzero exit) or completed but reported scan errors for every input
206
- # (#518 R2: exit 0 + results:[] + errors:[{...}] used to fall through to
207
- # PASS). An operational failure must neither block nor claim a pass
208
- # (#475 kept).
209
- echo " ⚠️ semgrep exit=${SEMGREP_EXIT}, scan errors=${V_ERRORS}, findings=${V_RESULTS} — nothing to judge"
210
- if [ -n "$SEMGREP_OUTPUT" ]; then
211
- echo " ── what semgrep reported ──"
212
- printf '%s\n' "$SEMGREP_OUTPUT" | tail -10 | sed 's/^/ /'
213
- fi
214
- echo " ⏭️ SKIPPED - SAST (semgrep runtime error)"
215
- GATE_9_STATUS="SKIP"
216
- elif [ "$V_BLOCKING" -gt 0 ]; then
217
- echo ""
218
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
219
- echo " GATE 9: Semgrep Security Gate"
220
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
221
- echo " CRITICAL/HIGH/ERROR: ${V_BLOCKING} ❌ BLOCKED"
222
- echo " MEDIUM/LOW/WARNING/INFO: ${V_WARNINGS} ⚠️ warning"
223
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
224
- echo " ❌ BLOCKED — blocking-severity vulnerability found"
225
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
226
- echo "$FINDING_DETAILS"
227
- # #717: name BOTH rulesets. A C# finding comes from p/csharp, so
228
- # pointing the author at p/security-audit alone reproduces zero hits.
229
- echo " Run 'semgrep scan --config=p/security-audit --config=p/csharp' to review all findings."
230
- GATE_9_STATUS="FAIL"
231
- exit 1
232
- else
233
- echo ""
234
- echo " ✅ PASSED - No blocking vulnerabilities (files scanned: $SEMGREP_FILE_COUNT)"
235
- if [ "$V_WARNINGS" -gt 0 ]; then
236
- echo " ⚠️ ${V_WARNINGS} non-blocking findings (warnings only)"
237
- echo "$FINDING_DETAILS"
238
- fi
239
- GATE_9_STATUS="PASS"
240
- fi
241
-
242
- fi # node available
243
- fi
244
- fi
245
- record_gate_audit "gate-9" "sast-security" "$GATE_9_STATUS" "0" "$GATE_9_START"