|
| 1 | +# Custom semgrep rules: silent-success masking (AI004 / CA-09, issue #257). |
| 2 | +# |
| 3 | +# Detects catch/except blocks that swallow a failure and return a |
| 4 | +# plausible-but-empty default ([] / {} / null / undefined / None / "" ...) |
| 5 | +# instead of surfacing the error. Bare-except and empty-catch are covered by |
| 6 | +# existing gates (ruff E722/B, eslint); this targets the more dangerous |
| 7 | +# variant where the caller sees a "successful" empty result and cannot tell |
| 8 | +# it apart from a real one. |
| 9 | +# |
| 10 | +# Allowlist mechanism: intentional degraded-mode fallbacks get an inline |
| 11 | +# // nosemgrep: <rule-id> -- <justification> (TS) |
| 12 | +# # nosemgrep: <rule-id> -- <justification> (Py) |
| 13 | +# on the flagged return line (repo convention; precedent in |
| 14 | +# scripts/check-types-sync.ts and agent/src/config.py). |
| 15 | +# |
| 16 | +# Known limitation: a `return null`/`return []` inside a callback *defined |
| 17 | +# within* a catch block (e.g. `.map(i => ... return null ...)`) is flagged |
| 18 | +# even though it does not mask the outer failure. Rare in practice; use the |
| 19 | +# nosemgrep allowlist if it occurs. |
| 20 | +# |
| 21 | +# Run: mise run security:sast:masking (blocking; emits SARIF) |
| 22 | +# Test: semgrep test .semgrep/ |
| 23 | +rules: |
| 24 | + - id: ts-silent-success-masking |
| 25 | + languages: [typescript, javascript] |
| 26 | + severity: WARNING |
| 27 | + metadata: |
| 28 | + category: correctness |
| 29 | + confidence: MEDIUM |
| 30 | + abca-finding: AI004 |
| 31 | + references: |
| 32 | + - https://github.com/aws-samples/sample-autonomous-cloud-coding-agents/issues/257 |
| 33 | + message: >- |
| 34 | + This catch block swallows the error and returns an empty default, so |
| 35 | + the caller cannot distinguish failure from a genuinely empty result |
| 36 | + (silent-success masking, AI004). Fix: re-throw (`throw err;`), throw a |
| 37 | + typed error that adds context, or return a result shape that encodes |
| 38 | + the failure. Logging alone is not enough — the failure must reach the |
| 39 | + caller. If this fallback is intentional degraded-mode behavior, keep it |
| 40 | + and add on the return line |
| 41 | + "// nosemgrep: ts-silent-success-masking -- <why callers may safely |
| 42 | + treat this failure as an empty success>". |
| 43 | + patterns: |
| 44 | + - pattern-either: |
| 45 | + - pattern: | |
| 46 | + try { ... } catch ($E) { ... return $RET; ... } |
| 47 | + - pattern: | |
| 48 | + try { ... } catch { ... return $RET; ... } |
| 49 | + - pattern: | |
| 50 | + try { ... } catch ($E) { ... return $RET; ... } finally { ... } |
| 51 | + - pattern: | |
| 52 | + try { ... } catch { ... return $RET; ... } finally { ... } |
| 53 | + - metavariable-regex: |
| 54 | + metavariable: $RET |
| 55 | + regex: ^(null|undefined|\[\s*\]|\{\s*\}|""|''|``)$ |
| 56 | + - focus-metavariable: $RET |
| 57 | + |
| 58 | + - id: py-silent-success-masking |
| 59 | + languages: [python] |
| 60 | + severity: WARNING |
| 61 | + metadata: |
| 62 | + category: correctness |
| 63 | + confidence: MEDIUM |
| 64 | + abca-finding: AI004 |
| 65 | + references: |
| 66 | + - https://github.com/aws-samples/sample-autonomous-cloud-coding-agents/issues/257 |
| 67 | + message: >- |
| 68 | + This except block swallows the error and returns an empty default, so |
| 69 | + the caller cannot distinguish failure from a genuinely empty result |
| 70 | + (silent-success masking, AI004). Fix: re-raise (`raise`), raise a typed |
| 71 | + error that adds context (`raise XError(...) from exc`), or return a |
| 72 | + result shape that encodes the failure. Logging alone is not enough — |
| 73 | + the failure must reach the caller. If this fallback is intentional |
| 74 | + degraded-mode behavior, keep it and add on the return line |
| 75 | + "# nosemgrep: py-silent-success-masking -- <why callers may safely |
| 76 | + treat this failure as an empty success>". |
| 77 | + patterns: |
| 78 | + - pattern-either: |
| 79 | + - pattern: | |
| 80 | + try: |
| 81 | + ... |
| 82 | + except $EXC: |
| 83 | + ... |
| 84 | + return $RET |
| 85 | + - pattern: | |
| 86 | + try: |
| 87 | + ... |
| 88 | + except $EXC as $E: |
| 89 | + ... |
| 90 | + return $RET |
| 91 | + - pattern: | |
| 92 | + try: |
| 93 | + ... |
| 94 | + except: |
| 95 | + ... |
| 96 | + return $RET |
| 97 | + - pattern: | |
| 98 | + try: |
| 99 | + ... |
| 100 | + except $EXC: |
| 101 | + ... |
| 102 | + return $RET |
| 103 | + finally: |
| 104 | + ... |
| 105 | + - pattern: | |
| 106 | + try: |
| 107 | + ... |
| 108 | + except $EXC as $E: |
| 109 | + ... |
| 110 | + return $RET |
| 111 | + finally: |
| 112 | + ... |
| 113 | + - pattern: | |
| 114 | + try: |
| 115 | + ... |
| 116 | + except: |
| 117 | + ... |
| 118 | + return $RET |
| 119 | + finally: |
| 120 | + ... |
| 121 | + - metavariable-regex: |
| 122 | + metavariable: $RET |
| 123 | + regex: ^(None|\[\s*\]|\{\s*\}|""|''|\(\s*\)|set\(\s*\)|dict\(\s*\)|list\(\s*\)|tuple\(\s*\))$ |
| 124 | + - focus-metavariable: $RET |
0 commit comments