rules/semgrep/engineering.yml
the org rules: corrections turned into code · 108 lines · compiled into this Worker from the repository as it is
| 1 | rules: |
| 2 | - id: no-node |
| 3 | message: No Node, npm or wrangler; Workers are Rust, built by Terraform. |
| 4 | severity: ERROR |
| 5 | languages: [generic] |
| 6 | paths: |
| 7 | include: ["package.json", "package-lock.json", "wrangler.toml", "wrangler.json", "wrangler.jsonc"] |
| 8 | # The file being there is the finding: once, at its top. |
| 9 | pattern-regex: "\\A[^\\n]*" |
| 10 | - id: no-hardcoded-domain |
| 11 | message: The domain comes from terraform.tfvars (domain); never hard-code it. |
| 12 | severity: ERROR |
| 13 | languages: [generic] |
| 14 | paths: |
| 15 | exclude: ["rules/**", "terraform.tfvars", "*.tfstate*", "tools/chat-e2e/**"] |
| 16 | pattern-regex: "cloudflareworld\\.com" |
| 17 | - id: no-cargo-fmt |
| 18 | message: Never run cargo fmt or rustfmt that writes files; the code is hand-formatted. |
| 19 | severity: ERROR |
| 20 | languages: [generic] |
| 21 | paths: |
| 22 | include: ["*.sh", "*.md", "*.toml", "*.yml", "*.yaml", "Dockerfile", "*.rs", "*.tf"] |
| 23 | exclude: ["rules/**"] |
| 24 | patterns: |
| 25 | - pattern-regex: "\\bcargo\\s+fmt\\b(?![^\\n]*--check)|\\brustfmt\\b(?![^\\n]*(--check|--emit))" |
| 26 | # The rule itself, said in prose. |
| 27 | - pattern-not-regex: "(?i)\\b(never|don't|do not|no)\\b[^\\n]*(cargo\\s+fmt|rustfmt)" |
| 28 | - id: no-var-overrides-in-apply |
| 29 | message: Never pass -var overrides to terraform apply. |
| 30 | severity: ERROR |
| 31 | languages: [generic] |
| 32 | paths: |
| 33 | include: ["*.sh", "*.tf", "*.md", "*.rs", "Makefile"] |
| 34 | exclude: ["rules/**"] |
| 35 | patterns: |
| 36 | - pattern-regex: "terraform\\s+apply[^\\n]*\\s-var[\\s=]" |
| 37 | - pattern-not-regex: "(?i)\\b(never|don't|do not|no)\\b[^\\n]*terraform\\s+apply" |
| 38 | - id: no-handwritten-javascript |
| 39 | message: Browser code is Rust compiled to wasm; no hand-written JavaScript or inline scripts. |
| 40 | severity: ERROR |
| 41 | languages: [generic] |
| 42 | paths: |
| 43 | include: ["*.rs", "*.html", "*.js"] |
| 44 | exclude: ["rules/**", "**/dist/**", "**/pkg/**", "**/tests.rs", "**/tests/**"] |
| 45 | patterns: |
| 46 | - pattern-either: |
| 47 | # A real inline <script> tag with a body (one tag, on one line): |
| 48 | # not "<script" searched for in code, nor <script src=…>. |
| 49 | - pattern-regex: "<script(?:\\s(?![^>\\n]*\\bsrc=)[^>\\n]*)?>[^<]" |
| 50 | - pattern-regex: "\\son(click|load|change|submit|input)=(\\\\?\")" |
| 51 | # Doc and line comments, and test modules (fixtures of the loader, |
| 52 | # sanitizer tests). |
| 53 | - pattern-not-regex: "(?m)^\\s*//[^\\n]*" |
| 54 | - pattern-not-regex: "(?s)#\\[cfg\\(test\\)\\]\\s*mod tests\\s*\\{.*" |
| 55 | - id: no-javascript-files |
| 56 | message: No JavaScript files at all (the owner, 2026-12-04); only wasm-bindgen's generated loaders in pkg/ and Trunk's dist/. |
| 57 | severity: ERROR |
| 58 | languages: [generic] |
| 59 | paths: |
| 60 | include: ["*.js", "*.mjs", "*.cjs", "*.ts"] |
| 61 | exclude: ["rules/**", "**/dist/**", "**/pkg/**", "**/node_modules/**", "**/target/**"] |
| 62 | pattern-regex: "(?s)\\A.+" |
| 63 | - id: no-script-in-the-browser |
| 64 | message: Never hand a browser JavaScript to run (CDP Runtime.evaluate, evaluate_on_new_document, js_sys::eval, Function constructors); browser work is Rust → wasm (chat-e2e's probe). |
| 65 | severity: ERROR |
| 66 | languages: [generic] |
| 67 | paths: |
| 68 | include: ["*.rs"] |
| 69 | exclude: ["rules/**"] |
| 70 | patterns: |
| 71 | - pattern-either: |
| 72 | - pattern-regex: "\\b(EvaluateParams|evaluate_on_new_document|evaluate_expression|evaluate_function|CallFunctionOnParams)\\b" |
| 73 | - pattern-regex: "js_sys::eval\\b|Function::new_(no_args|with_args)\\b" |
| 74 | - pattern-not-regex: "(?m)^\\s*//[^\\n]*" |
| 75 | - id: r2-writes-through-chunker |
| 76 | message: 'R2 writes go through platform-storage''s chunker (r2::put_chunk and put_manifest, sealed and chunked). A deliberate exception carries `// nosemgrep: r2-writes-through-chunker` with why.' |
| 77 | severity: ERROR |
| 78 | languages: [generic] |
| 79 | paths: |
| 80 | include: ["*.rs"] |
| 81 | exclude: ["rules/**", "crates/platform-storage/**"] |
| 82 | patterns: |
| 83 | - pattern-either: |
| 84 | # An R2 put is a builder that ends in .execute() (Durable Object |
| 85 | # storage's put has none; KV's is `put(..)?.execute()`). |
| 86 | - pattern-regex: "\\.put\\([^;]*?\\)(\\s*\\.[a-z_]+\\([^;]*?\\))*\\s*\\.execute\\(\\)" |
| 87 | - pattern-regex: "\\.create_multipart_upload\\(" |
| 88 | - pattern-not-regex: "(?m)^\\s*//[^\\n]*" |
| 89 | - id: raw-logs-only-where-anonymized |
| 90 | message: 'Raw Logpush files are read only by the log processor (counts), the admin''s Logs tab and the log download (admin-site src/export.rs, through platform-anonymize). Anything else that needs log data goes through the download or the rollups.' |
| 91 | severity: ERROR |
| 92 | languages: [generic] |
| 93 | paths: |
| 94 | include: ["*.rs"] |
| 95 | exclude: ["rules/**", "crates/platform-metrics/**", "workers/internal/log-processor/**", "workers/admin/admin-site/src/dash.rs", "workers/admin/admin-site/src/export.rs"] |
| 96 | patterns: |
| 97 | - pattern-regex: "\\b(parse_file|decode_raw)\\s*\\(|bucket\\(\"LOGS\"\\)" |
| 98 | - pattern-not-regex: "(?m)^\\s*//[^\\n]*" |
| 99 | - id: logs-download-through-export |
| 100 | message: 'The admin site hands out files only through the log download (src/export.rs), which anonymizes every line with platform-anonymize.' |
| 101 | severity: ERROR |
| 102 | languages: [generic] |
| 103 | paths: |
| 104 | include: ["workers/admin/**/*.rs"] |
| 105 | exclude: ["workers/admin/admin-site/src/export.rs"] |
| 106 | patterns: |
| 107 | - pattern-regex: "Content-Disposition" |
| 108 | - pattern-not-regex: "(?m)^\\s*//[^\\n]*" |