cora-code 0.17.2

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Table-driven, end-to-end tests for the hardcoded-secret heuristics (#610).
//!
//! Every row is one added line in a one-line diff, run through the real review
//! path: all deterministic scanners (`deterministic::run`: rule engine, secrets
//! scanner, security scanner), merged into issues, then `postprocess` (ignore
//! rules and inline `cora-ignore:` markers). A row says whether a hardcoded
//! secret finding survives. A failure prints every mismatching row.
//!
//! Rows that expose a real false positive/negative which is not fixed yet live
//! in [`KNOWN_GAPS`], asserting the CURRENT behaviour so a fix flips the test
//! and forces the row to move into [`CASES`].

use crate::config::schema::Config;
use crate::engine::deterministic;
use crate::engine::diff_parser::parse_diff;
use crate::engine::index_bridge::IndexBridge;
use crate::engine::postprocess::{Source, postprocess};

/// (file extension, added line, expect a hardcoded-secret finding)
type Row = (&'static str, &'static str, bool);

/// Is a hardcoded-secret finding reported for `line` in a `.{ext}` file?
fn flagged(ext: &str, line: &str) -> bool {
    let diff = format!(
        "diff --git a/src/app.{ext} b/src/app.{ext}\n--- a/src/app.{ext}\n+++ b/src/app.{ext}\n@@ -0,0 +1 @@\n+{line}\n"
    );
    let chunks = parse_diff(&diff);
    let report = deterministic::run(&chunks, &Config::default(), &IndexBridge::unavailable());
    let issues = report.merge_into(Vec::new());
    postprocess(issues, &Source::Diff(&chunks), &Config::default())
        .iter()
        .any(|i| {
            let id = i.rule_id.as_deref().unwrap_or("");
            id.contains("hardcoded-secret") || id.starts_with("secrets/")
        })
}

fn mismatches(rows: &[Row]) -> Vec<String> {
    rows.iter()
        .filter(|(ext, line, expect)| flagged(ext, line) != *expect)
        .map(|(ext, line, expect)| {
            format!(
                "  ({ext:?}, {line:?}): expected flagged={expect}, got {}",
                !expect
            )
        })
        .collect()
}

const CASES: &[Row] = &[
    // ── js / ts: quotes ──
    ("js", r#"const password = "hunter2hunter2";"#, true),
    ("js", "const apiKey = 'abcd1234efgh5678';", true),
    ("js", "const token = `abcd1234efgh5678`;", true),
    ("ts", r#"const password: string = "hunter2hunter2";"#, true),
    (
        "ts",
        r#"  readonly secret: string = "hunter2hunter2";"#,
        true,
    ),
    ("ts", "  secret: string;", false),
    // ── js / ts: trailing comments, with and without a colon ──
    (
        "js",
        r#"const password = "hunter2hunter2"; // rotate later"#,
        true,
    ),
    (
        "js",
        r#"const password = "hunter2hunter2"; // note: rotate later"#,
        true,
    ),
    (
        "ts",
        r#"const password: string = "hunter2hunter2"; // TODO: move to vault"#,
        true,
    ),
    // ── cora-ignore markers ──
    (
        "js",
        r#"const password = "hunter2hunter2"; // cora-ignore: crypto/hardcoded-secret"#,
        false,
    ),
    (
        "js",
        r#"const password = "hunter2hunter2"; // cora-ignore: sec-hardcoded-secret"#,
        false,
    ),
    (
        "py",
        r#"password = "hunter2hunter2"  # cora-ignore: crypto/hardcoded-secret, sec-hardcoded-secret"#,
        false,
    ),
    // a bare marker (no rule list) never suppresses
    (
        "js",
        r#"const password = "hunter2hunter2"; // cora-ignore"#,
        true,
    ),
    // ── js / ts: object shorthand and variable references ──
    ("ts", "  { app_secret: formAppSecret }", false),
    ("ts", "const body = { password: formPassword };", false),
    // ── js / ts: empty strings and UI bindings ──
    ("js", r#"let password = "";"#, false),
    ("js", "let password = '';", false),
    ("ts", r#"let password = $state('');"#, false),
    (
        "svelte",
        "<input type=\"password\" bind:value={password} />",
        false,
    ),
    ("ts", "let secret = $state(initialSecretValue);", false),
    // ── placeholder / real-looking values ──
    ("js", r#"const password = "changeme-please-123";"#, true),
    // ── python ──
    ("py", r#"password = "hunter2hunter2""#, true),
    ("py", "api_key = 'abcd1234efgh5678'", true),
    ("py", r#"password: str = "hunter2hunter2""#, true),
    ("py", r#"password = "hunter2hunter2"  # note: temp"#, true),
    ("py", r#"password = """#, false),
    ("py", "password = ''", false),
    // ── rust ──
    ("rs", r#"let password = "hunter2hunter2";"#, true),
    ("rs", r#"let password: &str = "hunter2hunter2";"#, true),
    ("rs", r#"const API_KEY: &str = "abcd1234efgh5678";"#, true),
    (
        "rs",
        r#"let password = "hunter2hunter2"; // note: test only"#,
        true,
    ),
    (
        "rs",
        r#"let password = "hunter2hunter2"; // cora-ignore: crypto/hardcoded-secret"#,
        false,
    ),
    ("rs", r#"let password = "";"#, false),
    ("rs", "    pub password: String,", false),
    ("rs", "    api_key: extract_api_key.clone(),", false),
    // ── go ──
    ("go", r#"var password = "hunter2hunter2""#, true),
    ("go", "const token = `abcd1234efgh5678`", true),
    ("go", r#"password := os.Getenv("DB_PASSWORD")"#, false),
    ("go", r#"password := """#, false),
    // #618: short declarations and colon-free types
    ("go", r#"password := "hunter2hunter2""#, true),
    ("go", r#"var apiKey string = "abcd1234efgh5678""#, true),
    ("go", r#"password := "hunter2hunter2" // note: temp"#, true),
    (
        "go",
        r#"password := "hunter2hunter2" // cora-ignore: crypto/hardcoded-secret"#,
        false,
    ),
    ("go", r#"var apiKey string = """#, false),
    ("go", r#"var apiKey string = os.Getenv("API_KEY")"#, false),
    // ── java / kotlin ──
    (
        "java",
        r#"private static final String PASSWORD = "hunter2hunter2";"#,
        true,
    ),
    ("java", r#"String apiKey = "abcd1234efgh5678";"#, true),
    ("java", r#"String password = "";"#, false),
    (
        "java",
        r#"String password = "hunter2hunter2"; // cora-ignore: crypto/hardcoded-secret"#,
        false,
    ),
    ("kt", r#"val password = "hunter2hunter2""#, true),
    ("kt", r#"val apiKey: String = "abcd1234efgh5678""#, true),
    ("kt", r#"val password = """#, false),
    // ── yaml / json ──
    ("yaml", r#"password: "hunter2hunter2""#, true),
    ("yaml", "api_key: 'abcd1234efgh5678'", true),
    ("yaml", "password: ${DB_PASSWORD}", false),
    ("yaml", "password: \"\"", false),
    // #619: unquoted YAML scalars
    ("yaml", "password: hunter2hunter2", true),
    ("yaml", "  db_password: hunter2hunter2", true),
    ("yaml", "- token: abcd1234efgh5678", true),
    ("yaml", "password: hunter2hunter2 # prod db", true),
    ("yaml", "password: !secret db", false),
    ("yaml", "password: !vault db_password_1", false),
    ("yaml", "password: null", false),
    ("yaml", "password: ~", false),
    ("yaml", "password: required", false),
    ("yaml", "password: *dbpass1", false),
    ("yaml", "password: &dbpass1 hunter2", false),
    ("yaml", "password: $DB_PASSWORD_1", false),
    ("yaml", "password: \"${DB_PASSWORD}\"", false),
    ("yaml", "password_file: /run/secrets/db_password", false),
    ("yaml", "password: /run/secrets/db_password", false),
    ("yaml", "secretName: db-credentials-secret", false),
    ("ts", "  secret: Secret1Type", true),
    // #619: quoted JSON keys
    ("json", r#"  "password": "hunter2hunter2","#, true),
    ("json", r#"  "apiKey": "abcd1234efgh5678""#, true),
    ("json", r#"  "password": "hunter2hunter2" "#, true),
    ("json", r#"  "apiKey": "${API_KEY}""#, false),
    ("json", r#"  "password": "$DB_PASSWORD","#, false),
    ("json", r#"  "password": "{{ db_password }}","#, false),
    ("json", r#"  "password": null,"#, false),
    ("json", r#"  "password": "","#, false),
    ("json", r#"  "password": "${DB_PASSWORD}","#, false),
    // inline JSON: placeholders stay quiet, a real literal beside one is still flagged
    ("json", r#"{"password": "${X}"}"#, false),
    ("json", r#"{"password": "${X}", "apiKey": ""}"#, false),
    ("json", r#"{"password": "", "token": "${T}"}"#, false),
    (
        "json",
        r#"{"password": "${X}", "token": "hunter2hunter2xx"}"#,
        true,
    ),
    ("json", r#"{"password": "hunter2hunter2xx"}"#, true),
    // ── env / .properties ──
    ("env", "DB_PASSWORD=hunter2hunter2", true),
    ("env", "API_KEY=abcd1234efgh5678", true),
    ("env", "DB_PASSWORD=", false),
    ("properties", "db.password=hunter2hunter2", true),
    ("properties", "db.password=", false),
    // ── shell ──
    ("sh", r#"export DB_PASSWORD="hunter2hunter2""#, true),
    ("sh", "API_KEY='abcd1234efgh5678'", true),
    ("sh", r#"password="""#, false),
    (
        "sh",
        r#"export DB_PASSWORD="hunter2hunter2" # note: dev"#,
        true,
    ),
    // ── sql ──
    (
        "sql",
        "ALTER ROLE app SET password = 'hunter2hunter2';",
        true,
    ),
    ("sql", "ALTER ROLE app SET password = '';", false),
    // #620: PASSWORD '<literal>' without `=`
    (
        "sql",
        "CREATE USER app WITH PASSWORD 'hunter2hunter2';",
        true,
    ),
    ("sql", "ALTER USER app PASSWORD 'hunter2hunter2';", true),
    (
        "sql",
        "CREATE USER app IDENTIFIED BY 'hunter2hunter2';",
        true,
    ),
    (
        "sql",
        "CREATE USER app WITH PASSWORD 'hunter2hunter2'; -- note: temp",
        true,
    ),
    ("sql", "CREATE USER app WITH PASSWORD '';", false),
    ("sql", "CREATE USER app WITH PASSWORD '%s';", false),
    ("sql", "CREATE USER app WITH PASSWORD '$1';", false),
    ("sql", "CREATE USER app WITH PASSWORD ':pw';", false),
    ("sql", "CREATE USER app WITH PASSWORD '{password}';", false),
    ("sql", "CREATE USER app IDENTIFIED BY '?';", false),
    ("sql", "CREATE USER app WITH PASSWORD NULL;", false),
    ("go", r#"q := "CREATE USER app WITH PASSWORD '%s'""#, false),
    (
        "py",
        r#"q = f"CREATE USER app WITH PASSWORD '{pw}'""#,
        false,
    ),
    // ── #616: env reads / call / member expressions / identifiers are not literals ──
    ("js", "const token = process.env.API_TOKEN;", false),
    (
        "js",
        "const secret = process.env.SESSION_SECRET || fallbackValue;",
        false,
    ),
    ("py", r#"password = os.environ["DB_PASSWORD"]"#, false),
    ("py", r#"password = os.getenv("DB_PASSWORD")"#, false),
    ("py", "password = get_password_from_vault()", false),
    ("rs", r#"let token = std::env::var("API_TOKEN")?;"#, false),
    (
        "rs",
        r#"let token = std::env::var("API_TOKEN").unwrap_or_default();"#,
        false,
    ),
    ("rs", r#"let password = String::new();"#, false),
    (
        "java",
        r#"String password = System.getenv("DB_PASSWORD");"#,
        false,
    ),
    (
        "kt",
        r#"val password = System.getenv("DB_PASSWORD")"#,
        false,
    ),
    ("js", "const password = userInput;", false),
    ("rs", "let token = cfg.token_value_here;", false),
    ("ts", "const apiKey: string = config.apiKeyValue;", false),
    ("rb", r#"password = ENV["DB_PASSWORD"]"#, false),
    ("php", r#"$password = $_ENV['DB_PASSWORD'];"#, false),
    (
        "cs",
        "var token = Environment.GetEnvironmentVariable(name);",
        false,
    ),
    // a quoted or concatenated literal in code is still a secret
    ("js", r#"password = "abc" + "defgh12345";"#, true),
    ("py", r#"password = b"hunter2hunter2xx""#, true),
    ("py", r#"password = f"hunter2hunter2xx""#, true),
    ("rs", r##"let password = r#"hunter2hunter2xx"#;"##, true),
    (
        "rs",
        r#"let password = String::from("hunter2hunter2xx");"#,
        true,
    ),
    (
        "rs",
        r#"let password = "hunter2hunter2xx".to_string();"#,
        true,
    ),
    // ── #617: shell-style interpolation is a reference, a real value is not ──
    ("env", "DB_PASSWORD=${DB_PASSWORD_FROM_VAULT}", false),
    ("properties", "db.password=${DB_PASSWORD}", false),
    ("sh", r#"export DB_PASSWORD="$VAULT_DB_PASSWORD""#, false),
    ("env", "DB_PASSWORD=$VAULT_DB_PASSWORD", false),
    (
        "sh",
        "export API_KEY=$(vault read -field=key secret/api)",
        false,
    ),
    (
        "sh",
        r#"export API_KEY="$(vault read -field=key secret/api)""#,
        false,
    ),
    (
        "sh",
        r#"export DB_PASSWORD="${DB_PASSWORD_FROM_VAULT}""#,
        false,
    ),
    ("ini", "password=${DB_PASSWORD}", false),
    ("yaml", "      - DB_PASSWORD=${DB_PASSWORD}", false),
    // real values in assignment-style files stay flagged
    ("env", "DB_PASSWORD=hunter2hunter2xx", true),
    ("sh", "export API_KEY=abcd1234efgh5678", true),
    ("properties", "password=hunter2hunter2xx", true),
    ("sh", r#"export X_SECRET="hunter2hunter2xx""#, true),
    ("env", "DB_PASSWORD=hunter2hunter2xx # prod", true),
    ("toml", "api_key = abcd1234efgh5678", true),
    ("ini", "password = hunter2hunter2xx", true),
    ("yaml", "      - DB_PASSWORD=hunter2hunter2xx", true),
    // a literal mixed into an interpolation is not a pure reference
    (
        "sh",
        r#"export DB_PASSWORD="${PREFIX}hunter2hunter2xx""#,
        true,
    ),
];

/// Rows exposing a real false negative/positive that is not fixed yet. Each
/// asserts the CURRENT behaviour (`expect` = what the scanner does today, with
/// the ideal result in the trailing comment). Move a row into [`CASES`] with
/// the ideal expectation when its issue is fixed.
const KNOWN_GAPS: &[Row] = &[
    // KNOWN GAP #628 - FN: an unquoted RHS in source code is treated as an expression, so a real literal default inside a call is missed (ideal: flagged)
    (
        "py",
        r#"password = os.environ.get("DB_PASSWORD", "hunter2hunter2xx")"#,
        false,
    ),
    (
        "rs",
        r#"let password = std::env::var("DB_PASSWORD").unwrap_or("hunter2hunter2xx".into());"#,
        false,
    ),
    (
        "js",
        r#"const password = process.env.DB_PASSWORD || "hunter2hunter2xx";"#,
        false,
    ),
    // KNOWN GAP #630 - FN: the scanner regex needs a >= 8 char token after `=`, so `new String("lit")` and other short-prefixed constructors are never matched (ideal: flagged)
    (
        "java",
        r#"String password = new String("hunter2hunter2xx");"#,
        false,
    ),
    // KNOWN GAP #629 - FN: shell `${VAR:-literal}` default with a real literal is treated as interpolation (ideal: flagged)
    ("sh", "DB_PASSWORD=${DB_PASSWORD:-hunter2hunter2xx}", false),
];

#[test]
fn hardcoded_secret_table() {
    let bad = mismatches(CASES);
    assert!(
        bad.is_empty(),
        "{} of {} rows mismatched:\n{}",
        bad.len(),
        CASES.len(),
        bad.join("\n")
    );
}

#[test]
fn hardcoded_secret_known_gaps_keep_current_behaviour() {
    let bad = mismatches(KNOWN_GAPS);
    assert!(
        bad.is_empty(),
        "a KNOWN_GAPS row changed behaviour (fixed? move it into CASES):\n{}",
        bad.join("\n")
    );
}