keyhog-scanner 0.5.50

keyhog-scanner: high-performance SIMD-accelerated secret detection engine
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use super::ExtractedPair;

/// Parse Terraform / HCL `variable "<name>" { default = "<value>" }`
/// blocks, flat `.tfvars` assignments, and simple `locals { x = "v" }`
/// assignment shapes into `(context, value)` pairs.
pub(crate) fn parse_hcl(text: &str) -> Vec<ExtractedPair> {
    let mut pairs = Vec::new();
    let lines: Vec<&str> = text.lines().collect();
    let mut index = 0;
    let mut in_block_comment = false;
    while index < lines.len() {
        let line = lines[index];
        let code_line = strip_hcl_comments(line, &mut in_block_comment);
        let trimmed = code_line.trim_start();
        if let Some(var_name) = parse_variable_header(trimmed) {
            let (header_opens, header_closes) = brace_delta_outside_strings_and_comments(trimmed);
            let mut depth = header_opens.saturating_sub(header_closes);
            let mut consumed = 1usize;
            let mut default_composite_depth: Option<usize> = None;
            if let Some(HclValue::Scalar(value)) = parse_hcl_default_in_fragment(trimmed) {
                if !value.is_empty() {
                    pairs.push(ExtractedPair {
                        context: var_name.clone(),
                        value,
                        line: index + 1,
                        transport_decoded: false,
                    });
                }
            }
            if depth == 0 {
                index += consumed;
                continue;
            }
            let mut offset = 1usize;
            let mut structural_lines = 0usize;
            while structural_lines < MAX_VARIABLE_BLOCK_LINES && index + offset < lines.len() {
                let line_index = index + offset;
                let inner = lines[line_index];
                let inner_code = strip_hcl_comments(inner, &mut in_block_comment);
                let body = inner_code.trim();
                consumed = offset + 1;

                if let Some(value) = parse_hcl_default(body) {
                    match value {
                        HclValue::Scalar(value) => {
                            if !value.is_empty() {
                                pairs.push(ExtractedPair {
                                    context: var_name.clone(),
                                    value,
                                    line: line_index + 1,
                                    transport_decoded: false,
                                });
                            }
                        }
                        HclValue::HeredocStart(marker) => {
                            if let Some((value, next_index)) =
                                collect_heredoc(&lines, line_index + 1, &marker)
                            {
                                if !value.is_empty() {
                                    pairs.push(ExtractedPair {
                                        context: var_name.clone(),
                                        value,
                                        line: line_index + 2,
                                        transport_decoded: false,
                                    });
                                }
                                consumed = next_index.saturating_sub(index);
                                offset = next_index.saturating_sub(index);
                                continue;
                            }
                        }
                        HclValue::CompositeStart => {
                            default_composite_depth = Some(0);
                        }
                    }
                } else if default_composite_depth.is_some() {
                    if let Some((name, value)) = parse_hcl_assignment(body) {
                        match value {
                            HclValue::Scalar(value) => {
                                if !name.is_empty() && !value.is_empty() {
                                    pairs.push(ExtractedPair {
                                        context: format!("{var_name}.{name}"),
                                        value,
                                        line: line_index + 1,
                                        transport_decoded: false,
                                    });
                                }
                            }
                            HclValue::HeredocStart(marker) => {
                                if let Some((value, next_index)) =
                                    collect_heredoc(&lines, line_index + 1, &marker)
                                {
                                    if !name.is_empty() && !value.is_empty() {
                                        pairs.push(ExtractedPair {
                                            context: format!("{var_name}.{name}"),
                                            value,
                                            line: line_index + 2,
                                            transport_decoded: false,
                                        });
                                    }
                                    consumed = next_index.saturating_sub(index);
                                    offset = next_index.saturating_sub(index);
                                    continue;
                                }
                            }
                            HclValue::CompositeStart => {}
                        }
                    }
                }

                let (opens, closes) = brace_delta_outside_strings_and_comments(body);
                if let Some(composite_depth) = default_composite_depth.as_mut() {
                    *composite_depth = composite_depth.saturating_add(opens);
                    *composite_depth = composite_depth.saturating_sub(closes);
                    if *composite_depth == 0 {
                        default_composite_depth = None;
                    }
                }
                depth = depth.saturating_add(opens);
                depth = depth.saturating_sub(closes);
                if depth == 0 {
                    break;
                }
                structural_lines += 1;
                offset += 1;
            }
            index += consumed;
            continue;
        }
        if let Some((name, value)) = parse_hcl_assignment(trimmed) {
            match value {
                HclValue::Scalar(value) => {
                    if !name.is_empty() && !value.is_empty() {
                        pairs.push(ExtractedPair {
                            context: name,
                            value,
                            line: index + 1,
                            transport_decoded: false,
                        });
                    }
                }
                HclValue::HeredocStart(marker) => {
                    if let Some((value, next_index)) = collect_heredoc(&lines, index + 1, &marker) {
                        if !name.is_empty() && !value.is_empty() {
                            pairs.push(ExtractedPair {
                                context: name,
                                value,
                                line: index + 2,
                                transport_decoded: false,
                            });
                        }
                        index = next_index;
                        continue;
                    }
                }
                HclValue::CompositeStart => {}
            }
        }
        index += 1;
    }
    pairs
}

/// Real terraform blocks are short; cap the lookahead so malformed files do not
/// run into the next block indefinitely.
const MAX_VARIABLE_BLOCK_LINES: usize = 16;
const MAX_HEREDOC_LINES: usize = 512;

enum HclValue {
    Scalar(String),
    HeredocStart(String),
    CompositeStart,
}

fn collect_heredoc(lines: &[&str], content_start: usize, marker: &str) -> Option<(String, usize)> {
    let mut value = String::new();
    let end = lines.len().min(content_start + MAX_HEREDOC_LINES);
    for cursor in content_start..end {
        if lines[cursor].trim() == marker {
            return Some((value, cursor + 1));
        }
        if !value.is_empty() {
            value.push('\n');
        }
        value.push_str(lines[cursor]);
    }
    None
}

/// True when `s` is a non-empty HCL identifier: every character is ASCII
/// alphanumeric, `_`, or `-`. One owner for the char-class shared by variable
/// names, assignment LHS keys, and heredoc markers, so the three call sites
/// can never drift on what counts as a valid identifier.
fn is_hcl_identifier(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

fn parse_variable_header(line: &str) -> Option<String> {
    let rest = line.strip_prefix("variable")?;
    if !rest.starts_with(|c: char| c.is_ascii_whitespace()) {
        return None;
    }
    let rest = rest.trim_start();
    let name = if let Some(quoted) = rest.strip_prefix('"') {
        let end = find_unescaped_quote(quoted, b'"')?;
        &quoted[..end]
    } else {
        rest.split(|c: char| c.is_ascii_whitespace() || c == '{')
            .next()?
    };
    if !is_hcl_identifier(name) {
        return None;
    }
    Some(name.to_string())
}

fn parse_hcl_default(line: &str) -> Option<HclValue> {
    let stripped = strip_hcl_line_comment(line);
    let trimmed = stripped.trim_start();
    let rest = trimmed.strip_prefix("default")?;
    let rest = rest.trim_start();
    let rest = rest.strip_prefix('=')?.trim_start();
    parse_hcl_value(rest)
}

fn parse_hcl_default_in_fragment(fragment: &str) -> Option<HclValue> {
    let mut search_start = 0usize;
    while search_start < fragment.len() {
        let pos = search_start + fragment[search_start..].find("default")?;
        let before_ok = pos == 0
            || fragment[..pos]
                .chars()
                .last()
                .is_none_or(|c| !c.is_ascii_alphanumeric() && c != '_' && c != '-');
        let after = &fragment[pos + "default".len()..];
        let after_ok = after
            .chars()
            .next()
            .is_some_and(|c| !c.is_ascii_alphanumeric() && c != '_' && c != '-');
        if before_ok && after_ok {
            if let Some(value) = parse_hcl_default(&fragment[pos..]) {
                return Some(value);
            }
        }
        search_start = pos + "default".len();
    }
    None
}

fn parse_hcl_assignment(line: &str) -> Option<(String, HclValue)> {
    let stripped = strip_hcl_line_comment(line);
    let line = stripped.trim();
    if line.starts_with('#') || line.starts_with("//") || !line.contains('=') {
        return None;
    }
    if let Some((name_part, value_part)) = line.split_once('=') {
        let name = name_part.trim();
        if !is_hcl_identifier(name) {
            return None;
        }
        let value = parse_hcl_value(value_part.trim_start())?;
        return Some((name.to_string(), value));
    }
    None
}

fn parse_hcl_value(s: &str) -> Option<HclValue> {
    if let Some(value) = extract_quoted_value(s) {
        return Some(HclValue::Scalar(value));
    }
    if let Some(marker) = parse_heredoc_marker(s) {
        return Some(HclValue::HeredocStart(marker));
    }
    let trimmed = s.trim_start();
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        return Some(HclValue::CompositeStart);
    }
    None
}

fn parse_heredoc_marker(s: &str) -> Option<String> {
    let rest = s.trim_start().strip_prefix("<<")?;
    let rest = match rest.strip_prefix('-') {
        Some(trimmed) => trimmed,
        None => rest,
    }
    .trim_start();
    let marker = rest
        .split(|c: char| c.is_ascii_whitespace())
        .next()?
        .trim_matches('"')
        .trim_matches('\'');
    if !is_hcl_identifier(marker) {
        return None;
    }
    Some(marker.to_string())
}

/// Strip `#` / `//` line comments AND inline `/* … */` block comments from a
/// SINGLE HCL line, returning the code with every comment span removed.
///
/// This is a thin single-line driver over the ONE comment-stripping owner
/// (`strip_hcl_comments`), invoked with a throwaway block-comment flag. Running
/// the shared state machine is what makes an inline block comment that opens AND
/// closes on the same line parse correctly: `a = 1 /* note */ b = 2` yields
/// `a = 1  b = 2`, preserving `b = 2`.
///
/// The previous hand-rolled body duplicated the `#`/`//`/quote logic but DIVERGED
/// on `/*`: it returned `&line[..index]`, truncating the whole line at the block
/// comment's open and silently dropping every assignment after it. Two comment
/// parsers that disagree on one token is a ONE-PLACE violation and a latent
/// recall bug (it was masked only because every current caller happens to receive
/// input already stripped by `strip_hcl_comments` upstream, a future raw-input
/// caller would have lost data). Routing through the single owner removes the
/// divergence outright: there is now exactly one HCL comment grammar.
///
/// Returns `String` (not `&str`) because correct inline-block stripping deletes
/// interior bytes, which no single sub-slice of the input can express.
///
/// `pub(crate)` (like [`parse_hcl`]) so `structured_hcl_parser_contract.rs` can
/// drive it directly through its `include!` of this source and lock that it never
/// re-diverges from [`strip_hcl_comments`].
pub(crate) fn strip_hcl_line_comment(line: &str) -> String {
    let mut in_block_comment = false;
    strip_hcl_comments(line, &mut in_block_comment)
}

/// The ONE HCL comment-stripping owner: removes `#` / `//` line comments and
/// `/* … */` block comments (tracking block state across lines via
/// `in_block_comment`), quote-aware so comment tokens inside strings are kept.
/// `pub(crate)` so the parser-contract test can differentially lock the
/// single-line driver [`strip_hcl_line_comment`] against it.
pub(crate) fn strip_hcl_comments(line: &str, in_block_comment: &mut bool) -> String {
    let bytes = line.as_bytes();
    let mut out = String::new();
    let mut quote = None;
    let mut escaped = false;
    let mut segment_start = 0usize;
    let mut index = 0usize;
    while index < bytes.len() {
        if *in_block_comment {
            if bytes.get(index) == Some(&b'*') && bytes.get(index + 1) == Some(&b'/') {
                *in_block_comment = false;
                index += 2;
                segment_start = index;
            } else {
                index += 1;
            }
            continue;
        }

        let byte = bytes[index];
        if let Some(active_quote) = quote {
            if escaped {
                escaped = false;
            } else if byte == b'\\' {
                escaped = true;
            } else if byte == active_quote {
                quote = None;
            }
            index += 1;
            continue;
        }

        match byte {
            b'"' | b'\'' | b'`' => quote = Some(byte),
            b'#' => {
                out.push_str(&line[segment_start..index]);
                return out;
            }
            b'/' if bytes.get(index + 1) == Some(&b'/') => {
                out.push_str(&line[segment_start..index]);
                return out;
            }
            b'/' if bytes.get(index + 1) == Some(&b'*') => {
                out.push_str(&line[segment_start..index]);
                *in_block_comment = true;
                index += 2;
                segment_start = index;
                continue;
            }
            _ => {}
        }
        index += 1;
    }
    if !*in_block_comment {
        out.push_str(&line[segment_start..]);
    }
    out
}

fn brace_delta_outside_strings_and_comments(line: &str) -> (usize, usize) {
    let code = strip_hcl_line_comment(line);
    let bytes = code.as_bytes();
    let mut quote = None;
    let mut escaped = false;
    let mut opens = 0usize;
    let mut closes = 0usize;
    for byte in bytes.iter().copied() {
        if let Some(active_quote) = quote {
            if escaped {
                escaped = false;
            } else if byte == b'\\' {
                escaped = true;
            } else if byte == active_quote {
                quote = None;
            }
            continue;
        }
        match byte {
            b'"' | b'\'' | b'`' => quote = Some(byte),
            b'{' => opens += 1,
            b'}' => closes += 1,
            _ => {}
        }
    }
    (opens, closes)
}

fn extract_quoted_value(s: &str) -> Option<String> {
    let bytes = s.as_bytes();
    if bytes.is_empty() {
        return None;
    }
    let quote = bytes[0];
    if !matches!(quote, b'"' | b'\'' | b'`') {
        return None;
    }
    let body = &s[1..];
    let end = find_unescaped_quote(body, quote)?;
    Some(body[..end].to_string())
}

fn find_unescaped_quote(body: &str, quote: u8) -> Option<usize> {
    let bytes = body.as_bytes();
    let mut escaped = false;
    let mut interpolation_depth = 0usize;
    let mut interpolation_quote = None;
    let mut offset = 0usize;
    while offset < bytes.len() {
        let byte = bytes[offset];
        if escaped {
            escaped = false;
            offset += 1;
            continue;
        }
        if byte == b'\\' {
            escaped = true;
            offset += 1;
            continue;
        }
        if let Some(active_quote) = interpolation_quote {
            if byte == active_quote {
                interpolation_quote = None;
            }
            offset += 1;
            continue;
        }
        if interpolation_depth > 0 {
            match byte {
                b'"' | b'\'' => interpolation_quote = Some(byte),
                b'{' => interpolation_depth += 1,
                b'}' => interpolation_depth = interpolation_depth.saturating_sub(1),
                _ => {}
            }
            offset += 1;
            continue;
        }
        if byte == b'$' && bytes.get(offset + 1) == Some(&b'{') {
            interpolation_depth = 1;
            offset += 2;
            continue;
        }
        if byte == quote {
            return Some(offset);
        }
        offset += 1;
    }
    None
}