-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.dart
115 lines (103 loc) · 4.08 KB
/
script.dart
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
/*
* Copyright 2024 Infosys Ltd.
* Use of this source code is governed by Apache License 2.0 that can be found in the LICENSE file or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
import 'dart:io';
void main() {
final projectDir = Directory.current;
final assetsDir = Directory('${projectDir.path}/assets');
final libDir = Directory('${projectDir.path}/lib');
final sensitivePatterns = [
RegExp(r'api[_-]?key', caseSensitive: false),
RegExp(r'api[_-]?secret', caseSensitive: false),
RegExp(r'db[_-]?password', caseSensitive: false),
RegExp(r'password', caseSensitive: false),
RegExp(r'private[_-]?key', caseSensitive: false),
RegExp(r'secret', caseSensitive: false),
RegExp(r'token', caseSensitive: false),
RegExp(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}',
caseSensitive: false), // Email addresses
RegExp(r'\b\d{13,19}\b'), // Credit card numbers (basic pattern)
RegExp(r'\b\d{3}-\d{2}-\d{4}\b'), // Social Security numbers (SSN)
RegExp(r'client[_-]?id', caseSensitive: false), // Client IDs
RegExp(r'client[_-]?secret', caseSensitive: false), // Client secrets
RegExp(r'\b\d{10}\b'), // Basic 10-digit phone numbers
RegExp(
r'\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b'), // Phone numbers with separators
RegExp(
r'\(\d{3}\)\s?\d{3}[-.\s]?\d{4}\b'), // Phone numbers with area code in parentheses
RegExp(r'\bdummy\b', caseSensitive: false), // Dummy data
RegExp(r'\btest\b', caseSensitive: false), // Test data
RegExp(r'\bsample\b', caseSensitive: false), // Sample data
RegExp(r'\bexample\b', caseSensitive: false), // Example data
RegExp(r'\bmock\b', caseSensitive: false), // Mock data
RegExp(r'\bfaker\b', caseSensitive: false), // Faker data
];
print('Scanning lib directory: ${libDir.path}');
scanDirectory(libDir, sensitivePatterns, baseDir: libDir);
print('--- End of lib directory ---\n');
print('Listing .json files in assets, lib, and root directories:\n');
listJsonFiles(projectDir, recursive: false); // Root directory, non-recursive
listJsonFiles(assetsDir, recursive: true); // assets directory, recursive
listJsonFiles(libDir, recursive: true); // lib directory, recursive
}
void scanDirectory(Directory dir, List<RegExp> sensitivePatterns,
{Directory? baseDir}) {
if (!dir.existsSync()) {
print('Directory ${dir.path} does not exist.');
return;
}
final filesWithSensitiveInfo = <String, List<String>>{};
dir.listSync(recursive: true).forEach((entity) {
if (entity is File) {
final relativePath = baseDir != null
? entity.path.replaceFirst(baseDir.path, '')
: entity.path;
try {
final content = entity.readAsStringSync();
for (var pattern in sensitivePatterns) {
final matches = pattern.allMatches(content);
if (matches.isNotEmpty) {
filesWithSensitiveInfo.putIfAbsent(relativePath, () => []);
for (var match in matches) {
filesWithSensitiveInfo[relativePath]!.add(match.group(0)!);
}
}
}
} catch (e) {
// Skip files that cannot be read as text
print('Skipping file: $relativePath due to error: $e');
}
}
});
if (filesWithSensitiveInfo.isNotEmpty) {
print(
'\x1B[31m// Files containing potential sensitive information:\x1B[0m');
filesWithSensitiveInfo.forEach((filePath, sensitiveData) {
print('\x1B[34m$filePath:\x1B[0m');
for (var data in sensitiveData) {
print(' - \x1B[33m$data\x1B[0m');
}
});
print('');
}
}
void listJsonFiles(Directory dir, {bool recursive = false}) {
if (!dir.existsSync()) {
print('Directory ${dir.path} does not exist.');
return;
}
final jsonFiles = dir
.listSync(recursive: recursive)
.where((entity) => entity is File && entity.path.endsWith('.json'))
.map((entity) =>
entity.path.replaceFirst(dir.path, '').replaceFirst('/', ''));
if (jsonFiles.isNotEmpty) {
print('Directory: ${dir.path}');
for (var file in jsonFiles) {
print(' - $file');
}
print('');
}
}