# Prüfwerk's own semgrep rules, the only ones it runs: the Semgrep registry rules # are licensed for internal use only, not for a service offered to others. # Each rule id is `cra-ai--` 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(...)' # --- Python and JavaScript/TypeScript: injection, credentials, transport ------- # A constant string never matches; the reflected-input rule follows Express # request input (`req.query`, `req.params`, `req.body`) into the response. # Annotated cases: cra_semgrep.py / cra_semgrep.js, run with # `semgrep --test compliance-agent/rules`. - id: cra-ai-20-python-shell-command languages: [python] severity: ERROR message: A shell command is built from a variable, so whoever controls it can run any command (CWE-78). Pass an argument list without shell=True, or validate the value against an allowlist. metadata: cwe: ["CWE-78: Improper Neutralization of Special Elements used in an OS Command"] control: cra-ai-20 patterns: - pattern-either: - pattern: os.system($CMD) - pattern: os.popen($CMD, ...) - pattern: subprocess.$F($CMD, ..., shell=True, ...) - metavariable-pattern: metavariable: $CMD patterns: - pattern-not: '"..."' - id: cra-ai-20-python-sql-string languages: [python] severity: ERROR message: An SQL statement is assembled from a variable, so its value can change the query (SQL injection, CWE-89). Pass values as query parameters (cursor.execute(sql, params)). metadata: cwe: ["CWE-89: Improper Neutralization of Special Elements used in an SQL Command"] control: cra-ai-20 patterns: - pattern-either: - pattern: $CUR.execute("..." + $X, ...) - pattern: $CUR.execute("..." % $X, ...) - pattern: $CUR.execute(f"...", ...) - pattern: $CUR.execute("...".format(...), ...) - pattern: $CUR.executemany("..." + $X, ...) - pattern-not: $CUR.execute(f"{$C}", ...) - id: cra-ai-20-python-unsafe-deserialization languages: [python] severity: ERROR message: Data is deserialised with a loader that can build arbitrary objects, so crafted input runs code (CWE-502). Use yaml.safe_load, or JSON for data from outside. metadata: cwe: ["CWE-502: Deserialization of Untrusted Data"] control: cra-ai-20 pattern-either: - pattern: pickle.loads(...) - pattern: pickle.load(...) - pattern: cPickle.loads(...) - pattern: marshal.loads(...) - patterns: - pattern: yaml.load(...) - pattern-not: yaml.load(..., Loader=yaml.SafeLoader, ...) - pattern-not: yaml.load(..., Loader=yaml.CSafeLoader, ...) - pattern-not: yaml.load(..., Loader=SafeLoader, ...) - pattern: yaml.unsafe_load(...) - id: cra-ai-20-node-shell-command languages: [javascript, typescript] severity: ERROR message: A shell command is built from a variable, so whoever controls it can run any command (CWE-78). Use execFile/spawn with an argument list, or validate the value against an allowlist. metadata: cwe: ["CWE-78: Improper Neutralization of Special Elements used in an OS Command"] control: cra-ai-20 patterns: - pattern-either: - pattern: exec($CMD, ...) - pattern: execSync($CMD, ...) - pattern: $CP.exec($CMD, ...) - pattern: $CP.execSync($CMD, ...) - pattern-not: exec("...", ...) - pattern-not: execSync("...", ...) - pattern-not: $CP.exec("...", ...) - pattern-not: $CP.execSync("...", ...) - pattern-not: $CP.exec(`...`, ...) - pattern-inside: | $REQ = require("child_process") ... - pattern-not: $RE.exec($X) - id: cra-ai-20-node-reflected-input languages: [javascript, typescript] severity: WARNING message: Request input is written into the response without encoding, so a crafted link runs script in the user's browser (reflected XSS, CWE-79). Encode it, or use a template engine that escapes by default. metadata: cwe: ["CWE-79: Improper Neutralization of Input During Web Page Generation"] control: cra-ai-20 mode: taint pattern-sources: - pattern-either: - pattern: $REQ.query - pattern: $REQ.params - pattern: $REQ.body pattern-sinks: - pattern-either: - pattern: $RES.send(...) - pattern: $RES.write(...) pattern-sanitizers: - pattern-either: - pattern: encodeURIComponent(...) - pattern: escape(...) - pattern: escapeHtml(...) - pattern: parseInt(...) - pattern: Number(...) - id: cra-ai-8-python-hardcoded-credential languages: [python] severity: ERROR message: $NAME holds a credential written into the code, so every installation ships the same secret (CWE-798). Read it from the environment or a secret store. metadata: cwe: ["CWE-798: Use of Hard-coded Credentials"] control: cra-ai-8 patterns: - pattern-either: - pattern: $NAME = "$VALUE" - pattern: $OBJ.$NAME = "$VALUE" - metavariable-regex: metavariable: $NAME regex: (?i)^[a-z_]*(password|passwd|pwd|secret|api_?key|access_?key|token|credential)s?$ - metavariable-regex: metavariable: $VALUE regex: ^(?!(?i:changeme|change-me|xxx+|\*+|todo|example|placeholder|dummy|test)$).{4,}$ - id: cra-ai-15-python-cleartext-http languages: [python] severity: WARNING message: A request goes over plain http://, so it can be read and altered in transit (CWE-319). Use https:// with certificate checking. metadata: cwe: ["CWE-319: Cleartext Transmission of Sensitive Information"] control: cra-ai-15 patterns: - pattern-either: - pattern: requests.$M("$URL", ...) - pattern: requests.$M("$URL" + $X, ...) - pattern: urllib.request.urlopen("$URL", ...) - pattern: httpx.$M("$URL", ...) - pattern: httpx.$M("$URL" + $X, ...) - metavariable-regex: metavariable: $URL regex: ^http://(?!(localhost|127\.|0\.0\.0\.0|\[::1\])) - id: cra-ai-15-node-cleartext-http languages: [javascript, typescript] severity: WARNING message: A request goes over plain http://, so it can be read and altered in transit (CWE-319). Use https:// with certificate checking. metadata: cwe: ["CWE-319: Cleartext Transmission of Sensitive Information"] control: cra-ai-15 patterns: - pattern-either: - pattern: fetch("$URL", ...) - pattern: fetch("$URL" + $X, ...) - pattern: axios.$M("$URL", ...) - pattern: axios.$M("$URL" + $X, ...) - pattern: http.get("$URL", ...) - pattern: http.request("$URL", ...) - metavariable-regex: metavariable: $URL regex: ^http://(?!(localhost|127\.|0\.0\.0\.0|\[::1\])) # --- C/C++ firmware ------------------------------------------------------------ # C and C++ sources alike (`languages: [c, cpp]`), with the `std::` spellings. # 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, cpp] 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: ^(std::)?(gets|strcpy|strcat|sprintf|vsprintf|wcscpy|wcscat)$ - id: cra-ai-20-c-scanf-unbounded-string languages: [c, cpp] 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, cpp] 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, ...) # C++ reaches the same functions through std::. - pattern: std::printf($FMT, ...) - pattern: std::fprintf($F, $FMT, ...) - pattern: std::sprintf($B, $FMT, ...) - pattern: std::snprintf($B, $N, $FMT, ...) # Not a literal, and not an upper-case macro (`#define GET_REQUEST # "GET / HTTP/1.1"`): a macro like that expands to a literal. - metavariable-regex: metavariable: $FMT regex: ^(?![A-Z][A-Z0-9_]*$)[^"] - id: cra-ai-8-c-hardcoded-credential languages: [c, cpp] 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"; # The usual embedded style: a macro in a config header # (`#define WIFI_PASS "..."`), which semgrep's C patterns do not see. - pattern-regex: '(?m)^[ \t]*#[ \t]*define[ \t]+(?P\w+)[ \t]+"(?P[^"]*)"' - metavariable-regex: metavariable: $NAME regex: (?i).*((^|_)pass($|_)|passw|pwd|secret|api_?key|token|psk|credential).* # Six characters or more, and not a placeholder left for the installer. - metavariable-regex: metavariable: $VALUE regex: ^(?!(?i:changeme|change_me|password|secret|admin|x{3,}|your_\w*|<[^>]*>|\*+)$).{6,}$ - id: cra-ai-13-c-broken-hash-or-cipher languages: [c, cpp] 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, cpp] 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).*