@neuralaxis/vibedoctor 0.1.0 → 0.1.2
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/README.md +203 -280
- package/dist/adapters/customDeadChain.d.ts +2 -1
- package/dist/adapters/customDeadChain.js +100 -8
- package/dist/adapters/customLeftovers.js +33 -10
- package/dist/adapters/index.d.ts +3 -0
- package/dist/adapters/index.js +7 -1
- package/dist/adapters/jscpd.d.ts +1 -0
- package/dist/adapters/jscpd.js +10 -1
- package/dist/adapters/presidio.d.ts +2 -0
- package/dist/adapters/presidio.js +158 -0
- package/dist/adapters/privacyDetector.d.ts +10 -0
- package/dist/adapters/privacyDetector.js +600 -0
- package/dist/adapters/telemetryDetector.d.ts +2 -0
- package/dist/adapters/telemetryDetector.js +118 -0
- package/dist/agentPack/generateAgentPack.d.ts +7 -0
- package/dist/agentPack/generateAgentPack.js +89 -1
- package/dist/agentPack/templates/index.js +14 -11
- package/dist/agentPack/templates/skills.js +47 -0
- package/dist/cli/commands/agent.d.ts +5 -0
- package/dist/cli/commands/agent.js +10 -0
- package/dist/cli/commands/init.js +38 -13
- package/dist/cli/commands/privacyReview.d.ts +9 -0
- package/dist/cli/commands/privacyReview.js +304 -0
- package/dist/cli/commands/scan.js +27 -1
- package/dist/cli/commands/setup.js +49 -9
- package/dist/cli/index.js +63 -7
- package/dist/core/config.d.ts +23 -0
- package/dist/core/config.js +114 -5
- package/dist/core/engine.d.ts +3 -0
- package/dist/core/engine.js +31 -9
- package/dist/core/finding.d.ts +16 -2
- package/dist/core/finding.js +5 -0
- package/dist/core/paths.js +5 -0
- package/dist/core/privacyReview.d.ts +51 -0
- package/dist/core/privacyReview.js +94 -0
- package/dist/core/projectDetector.js +6 -1
- package/dist/core/scanPlanner.js +4 -0
- package/dist/core/scoring.js +6 -0
- package/dist/core/toolRunner.js +25 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/mcp/tools/shared.d.ts +1 -1
- package/dist/reporters/html.js +8 -6
- package/dist/reporters/json.js +3 -5
- package/dist/reporters/markdown.js +7 -0
- package/dist/reporters/sarif.js +6 -0
- package/dist/reporters/terminal.js +1 -5
- package/dist/rules/severityMap.d.ts +1 -0
- package/dist/rules/severityMap.js +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.privacyDetectorAdapter = void 0;
|
|
7
|
+
exports.confidenceFromScore = confidenceFromScore;
|
|
8
|
+
exports.passesLuhn = passesLuhn;
|
|
9
|
+
exports.isValidAadhaar = isValidAadhaar;
|
|
10
|
+
exports.maskPiiValue = maskPiiValue;
|
|
11
|
+
exports.detectPiiFindings = detectPiiFindings;
|
|
12
|
+
exports.normalizeExternalFile = normalizeExternalFile;
|
|
13
|
+
const node_fs_1 = require("node:fs");
|
|
14
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
15
|
+
const finding_1 = require("../core/finding");
|
|
16
|
+
const paths_1 = require("../core/paths");
|
|
17
|
+
const TEXT_EXTENSIONS = new Set([
|
|
18
|
+
".ts",
|
|
19
|
+
".tsx",
|
|
20
|
+
".js",
|
|
21
|
+
".jsx",
|
|
22
|
+
".py",
|
|
23
|
+
".json",
|
|
24
|
+
".jsonl",
|
|
25
|
+
".yaml",
|
|
26
|
+
".yml",
|
|
27
|
+
".toml",
|
|
28
|
+
".env",
|
|
29
|
+
".md",
|
|
30
|
+
".txt",
|
|
31
|
+
".log",
|
|
32
|
+
".csv",
|
|
33
|
+
".tsv",
|
|
34
|
+
".sql",
|
|
35
|
+
".graphql",
|
|
36
|
+
".gql",
|
|
37
|
+
".xml",
|
|
38
|
+
".prisma"
|
|
39
|
+
]);
|
|
40
|
+
const CONFIDENCE_RANK = {
|
|
41
|
+
low: 0,
|
|
42
|
+
medium: 1,
|
|
43
|
+
high: 2
|
|
44
|
+
};
|
|
45
|
+
const verhoeffD = [
|
|
46
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
47
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
48
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
49
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
50
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
51
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
52
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
53
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
54
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
55
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
|
56
|
+
];
|
|
57
|
+
const verhoeffP = [
|
|
58
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
59
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
60
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
61
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
62
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
63
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
64
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
65
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
|
|
66
|
+
];
|
|
67
|
+
function normalizeDigits(value) {
|
|
68
|
+
return value.replace(/\D/g, "");
|
|
69
|
+
}
|
|
70
|
+
function normalizeFieldName(value) {
|
|
71
|
+
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
72
|
+
}
|
|
73
|
+
function confidenceFromScore(score) {
|
|
74
|
+
if (score >= 0.85) {
|
|
75
|
+
return "high";
|
|
76
|
+
}
|
|
77
|
+
if (score >= 0.6) {
|
|
78
|
+
return "medium";
|
|
79
|
+
}
|
|
80
|
+
return "low";
|
|
81
|
+
}
|
|
82
|
+
function passesLuhn(value) {
|
|
83
|
+
const digits = normalizeDigits(value);
|
|
84
|
+
if (digits.length < 13 || digits.length > 19) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
let sum = 0;
|
|
88
|
+
let doubleDigit = false;
|
|
89
|
+
for (let index = digits.length - 1; index >= 0; index -= 1) {
|
|
90
|
+
let digit = Number(digits[index]);
|
|
91
|
+
if (doubleDigit) {
|
|
92
|
+
digit *= 2;
|
|
93
|
+
if (digit > 9) {
|
|
94
|
+
digit -= 9;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
sum += digit;
|
|
98
|
+
doubleDigit = !doubleDigit;
|
|
99
|
+
}
|
|
100
|
+
return sum % 10 === 0;
|
|
101
|
+
}
|
|
102
|
+
function isValidAadhaar(value) {
|
|
103
|
+
const digits = normalizeDigits(value);
|
|
104
|
+
if (!/^[2-9]\d{11}$/.test(digits)) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
let checksum = 0;
|
|
108
|
+
const reversed = digits.split("").reverse().map((digit) => Number(digit));
|
|
109
|
+
for (const [index, digit] of reversed.entries()) {
|
|
110
|
+
checksum = verhoeffD[checksum][verhoeffP[index % 8][digit]];
|
|
111
|
+
}
|
|
112
|
+
return checksum === 0;
|
|
113
|
+
}
|
|
114
|
+
function looksLikePan(value) {
|
|
115
|
+
return /^[A-Z]{5}[0-9]{4}[A-Z]$/.test(value.toUpperCase());
|
|
116
|
+
}
|
|
117
|
+
function isPublicIp(value) {
|
|
118
|
+
const parts = value.split(".").map((item) => Number(item));
|
|
119
|
+
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const [first, second] = parts;
|
|
123
|
+
if (first === 10 || first === 127 || first === 0) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (first === 192 && second === 168) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (first === 172 && second >= 16 && second <= 31) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
function maskPiiValue(value, entityType, maskExamples = true) {
|
|
135
|
+
if (!maskExamples) {
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
const trimmed = value.trim();
|
|
139
|
+
const digits = normalizeDigits(trimmed);
|
|
140
|
+
if (entityType === "email") {
|
|
141
|
+
const [local, domain] = trimmed.split("@");
|
|
142
|
+
return `${local.slice(0, 2)}***@${domain ?? ""}`;
|
|
143
|
+
}
|
|
144
|
+
if (entityType === "pan") {
|
|
145
|
+
const upper = trimmed.toUpperCase();
|
|
146
|
+
return `${upper.slice(0, 1)}****${upper.slice(5, 9)}*`;
|
|
147
|
+
}
|
|
148
|
+
if (entityType === "aadhaar") {
|
|
149
|
+
return `********${digits.slice(-4)}`;
|
|
150
|
+
}
|
|
151
|
+
if (entityType === "payment_card") {
|
|
152
|
+
return `${digits.slice(0, 4)}********${digits.slice(-4)}`;
|
|
153
|
+
}
|
|
154
|
+
if (digits.length >= 8) {
|
|
155
|
+
return `${"*".repeat(Math.max(4, digits.length - 4))}${digits.slice(-4)}`;
|
|
156
|
+
}
|
|
157
|
+
if (trimmed.length <= 3) {
|
|
158
|
+
return "***";
|
|
159
|
+
}
|
|
160
|
+
return `${trimmed.slice(0, 2)}***`;
|
|
161
|
+
}
|
|
162
|
+
function redactKnownValues(line, maskExamples) {
|
|
163
|
+
let redacted = line;
|
|
164
|
+
redacted = redacted.replace(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi, (match) => maskPiiValue(match, "email", maskExamples));
|
|
165
|
+
redacted = redacted.replace(/\b[A-Z]{5}[0-9]{4}[A-Z]\b/gi, (match) => maskPiiValue(match, "pan", maskExamples));
|
|
166
|
+
redacted = redacted.replace(/\b[2-9]\d{3}[\s-]?\d{4}[\s-]?\d{4}\b/g, (match) => isValidAadhaar(match) ? maskPiiValue(match, "aadhaar", maskExamples) : match);
|
|
167
|
+
redacted = redacted.replace(/\b(?:\d[ -]?){13,19}\b/g, (match) => passesLuhn(match) ? maskPiiValue(match, "payment_card", maskExamples) : match);
|
|
168
|
+
redacted = redacted.replace(/\b(?:\d{1,3}\.){3}\d{1,3}\b/g, (match) => maskPiiValue(match, "ip_address", maskExamples));
|
|
169
|
+
redacted = redacted.replace(/\b(?:\d{4}-\d{2}-\d{2}|\d{2}[/-]\d{2}[/-]\d{4})\b/g, (match) => maskPiiValue(match, "date_of_birth", maskExamples));
|
|
170
|
+
redacted = redacted.replace(/(?:\+\d{1,3}[-.\s]?)?(?:\(?\d{3,5}\)?[-.\s]?){2,}\d{3,4}/g, (match) => {
|
|
171
|
+
const digits = normalizeDigits(match);
|
|
172
|
+
if (digits.length < 10 || digits.length > 14 || passesLuhn(digits) || isValidAadhaar(digits)) {
|
|
173
|
+
return match;
|
|
174
|
+
}
|
|
175
|
+
return maskPiiValue(match, "phone", maskExamples);
|
|
176
|
+
});
|
|
177
|
+
return redacted;
|
|
178
|
+
}
|
|
179
|
+
function redactSnippet(line, value, entityType, maskExamples) {
|
|
180
|
+
const compact = redactKnownValues(line.trim(), maskExamples);
|
|
181
|
+
if (!value) {
|
|
182
|
+
return compact.length > 240 ? `${compact.slice(0, 237)}...` : compact;
|
|
183
|
+
}
|
|
184
|
+
const redacted = compact.replace(value, maskPiiValue(value, entityType, maskExamples));
|
|
185
|
+
return redacted.length > 240 ? `${redacted.slice(0, 237)}...` : redacted;
|
|
186
|
+
}
|
|
187
|
+
function hasContext(line, patterns) {
|
|
188
|
+
return patterns.some((pattern) => pattern.test(line));
|
|
189
|
+
}
|
|
190
|
+
function isTextCandidate(file) {
|
|
191
|
+
const extension = node_path_1.default.extname(file).toLowerCase();
|
|
192
|
+
if (TEXT_EXTENSIONS.has(extension)) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
return /(^|\/)(openapi|schema|graphql|Dockerfile|\.env)/i.test(file);
|
|
196
|
+
}
|
|
197
|
+
function fieldEntity(field) {
|
|
198
|
+
const normalized = normalizeFieldName(field);
|
|
199
|
+
const reason = `field name '${field}' matched privacy vocabulary`;
|
|
200
|
+
if (/aadhaar|aadhar|uidai/.test(normalized)) {
|
|
201
|
+
return { entityType: "aadhaar", sensitivity: "regulated_identifier", confidenceScore: 0.82, matchedPattern: "aadhaar-field", reasons: [reason] };
|
|
202
|
+
}
|
|
203
|
+
if (/permanentaccountnumber|pan(number|no|card)?$/.test(normalized)) {
|
|
204
|
+
return { entityType: "pan", sensitivity: "regulated_identifier", confidenceScore: 0.82, matchedPattern: "pan-field", reasons: [reason] };
|
|
205
|
+
}
|
|
206
|
+
if (/passport/.test(normalized)) {
|
|
207
|
+
return { entityType: "passport", sensitivity: "regulated_identifier", confidenceScore: 0.78, matchedPattern: "passport-field", reasons: [reason] };
|
|
208
|
+
}
|
|
209
|
+
if (/creditcard|cardnumber|paymentcard/.test(normalized)) {
|
|
210
|
+
return { entityType: "payment_card", sensitivity: "regulated_identifier", confidenceScore: 0.78, matchedPattern: "payment-card-field", reasons: [reason] };
|
|
211
|
+
}
|
|
212
|
+
if (/bankaccount|accountnumber|accountno|iban|ifsc/.test(normalized)) {
|
|
213
|
+
return { entityType: "bank_account", sensitivity: "regulated_identifier", confidenceScore: 0.76, matchedPattern: "bank-account-field", reasons: [reason] };
|
|
214
|
+
}
|
|
215
|
+
if (/email|mailaddress/.test(normalized)) {
|
|
216
|
+
return { entityType: "email", sensitivity: "low", confidenceScore: 0.7, matchedPattern: "email-field", reasons: [reason] };
|
|
217
|
+
}
|
|
218
|
+
if (/phone|mobile|telephone|contactnumber|contactno/.test(normalized)) {
|
|
219
|
+
return { entityType: "phone", sensitivity: "moderate", confidenceScore: 0.68, matchedPattern: "phone-field", reasons: [reason] };
|
|
220
|
+
}
|
|
221
|
+
if (/dateofbirth|birthdate|dob/.test(normalized)) {
|
|
222
|
+
return { entityType: "date_of_birth", sensitivity: "moderate", confidenceScore: 0.72, matchedPattern: "dob-field", reasons: [reason] };
|
|
223
|
+
}
|
|
224
|
+
if (/address|street|postcode|postalcode|zipcode|zip/.test(normalized)) {
|
|
225
|
+
return { entityType: "address", sensitivity: "moderate", confidenceScore: 0.66, matchedPattern: "address-field", reasons: [reason] };
|
|
226
|
+
}
|
|
227
|
+
if (/ipaddress|clientip|remoteaddr|remoteaddress/.test(normalized)) {
|
|
228
|
+
return { entityType: "ip_address", sensitivity: "moderate", confidenceScore: 0.7, matchedPattern: "ip-field", reasons: [reason] };
|
|
229
|
+
}
|
|
230
|
+
if (/deviceid|cookieid|advertisingid|imei|imsi/.test(normalized)) {
|
|
231
|
+
return { entityType: "device_id", sensitivity: "moderate", confidenceScore: 0.72, matchedPattern: "device-field", reasons: [reason] };
|
|
232
|
+
}
|
|
233
|
+
if (/(employee|customer|user|patient|applicant|member|policy)(id|number|no)$/.test(normalized)) {
|
|
234
|
+
return { entityType: "person_linkable_id", sensitivity: "moderate", confidenceScore: 0.65, matchedPattern: "person-id-field", reasons: [reason] };
|
|
235
|
+
}
|
|
236
|
+
if (/medicalrecord|patientdiagnosis|diagnosis|patientcondition|healthcondition|prescription/.test(normalized)) {
|
|
237
|
+
return { entityType: "health_data", sensitivity: "high", confidenceScore: 0.78, matchedPattern: "health-field", reasons: [reason] };
|
|
238
|
+
}
|
|
239
|
+
if (/salary|income|payroll|compensation/.test(normalized)) {
|
|
240
|
+
return { entityType: "financial_attribute", sensitivity: "high", confidenceScore: 0.74, matchedPattern: "financial-field", reasons: [reason] };
|
|
241
|
+
}
|
|
242
|
+
if (/disciplinary|performancereview|terminationreason/.test(normalized)) {
|
|
243
|
+
return { entityType: "employment_sensitive", sensitivity: "high", confidenceScore: 0.74, matchedPattern: "employment-sensitive-field", reasons: [reason] };
|
|
244
|
+
}
|
|
245
|
+
if (/preciselocation|gps|latitude|longitude|geolocation/.test(normalized)) {
|
|
246
|
+
return { entityType: "precise_location", sensitivity: "high", confidenceScore: 0.74, matchedPattern: "location-field", reasons: [reason] };
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
function extractFields(file, line, lineNumber) {
|
|
251
|
+
const fields = [];
|
|
252
|
+
const trimmed = line.trim();
|
|
253
|
+
const extension = node_path_1.default.extname(file).toLowerCase();
|
|
254
|
+
if ((extension === ".csv" || extension === ".tsv") && lineNumber <= 5) {
|
|
255
|
+
const separator = extension === ".tsv" ? "\t" : ",";
|
|
256
|
+
const cells = trimmed.split(separator).map((cell) => cell.trim().replace(/^["']|["']$/g, ""));
|
|
257
|
+
if (cells.length > 1 && cells.every((cell) => /^[A-Za-z_][A-Za-z0-9_. -]{0,80}$/.test(cell))) {
|
|
258
|
+
for (const cell of cells) {
|
|
259
|
+
fields.push({ field: cell, normalized: normalizeFieldName(cell), line: lineNumber });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const keyPatterns = [
|
|
264
|
+
/["']?([A-Za-z_][A-Za-z0-9_.-]{1,80})["']?\s*[:=]/g,
|
|
265
|
+
/\b([A-Za-z_][A-Za-z0-9_]{1,80})\??:\s*[A-Za-z_{[]/g
|
|
266
|
+
];
|
|
267
|
+
for (const pattern of keyPatterns) {
|
|
268
|
+
for (const match of trimmed.matchAll(pattern)) {
|
|
269
|
+
const field = match[1];
|
|
270
|
+
if (!field || ["http", "https", "return", "case", "default"].includes(field.toLowerCase())) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
fields.push({ field, normalized: normalizeFieldName(field), line: lineNumber });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const seen = new Set();
|
|
277
|
+
return fields.filter((field) => {
|
|
278
|
+
const key = `${field.normalized}:${field.line}`;
|
|
279
|
+
if (seen.has(key)) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
seen.add(key);
|
|
283
|
+
return true;
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
function makeFieldMatch(file, line, signal, ctx) {
|
|
287
|
+
const entity = fieldEntity(signal.field);
|
|
288
|
+
if (!entity) {
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
...entity,
|
|
293
|
+
file,
|
|
294
|
+
line: signal.line,
|
|
295
|
+
title: `Privacy field: ${entity.entityType}`,
|
|
296
|
+
message: `${signal.field} appears to describe ${entity.entityType}.`,
|
|
297
|
+
snippet: redactSnippet(line, undefined, entity.entityType, ctx.config.checks.privacy.maskExamples),
|
|
298
|
+
detectionPath: ["metadata"],
|
|
299
|
+
reasons: [...entity.reasons, "metadata-only finding; sample values were not required"]
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function makeValueMatch(file, line, lineNumber, value, entityType, sensitivity, confidenceScore, matchedPattern, detectionPath, reasons, ctx) {
|
|
303
|
+
const maskedValue = maskPiiValue(value, entityType, ctx.config.checks.privacy.maskExamples);
|
|
304
|
+
return {
|
|
305
|
+
entityType,
|
|
306
|
+
sensitivity,
|
|
307
|
+
confidenceScore,
|
|
308
|
+
title: `PII detected: ${entityType}`,
|
|
309
|
+
message: `${entityType} detected with ${confidenceFromScore(confidenceScore)} confidence.`,
|
|
310
|
+
file,
|
|
311
|
+
line: lineNumber,
|
|
312
|
+
value,
|
|
313
|
+
maskedValue,
|
|
314
|
+
snippet: redactSnippet(line, value, entityType, ctx.config.checks.privacy.maskExamples),
|
|
315
|
+
matchedPattern,
|
|
316
|
+
detectionPath,
|
|
317
|
+
reasons
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
function findValueMatches(file, line, lineNumber, ctx) {
|
|
321
|
+
const matches = [];
|
|
322
|
+
const lower = line.toLowerCase();
|
|
323
|
+
for (const match of line.matchAll(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi)) {
|
|
324
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "email", "low", 0.88, "email-regex", ["regex"], ["email address format matched"], ctx));
|
|
325
|
+
}
|
|
326
|
+
for (const match of line.matchAll(/\b[A-Z]{5}[0-9]{4}[A-Z]\b/gi)) {
|
|
327
|
+
const value = match[0].toUpperCase();
|
|
328
|
+
if (looksLikePan(value)) {
|
|
329
|
+
matches.push(makeValueMatch(file, line, lineNumber, value, "pan", "regulated_identifier", 0.93, "pan-regex", ["regex"], ["PAN format matched"], ctx));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
for (const match of line.matchAll(/\b[2-9]\d{3}[\s-]?\d{4}[\s-]?\d{4}\b/g)) {
|
|
333
|
+
const confidence = isValidAadhaar(match[0]) ? 0.93 : 0.78;
|
|
334
|
+
if (confidence >= 0.85 || hasContext(lower, [/aadhaar|aadhar|uidai/])) {
|
|
335
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "aadhaar", "regulated_identifier", confidence, "aadhaar-regex", isValidAadhaar(match[0]) ? ["regex", "checksum"] : ["regex", "context"], isValidAadhaar(match[0]) ? ["Aadhaar format and Verhoeff checksum matched"] : ["Aadhaar-like number appeared near Aadhaar context"], ctx));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const match of line.matchAll(/\b(?:\d[ -]?){13,19}\b/g)) {
|
|
339
|
+
const digits = normalizeDigits(match[0]);
|
|
340
|
+
if (passesLuhn(digits)) {
|
|
341
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "payment_card", "regulated_identifier", 0.92, "payment-card-luhn", ["regex", "checksum"], ["payment-card length and Luhn checksum matched"], ctx));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (hasContext(lower, [/passport/])) {
|
|
345
|
+
for (const match of line.matchAll(/\b[A-Z][0-9]{7}\b/gi)) {
|
|
346
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0].toUpperCase(), "passport", "regulated_identifier", 0.84, "passport-context-regex", ["regex", "context"], ["passport-like value appeared near passport context"], ctx));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (hasContext(lower, [/bank|account\s*(number|no)|iban|ifsc/])) {
|
|
350
|
+
for (const match of line.matchAll(/\b\d{9,18}\b/g)) {
|
|
351
|
+
const digits = normalizeDigits(match[0]);
|
|
352
|
+
if (!passesLuhn(digits) && !isValidAadhaar(digits)) {
|
|
353
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "bank_account", "regulated_identifier", 0.78, "bank-account-context-regex", ["regex", "context"], ["long numeric value appeared near bank/account context"], ctx));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (hasContext(lower, [/dob|date of birth|birthdate|birth_date/])) {
|
|
358
|
+
for (const match of line.matchAll(/\b(?:\d{4}-\d{2}-\d{2}|\d{2}[/-]\d{2}[/-]\d{4})\b/g)) {
|
|
359
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "date_of_birth", "moderate", 0.78, "dob-context-regex", ["regex", "context"], ["date appeared near date-of-birth context"], ctx));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
for (const match of line.matchAll(/\b(?:\d{1,3}\.){3}\d{1,3}\b/g)) {
|
|
363
|
+
if (isPublicIp(match[0]) || hasContext(lower, [/client[_\s-]?ip|ip[_\s-]?address|remote[_\s-]?addr/])) {
|
|
364
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "ip_address", "moderate", isPublicIp(match[0]) ? 0.76 : 0.66, "ip-regex", ["regex"], ["IP address format matched"], ctx));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const match of line.matchAll(/(?:\+\d{1,3}[-.\s]?)?(?:\(?\d{3,5}\)?[-.\s]?){2,}\d{3,4}/g)) {
|
|
368
|
+
const digits = normalizeDigits(match[0]);
|
|
369
|
+
if (digits.length < 10 || digits.length > 14 || passesLuhn(digits) || isValidAadhaar(digits)) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const contextual = hasContext(lower, [/phone|mobile|telephone|contact/]);
|
|
373
|
+
matches.push(makeValueMatch(file, line, lineNumber, match[0], "phone", "moderate", contextual ? 0.78 : 0.56, "phone-regex", contextual ? ["regex", "context"] : ["regex"], contextual ? ["phone-like value appeared near phone context"] : ["phone-like value matched without strong context"], ctx));
|
|
374
|
+
}
|
|
375
|
+
return matches;
|
|
376
|
+
}
|
|
377
|
+
function combinationRisk(file, fields) {
|
|
378
|
+
const groups = new Map();
|
|
379
|
+
for (const field of fields) {
|
|
380
|
+
const normalized = field.normalized;
|
|
381
|
+
if (/age|dob|dateofbirth|birthdate/.test(normalized)) {
|
|
382
|
+
groups.set("age_or_dob", [...(groups.get("age_or_dob") ?? []), field.field]);
|
|
383
|
+
}
|
|
384
|
+
if (/gender|sex/.test(normalized)) {
|
|
385
|
+
groups.set("gender", [...(groups.get("gender") ?? []), field.field]);
|
|
386
|
+
}
|
|
387
|
+
if (/location|site|city|branch|address|latitude|longitude|gps/.test(normalized)) {
|
|
388
|
+
groups.set("location", [...(groups.get("location") ?? []), field.field]);
|
|
389
|
+
}
|
|
390
|
+
if (/designation|role|title|department|team|job/.test(normalized)) {
|
|
391
|
+
groups.set("role_or_department", [...(groups.get("role_or_department") ?? []), field.field]);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (!groups.has("age_or_dob") || groups.size < 3) {
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
const reasons = Array.from(groups.entries()).map(([group, names]) => `${group}: ${Array.from(new Set(names)).slice(0, 4).join(", ")}`);
|
|
398
|
+
return {
|
|
399
|
+
entityType: "combination_risk",
|
|
400
|
+
sensitivity: "combination_risk",
|
|
401
|
+
confidenceScore: 0.78,
|
|
402
|
+
title: "Combination-risk personal data fields",
|
|
403
|
+
message: "Multiple fields together may raise re-identification risk.",
|
|
404
|
+
file,
|
|
405
|
+
line: fields[0]?.line,
|
|
406
|
+
matchedPattern: "combination-risk-fields",
|
|
407
|
+
detectionPath: ["metadata", "combination-risk"],
|
|
408
|
+
reasons
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
function toFinding(match) {
|
|
412
|
+
const confidence = confidenceFromScore(match.confidenceScore);
|
|
413
|
+
return {
|
|
414
|
+
id: `privacy:${match.file}:${match.line ?? 0}:${match.entityType}:${match.matchedPattern}`,
|
|
415
|
+
source: "privacy-detector",
|
|
416
|
+
category: "privacy",
|
|
417
|
+
severity: match.sensitivity === "low" ? "low" : "medium",
|
|
418
|
+
confidence,
|
|
419
|
+
title: match.title,
|
|
420
|
+
message: match.message,
|
|
421
|
+
file: match.file,
|
|
422
|
+
startLine: match.line,
|
|
423
|
+
isNew: true,
|
|
424
|
+
isAutofixable: false,
|
|
425
|
+
safeToAutofix: false,
|
|
426
|
+
agentInstruction: match.confidenceScore >= 0.85
|
|
427
|
+
? "Verify whether this personal data belongs in the repository. Remove, anonymize, or move it to controlled test data if not required."
|
|
428
|
+
: "Review the privacy signal before treating it as confirmed PII.",
|
|
429
|
+
tags: ["privacy", match.entityType, match.sensitivity],
|
|
430
|
+
evidence: {
|
|
431
|
+
snippet: match.snippet,
|
|
432
|
+
detector: "privacy-detector",
|
|
433
|
+
entityType: match.entityType,
|
|
434
|
+
sensitivity: match.sensitivity,
|
|
435
|
+
maskedValue: match.maskedValue,
|
|
436
|
+
fieldPath: match.line ? `${match.file}:${match.line}` : match.file,
|
|
437
|
+
matchedPattern: match.matchedPattern,
|
|
438
|
+
detectionPath: match.detectionPath.join(" + "),
|
|
439
|
+
confidenceScore: Number(match.confidenceScore.toFixed(2)),
|
|
440
|
+
coverageConfidence: 0.9,
|
|
441
|
+
reviewState: match.confidenceScore >= 0.85 ? "confirmed" : "needs_human_review",
|
|
442
|
+
reasons: match.reasons
|
|
443
|
+
},
|
|
444
|
+
scoreImpact: 0
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
async function readCandidate(root, file, maxFileBytes) {
|
|
448
|
+
const absolutePath = node_path_1.default.join(root, file);
|
|
449
|
+
const stat = await node_fs_1.promises.stat(absolutePath).catch(() => undefined);
|
|
450
|
+
if (!stat || stat.size > maxFileBytes) {
|
|
451
|
+
return undefined;
|
|
452
|
+
}
|
|
453
|
+
const content = await node_fs_1.promises.readFile(absolutePath, "utf8").catch(() => undefined);
|
|
454
|
+
if (!content || content.includes("\0")) {
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
return content;
|
|
458
|
+
}
|
|
459
|
+
function passesConfidenceThreshold(finding, minimum) {
|
|
460
|
+
return CONFIDENCE_RANK[finding.confidence] >= CONFIDENCE_RANK[minimum];
|
|
461
|
+
}
|
|
462
|
+
async function detectPiiFindings(ctx) {
|
|
463
|
+
const privacyCfg = ctx.config.checks.privacy;
|
|
464
|
+
const candidates = (0, paths_1.filterPaths)(ctx.project.projectFiles, ctx.config.paths.include, ctx.config.paths.exclude).filter(isTextCandidate);
|
|
465
|
+
const findings = [];
|
|
466
|
+
for (const file of candidates) {
|
|
467
|
+
const content = await readCandidate(ctx.root, file, privacyCfg.maxFileBytes);
|
|
468
|
+
if (!content) {
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
const lines = content.split(/\r?\n/);
|
|
472
|
+
const fieldSignals = [];
|
|
473
|
+
for (const [index, line] of lines.entries()) {
|
|
474
|
+
const lineNumber = index + 1;
|
|
475
|
+
const fields = extractFields(file, line, lineNumber);
|
|
476
|
+
fieldSignals.push(...fields);
|
|
477
|
+
const matches = [
|
|
478
|
+
...fields.map((field) => makeFieldMatch(file, line, field, ctx)).filter((match) => Boolean(match)),
|
|
479
|
+
...findValueMatches(file, line, lineNumber, ctx)
|
|
480
|
+
];
|
|
481
|
+
for (const match of matches) {
|
|
482
|
+
findings.push(toFinding(match));
|
|
483
|
+
}
|
|
484
|
+
// Log Sanitization Check
|
|
485
|
+
if (privacyCfg.detectUnsanitizedLogs &&
|
|
486
|
+
/\.(ts|tsx|js|jsx|py)$/.test(file) &&
|
|
487
|
+
/\b(console|logger|log|info|warn|error|debug)\.(log|info|warn|error|debug|trace)\s*\(/i.test(line) &&
|
|
488
|
+
/\b(user|customer|member|client|patient|email|phone|password|address|dob|pan|aadhaar)\b/i.test(line) &&
|
|
489
|
+
!/\b(redact|mask|safe|anon|hide|hash|encrypt|obfuscate)\b/i.test(line)) {
|
|
490
|
+
findings.push({
|
|
491
|
+
id: `privacy:unsanitized-log:${file}:${lineNumber}`,
|
|
492
|
+
source: "privacy-detector",
|
|
493
|
+
category: "privacy",
|
|
494
|
+
severity: "medium",
|
|
495
|
+
confidence: "medium",
|
|
496
|
+
title: "Potential unsanitized PII in logger call",
|
|
497
|
+
message: "Potential unsanitized variable passed to logger. Consider wrapping it in a redaction helper.",
|
|
498
|
+
file,
|
|
499
|
+
startLine: lineNumber,
|
|
500
|
+
isNew: true,
|
|
501
|
+
isAutofixable: false,
|
|
502
|
+
safeToAutofix: false,
|
|
503
|
+
agentInstruction: "Configure log serialization to redact PII keys or wrap this log call in a redaction helper (e.g., redact(user)).",
|
|
504
|
+
tags: ["privacy", "logging", "leaks"],
|
|
505
|
+
evidence: {
|
|
506
|
+
detector: "privacy-detector",
|
|
507
|
+
reasons: ["Logger method call detected on same line as PII keyword", "No redaction function detected"]
|
|
508
|
+
},
|
|
509
|
+
scoreImpact: 5
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
// API Over-fetching Check
|
|
513
|
+
if (privacyCfg.detectApiOverfetching && /\.(ts|tsx|js|jsx|py)$/.test(file)) {
|
|
514
|
+
const hasExpressApi = /\b(res|reply)\.(json|send)\s*\(\s*\b(user|customer|member|client|patient|profile|account)\b\s*\)/i.test(line);
|
|
515
|
+
const hasPythonApi = /\bjsonify\s*\(\s*\b(user|customer|member|client|patient|profile|account)\b\s*\)/i.test(line);
|
|
516
|
+
if (hasExpressApi || hasPythonApi) {
|
|
517
|
+
findings.push({
|
|
518
|
+
id: `privacy:api-overfetching:${file}:${lineNumber}`,
|
|
519
|
+
source: "privacy-detector",
|
|
520
|
+
category: "privacy",
|
|
521
|
+
severity: "medium",
|
|
522
|
+
confidence: "medium",
|
|
523
|
+
title: "Potential API over-fetching",
|
|
524
|
+
message: "Direct serialization of user/customer entity in API response. This may leak sensitive fields.",
|
|
525
|
+
file,
|
|
526
|
+
startLine: lineNumber,
|
|
527
|
+
isNew: true,
|
|
528
|
+
isAutofixable: false,
|
|
529
|
+
safeToAutofix: false,
|
|
530
|
+
agentInstruction: "Do not serialize ORM models directly in API responses. Define a strict projection select list or wrap the output in a safe Data Transfer Object (DTO) class.",
|
|
531
|
+
tags: ["privacy", "api", "overfetching"],
|
|
532
|
+
evidence: {
|
|
533
|
+
detector: "privacy-detector",
|
|
534
|
+
reasons: ["Direct model serialization pattern matched in routing code"]
|
|
535
|
+
},
|
|
536
|
+
scoreImpact: 10
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
const combination = combinationRisk(file, fieldSignals);
|
|
542
|
+
if (combination) {
|
|
543
|
+
findings.push(toFinding(combination));
|
|
544
|
+
}
|
|
545
|
+
// Database Retention Check
|
|
546
|
+
if (privacyCfg.detectMissingRetention && file.endsWith(".prisma")) {
|
|
547
|
+
const modelBlocks = content.match(/model\s+(\w+)\s*\{([^}]*)\}/g);
|
|
548
|
+
if (modelBlocks) {
|
|
549
|
+
for (const block of modelBlocks) {
|
|
550
|
+
const modelNameMatch = block.match(/model\s+(\w+)/);
|
|
551
|
+
const modelName = modelNameMatch ? modelNameMatch[1] : "Unknown";
|
|
552
|
+
const blockLines = block.split("\n");
|
|
553
|
+
const hasPii = blockLines.some((l) => /\b(email|phone|ssn|password|address|dob|pan|aadhaar)\b/i.test(l));
|
|
554
|
+
const hasRetention = blockLines.some((l) => /\b(deletedAt|deleted_at|expiresAt|expires_at|ttl)\b/i.test(l));
|
|
555
|
+
if (hasPii && !hasRetention) {
|
|
556
|
+
const blockStartIndex = content.indexOf(block);
|
|
557
|
+
const lineOffset = content.slice(0, blockStartIndex).split("\n").length;
|
|
558
|
+
findings.push({
|
|
559
|
+
id: `privacy:missing-retention:${file}:${modelName}`,
|
|
560
|
+
source: "privacy-detector",
|
|
561
|
+
category: "privacy",
|
|
562
|
+
severity: "medium",
|
|
563
|
+
confidence: "medium",
|
|
564
|
+
title: "Database table storing PII lacks retention fields",
|
|
565
|
+
message: `Database model '${modelName}' stores personal data but has no soft-delete (deletedAt) or expiration (ttl) field.`,
|
|
566
|
+
file,
|
|
567
|
+
startLine: lineOffset,
|
|
568
|
+
isNew: true,
|
|
569
|
+
isAutofixable: false,
|
|
570
|
+
safeToAutofix: false,
|
|
571
|
+
agentInstruction: "Add a soft-delete (deletedAt) column or database TTL configuration to enforce data retention limits.",
|
|
572
|
+
tags: ["privacy", "database", "retention"],
|
|
573
|
+
evidence: {
|
|
574
|
+
detector: "privacy-detector",
|
|
575
|
+
reasons: [`Model '${modelName}' contains PII attributes`, `No retention columns (deletedAt/ttl) found in model definition`]
|
|
576
|
+
},
|
|
577
|
+
scoreImpact: 10
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return findings.filter((finding) => passesConfidenceThreshold(finding, privacyCfg.minConfidenceToReport));
|
|
585
|
+
}
|
|
586
|
+
exports.privacyDetectorAdapter = {
|
|
587
|
+
id: "privacy-detector",
|
|
588
|
+
category: "privacy",
|
|
589
|
+
async detect(_project, config) {
|
|
590
|
+
return config.checks.privacy.enabled;
|
|
591
|
+
},
|
|
592
|
+
async runStandalone(ctx) {
|
|
593
|
+
const findings = await detectPiiFindings(ctx);
|
|
594
|
+
return { findings };
|
|
595
|
+
},
|
|
596
|
+
installHint: "Built-in deterministic privacy detector. No external install needed."
|
|
597
|
+
};
|
|
598
|
+
function normalizeExternalFile(file, root) {
|
|
599
|
+
return (0, finding_1.normalizeFilePath)(file, root);
|
|
600
|
+
}
|