dart-decimate 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/README.md +50 -49
- package/package.json +2 -1
- package/src/baseline.rs +17 -12
- package/src/changed_scope.rs +98 -5
- package/src/cli/audit_run.rs +2 -1
- package/src/cli/error.rs +1 -1
- package/src/cli/fix_run.rs +6 -11
- package/src/cli/scope_args.rs +14 -1
- package/src/cli/security_args.rs +4 -1
- package/src/cli.rs +16 -1
- package/src/config/rule_aliases.rs +8 -0
- package/src/config/rules.rs +9 -4
- package/src/config.rs +1 -0
- package/src/explain.rs +1 -0
- package/src/manifest/mcp.rs +9 -0
- package/src/manifest.rs +9 -9
- package/src/mcp/cli_args.rs +4 -1
- package/src/mcp/tools.rs +9 -0
- package/src/output/html/assets.rs +164 -0
- package/src/output/html/tests.rs +410 -0
- package/src/output/html.rs +176 -241
- package/src/output/next_steps.rs +11 -0
- package/src/output/security_findings.rs +5 -1
- package/src/output/types.rs +7 -0
- package/src/report_schema/inventory.rs +1 -0
- package/src/security/detect.rs +724 -11
- package/src/security/tests.rs +390 -1
- package/src/security.rs +6 -0
- package/src/security_gate.rs +7 -2
package/src/security/detect.rs
CHANGED
|
@@ -6,6 +6,7 @@ use crate::generated::is_generated_dart_path;
|
|
|
6
6
|
|
|
7
7
|
pub(super) fn detect_in_source(path: &Path, source: &str) -> Vec<DetectedSecurityCandidate> {
|
|
8
8
|
let mut candidates = Vec::new();
|
|
9
|
+
detect_firebase_api_keys(path, source, &mut candidates);
|
|
9
10
|
detect_hardcoded_secrets(path, source, &mut candidates);
|
|
10
11
|
detect_insecure_transport(path, source, &mut candidates);
|
|
11
12
|
detect_tls_bypass(path, source, &mut candidates);
|
|
@@ -32,20 +33,43 @@ fn detect_hardcoded_secrets(
|
|
|
32
33
|
candidates: &mut Vec<DetectedSecurityCandidate>,
|
|
33
34
|
) {
|
|
34
35
|
for literal in string_literals(source) {
|
|
35
|
-
if is_comment_match(source, literal.index)
|
|
36
|
+
if is_comment_match(source, literal.index) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if let Some(relative_index) = javascript_password_autofill_index(&literal.value) {
|
|
40
|
+
candidates.push(detected(
|
|
41
|
+
path,
|
|
42
|
+
source,
|
|
43
|
+
literal.value_index + relative_index,
|
|
44
|
+
SecurityCategory::HardcodedSecret,
|
|
45
|
+
"javascript-password-autofill",
|
|
46
|
+
SecurityConfidence::High,
|
|
47
|
+
"password-autofill-literal",
|
|
48
|
+
));
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if is_placeholder(&literal.value) {
|
|
36
52
|
continue;
|
|
37
53
|
}
|
|
38
54
|
let line = line_at(source, literal.index);
|
|
39
55
|
let secret_value = has_secret_shape(&literal.value);
|
|
40
|
-
|
|
41
|
-
|
|
56
|
+
let firebase_options_literal = firebase_options_context(source, literal.index);
|
|
57
|
+
let secret_name = if firebase_options_literal {
|
|
58
|
+
firebase_secret_name_context(source, literal.index)
|
|
59
|
+
} else {
|
|
60
|
+
has_secret_like_name(line)
|
|
61
|
+
};
|
|
62
|
+
if is_module_uri_directive_line(line)
|
|
63
|
+
|| firebase_api_key_context(source, literal.index)
|
|
64
|
+
|| (!secret_value && benign_secret_named_literal(&literal.value))
|
|
65
|
+
|| (!secret_value && firebase_options_literal && !secret_name)
|
|
66
|
+
|| google_app_id_context(source, literal.index)
|
|
42
67
|
|| (!secret_value && literal_looks_like_storage_key(&literal.value))
|
|
43
68
|
|| (!secret_value && diagnostic_context(source, literal.index))
|
|
44
69
|
|| (!secret_value && literal.value.contains('$'))
|
|
45
70
|
{
|
|
46
71
|
continue;
|
|
47
72
|
}
|
|
48
|
-
let secret_name = has_secret_like_name(line);
|
|
49
73
|
if secret_name && literal.value.len() >= 12 || secret_value {
|
|
50
74
|
candidates.push(detected(
|
|
51
75
|
path,
|
|
@@ -64,6 +88,31 @@ fn detect_hardcoded_secrets(
|
|
|
64
88
|
}
|
|
65
89
|
}
|
|
66
90
|
|
|
91
|
+
fn detect_firebase_api_keys(
|
|
92
|
+
path: &Path,
|
|
93
|
+
source: &str,
|
|
94
|
+
candidates: &mut Vec<DetectedSecurityCandidate>,
|
|
95
|
+
) {
|
|
96
|
+
for literal in string_literals(source) {
|
|
97
|
+
if is_comment_match(source, literal.index)
|
|
98
|
+
|| is_placeholder(&literal.value)
|
|
99
|
+
|| !concrete_firebase_api_key_literal(&literal.value)
|
|
100
|
+
|| !firebase_api_key_context(source, literal.index)
|
|
101
|
+
{
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
candidates.push(detected(
|
|
105
|
+
path,
|
|
106
|
+
source,
|
|
107
|
+
literal.index,
|
|
108
|
+
SecurityCategory::FirebaseApiKey,
|
|
109
|
+
"firebase-api-key",
|
|
110
|
+
SecurityConfidence::Medium,
|
|
111
|
+
"FirebaseOptions.apiKey",
|
|
112
|
+
));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
67
116
|
fn detect_insecure_transport(
|
|
68
117
|
path: &Path,
|
|
69
118
|
source: &str,
|
|
@@ -303,6 +352,7 @@ fn detected(
|
|
|
303
352
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
304
353
|
struct StringLiteral {
|
|
305
354
|
index: usize,
|
|
355
|
+
value_index: usize,
|
|
306
356
|
value: String,
|
|
307
357
|
}
|
|
308
358
|
|
|
@@ -312,8 +362,12 @@ fn string_literals(source: &str) -> Vec<StringLiteral> {
|
|
|
312
362
|
let bytes = source.as_bytes();
|
|
313
363
|
while index < bytes.len() {
|
|
314
364
|
if matches!(bytes[index], b'\'' | b'"') && !is_comment_match(source, index) {
|
|
315
|
-
if let Some((value, end)) = read_string(source, index) {
|
|
316
|
-
literals.push(StringLiteral {
|
|
365
|
+
if let Some((value, value_index, end)) = read_string(source, index) {
|
|
366
|
+
literals.push(StringLiteral {
|
|
367
|
+
index,
|
|
368
|
+
value_index,
|
|
369
|
+
value,
|
|
370
|
+
});
|
|
317
371
|
index = end;
|
|
318
372
|
continue;
|
|
319
373
|
}
|
|
@@ -323,9 +377,24 @@ fn string_literals(source: &str) -> Vec<StringLiteral> {
|
|
|
323
377
|
literals
|
|
324
378
|
}
|
|
325
379
|
|
|
326
|
-
fn read_string(source: &str, start: usize) -> Option<(String, usize)> {
|
|
380
|
+
fn read_string(source: &str, start: usize) -> Option<(String, usize, usize)> {
|
|
327
381
|
let bytes = source.as_bytes();
|
|
328
382
|
let quote = *bytes.get(start)?;
|
|
383
|
+
if bytes.get(start..start + 3) == Some([quote, quote, quote].as_slice()) {
|
|
384
|
+
let mut index = start + 3;
|
|
385
|
+
let value_start = index;
|
|
386
|
+
while index + 2 < bytes.len() {
|
|
387
|
+
if bytes.get(index..index + 3) == Some([quote, quote, quote].as_slice()) {
|
|
388
|
+
return Some((
|
|
389
|
+
source[value_start..index].to_owned(),
|
|
390
|
+
value_start,
|
|
391
|
+
index + 3,
|
|
392
|
+
));
|
|
393
|
+
}
|
|
394
|
+
index += 1;
|
|
395
|
+
}
|
|
396
|
+
return None;
|
|
397
|
+
}
|
|
329
398
|
let mut index = start + 1;
|
|
330
399
|
let value_start = index;
|
|
331
400
|
while index < bytes.len() {
|
|
@@ -334,7 +403,11 @@ fn read_string(source: &str, start: usize) -> Option<(String, usize)> {
|
|
|
334
403
|
continue;
|
|
335
404
|
}
|
|
336
405
|
if bytes[index] == quote {
|
|
337
|
-
return Some((
|
|
406
|
+
return Some((
|
|
407
|
+
source[value_start..index].to_owned(),
|
|
408
|
+
value_start,
|
|
409
|
+
index + 1,
|
|
410
|
+
));
|
|
338
411
|
}
|
|
339
412
|
index += 1;
|
|
340
413
|
}
|
|
@@ -361,7 +434,7 @@ fn call_inside_after(source: &str, start: usize) -> Option<String> {
|
|
|
361
434
|
while index < bytes.len() {
|
|
362
435
|
match bytes[index] {
|
|
363
436
|
b'\'' | b'"' => {
|
|
364
|
-
index = read_string(source, index)?.
|
|
437
|
+
index = read_string(source, index)?.2;
|
|
365
438
|
continue;
|
|
366
439
|
}
|
|
367
440
|
b'(' => depth += 1,
|
|
@@ -383,7 +456,7 @@ fn first_call_arg_is_fixed_literal(call: &str) -> bool {
|
|
|
383
456
|
if !matches!(trimmed.as_bytes().first(), Some(b'\'' | b'"')) {
|
|
384
457
|
return false;
|
|
385
458
|
}
|
|
386
|
-
let Some((_, end)) = read_string(trimmed, 0) else {
|
|
459
|
+
let Some((_, _, end)) = read_string(trimmed, 0) else {
|
|
387
460
|
return false;
|
|
388
461
|
};
|
|
389
462
|
let rest = trimmed[end..].trim_start();
|
|
@@ -450,6 +523,646 @@ fn has_secret_like_name(text: &str) -> bool {
|
|
|
450
523
|
.any(|needle| lower.contains(needle))
|
|
451
524
|
}
|
|
452
525
|
|
|
526
|
+
fn firebase_api_key_context(source: &str, index: usize) -> bool {
|
|
527
|
+
firebase_options_context(source, index) && named_argument_context(source, index, "apiKey")
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
fn firebase_options_context(source: &str, index: usize) -> bool {
|
|
531
|
+
enclosing_call_name(source, index).is_some_and(|name| name == "FirebaseOptions")
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
fn concrete_firebase_api_key_literal(value: &str) -> bool {
|
|
535
|
+
!value.trim().is_empty() && !value.contains('$')
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
fn firebase_secret_name_context(source: &str, index: usize) -> bool {
|
|
539
|
+
literal_argument_context(source, index).is_some_and(has_secret_like_name)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
fn named_argument_context(source: &str, index: usize, name: &str) -> bool {
|
|
543
|
+
let Some(context) = literal_argument_context(source, index) else {
|
|
544
|
+
return false;
|
|
545
|
+
};
|
|
546
|
+
let Some((candidate, rest)) = context.rsplit_once(':') else {
|
|
547
|
+
return false;
|
|
548
|
+
};
|
|
549
|
+
rest.trim().is_empty()
|
|
550
|
+
&& trailing_identifier(candidate).is_some_and(|identifier| identifier == name)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn google_app_id_context(source: &str, index: usize) -> bool {
|
|
554
|
+
named_argument_context(source, index, "googleAppId")
|
|
555
|
+
|| assignment_context(source, index, "googleAppId")
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
fn assignment_context(source: &str, index: usize, name: &str) -> bool {
|
|
559
|
+
let Some(context) = literal_context(source, index) else {
|
|
560
|
+
return false;
|
|
561
|
+
};
|
|
562
|
+
let Some((candidate, rest)) = context.rsplit_once('=') else {
|
|
563
|
+
return false;
|
|
564
|
+
};
|
|
565
|
+
rest.trim().is_empty()
|
|
566
|
+
&& trailing_identifier(candidate).is_some_and(|identifier| identifier == name)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
fn literal_context(source: &str, index: usize) -> Option<&str> {
|
|
570
|
+
literal_context_until_separator(source, index, true)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
fn literal_argument_context(source: &str, index: usize) -> Option<&str> {
|
|
574
|
+
literal_context_until_separator(source, index, false)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
fn literal_context_until_separator(
|
|
578
|
+
source: &str,
|
|
579
|
+
index: usize,
|
|
580
|
+
stop_at_newline: bool,
|
|
581
|
+
) -> Option<&str> {
|
|
582
|
+
let bytes = source.as_bytes();
|
|
583
|
+
let mut depth = 0usize;
|
|
584
|
+
let mut cursor = index.min(bytes.len());
|
|
585
|
+
while cursor > 0 {
|
|
586
|
+
cursor -= 1;
|
|
587
|
+
match bytes[cursor] {
|
|
588
|
+
b')' | b']' | b'}' => depth += 1,
|
|
589
|
+
b'(' | b'[' | b'{' => {
|
|
590
|
+
if depth == 0 {
|
|
591
|
+
return trimmed_context(source, cursor + 1, index);
|
|
592
|
+
}
|
|
593
|
+
depth -= 1;
|
|
594
|
+
}
|
|
595
|
+
b',' | b';' if depth == 0 => {
|
|
596
|
+
return trimmed_context(source, cursor + 1, index);
|
|
597
|
+
}
|
|
598
|
+
b'\n' if depth == 0 && stop_at_newline => {
|
|
599
|
+
return trimmed_context(source, cursor + 1, index);
|
|
600
|
+
}
|
|
601
|
+
_ => {}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
trimmed_context(source, 0, index)
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
fn trimmed_context(source: &str, start: usize, end: usize) -> Option<&str> {
|
|
608
|
+
source
|
|
609
|
+
.get(start..end)
|
|
610
|
+
.map(str::trim)
|
|
611
|
+
.filter(|value| !value.is_empty())
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
fn enclosing_call_name(source: &str, index: usize) -> Option<&str> {
|
|
615
|
+
let bytes = source.as_bytes();
|
|
616
|
+
let mut depth = 0usize;
|
|
617
|
+
let mut cursor = index.min(bytes.len());
|
|
618
|
+
while cursor > 0 {
|
|
619
|
+
cursor -= 1;
|
|
620
|
+
match bytes[cursor] {
|
|
621
|
+
b')' => depth += 1,
|
|
622
|
+
b'(' => {
|
|
623
|
+
if depth == 0 {
|
|
624
|
+
return preceding_identifier(source, cursor);
|
|
625
|
+
}
|
|
626
|
+
depth -= 1;
|
|
627
|
+
}
|
|
628
|
+
_ => {}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
None
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
fn preceding_identifier(source: &str, index: usize) -> Option<&str> {
|
|
635
|
+
let bytes = source.as_bytes();
|
|
636
|
+
let mut end = index.min(bytes.len());
|
|
637
|
+
while end > 0 && bytes[end - 1].is_ascii_whitespace() {
|
|
638
|
+
end -= 1;
|
|
639
|
+
}
|
|
640
|
+
let mut start = end;
|
|
641
|
+
while start > 0
|
|
642
|
+
&& (bytes[start - 1].is_ascii_alphanumeric()
|
|
643
|
+
|| bytes[start - 1] == b'_'
|
|
644
|
+
|| bytes[start - 1] == b'.')
|
|
645
|
+
{
|
|
646
|
+
start -= 1;
|
|
647
|
+
}
|
|
648
|
+
source
|
|
649
|
+
.get(start..end)
|
|
650
|
+
.and_then(|name| name.rsplit('.').next())
|
|
651
|
+
.filter(|name| !name.is_empty())
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
fn trailing_identifier(text: &str) -> Option<&str> {
|
|
655
|
+
let trimmed = text.trim_end();
|
|
656
|
+
let bytes = trimmed.as_bytes();
|
|
657
|
+
let mut start = trimmed.len();
|
|
658
|
+
while start > 0 && (bytes[start - 1].is_ascii_alphanumeric() || bytes[start - 1] == b'_') {
|
|
659
|
+
start -= 1;
|
|
660
|
+
}
|
|
661
|
+
trimmed.get(start..).filter(|name| !name.is_empty())
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
fn javascript_password_autofill_index(value: &str) -> Option<usize> {
|
|
665
|
+
let lower = value.to_ascii_lowercase();
|
|
666
|
+
if !lower.contains(".value") {
|
|
667
|
+
return None;
|
|
668
|
+
}
|
|
669
|
+
let mut offset = 0;
|
|
670
|
+
let mut previous_non_empty = None;
|
|
671
|
+
for raw_line in value.split_inclusive('\n') {
|
|
672
|
+
let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
|
|
673
|
+
let line_lower = line.to_ascii_lowercase();
|
|
674
|
+
if !line_lower.contains(".value") {
|
|
675
|
+
if !line.trim().is_empty() {
|
|
676
|
+
previous_non_empty = Some(line);
|
|
677
|
+
}
|
|
678
|
+
offset += raw_line.len();
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
let Some(quote_index) =
|
|
682
|
+
value_assignment_literal_index(line, &line_lower, previous_non_empty)
|
|
683
|
+
else {
|
|
684
|
+
if !line.trim().is_empty() {
|
|
685
|
+
previous_non_empty = Some(line);
|
|
686
|
+
}
|
|
687
|
+
offset += raw_line.len();
|
|
688
|
+
continue;
|
|
689
|
+
};
|
|
690
|
+
let Some((assigned, _, _)) = read_string(line, quote_index) else {
|
|
691
|
+
if !line.trim().is_empty() {
|
|
692
|
+
previous_non_empty = Some(line);
|
|
693
|
+
}
|
|
694
|
+
offset += raw_line.len();
|
|
695
|
+
continue;
|
|
696
|
+
};
|
|
697
|
+
if assigned.len() >= 8 && !is_placeholder(&assigned) {
|
|
698
|
+
return Some(offset + quote_index);
|
|
699
|
+
}
|
|
700
|
+
if !line.trim().is_empty() {
|
|
701
|
+
previous_non_empty = Some(line);
|
|
702
|
+
}
|
|
703
|
+
offset += raw_line.len();
|
|
704
|
+
}
|
|
705
|
+
None
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
struct ValueAssignmentTarget<'a> {
|
|
709
|
+
start: usize,
|
|
710
|
+
text: &'a str,
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
fn value_assignment_literal_index(
|
|
714
|
+
line: &str,
|
|
715
|
+
line_lower: &str,
|
|
716
|
+
previous_non_empty: Option<&str>,
|
|
717
|
+
) -> Option<usize> {
|
|
718
|
+
let bytes = line.as_bytes();
|
|
719
|
+
let mut search_start = 0;
|
|
720
|
+
while let Some(relative_index) = line_lower[search_start..].find(".value") {
|
|
721
|
+
let value_index = search_start + relative_index;
|
|
722
|
+
let Some(target) = value_assignment_target(line, value_index) else {
|
|
723
|
+
search_start = value_index + ".value".len();
|
|
724
|
+
continue;
|
|
725
|
+
};
|
|
726
|
+
let mut cursor = value_index + ".value".len();
|
|
727
|
+
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
|
|
728
|
+
cursor += 1;
|
|
729
|
+
}
|
|
730
|
+
if bytes.get(cursor) != Some(&b'=') {
|
|
731
|
+
search_start = cursor.min(bytes.len());
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
cursor += 1;
|
|
735
|
+
if matches!(bytes.get(cursor), Some(b'=' | b'>')) {
|
|
736
|
+
search_start = cursor;
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
|
|
740
|
+
cursor += 1;
|
|
741
|
+
}
|
|
742
|
+
if matches!(bytes.get(cursor), Some(b'\'' | b'"'))
|
|
743
|
+
&& password_autofill_assignment_context(line_lower, previous_non_empty, &target)
|
|
744
|
+
{
|
|
745
|
+
return Some(cursor);
|
|
746
|
+
}
|
|
747
|
+
search_start = cursor;
|
|
748
|
+
}
|
|
749
|
+
None
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
fn value_assignment_target(line: &str, value_index: usize) -> Option<ValueAssignmentTarget<'_>> {
|
|
753
|
+
let bytes = line.as_bytes();
|
|
754
|
+
let mut cursor = value_index;
|
|
755
|
+
let mut paren_depth = 0usize;
|
|
756
|
+
let mut bracket_depth = 0usize;
|
|
757
|
+
while cursor > 0 {
|
|
758
|
+
let byte = bytes[cursor - 1];
|
|
759
|
+
if paren_depth > 0 {
|
|
760
|
+
match byte {
|
|
761
|
+
b')' => paren_depth += 1,
|
|
762
|
+
b'(' => paren_depth -= 1,
|
|
763
|
+
_ => {}
|
|
764
|
+
}
|
|
765
|
+
cursor -= 1;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
if bracket_depth > 0 {
|
|
769
|
+
match byte {
|
|
770
|
+
b']' => bracket_depth += 1,
|
|
771
|
+
b'[' => bracket_depth -= 1,
|
|
772
|
+
_ => {}
|
|
773
|
+
}
|
|
774
|
+
cursor -= 1;
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
match byte {
|
|
778
|
+
b')' => {
|
|
779
|
+
paren_depth = 1;
|
|
780
|
+
cursor -= 1;
|
|
781
|
+
}
|
|
782
|
+
b']' => {
|
|
783
|
+
bracket_depth = 1;
|
|
784
|
+
cursor -= 1;
|
|
785
|
+
}
|
|
786
|
+
byte if is_value_target_byte(byte) => cursor -= 1,
|
|
787
|
+
_ => break,
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
line.get(cursor..value_index)
|
|
791
|
+
.map(str::trim)
|
|
792
|
+
.filter(|text| !text.is_empty())
|
|
793
|
+
.map(|text| ValueAssignmentTarget {
|
|
794
|
+
start: value_index - text.len(),
|
|
795
|
+
text,
|
|
796
|
+
})
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
fn is_value_target_byte(byte: u8) -> bool {
|
|
800
|
+
byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'$' | b'.' | b'?')
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
fn password_autofill_assignment_context(
|
|
804
|
+
line_lower: &str,
|
|
805
|
+
previous_non_empty: Option<&str>,
|
|
806
|
+
target: &ValueAssignmentTarget<'_>,
|
|
807
|
+
) -> bool {
|
|
808
|
+
let target_lower = target.text.to_ascii_lowercase();
|
|
809
|
+
if value_target_has_password_context(&target_lower) {
|
|
810
|
+
return true;
|
|
811
|
+
}
|
|
812
|
+
if line_lower
|
|
813
|
+
.get(..target.start)
|
|
814
|
+
.is_some_and(|context| context_ties_password_to_target(context, &target_lower))
|
|
815
|
+
{
|
|
816
|
+
return true;
|
|
817
|
+
}
|
|
818
|
+
previous_non_empty.is_some_and(|line| {
|
|
819
|
+
let context = line.to_ascii_lowercase();
|
|
820
|
+
context_ties_password_to_target(&context, &target_lower)
|
|
821
|
+
})
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
fn value_target_has_password_context(target_lower: &str) -> bool {
|
|
825
|
+
let terminal = terminal_value_target_segment(target_lower);
|
|
826
|
+
let compact = compact_without_ascii_whitespace(terminal);
|
|
827
|
+
!negative_password_input_hint(&compact) && terminal.contains("password")
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
fn terminal_value_target_segment(target_lower: &str) -> &str {
|
|
831
|
+
let target = target_lower.trim();
|
|
832
|
+
if let Some(segment) = target
|
|
833
|
+
.ends_with(']')
|
|
834
|
+
.then(|| matching_open_bracket(target).and_then(|start| target.get(start..)))
|
|
835
|
+
.flatten()
|
|
836
|
+
{
|
|
837
|
+
return segment;
|
|
838
|
+
}
|
|
839
|
+
if let Some(index) = last_top_level_dot(target) {
|
|
840
|
+
target.get(index + 1..).unwrap_or(target)
|
|
841
|
+
} else {
|
|
842
|
+
target
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
fn matching_open_bracket(text: &str) -> Option<usize> {
|
|
847
|
+
let bytes = text.as_bytes();
|
|
848
|
+
let mut depth = 0usize;
|
|
849
|
+
for index in (0..bytes.len()).rev() {
|
|
850
|
+
match bytes[index] {
|
|
851
|
+
b']' => depth += 1,
|
|
852
|
+
b'[' => {
|
|
853
|
+
depth = depth.saturating_sub(1);
|
|
854
|
+
if depth == 0 {
|
|
855
|
+
return Some(index);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
_ => {}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
None
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
fn last_top_level_dot(text: &str) -> Option<usize> {
|
|
865
|
+
let bytes = text.as_bytes();
|
|
866
|
+
let mut paren_depth = 0usize;
|
|
867
|
+
let mut bracket_depth = 0usize;
|
|
868
|
+
for index in (0..bytes.len()).rev() {
|
|
869
|
+
match bytes[index] {
|
|
870
|
+
b')' => paren_depth += 1,
|
|
871
|
+
b'(' => paren_depth = paren_depth.saturating_sub(1),
|
|
872
|
+
b']' => bracket_depth += 1,
|
|
873
|
+
b'[' => bracket_depth = bracket_depth.saturating_sub(1),
|
|
874
|
+
b'.' if paren_depth == 0 && bracket_depth == 0 => return Some(index),
|
|
875
|
+
_ => {}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
None
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
fn context_ties_password_to_target(context_lower: &str, target_lower: &str) -> bool {
|
|
882
|
+
let mut search_start = 0;
|
|
883
|
+
while let Some((start, end)) = target_reference_range(context_lower, target_lower, search_start)
|
|
884
|
+
{
|
|
885
|
+
if password_input_hint(target_reference_context(context_lower, start, end)) {
|
|
886
|
+
return true;
|
|
887
|
+
}
|
|
888
|
+
search_start = end;
|
|
889
|
+
}
|
|
890
|
+
false
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
fn password_input_hint(context_lower: &str) -> bool {
|
|
894
|
+
let compact = compact_without_ascii_whitespace(context_lower);
|
|
895
|
+
if negative_password_input_hint(&compact) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
positive_password_input_hint(&compact)
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
fn negative_password_input_hint(compact_context: &str) -> bool {
|
|
902
|
+
[
|
|
903
|
+
"type!=='password'",
|
|
904
|
+
"type!==\"password\"",
|
|
905
|
+
"type!==password",
|
|
906
|
+
"type!='password'",
|
|
907
|
+
"type!=\"password\"",
|
|
908
|
+
"type!=password",
|
|
909
|
+
":not([type=password",
|
|
910
|
+
":not([type='password'",
|
|
911
|
+
":not([type=\"password\"",
|
|
912
|
+
"not([type=password",
|
|
913
|
+
"not([type='password'",
|
|
914
|
+
"not([type=\"password\"",
|
|
915
|
+
]
|
|
916
|
+
.iter()
|
|
917
|
+
.any(|needle| compact_context.contains(needle))
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
fn compact_without_ascii_whitespace(text: &str) -> String {
|
|
921
|
+
text.chars()
|
|
922
|
+
.filter(|character| !character.is_ascii_whitespace())
|
|
923
|
+
.collect()
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
fn positive_password_input_hint(compact_context: &str) -> bool {
|
|
927
|
+
[
|
|
928
|
+
"type==='password'",
|
|
929
|
+
"type===\"password\"",
|
|
930
|
+
"type=='password'",
|
|
931
|
+
"type==\"password\"",
|
|
932
|
+
"type=password",
|
|
933
|
+
"type='password'",
|
|
934
|
+
"type=\"password\"",
|
|
935
|
+
"[type=password",
|
|
936
|
+
"[type='password'",
|
|
937
|
+
"[type=\"password\"",
|
|
938
|
+
]
|
|
939
|
+
.iter()
|
|
940
|
+
.any(|needle| compact_context.contains(needle))
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
fn target_reference_range(
|
|
944
|
+
context_lower: &str,
|
|
945
|
+
target_lower: &str,
|
|
946
|
+
search_start: usize,
|
|
947
|
+
) -> Option<(usize, usize)> {
|
|
948
|
+
let bounded_identifier = target_lower.bytes().all(is_identifier_byte);
|
|
949
|
+
let mut cursor = search_start;
|
|
950
|
+
while let Some(relative_index) = context_lower[cursor..].find(target_lower) {
|
|
951
|
+
let start = cursor + relative_index;
|
|
952
|
+
let end = start + target_lower.len();
|
|
953
|
+
if !bounded_identifier || identifier_boundaries(context_lower, start, end) {
|
|
954
|
+
return Some((start, end));
|
|
955
|
+
}
|
|
956
|
+
cursor = end;
|
|
957
|
+
}
|
|
958
|
+
None
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
fn identifier_boundaries(context_lower: &str, start: usize, end: usize) -> bool {
|
|
962
|
+
let before_boundary = start == 0 || !is_identifier_byte(context_lower.as_bytes()[start - 1]);
|
|
963
|
+
let after_boundary =
|
|
964
|
+
end == context_lower.len() || !is_identifier_byte(context_lower.as_bytes()[end]);
|
|
965
|
+
before_boundary && after_boundary
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
fn target_reference_context(context_lower: &str, start: usize, end: usize) -> &str {
|
|
969
|
+
let context_start = logical_expression_start(context_lower, start);
|
|
970
|
+
let context_end = logical_expression_end(context_lower, end);
|
|
971
|
+
&context_lower[context_start..context_end]
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
fn logical_expression_start(context_lower: &str, start: usize) -> usize {
|
|
975
|
+
let bytes = context_lower.as_bytes();
|
|
976
|
+
let mut cursor = start;
|
|
977
|
+
while cursor > 0 {
|
|
978
|
+
if cursor >= 2 && bytes.get(cursor - 2..cursor) == Some(b"&&") {
|
|
979
|
+
return cursor;
|
|
980
|
+
}
|
|
981
|
+
if cursor >= 2 && bytes.get(cursor - 2..cursor) == Some(b"||") {
|
|
982
|
+
return cursor;
|
|
983
|
+
}
|
|
984
|
+
if matches!(bytes[cursor - 1], b';' | b'{' | b'}' | b'\n' | b',') {
|
|
985
|
+
return cursor;
|
|
986
|
+
}
|
|
987
|
+
cursor -= 1;
|
|
988
|
+
}
|
|
989
|
+
0
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
fn logical_expression_end(context_lower: &str, end: usize) -> usize {
|
|
993
|
+
let bytes = context_lower.as_bytes();
|
|
994
|
+
let mut cursor = end;
|
|
995
|
+
while cursor < bytes.len() {
|
|
996
|
+
if bytes.get(cursor..cursor + 2) == Some(b"&&")
|
|
997
|
+
|| bytes.get(cursor..cursor + 2) == Some(b"||")
|
|
998
|
+
{
|
|
999
|
+
return cursor;
|
|
1000
|
+
}
|
|
1001
|
+
if matches!(bytes[cursor], b';' | b'{' | b'}' | b'\n' | b',') {
|
|
1002
|
+
return cursor;
|
|
1003
|
+
}
|
|
1004
|
+
cursor += 1;
|
|
1005
|
+
}
|
|
1006
|
+
bytes.len()
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
fn is_identifier_byte(byte: u8) -> bool {
|
|
1010
|
+
byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'$')
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
fn is_module_uri_directive_line(line: &str) -> bool {
|
|
1014
|
+
let trimmed = line.trim_start();
|
|
1015
|
+
["import ", "export ", "part ", "part of "]
|
|
1016
|
+
.iter()
|
|
1017
|
+
.any(|prefix| trimmed.starts_with(prefix))
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
const RESET_OR_RECOVERY_PATH_MARKERS: &[&str] = &[
|
|
1021
|
+
"forgot-password",
|
|
1022
|
+
"reset-password",
|
|
1023
|
+
"passwordreset",
|
|
1024
|
+
"password-reset",
|
|
1025
|
+
"password-recovery",
|
|
1026
|
+
"recover-password",
|
|
1027
|
+
];
|
|
1028
|
+
|
|
1029
|
+
fn benign_secret_named_literal(value: &str) -> bool {
|
|
1030
|
+
let route_or_reset_url =
|
|
1031
|
+
literal_looks_like_route_path(value) || literal_looks_like_reset_or_recovery_url(value);
|
|
1032
|
+
(route_or_reset_url
|
|
1033
|
+
&& !literal_has_secret_like_url_parameter(value)
|
|
1034
|
+
&& !literal_has_secret_like_reset_path_segment(value))
|
|
1035
|
+
|| literal_looks_like_user_facing_copy(value)
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
fn literal_looks_like_route_path(value: &str) -> bool {
|
|
1039
|
+
let trimmed = value.trim();
|
|
1040
|
+
trimmed.starts_with('/')
|
|
1041
|
+
&& trimmed.len() <= 128
|
|
1042
|
+
&& !trimmed.contains(char::is_whitespace)
|
|
1043
|
+
&& trimmed.bytes().all(|byte| {
|
|
1044
|
+
byte.is_ascii_alphanumeric()
|
|
1045
|
+
|| matches!(
|
|
1046
|
+
byte,
|
|
1047
|
+
b'/' | b'-' | b'_' | b'.' | b':' | b'?' | b'&' | b'=' | b'#'
|
|
1048
|
+
)
|
|
1049
|
+
})
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
fn literal_looks_like_reset_or_recovery_url(value: &str) -> bool {
|
|
1053
|
+
let lower = value.to_ascii_lowercase();
|
|
1054
|
+
(lower.starts_with("https://") || lower.starts_with("http://"))
|
|
1055
|
+
&& RESET_OR_RECOVERY_PATH_MARKERS
|
|
1056
|
+
.iter()
|
|
1057
|
+
.any(|needle| lower.contains(needle))
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
fn literal_has_secret_like_url_parameter(value: &str) -> bool {
|
|
1061
|
+
let trimmed = value.trim();
|
|
1062
|
+
if let Some(query_index) = trimmed.find('?') {
|
|
1063
|
+
let query_start = query_index + 1;
|
|
1064
|
+
let query_end = trimmed[query_start..]
|
|
1065
|
+
.find('#')
|
|
1066
|
+
.map_or(trimmed.len(), |fragment_index| query_start + fragment_index);
|
|
1067
|
+
if segment_has_secret_like_url_parameter(&trimmed[query_start..query_end]) {
|
|
1068
|
+
return true;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
trimmed.find('#').is_some_and(|fragment_index| {
|
|
1072
|
+
segment_has_secret_like_url_parameter(&trimmed[fragment_index + 1..])
|
|
1073
|
+
})
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
fn segment_has_secret_like_url_parameter(segment: &str) -> bool {
|
|
1077
|
+
segment.split('&').any(|parameter| {
|
|
1078
|
+
let Some((name, value)) = parameter.split_once('=') else {
|
|
1079
|
+
return false;
|
|
1080
|
+
};
|
|
1081
|
+
has_secret_like_name(name) && concrete_url_parameter_value(value)
|
|
1082
|
+
})
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
fn concrete_url_parameter_value(value: &str) -> bool {
|
|
1086
|
+
let trimmed = value.trim();
|
|
1087
|
+
if trimmed.len() < 8
|
|
1088
|
+
|| trimmed.contains('$')
|
|
1089
|
+
|| trimmed.starts_with(':')
|
|
1090
|
+
|| (trimmed.starts_with('{') && trimmed.ends_with('}'))
|
|
1091
|
+
|| (trimmed.starts_with('<') && trimmed.ends_with('>'))
|
|
1092
|
+
{
|
|
1093
|
+
return false;
|
|
1094
|
+
}
|
|
1095
|
+
!is_placeholder(trimmed)
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
fn literal_has_secret_like_reset_path_segment(value: &str) -> bool {
|
|
1099
|
+
let trimmed = value.trim();
|
|
1100
|
+
let path_end = trimmed.find(['?', '#']).unwrap_or(trimmed.len());
|
|
1101
|
+
let path = &trimmed[..path_end];
|
|
1102
|
+
let mut reset_or_recovery_path_seen = false;
|
|
1103
|
+
for segment in path.split('/') {
|
|
1104
|
+
let lower = segment.to_ascii_lowercase();
|
|
1105
|
+
if reset_or_recovery_path_seen && concrete_secret_like_path_segment(segment) {
|
|
1106
|
+
return true;
|
|
1107
|
+
}
|
|
1108
|
+
if RESET_OR_RECOVERY_PATH_MARKERS.contains(&lower.as_str()) {
|
|
1109
|
+
reset_or_recovery_path_seen = true;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
false
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
fn concrete_secret_like_path_segment(segment: &str) -> bool {
|
|
1116
|
+
let trimmed = segment.trim();
|
|
1117
|
+
if !concrete_url_parameter_value(trimmed) || trimmed.len() < 12 {
|
|
1118
|
+
return false;
|
|
1119
|
+
}
|
|
1120
|
+
let mut has_alpha = false;
|
|
1121
|
+
let mut has_digit = false;
|
|
1122
|
+
for byte in trimmed.bytes() {
|
|
1123
|
+
if byte.is_ascii_alphabetic() {
|
|
1124
|
+
has_alpha = true;
|
|
1125
|
+
} else if byte.is_ascii_digit() {
|
|
1126
|
+
has_digit = true;
|
|
1127
|
+
} else if !matches!(byte, b'-' | b'_' | b'.' | b'~') {
|
|
1128
|
+
return false;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
has_alpha && has_digit
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
fn literal_looks_like_user_facing_copy(value: &str) -> bool {
|
|
1135
|
+
let trimmed = value.trim();
|
|
1136
|
+
let lower = trimmed.to_ascii_lowercase();
|
|
1137
|
+
trimmed.len() <= 120
|
|
1138
|
+
&& trimmed.contains(char::is_whitespace)
|
|
1139
|
+
&& ["password", "secret", "token", "credential"]
|
|
1140
|
+
.iter()
|
|
1141
|
+
.any(|word| lower.contains(word))
|
|
1142
|
+
&& [
|
|
1143
|
+
"change password",
|
|
1144
|
+
"invalid email or password",
|
|
1145
|
+
"forgot password",
|
|
1146
|
+
"reset password",
|
|
1147
|
+
"enter password",
|
|
1148
|
+
"confirm password",
|
|
1149
|
+
"password must",
|
|
1150
|
+
"password is",
|
|
1151
|
+
"token expired",
|
|
1152
|
+
"invalid token",
|
|
1153
|
+
]
|
|
1154
|
+
.iter()
|
|
1155
|
+
.any(|phrase| lower.contains(phrase))
|
|
1156
|
+
&& trimmed.chars().all(|character| {
|
|
1157
|
+
character.is_ascii_alphanumeric()
|
|
1158
|
+
|| character.is_ascii_whitespace()
|
|
1159
|
+
|| matches!(
|
|
1160
|
+
character,
|
|
1161
|
+
'.' | ',' | '!' | '?' | ':' | ';' | '\'' | '"' | '-' | '/' | '(' | ')'
|
|
1162
|
+
)
|
|
1163
|
+
})
|
|
1164
|
+
}
|
|
1165
|
+
|
|
453
1166
|
fn has_secret_shape(value: &str) -> bool {
|
|
454
1167
|
if value.contains('$') {
|
|
455
1168
|
return false;
|
|
@@ -602,7 +1315,7 @@ fn redact_line(line: &str) -> String {
|
|
|
602
1315
|
redacted.push(quote);
|
|
603
1316
|
redacted.push_str("<redacted>");
|
|
604
1317
|
redacted.push(quote);
|
|
605
|
-
if let Some((_, end)) = read_string(line, index) {
|
|
1318
|
+
if let Some((_, _, end)) = read_string(line, index) {
|
|
606
1319
|
index = end;
|
|
607
1320
|
continue;
|
|
608
1321
|
}
|