Skip to content

feat(scan): weak-crypto rules for Python — coverage 65.9% → 71.3% (v0.9.0) - #102

Merged
ralyodio merged 1 commit into
masterfrom
fix/weak-crypto-coverage
Aug 11, 2026
Merged

feat(scan): weak-crypto rules for Python — coverage 65.9% → 71.3% (v0.9.0)#102
ralyodio merged 1 commit into
masterfrom
fix/weak-crypto-coverage

Conversation

@ralyodio

Copy link
Copy Markdown
Contributor

Release 0.9.0. Raises detection coverage against the testbed from 65.9% → 71.3% (85 → 92 of 129), false-positive rate still 0%.

The gap

The CWE-327/338 cluster was the largest recoverable miss — primitives threatcrush had generic rules for (weak-hash-on-credential, insecure-randomness-for-secret), but scoped to a credential on the matched line. Python's integrity- and confidentiality-use cases carry the security role in the enclosing function, not on the line, so they slipped through.

Three Python rules

Each keyed on a signal that survives without trusting a name:

rule catches note
py-broken-cipher DES/RC2/RC4/Blowfish.new(...), AES.new(..., MODE_ECB) no safe use → inherent; AES matches only on ECB, GCM/CTR/CBC left alone
py-weak-hash hashlib.md5/sha1(...) exempt when the line carries Python's own usedforsecurity=False
py-predictable-random-seed random.seed(<clock/pid>) a fixed integer seed (reproducible tests) is left alone

What I deliberately did not add

A rule for a random-drawn token whose only signal is the enclosing function name (generate_session_id, generate_mfa_code). Guard windows exclude definition lines on purpose — a name is not evidence, the same reason a def sanitize_… doesn't count as sanitisation. There's no line-level signal left to key on, so forcing it would mean either trusting names (a documented regression risk) or shape-heuristics that add false positives for marginal gain.

The generic insecure-randomness-for-secret still catches the common real shape (token = "".join(random.choice(...))). The name-only cases are the documented tail, pinned by a test asserting they stay silent. Honest boundary, not an oversight.

Verification

  • Testbed: 65.9% → 71.3% TPR, 0% FPR. All 5 CWE-327 lines caught; the predictable-seed CWE-338 line caught; the name-only CWE-338 lines left as tail.
  • No new false positives on real code: capacitor unchanged at 10 (0 py-rules — no Python), self-scan unchanged at 67 (0 py-rules). The rules are Python-only and precise.
  • The gate floor moves 60 → 68 to lock the gain in — a regression below the new baseline now fails the job.
  • 141 tests (up from 134). tsc --noEmit clean.

The debugging that shaped this is worth a line: my first attempt keyed the PRNG rule on a credential term in the enclosing def, and it only "worked" on one line — by a fluke, its trailing comment contained "MFA". Tracing that is what surfaced the deliberate definition-line exclusion, and turned a rule that passed by accident into a boundary I can defend.

…e seed)

Raises detection coverage against the testbed from 65.9% to 71.3% (85 to 92 of
129), false-positive rate still 0%. The CWE-327/338 cluster was the largest
recoverable gap: primitives threatcrush had generic rules for, but scoped to a
credential on the matched line, so Python's integrity- and confidentiality-use
cases slipped through.

Three Python rules, each keyed on a signal that survives without trusting a
name:

  - py-broken-cipher: DES/RC2/RC4/Blowfish construction, and AES in ECB mode.
    No safe use, so inherent — flagged wherever it appears. AES matches only on
    ECB, leaving GCM/CTR/CBC alone.
  - py-weak-hash: hashlib.md5/sha1, unless the line carries Python's own
    `usedforsecurity=False` opt-out for a non-security digest.
  - py-predictable-random-seed: random.seed() from the clock or pid, which
    makes the whole sequence reproducible. A fixed integer seed (reproducible
    tests) is left alone.

What is deliberately NOT added: a rule for a `random`-drawn token whose only
signal is the enclosing function name (`generate_session_id`). Guard windows
exclude definition lines on purpose — a name is not evidence, the same reason a
`def sanitize_…` does not count as sanitisation — so there is no line-level
signal to key on. The generic `insecure-randomness-for-secret` still catches
the common `token = …random…` shape. Those name-only cases are the documented
tail, pinned by a test that asserts they stay silent.

The coverage gate floor moves 60 to 68 to lock the gain in; a regression below
the new baseline now fails the job. No new findings on capacitor (10,
unchanged) or the self-scan (67, unchanged) — the rules are Python-only and
precise. 141 tests, up from 134.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

ThreatCrush Security Scan

67 finding(s)

HIGH/CRITICAL: 11 | MEDIUM: 55 | LOW: 1

Severity Rule Location
HIGH secret-aws-access-key prd/0003-detect-hardcoded-secrets-before-they-are-committed-or-served.md:126
HIGH sh-eval-expansion .githooks/pre-commit:26
HIGH sh-remote-script-execution apps/web/public/install.sh:272
HIGH sh-remote-script-execution apps/web/public/install.sh:320
HIGH secret-generic-credential modules/spend-guard/config/example.conf.toml:13
HIGH secret-generic-credential modules/spend-guard/README.md:84
HIGH secret-generic-credential PRD.md:268
HIGH tls-verification-disabled prd/0004-find-dangerous-code-patterns-without-pretending-to-be-a-compiler.md:121
HIGH tls-verification-disabled prd/0004-find-dangerous-code-patterns-without-pretending-to-be-a-compiler.md:122
HIGH sh-remote-script-execution scripts/smoke-test.sh:46
HIGH sh-remote-script-execution scripts/smoke-test.sh:47
MEDIUM insecure-temp-file .githooks/commit-msg:16
MEDIUM insecure-temp-file .githooks/post-commit:20
MEDIUM js-shell-exec-interpolation apps/cli/src/commands/init.ts:70
MEDIUM js-shell-exec-interpolation apps/cli/src/commands/init.ts:79
MEDIUM js-shell-exec-interpolation apps/cli/src/commands/service.ts:88
MEDIUM js-shell-exec-interpolation apps/cli/src/commands/service.ts:111
MEDIUM sql-template-interpolation apps/cli/src/core/state.ts:121
MEDIUM sql-template-interpolation apps/cli/src/core/state.ts:125
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:31
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:33
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:34
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:35
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:36
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:43
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:49
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:56
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:63
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:82
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:84
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:85
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:93
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:98
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:105
MEDIUM js-shell-exec-interpolation apps/cli/src/daemon/firewall/adapters.ts:112
MEDIUM js-shell-exec-interpolation apps/cli/src/index.ts:419
MEDIUM js-unescaped-html-sink apps/web/src/app/about/page.tsx:180
MEDIUM js-unescaped-html-sink apps/web/src/app/about/page.tsx:184
MEDIUM js-open-redirect apps/web/src/app/auth/login/page.tsx:50
MEDIUM js-unescaped-html-sink apps/web/src/app/blog/[slug]/page.tsx:125
MEDIUM js-unescaped-html-sink apps/web/src/app/blog/[slug]/page.tsx:153
MEDIUM js-unescaped-html-sink apps/web/src/app/blog/[slug]/page.tsx:157
MEDIUM js-unescaped-html-sink apps/web/src/app/get-whitepaper/page.tsx:346
MEDIUM js-unescaped-html-sink apps/web/src/app/layout.tsx:211
MEDIUM js-unescaped-html-sink apps/web/src/app/layout.tsx:215
MEDIUM js-unescaped-html-sink apps/web/src/app/layout.tsx:219
MEDIUM js-unescaped-html-sink apps/web/src/app/page.tsx:120
MEDIUM js-unescaped-html-sink apps/web/src/app/store/[slug]/page.tsx:107
MEDIUM js-open-redirect apps/web/src/components/funding/FundingClient.tsx:97
MEDIUM manifest-install-lifecycle-script package.json:24

…and 17 more. Full results in the Security tab.

Snippets are redacted; ThreatCrush never prints matched credential material.

@ralyodio
ralyodio merged commit 1cc1d9c into master Aug 11, 2026
11 checks passed
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