Files
pruefwerk/compliance-agent/rules/cra_semgrep.yaml
T
sharang fbf3a7cf29
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 6s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 6m55s
feat(sast): C/C++ SAST for microcontroller firmware (#334) (#339)
2026-09-23 13:07:50 +00:00

222 lines
9.2 KiB
YAML

# Custom semgrep rules for CRA controls that no off-the-shelf ruleset digs out.
# Each rule id is `cra-ai-<n>-<slug>` and is keyed back to its control via the
# `control-map` LUT (by rule-id suffix, so semgrep's path prefix on check_id does
# not matter). Detection here is deterministic; the grounded LLM judge downstream
# only confirms/refutes — it never detects. Keep patterns tight: a false positive
# that the judge refutes marks the whole finding a false positive.
rules:
# --- cra-ai-1: Secure-by-Default-Konfiguration -------------------------------
- id: cra-ai-1-flask-debug-enabled
languages: [python]
severity: WARNING
message: Flask app started with debug=True — ships an interactive debugger / code execution in production (secure-by-default violation).
metadata:
cwe: ["CWE-489: Active Debug Code"]
control: cra-ai-1
patterns:
- pattern: '$APP.run(..., debug=True, ...)'
- id: cra-ai-1-django-debug-true
languages: [python]
severity: WARNING
message: Django DEBUG = True — leaks stack traces / settings in production (secure-by-default violation).
metadata:
cwe: ["CWE-489: Active Debug Code"]
control: cra-ai-1
patterns:
- pattern: 'DEBUG = True'
- id: cra-ai-1-tls-verify-disabled
languages: [python]
severity: ERROR
message: TLS certificate verification disabled (verify=False) — defeats transport security by default.
metadata:
cwe: ["CWE-295: Improper Certificate Validation"]
control: cra-ai-1
patterns:
- pattern: 'requests.$M(..., verify=False, ...)'
- id: cra-ai-1-cors-wildcard
languages: [javascript, typescript]
severity: WARNING
message: CORS Access-Control-Allow-Origin set to "*" — opens the API to any origin by default.
metadata:
cwe: ["CWE-942: Permissive Cross-domain Policy with Untrusted Domains"]
control: cra-ai-1
patterns:
- pattern-either:
- pattern: '$RES.header("Access-Control-Allow-Origin", "*")'
- pattern: '$RES.setHeader("Access-Control-Allow-Origin", "*")'
# --- cra-ai-7: Starke Authentifizierung (weak password hashing) --------------
- id: cra-ai-7-weak-password-hash
languages: [python]
severity: ERROR
message: Password/secret hashed with a fast, broken digest (md5/sha1) — use a password KDF (bcrypt/scrypt/argon2).
metadata:
cwe: ["CWE-916: Use of Password Hash With Insufficient Computational Effort"]
control: cra-ai-7
patterns:
- pattern-either:
- pattern: 'hashlib.md5($PW)'
- pattern: 'hashlib.sha1($PW)'
- metavariable-regex:
metavariable: $PW
regex: '(?i).*(pass|pwd|secret|cred|token).*'
# --- cra-ai-10: Sitzungsmanagement (insecure session cookies) ----------------
- id: cra-ai-10-session-cookie-insecure
languages: [python]
severity: ERROR
message: Session cookie hardened flag explicitly disabled (Secure/HttpOnly = False) — session token exposed to theft.
metadata:
cwe: ["CWE-614: Sensitive Cookie in HTTPS Session Without 'Secure' Attribute"]
control: cra-ai-10
patterns:
- pattern-either:
- pattern: 'SESSION_COOKIE_SECURE = False'
- pattern: 'SESSION_COOKIE_HTTPONLY = False'
- id: cra-ai-10-express-cookie-insecure
languages: [javascript, typescript]
severity: ERROR
message: Express cookie set with secure/httpOnly = false — session token exposed to interception / XSS theft.
metadata:
cwe: ["CWE-614: Sensitive Cookie in HTTPS Session Without 'Secure' Attribute"]
control: cra-ai-10
patterns:
- pattern-either:
- pattern: '$RES.cookie($NAME, $VAL, {..., secure: false, ...})'
- pattern: '$RES.cookie($NAME, $VAL, {..., httpOnly: false, ...})'
# --- cra-ai-14: Speicher-Schutz / Data at Rest (weak cipher) -----------------
- id: cra-ai-14-python-weak-cipher
languages: [python]
severity: ERROR
message: Data-at-rest encrypted with a broken cipher/mode (ECB, DES, 3DES) — provides no real confidentiality.
metadata:
cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
control: cra-ai-14
patterns:
- pattern-either:
- pattern: 'AES.new($K, AES.MODE_ECB, ...)'
- pattern: 'DES.new(...)'
- pattern: 'DES3.new(...)'
- id: cra-ai-14-node-weak-cipher
languages: [javascript, typescript]
severity: ERROR
message: Data-at-rest encrypted with a broken cipher (DES / deprecated createCipher) — provides no real confidentiality.
metadata:
cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
control: cra-ai-14
patterns:
- pattern-either:
- pattern: 'crypto.createCipheriv("des-ecb", ...)'
- pattern: 'crypto.createCipheriv("des", ...)'
- pattern: 'crypto.createCipher(...)'
# --- C/C++ firmware ------------------------------------------------------------
# Microcontroller code: unbounded string handling, non-literal format strings,
# credentials compiled into every device, broken crypto and predictable
# randomness. Memory-safety defects that need data flow (out-of-bounds
# writes, uninitialised reads) come from cppcheck, not from these patterns.
- id: cra-ai-20-c-unbounded-string-function
languages: [c]
severity: ERROR
message: $FN() writes without a bound on the destination — input longer than the buffer overflows it (CWE-120). Use a bounded call (snprintf, strncpy/strlcpy with the buffer size).
metadata:
cwe: ["CWE-120: Buffer Copy without Checking Size of Input"]
control: cra-ai-20
patterns:
- pattern: $FN(...)
- metavariable-regex:
metavariable: $FN
regex: ^(gets|strcpy|strcat|sprintf|vsprintf|wcscpy|wcscat)$
- id: cra-ai-20-c-scanf-unbounded-string
languages: [c]
severity: ERROR
message: A "%s" conversion without a field width reads unbounded input into a fixed buffer (CWE-120). Give the conversion a width, e.g. "%15s".
metadata:
cwe: ["CWE-120: Buffer Copy without Checking Size of Input"]
control: cra-ai-20
patterns:
- pattern-either:
- pattern: scanf($FMT, ...)
- pattern: sscanf($SRC, $FMT, ...)
- pattern: fscanf($FILE, $FMT, ...)
- metavariable-regex:
metavariable: $FMT
regex: ^".*%s.*"$
- id: cra-ai-20-c-non-literal-format-string
languages: [c]
severity: WARNING
message: The format string is not a literal — text an attacker controls can read or write memory through format specifiers (CWE-134). Print it with a literal format, e.g. printf("%s", text).
metadata:
cwe: ["CWE-134: Use of Externally-Controlled Format String"]
control: cra-ai-20
patterns:
- pattern-either:
- pattern: printf($FMT, ...)
- pattern: vprintf($FMT, ...)
- pattern: fprintf($F, $FMT, ...)
- pattern: sprintf($B, $FMT, ...)
- pattern: snprintf($B, $N, $FMT, ...)
- pattern: syslog($P, $FMT, ...)
- metavariable-regex:
metavariable: $FMT
regex: ^[^"]
- id: cra-ai-8-c-hardcoded-credential
languages: [c]
severity: ERROR
message: A credential ($NAME) is written into the firmware source — every device ships the same secret, and it can be read out of the image (CWE-798). Provision it per device at production or first boot.
metadata:
cwe: ["CWE-798: Use of Hard-coded Credentials"]
control: cra-ai-8
patterns:
- pattern-either:
- pattern: $TYPE $NAME = "$VALUE";
- pattern: $TYPE *$NAME = "$VALUE";
- pattern: $TYPE $NAME[] = "$VALUE";
- pattern: $TYPE $NAME[$N] = "$VALUE";
- metavariable-regex:
metavariable: $NAME
regex: (?i).*(passw|passwd|pwd|secret|api_?key|token|psk|credential).*
- metavariable-regex:
metavariable: $VALUE
regex: .{6,}
- id: cra-ai-13-c-broken-hash-or-cipher
languages: [c]
severity: WARNING
message: $FN uses a broken algorithm (MD5, SHA-1 or DES) that protects neither integrity nor confidentiality (CWE-327). Use SHA-256 or better, and AES-GCM or ChaCha20-Poly1305.
metadata:
cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
control: cra-ai-13
patterns:
- pattern: $FN(...)
- metavariable-regex:
metavariable: $FN
regex: ^(MD5|MD5_Init|MD5_Update|MD5_Final|SHA1|SHA1_Init|SHA1_Update|SHA1_Final|DES_\w+|mbedtls_md5\w*|mbedtls_sha1\w*|mbedtls_des_\w+|wc_Md5\w*|wc_InitSha|wc_ShaUpdate|wc_ShaFinal|wc_Des_\w+)$
- id: cra-ai-16-c-predictable-random
languages: [c]
severity: WARNING
message: rand() is predictable, so $NAME can be guessed (CWE-338). Take keys, nonces and tokens from the MCU's hardware RNG or a DRBG.
metadata:
cwe: ["CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)"]
control: cra-ai-16
patterns:
- pattern-either:
- pattern: $NAME = rand();
- pattern: $NAME = rand() % $M;
- pattern: $NAME = ($T) rand();
- pattern: $TYPE $NAME = rand();
- pattern: $TYPE $NAME = rand() % $M;
- metavariable-regex:
metavariable: $NAME
regex: (?i).*(key|nonce|iv|token|salt|secret|challenge|session).*