eslint-plugin-no-pii 1.0.0 → 1.2.0
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 +42 -6
- package/dist/index.js +46 -2
- package/dist/internal/create-pii-rule.d.ts +8 -2
- package/dist/internal/create-pii-rule.js +16 -6
- package/dist/internal/luhn.d.ts +2 -0
- package/dist/internal/luhn.js +13 -0
- package/dist/rules/no-credit-card.d.ts +2 -0
- package/dist/rules/no-credit-card.js +11 -0
- package/dist/rules/no-crypto-wallet.d.ts +2 -0
- package/dist/rules/no-crypto-wallet.js +68 -0
- package/dist/rules/no-iban.d.ts +2 -0
- package/dist/rules/no-iban.js +109 -0
- package/dist/rules/no-imei.d.ts +2 -0
- package/dist/rules/no-imei.js +16 -0
- package/dist/rules/no-ip.js +20 -4
- package/dist/rules/no-jwt.d.ts +2 -0
- package/dist/rules/no-jwt.js +21 -0
- package/dist/rules/no-mac-address.d.ts +2 -0
- package/dist/rules/no-mac-address.js +12 -0
- package/dist/rules/no-national-id.d.ts +2 -0
- package/dist/rules/no-national-id.js +45 -0
- package/dist/rules/no-pii-identifier.d.ts +8 -0
- package/dist/rules/no-pii-identifier.js +154 -0
- package/dist/rules/no-ssn.d.ts +2 -0
- package/dist/rules/no-ssn.js +10 -0
- package/dist/rules/no-url-credentials.d.ts +2 -0
- package/dist/rules/no-url-credentials.js +13 -0
- package/dist/rules/no-vin.d.ts +2 -0
- package/dist/rules/no-vin.js +51 -0
- package/llms.txt +45 -0
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# eslint-plugin-no-pii
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/eslint-plugin-no-pii)
|
|
4
|
+
[](https://ci.appveyor.com/project/lwojcik/eslint-plugin-no-pii/branch/main)
|
|
5
|
+
[](https://codecov.io/github/lwojcik/eslint-plugin-no-pii)
|
|
6
|
+
|
|
7
|
+
PII (Personally Identifiable Information) linting rules for ESLint 10.
|
|
4
8
|
|
|
5
9
|
Fork of [shiva-hack/eslint-plugin-pii](https://github.com/shiva-hack/eslint-plugin-pii), modernized and updated for ESLint 10+.
|
|
6
10
|
|
|
@@ -29,10 +33,21 @@ export default [
|
|
|
29
33
|
{
|
|
30
34
|
plugins: { pii },
|
|
31
35
|
rules: {
|
|
32
|
-
"pii/no-
|
|
36
|
+
"pii/no-credit-card": "error",
|
|
37
|
+
"pii/no-crypto-wallet": "error",
|
|
33
38
|
"pii/no-dob": "warn",
|
|
39
|
+
"pii/no-email": "error",
|
|
40
|
+
"pii/no-iban": "error",
|
|
41
|
+
"pii/no-imei": "error",
|
|
34
42
|
"pii/no-ip": "error",
|
|
43
|
+
"pii/no-jwt": "error",
|
|
44
|
+
"pii/no-mac-address": "error",
|
|
45
|
+
"pii/no-national-id": "error",
|
|
35
46
|
"pii/no-phone-number": "error",
|
|
47
|
+
"pii/no-pii-identifier": "error",
|
|
48
|
+
"pii/no-ssn": "error",
|
|
49
|
+
"pii/no-url-credentials": "error",
|
|
50
|
+
"pii/no-vin": "error",
|
|
36
51
|
},
|
|
37
52
|
},
|
|
38
53
|
];
|
|
@@ -40,7 +55,28 @@ export default [
|
|
|
40
55
|
|
|
41
56
|
## Supported Rules
|
|
42
57
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
- `pii/no-
|
|
46
|
-
- `pii/no-
|
|
58
|
+
Each rule has its own page with examples, fix guidance and known limits.
|
|
59
|
+
|
|
60
|
+
- [`pii/no-credit-card`](docs/rules/no-credit-card.md) - credit/debit card numbers (Luhn-validated)
|
|
61
|
+
- [`pii/no-crypto-wallet`](docs/rules/no-crypto-wallet.md) - Bitcoin and Ethereum wallet addresses
|
|
62
|
+
- [`pii/no-dob`](docs/rules/no-dob.md) - birth dates
|
|
63
|
+
- [`pii/no-email`](docs/rules/no-email.md) - email addresses
|
|
64
|
+
- [`pii/no-iban`](docs/rules/no-iban.md) - IBANs (length and mod-97 validated)
|
|
65
|
+
- [`pii/no-imei`](docs/rules/no-imei.md) - IMEI numbers (Luhn-validated)
|
|
66
|
+
- [`pii/no-ip`](docs/rules/no-ip.md) - IPv4 and IPv6 addresses
|
|
67
|
+
- [`pii/no-jwt`](docs/rules/no-jwt.md) - JSON Web Tokens
|
|
68
|
+
- [`pii/no-mac-address`](docs/rules/no-mac-address.md) - MAC addresses
|
|
69
|
+
- [`pii/no-national-id`](docs/rules/no-national-id.md) - national ID numbers (Polish PESEL/NIP, UK NINO, Spanish DNI/NIE)
|
|
70
|
+
- [`pii/no-phone-number`](docs/rules/no-phone-number.md) - phone numbers
|
|
71
|
+
- [`pii/no-pii-identifier`](docs/rules/no-pii-identifier.md) - hardcoded values assigned to PII-named identifiers (`ssn`, `password`, …)
|
|
72
|
+
- [`pii/no-ssn`](docs/rules/no-ssn.md) - US Social Security numbers
|
|
73
|
+
- [`pii/no-url-credentials`](docs/rules/no-url-credentials.md) - credentials embedded in URLs and connection strings
|
|
74
|
+
- [`pii/no-vin`](docs/rules/no-vin.md) - vehicle identification numbers
|
|
75
|
+
|
|
76
|
+
## Fixing reports (for humans and AI agents)
|
|
77
|
+
|
|
78
|
+
Every message says what was found and how to fix it. In short:
|
|
79
|
+
|
|
80
|
+
- Don't hardcode the value: load it from env/config/test fixtures, or replace it with a redaction token such as `"[REDACTED email address]"`.
|
|
81
|
+
- `// eslint-disable-line` and `// eslint-disable-next-line` do **not** silence the value-based rules. Don't try to suppress them.
|
|
82
|
+
- If a file legitimately needs such values, allow-list it in `eslint.config.js` (`{ files: [...], rules: { "pii/no-email": "off" } }`) so the exception shows up in review. For `pii/no-pii-identifier`, use its `ignore` option.
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,71 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import noCreditCard from "./rules/no-credit-card.js";
|
|
3
|
+
import noCryptoWallet from "./rules/no-crypto-wallet.js";
|
|
2
4
|
import noDob from "./rules/no-dob.js";
|
|
3
5
|
import noEmail from "./rules/no-email.js";
|
|
6
|
+
import noIban from "./rules/no-iban.js";
|
|
7
|
+
import noImei from "./rules/no-imei.js";
|
|
4
8
|
import noIp from "./rules/no-ip.js";
|
|
9
|
+
import noJwt from "./rules/no-jwt.js";
|
|
10
|
+
import noUrlCredentials from "./rules/no-url-credentials.js";
|
|
11
|
+
import noVin from "./rules/no-vin.js";
|
|
12
|
+
import noSsn from "./rules/no-ssn.js";
|
|
13
|
+
import noMacAddress from "./rules/no-mac-address.js";
|
|
14
|
+
import noNationalId from "./rules/no-national-id.js";
|
|
15
|
+
import noPiiIdentifier from "./rules/no-pii-identifier.js";
|
|
5
16
|
import noPhoneNumber from "./rules/no-phone-number.js";
|
|
6
17
|
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
18
|
+
const DOCS_BASE = "https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules";
|
|
19
|
+
const withDocs = (rules) => Object.fromEntries(Object.entries(rules).map(([name, rule]) => [
|
|
20
|
+
name,
|
|
21
|
+
{
|
|
22
|
+
...rule,
|
|
23
|
+
meta: {
|
|
24
|
+
...rule.meta,
|
|
25
|
+
docs: { ...rule.meta?.docs, url: `${DOCS_BASE}/${name}.md` },
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
]));
|
|
7
29
|
const plugin = {
|
|
8
30
|
meta: { name: pkg.name, version: pkg.version },
|
|
9
|
-
rules: {
|
|
31
|
+
rules: withDocs({
|
|
32
|
+
"no-credit-card": noCreditCard,
|
|
33
|
+
"no-crypto-wallet": noCryptoWallet,
|
|
10
34
|
"no-dob": noDob,
|
|
11
35
|
"no-email": noEmail,
|
|
36
|
+
"no-iban": noIban,
|
|
37
|
+
"no-imei": noImei,
|
|
12
38
|
"no-ip": noIp,
|
|
39
|
+
"no-jwt": noJwt,
|
|
40
|
+
"no-mac-address": noMacAddress,
|
|
41
|
+
"no-national-id": noNationalId,
|
|
13
42
|
"no-phone-number": noPhoneNumber,
|
|
14
|
-
|
|
43
|
+
"no-pii-identifier": noPiiIdentifier,
|
|
44
|
+
"no-ssn": noSsn,
|
|
45
|
+
"no-url-credentials": noUrlCredentials,
|
|
46
|
+
"no-vin": noVin,
|
|
47
|
+
}),
|
|
15
48
|
};
|
|
16
49
|
plugin.configs = {
|
|
17
50
|
recommended: {
|
|
18
51
|
name: "pii/recommended",
|
|
19
52
|
plugins: { pii: plugin },
|
|
20
53
|
rules: {
|
|
54
|
+
"pii/no-credit-card": "error",
|
|
55
|
+
"pii/no-crypto-wallet": "error",
|
|
21
56
|
"pii/no-email": "error",
|
|
22
57
|
"pii/no-dob": "warn",
|
|
58
|
+
"pii/no-iban": "error",
|
|
59
|
+
"pii/no-imei": "error",
|
|
23
60
|
"pii/no-ip": "error",
|
|
61
|
+
"pii/no-jwt": "error",
|
|
62
|
+
"pii/no-mac-address": "error",
|
|
63
|
+
"pii/no-national-id": "error",
|
|
24
64
|
"pii/no-phone-number": "error",
|
|
65
|
+
"pii/no-pii-identifier": "error",
|
|
66
|
+
"pii/no-ssn": "error",
|
|
67
|
+
"pii/no-url-credentials": "error",
|
|
68
|
+
"pii/no-vin": "error",
|
|
25
69
|
},
|
|
26
70
|
},
|
|
27
71
|
};
|
|
@@ -7,9 +7,15 @@ interface PiiRuleOptions {
|
|
|
7
7
|
/** Human readable name used in messages, e.g. "IP address". */
|
|
8
8
|
subject: string;
|
|
9
9
|
/** Non-global patterns (stateless `.test`). */
|
|
10
|
-
patterns
|
|
10
|
+
patterns?: RegExp[];
|
|
11
|
+
/** Custom detector for checks a regex can't express (e.g. checksums). */
|
|
12
|
+
detect?: (value: string) => boolean;
|
|
11
13
|
/** Also test `<identifier> <literal>` pairs. */
|
|
12
14
|
checkDeclarators?: boolean;
|
|
15
|
+
/** Also test the static text parts of template literals. */
|
|
16
|
+
checkTemplateLiterals?: boolean;
|
|
17
|
+
/** Rule-specific advice for literals; replaces the generic redaction hint. */
|
|
18
|
+
literalFix?: string;
|
|
13
19
|
}
|
|
14
|
-
export declare function createPiiRule({ description, subject, patterns, checkDeclarators, }: PiiRuleOptions): Rule.RuleModule;
|
|
20
|
+
export declare function createPiiRule({ description, subject, patterns, detect, checkDeclarators, checkTemplateLiterals, literalFix, }: PiiRuleOptions): Rule.RuleModule;
|
|
15
21
|
export {};
|
|
@@ -2,23 +2,26 @@
|
|
|
2
2
|
* Shared factory for rules that flag PII patterns in comments and string literals.
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* Report location that ignores `eslint-disable`
|
|
6
|
-
* report to be treated as covering the whole start line).
|
|
5
|
+
* Report location that ignores `eslint-disable-line` / `-next-line` directives
|
|
6
|
+
* (column -1 forces the report to be treated as covering the whole start line).
|
|
7
|
+
* Block `/* eslint-disable *\/` ranges still apply.
|
|
7
8
|
*/
|
|
8
9
|
const toForceLocation = (loc) => ({
|
|
9
10
|
start: { line: loc.start.line, column: -1 },
|
|
10
11
|
end: loc.end,
|
|
11
12
|
});
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const NO_SUPPRESS = "eslint-disable-line and eslint-disable-next-line do not silence this rule; do not try to suppress it.";
|
|
14
|
+
export function createPiiRule({ description, subject, patterns = [], detect, checkDeclarators = false, checkTemplateLiterals = false, literalFix, }) {
|
|
15
|
+
const matches = (value) => patterns.some((pattern) => pattern.test(value)) ||
|
|
16
|
+
(detect?.(value) ?? false);
|
|
14
17
|
return {
|
|
15
18
|
meta: {
|
|
16
19
|
type: "problem",
|
|
17
20
|
docs: { description },
|
|
18
21
|
schema: [],
|
|
19
22
|
messages: {
|
|
20
|
-
comment: `PII Violation: cannot use ${subject} in a comment`,
|
|
21
|
-
literal: `PII Violation: cannot use ${subject} in a literal`,
|
|
23
|
+
comment: `PII Violation: cannot use ${subject} in a comment. Remove it or replace it with "[REDACTED ${subject}]". ${NO_SUPPRESS}`,
|
|
24
|
+
literal: `PII Violation: cannot use ${subject} in a literal. ${literalFix ?? `Load it from env/config/test fixtures, or replace it with "[REDACTED ${subject}]".`} ${NO_SUPPRESS}`,
|
|
22
25
|
},
|
|
23
26
|
},
|
|
24
27
|
create(context) {
|
|
@@ -40,6 +43,13 @@ export function createPiiRule({ description, subject, patterns, checkDeclarators
|
|
|
40
43
|
report(node.loc, "literal");
|
|
41
44
|
}
|
|
42
45
|
},
|
|
46
|
+
...(checkTemplateLiterals && {
|
|
47
|
+
TemplateElement(node) {
|
|
48
|
+
if (node.value.cooked && matches(node.value.cooked)) {
|
|
49
|
+
report(node.loc, "literal");
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
43
53
|
...(checkDeclarators && {
|
|
44
54
|
VariableDeclarator(node) {
|
|
45
55
|
if (node.id.type !== "Identifier")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Luhn checksum over a string of ASCII digits. */
|
|
2
|
+
export function luhn(digits) {
|
|
3
|
+
let sum = 0;
|
|
4
|
+
let double = false;
|
|
5
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
6
|
+
let n = digits.charCodeAt(i) - 48;
|
|
7
|
+
if (double)
|
|
8
|
+
n *= 2;
|
|
9
|
+
sum += n > 9 ? n - 9 : n;
|
|
10
|
+
double = !double;
|
|
11
|
+
}
|
|
12
|
+
return sum % 10 === 0;
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
import { luhn } from "../internal/luhn.js";
|
|
3
|
+
// 13-19 digits, optionally separated by single spaces or dashes.
|
|
4
|
+
const CANDIDATE = /(?<![\d-])\d(?:[ -]?\d){12,18}(?![\d-])/g;
|
|
5
|
+
// Major issuer prefixes: Visa, Mastercard, Amex, Discover, Diners, JCB, UnionPay.
|
|
6
|
+
const ISSUER = /^(?:4|5[1-5]|2(?:2[2-9]|[3-6]\d|7[01]|720)|3[47]|6011|65|64[4-9]|30[0-5]|36|38|35(?:2[89]|[3-8]\d)|62)/;
|
|
7
|
+
export default createPiiRule({
|
|
8
|
+
description: "enforces no credit card numbers in comments or string literals",
|
|
9
|
+
subject: "credit card number",
|
|
10
|
+
detect: (value) => Array.from(value.matchAll(CANDIDATE), (m) => m[0].replace(/[ -]/g, "")).some((digits) => ISSUER.test(digits) && luhn(digits)),
|
|
11
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
3
|
+
// --- Bitcoin legacy (P2PKH `1...`, P2SH `3...`): Base58Check ----------------
|
|
4
|
+
const BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
5
|
+
const BASE58_ADDRESS = /(?<![A-Za-z\d])[13][1-9A-HJ-NP-Za-km-z]{25,34}(?![A-Za-z\d])/g;
|
|
6
|
+
const sha256 = (data) => createHash("sha256").update(data).digest();
|
|
7
|
+
function isBase58CheckAddress(address) {
|
|
8
|
+
let n = 0n;
|
|
9
|
+
for (const char of address)
|
|
10
|
+
n = n * 58n + BigInt(BASE58.indexOf(char));
|
|
11
|
+
const hex = n.toString(16);
|
|
12
|
+
const leadingZeros = address.length - address.replace(/^1+/, "").length;
|
|
13
|
+
const bytes = Buffer.concat([
|
|
14
|
+
Buffer.alloc(leadingZeros),
|
|
15
|
+
Buffer.from(hex.length % 2 ? `0${hex}` : hex, "hex"),
|
|
16
|
+
]);
|
|
17
|
+
if (bytes.length !== 25 || (bytes[0] !== 0x00 && bytes[0] !== 0x05)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const checksum = sha256(sha256(bytes.subarray(0, 21))).subarray(0, 4);
|
|
21
|
+
return checksum.equals(bytes.subarray(21));
|
|
22
|
+
}
|
|
23
|
+
// --- Bitcoin SegWit/Taproot (`bc1...`): Bech32 / Bech32m --------------------
|
|
24
|
+
const BECH32_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
|
25
|
+
const BECH32_ADDRESS = /(?<![a-z\d])bc1[ac-hj-np-z02-9]{11,87}(?![a-z\d])/gi;
|
|
26
|
+
const BECH32_GENERATOR = [
|
|
27
|
+
0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3,
|
|
28
|
+
];
|
|
29
|
+
const BECH32_CONST = 1;
|
|
30
|
+
const BECH32M_CONST = 0x2bc830a3;
|
|
31
|
+
function bech32Polymod(values) {
|
|
32
|
+
let checksum = 1;
|
|
33
|
+
for (const value of values) {
|
|
34
|
+
const top = checksum >> 25;
|
|
35
|
+
checksum = ((checksum & 0x1ffffff) << 5) ^ value;
|
|
36
|
+
BECH32_GENERATOR.forEach((generator, i) => {
|
|
37
|
+
if ((top >> i) & 1)
|
|
38
|
+
checksum ^= generator;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return checksum;
|
|
42
|
+
}
|
|
43
|
+
function isBech32Address(address) {
|
|
44
|
+
// Mixed case is invalid.
|
|
45
|
+
if (address !== address.toLowerCase() && address !== address.toUpperCase()) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const lower = address.toLowerCase();
|
|
49
|
+
const data = Array.from(lower.slice(3), (char) => BECH32_CHARSET.indexOf(char));
|
|
50
|
+
// HRP "bc" expanded: high bits, 0, low bits.
|
|
51
|
+
const checksum = bech32Polymod([3, 3, 0, 2, 3, ...data]);
|
|
52
|
+
return checksum === BECH32_CONST || checksum === BECH32M_CONST;
|
|
53
|
+
}
|
|
54
|
+
// --- Ethereum and EVM chains: 0x + 40 hex -----------------------------------
|
|
55
|
+
// No checksum is verified (EIP-55 needs Keccak-256, which node:crypto lacks).
|
|
56
|
+
const ETHEREUM_ADDRESS = /(?<![\w])0x[\da-f]{40}(?![\w])/gi;
|
|
57
|
+
function isEthereumAddress(address) {
|
|
58
|
+
// Drop placeholders such as 0x000...000 and 0xfff...fff.
|
|
59
|
+
return !/^0x(.)\1*$/i.test(address);
|
|
60
|
+
}
|
|
61
|
+
const matches = (value, pattern) => Array.from(value.matchAll(pattern), (m) => m[0]);
|
|
62
|
+
export default createPiiRule({
|
|
63
|
+
description: "enforces no crypto wallet addresses in comments or string literals",
|
|
64
|
+
subject: "crypto wallet address",
|
|
65
|
+
detect: (value) => matches(value, BASE58_ADDRESS).some(isBase58CheckAddress) ||
|
|
66
|
+
matches(value, BECH32_ADDRESS).some(isBech32Address) ||
|
|
67
|
+
matches(value, ETHEREUM_ADDRESS).some(isEthereumAddress),
|
|
68
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
// Total IBAN length per country (ISO 13616 registry).
|
|
3
|
+
const IBAN_LENGTHS = {
|
|
4
|
+
AD: 24,
|
|
5
|
+
AE: 23,
|
|
6
|
+
AL: 28,
|
|
7
|
+
AT: 20,
|
|
8
|
+
AZ: 28,
|
|
9
|
+
BA: 20,
|
|
10
|
+
BE: 16,
|
|
11
|
+
BG: 22,
|
|
12
|
+
BH: 22,
|
|
13
|
+
BR: 29,
|
|
14
|
+
BY: 28,
|
|
15
|
+
CH: 21,
|
|
16
|
+
CR: 22,
|
|
17
|
+
CY: 28,
|
|
18
|
+
CZ: 24,
|
|
19
|
+
DE: 22,
|
|
20
|
+
DK: 18,
|
|
21
|
+
DO: 28,
|
|
22
|
+
EE: 20,
|
|
23
|
+
EG: 29,
|
|
24
|
+
ES: 24,
|
|
25
|
+
FI: 18,
|
|
26
|
+
FO: 18,
|
|
27
|
+
FR: 27,
|
|
28
|
+
GB: 22,
|
|
29
|
+
GE: 22,
|
|
30
|
+
GI: 23,
|
|
31
|
+
GL: 18,
|
|
32
|
+
GR: 27,
|
|
33
|
+
GT: 28,
|
|
34
|
+
HR: 21,
|
|
35
|
+
HU: 28,
|
|
36
|
+
IE: 22,
|
|
37
|
+
IL: 23,
|
|
38
|
+
IQ: 23,
|
|
39
|
+
IS: 26,
|
|
40
|
+
IT: 27,
|
|
41
|
+
JO: 30,
|
|
42
|
+
KW: 30,
|
|
43
|
+
KZ: 20,
|
|
44
|
+
LB: 28,
|
|
45
|
+
LC: 32,
|
|
46
|
+
LI: 21,
|
|
47
|
+
LT: 20,
|
|
48
|
+
LU: 20,
|
|
49
|
+
LV: 21,
|
|
50
|
+
MC: 27,
|
|
51
|
+
MD: 24,
|
|
52
|
+
ME: 22,
|
|
53
|
+
MK: 19,
|
|
54
|
+
MR: 27,
|
|
55
|
+
MT: 31,
|
|
56
|
+
MU: 30,
|
|
57
|
+
NL: 18,
|
|
58
|
+
NO: 15,
|
|
59
|
+
PK: 24,
|
|
60
|
+
PL: 28,
|
|
61
|
+
PS: 29,
|
|
62
|
+
PT: 25,
|
|
63
|
+
QA: 29,
|
|
64
|
+
RO: 24,
|
|
65
|
+
RS: 22,
|
|
66
|
+
SA: 24,
|
|
67
|
+
SC: 31,
|
|
68
|
+
SE: 24,
|
|
69
|
+
SI: 19,
|
|
70
|
+
SK: 24,
|
|
71
|
+
SM: 27,
|
|
72
|
+
ST: 25,
|
|
73
|
+
SV: 28,
|
|
74
|
+
TL: 23,
|
|
75
|
+
TN: 24,
|
|
76
|
+
TR: 26,
|
|
77
|
+
UA: 29,
|
|
78
|
+
VA: 22,
|
|
79
|
+
VG: 24,
|
|
80
|
+
XK: 20,
|
|
81
|
+
};
|
|
82
|
+
// Country code + check digits, then up to 30 alphanumerics, optionally
|
|
83
|
+
// space-separated (usually in groups of four).
|
|
84
|
+
const CANDIDATE = /(?<![A-Z0-9])[A-Z]{2}\d{2}(?: ?[A-Z0-9]){11,30}/g;
|
|
85
|
+
/** ISO 7064 mod 97-10: move the first four chars to the end, letters -> 10..35. */
|
|
86
|
+
function mod97(iban) {
|
|
87
|
+
const rearranged = iban.slice(4) + iban.slice(0, 4);
|
|
88
|
+
let remainder = 0;
|
|
89
|
+
for (const char of rearranged) {
|
|
90
|
+
const code = char.charCodeAt(0);
|
|
91
|
+
const digits = code >= 65 ? String(code - 55) : char;
|
|
92
|
+
for (const digit of digits)
|
|
93
|
+
remainder = (remainder * 10 + +digit) % 97;
|
|
94
|
+
}
|
|
95
|
+
return remainder;
|
|
96
|
+
}
|
|
97
|
+
function isIban(compact) {
|
|
98
|
+
const length = IBAN_LENGTHS[compact.slice(0, 2)];
|
|
99
|
+
if (!length || compact.length < length)
|
|
100
|
+
return false;
|
|
101
|
+
// A greedy candidate may run into trailing uppercase words; trim to length.
|
|
102
|
+
const iban = compact.slice(0, length);
|
|
103
|
+
return mod97(iban) === 1;
|
|
104
|
+
}
|
|
105
|
+
export default createPiiRule({
|
|
106
|
+
description: "enforces no IBAN in comments or string literals",
|
|
107
|
+
subject: "IBAN",
|
|
108
|
+
detect: (value) => Array.from(value.matchAll(CANDIDATE), (m) => m[0].replace(/ /g, "")).some(isIban),
|
|
109
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
import { luhn } from "../internal/luhn.js";
|
|
3
|
+
// 15 digits, bare or grouped as AA-BBBBBB-CCCCCC-D / BBBBBB-AA-CCCCCC-D.
|
|
4
|
+
const CANDIDATE = /(?<![\d-])(?:\d{15}|\d{2}[- ]\d{6}[- ]\d{6}[- ]\d|\d{6}[- ]\d{2}[- ]\d{6}[- ]\d)(?![\d-])/g;
|
|
5
|
+
// Explicitly labelled: `IMEI: 490154203237518`.
|
|
6
|
+
const LABELLED = /\bIMEI\b[\s:#=-]{0,3}(\d{15})(?!\d)/gi;
|
|
7
|
+
// GSMA Reporting Body Identifiers: the first two digits of the TAC.
|
|
8
|
+
const REPORTING_BODY = /^(?:00|01|10|30|33|35|44|45|49|50|51|52|53|54|86|91|98|99)/;
|
|
9
|
+
export default createPiiRule({
|
|
10
|
+
description: "enforces no IMEI in comments or string literals",
|
|
11
|
+
subject: "IMEI",
|
|
12
|
+
detect: (value) =>
|
|
13
|
+
// Unlabelled 15-digit numbers are common, so also require a known TAC prefix.
|
|
14
|
+
Array.from(value.matchAll(CANDIDATE), (m) => m[0].replace(/[- ]/g, "")).some((digits) => REPORTING_BODY.test(digits) && luhn(digits)) ||
|
|
15
|
+
Array.from(value.matchAll(LABELLED), (m) => m[1]).some(luhn),
|
|
16
|
+
});
|
package/dist/rules/no-ip.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
+
import { isIPv6 } from "node:net";
|
|
1
2
|
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
3
|
+
// Maximal runs of hex digits, colons and dots (with an optional `%zone`) that
|
|
4
|
+
// are not glued to other word characters. This keeps `Foo::bar` and
|
|
5
|
+
// `00:1A:2B:3C:4D:5E` from being cut into IPv6-looking pieces.
|
|
6
|
+
const IPV6_CANDIDATE = /(?<![\w:.])[\da-f:.]*:[\da-f:.]*(?:%[\w.-]+)?(?![\w:.])/gi;
|
|
7
|
+
function isIPv6Address(candidate) {
|
|
8
|
+
// Needs at least one hex digit, so a bare "::" separator is not an address.
|
|
9
|
+
if (!/[\da-f]/i.test(candidate))
|
|
10
|
+
return false;
|
|
11
|
+
// Also try without punctuation that follows an address in prose:
|
|
12
|
+
// "reach 2001:db8::1." or "2001:db8::1: refused".
|
|
13
|
+
const withoutDots = candidate.replace(/\.+$/, "");
|
|
14
|
+
return [
|
|
15
|
+
candidate,
|
|
16
|
+
withoutDots,
|
|
17
|
+
withoutDots.endsWith("::") ? withoutDots : withoutDots.replace(/:$/, ""),
|
|
18
|
+
].some((value) => isIPv6(value));
|
|
19
|
+
}
|
|
2
20
|
export default createPiiRule({
|
|
3
21
|
description: "enforces no ip address in comments or string literals",
|
|
4
22
|
subject: "IP address",
|
|
5
|
-
patterns: [
|
|
6
|
-
|
|
7
|
-
/\b([\d\w]{4}|0)(:([\d\w]{4}|0)){7}\b/,
|
|
8
|
-
],
|
|
23
|
+
patterns: [/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/],
|
|
24
|
+
detect: (value) => Array.from(value.matchAll(IPV6_CANDIDATE), (m) => m[0]).some(isIPv6Address),
|
|
9
25
|
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
// header.payload[.signature]; both JSON segments start with `eyJ` (`{"`).
|
|
3
|
+
// The signature may be empty for unsecured tokens (`alg: none`).
|
|
4
|
+
const CANDIDATE = /(?<![\w-])(eyJ[\w-]{4,})\.(eyJ[\w-]{4,})(?:\.[\w-]*)?(?![\w-])/g;
|
|
5
|
+
const decodeObject = (segment) => {
|
|
6
|
+
try {
|
|
7
|
+
const json = JSON.parse(Buffer.from(segment, "base64url").toString("utf8"));
|
|
8
|
+
return typeof json === "object" && json !== null && !Array.isArray(json)
|
|
9
|
+
? json
|
|
10
|
+
: undefined;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export default createPiiRule({
|
|
17
|
+
description: "enforces no JSON Web Tokens in comments or string literals",
|
|
18
|
+
subject: "JWT",
|
|
19
|
+
detect: (value) => Array.from(value.matchAll(CANDIDATE)).some(([, header, payload]) => typeof decodeObject(header)?.["alg"] === "string" &&
|
|
20
|
+
decodeObject(payload) !== undefined),
|
|
21
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
export default createPiiRule({
|
|
3
|
+
description: "enforces no MAC address in comments or string literals",
|
|
4
|
+
subject: "MAC address",
|
|
5
|
+
patterns: [
|
|
6
|
+
// AA:BB:CC:DD:EE:FF or AA-BB-CC-DD-EE-FF (one consistent separator).
|
|
7
|
+
// Lookarounds keep it from matching inside longer hex/colon runs (IPv6, EUI-64).
|
|
8
|
+
/(?<![\da-f:-])[\da-f]{2}([:-])(?:[\da-f]{2}\1){4}[\da-f]{2}(?![\da-f:-])/i,
|
|
9
|
+
// Cisco notation: AABB.CCDD.EEFF
|
|
10
|
+
/(?<![\da-f.])[\da-f]{4}\.[\da-f]{4}\.[\da-f]{4}(?![\da-f.])/i,
|
|
11
|
+
],
|
|
12
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
const digitsOf = (value) => Array.from(value, Number);
|
|
3
|
+
const weightedSum = (digits, weights) => weights.reduce((sum, weight, i) => sum + weight * digits[i], 0);
|
|
4
|
+
// --- Poland: PESEL (11 digits, weighted checksum, encoded birth date) -------
|
|
5
|
+
const PESEL = /(?<![\d-])\d{11}(?![\d-])/g;
|
|
6
|
+
function isPesel(value) {
|
|
7
|
+
const digits = digitsOf(value);
|
|
8
|
+
const month = (digits[2] * 10 + digits[3]) % 20;
|
|
9
|
+
const day = digits[4] * 10 + digits[5];
|
|
10
|
+
if (month < 1 || month > 12 || day < 1 || day > 31)
|
|
11
|
+
return false;
|
|
12
|
+
const check = (10 - (weightedSum(digits, [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]) % 10)) % 10;
|
|
13
|
+
return check === digits[10];
|
|
14
|
+
}
|
|
15
|
+
// --- Poland: NIP (10 digits, mod 11) ----------------------------------------
|
|
16
|
+
// Bare 10-digit numbers are too common (e.g. epoch seconds), so require
|
|
17
|
+
// dash-separated groups or an explicit NIP / PL prefix.
|
|
18
|
+
const NIP_FORMATTED = /(?<![\d-])\d{3}(?:-\d{3}-\d{2}-\d{2}|-\d{2}-\d{2}-\d{3})(?![\d-])/g;
|
|
19
|
+
const NIP_PREFIXED = /\b(?:NIP|PL)[\s:.-]{0,3}(\d{10})(?!\d)/gi;
|
|
20
|
+
function isNip(value) {
|
|
21
|
+
const digits = digitsOf(value);
|
|
22
|
+
const check = weightedSum(digits, [6, 5, 7, 2, 3, 4, 5, 6, 7]) % 11;
|
|
23
|
+
return check !== 10 && check === digits[9];
|
|
24
|
+
}
|
|
25
|
+
// --- UK: National Insurance number (format rules only, no checksum) ---------
|
|
26
|
+
const NINO = /(?<![A-Z0-9])(?!BG|GB|KN|NK|NT|TN|ZZ)[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?\d{2} ?\d{2} ?\d{2} ?[A-D](?![A-Z0-9])/;
|
|
27
|
+
// --- Spain: DNI (8 digits + letter) and NIE (X/Y/Z + 7 digits + letter) -----
|
|
28
|
+
const DNI_NIE = /(?<![A-Z\d])([XYZ]\d{7}|\d{8})[ -]?([A-Z])(?![A-Z\d])/gi;
|
|
29
|
+
const DNI_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE";
|
|
30
|
+
function isDniNie(body, letter) {
|
|
31
|
+
const number = Number(body.replace(/^[XYZ]/i, (c) => "XYZ".indexOf(c.toUpperCase()).toString()));
|
|
32
|
+
return DNI_LETTERS[number % 23] === letter.toUpperCase();
|
|
33
|
+
}
|
|
34
|
+
function detect(value) {
|
|
35
|
+
return (Array.from(value.matchAll(PESEL), (m) => m[0]).some(isPesel) ||
|
|
36
|
+
Array.from(value.matchAll(NIP_FORMATTED), (m) => m[0].replace(/-/g, "")).some(isNip) ||
|
|
37
|
+
Array.from(value.matchAll(NIP_PREFIXED), (m) => m[1]).some(isNip) ||
|
|
38
|
+
NINO.test(value) ||
|
|
39
|
+
Array.from(value.matchAll(DNI_NIE)).some((m) => isDniNie(m[1], m[2])));
|
|
40
|
+
}
|
|
41
|
+
export default createPiiRule({
|
|
42
|
+
description: "enforces no national ID numbers in comments or string literals",
|
|
43
|
+
subject: "national ID number",
|
|
44
|
+
detect,
|
|
45
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flags identifiers whose *name* suggests PII (`ssn`, `password`, `dateOfBirth`,
|
|
3
|
+
* ...) when they are assigned a hardcoded value. Catches PII that the value-based
|
|
4
|
+
* rules can't recognise, such as free-form passwords or unformatted IDs.
|
|
5
|
+
*/
|
|
6
|
+
import type { Rule } from "eslint";
|
|
7
|
+
declare const rule: Rule.RuleModule;
|
|
8
|
+
export default rule;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flags identifiers whose *name* suggests PII (`ssn`, `password`, `dateOfBirth`,
|
|
3
|
+
* ...) when they are assigned a hardcoded value. Catches PII that the value-based
|
|
4
|
+
* rules can't recognise, such as free-form passwords or unformatted IDs.
|
|
5
|
+
*/
|
|
6
|
+
// Names are split into lowercase words and matched against the *end* of the
|
|
7
|
+
// name, so `userPassword` and `billing_iban` match but `passwordMinLength`
|
|
8
|
+
// and `passportOffice` don't.
|
|
9
|
+
const DEFAULT_TERMS = [
|
|
10
|
+
"ssn",
|
|
11
|
+
"social security",
|
|
12
|
+
"social security number",
|
|
13
|
+
"password",
|
|
14
|
+
"passwd",
|
|
15
|
+
"passphrase",
|
|
16
|
+
"dob",
|
|
17
|
+
"birthdate",
|
|
18
|
+
"birth date",
|
|
19
|
+
"date of birth",
|
|
20
|
+
"passport",
|
|
21
|
+
"passport number",
|
|
22
|
+
"national id",
|
|
23
|
+
"national id number",
|
|
24
|
+
"tax id",
|
|
25
|
+
"pesel",
|
|
26
|
+
"nino",
|
|
27
|
+
"credit card",
|
|
28
|
+
"credit card number",
|
|
29
|
+
"debit card",
|
|
30
|
+
"card number",
|
|
31
|
+
"cvv",
|
|
32
|
+
"cvc",
|
|
33
|
+
"iban",
|
|
34
|
+
"driver license",
|
|
35
|
+
"drivers license",
|
|
36
|
+
"driver licence",
|
|
37
|
+
"drivers licence",
|
|
38
|
+
"driving license",
|
|
39
|
+
"driving licence",
|
|
40
|
+
];
|
|
41
|
+
const toWords = (name) => name
|
|
42
|
+
.replace(/([a-z\d])([A-Z])/g, "$1 $2")
|
|
43
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
44
|
+
.split(/[^A-Za-z\d]+/)
|
|
45
|
+
.filter(Boolean)
|
|
46
|
+
.map((word) => word.toLowerCase());
|
|
47
|
+
/** A hardcoded, non-empty string/number (or template without expressions). */
|
|
48
|
+
function isHardcodedValue(node) {
|
|
49
|
+
if (!node)
|
|
50
|
+
return false;
|
|
51
|
+
if (node.type === "Literal") {
|
|
52
|
+
if (typeof node.value === "string")
|
|
53
|
+
return node.value.trim() !== "";
|
|
54
|
+
return typeof node.value === "number";
|
|
55
|
+
}
|
|
56
|
+
if (node.type === "TemplateLiteral") {
|
|
57
|
+
return (node.expressions.length === 0 &&
|
|
58
|
+
node.quasis.some((quasi) => quasi.value.cooked?.trim()));
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const ESLINT_SEVERITIES = new Set(["off", "warn", "error"]);
|
|
63
|
+
/** An ESLint rule severity such as `"error"`, as in `{ "pii/no-ssn": "error" }`. */
|
|
64
|
+
const isSeverity = (node) => node?.type === "Literal" &&
|
|
65
|
+
typeof node.value === "string" &&
|
|
66
|
+
ESLINT_SEVERITIES.has(node.value);
|
|
67
|
+
/** The static name a node is declared/assigned under, if there is one. */
|
|
68
|
+
function nameOf(node, computed = false) {
|
|
69
|
+
if (node.type === "Identifier")
|
|
70
|
+
return computed ? null : node.name;
|
|
71
|
+
if (node.type === "PrivateIdentifier")
|
|
72
|
+
return node.name;
|
|
73
|
+
if (node.type === "Literal" && typeof node.value === "string") {
|
|
74
|
+
return node.value;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const rule = {
|
|
79
|
+
meta: {
|
|
80
|
+
type: "problem",
|
|
81
|
+
docs: {
|
|
82
|
+
description: "enforces no hardcoded values for identifiers whose names suggest PII",
|
|
83
|
+
},
|
|
84
|
+
schema: [
|
|
85
|
+
{
|
|
86
|
+
type: "object",
|
|
87
|
+
properties: {
|
|
88
|
+
identifiers: { type: "array", items: { type: "string" } },
|
|
89
|
+
ignore: { type: "array", items: { type: "string" } },
|
|
90
|
+
},
|
|
91
|
+
additionalProperties: false,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
messages: {
|
|
95
|
+
identifier: 'PII Violation: "{{name}}" looks like PII ({{term}}) and cannot be assigned a hardcoded value. Load it from an environment variable, secret store or test fixture instead. If this is a false positive, add "{{name}}" to the rule\'s `ignore` option in your ESLint config.',
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
create(context) {
|
|
99
|
+
const options = context.options[0] ?? {};
|
|
100
|
+
const ignore = new Set(options.ignore ?? []);
|
|
101
|
+
const terms = [...DEFAULT_TERMS, ...(options.identifiers ?? [])].map((term) => toWords(term));
|
|
102
|
+
const findTerm = (name) => {
|
|
103
|
+
if (ignore.has(name))
|
|
104
|
+
return undefined;
|
|
105
|
+
const words = toWords(name);
|
|
106
|
+
return terms
|
|
107
|
+
.find((term) => term.length > 0 &&
|
|
108
|
+
term.length <= words.length &&
|
|
109
|
+
term.every((word, i) => words[words.length - term.length + i] === word))
|
|
110
|
+
?.join(" ");
|
|
111
|
+
};
|
|
112
|
+
const check = (node, name, value) => {
|
|
113
|
+
if (name === null || !isHardcodedValue(value))
|
|
114
|
+
return;
|
|
115
|
+
const term = findTerm(name);
|
|
116
|
+
if (term) {
|
|
117
|
+
context.report({ node, messageId: "identifier", data: { name, term } });
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
VariableDeclarator(node) {
|
|
122
|
+
if (node.id.type === "Identifier")
|
|
123
|
+
check(node, node.id.name, node.init);
|
|
124
|
+
},
|
|
125
|
+
Property(node) {
|
|
126
|
+
// Rule-severity maps (`{ "pii/no-ssn": "error" }`) configure ESLint;
|
|
127
|
+
// they don't assign PII.
|
|
128
|
+
if (node.kind === "init" && !node.method && !isSeverity(node.value)) {
|
|
129
|
+
check(node, nameOf(node.key, node.computed), node.value);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
PropertyDefinition(node) {
|
|
133
|
+
check(node, nameOf(node.key, node.computed), node.value);
|
|
134
|
+
},
|
|
135
|
+
AssignmentPattern(node) {
|
|
136
|
+
if (node.left.type === "Identifier") {
|
|
137
|
+
check(node, node.left.name, node.right);
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
AssignmentExpression(node) {
|
|
141
|
+
if (node.operator !== "=")
|
|
142
|
+
return;
|
|
143
|
+
const { left } = node;
|
|
144
|
+
if (left.type === "Identifier") {
|
|
145
|
+
check(node, left.name, node.right);
|
|
146
|
+
}
|
|
147
|
+
else if (left.type === "MemberExpression") {
|
|
148
|
+
check(node, nameOf(left.property, left.computed), node.right);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
export default rule;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
export default createPiiRule({
|
|
3
|
+
description: "enforces no US Social Security numbers in comments or string literals",
|
|
4
|
+
subject: "social security number",
|
|
5
|
+
patterns: [
|
|
6
|
+
// AAA-GG-SSSS (or space separated); area can't be 000, 666 or 9xx,
|
|
7
|
+
// group can't be 00, serial can't be 0000.
|
|
8
|
+
/(?<![\d-])(?!000|666|9\d\d)\d{3}([- ])(?!00)\d{2}\1(?!0000)\d{4}(?![\d-])/,
|
|
9
|
+
],
|
|
10
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
// scheme://user:password@ -- the host is optional so the static part of a
|
|
3
|
+
// template literal like `postgres://admin:secret@${host}/db` still matches.
|
|
4
|
+
const URL_CREDENTIALS = /\b[a-z][a-z\d+.-]*:\/\/([^\s:@/?#]+):([^\s@/?#]+)@/gi;
|
|
5
|
+
// Obvious placeholders in docs and templates, not real credentials.
|
|
6
|
+
const PLACEHOLDER = /^(?:[$<{[%*:]|\.{3}|x+$|(?:your[-_]?)?pass(?:word)?$|pwd$|secret$|changeme$)/i;
|
|
7
|
+
export default createPiiRule({
|
|
8
|
+
description: "enforces no credentials embedded in URLs or connection strings",
|
|
9
|
+
subject: "URL credentials",
|
|
10
|
+
checkTemplateLiterals: true,
|
|
11
|
+
literalFix: "Read the credentials from environment variables or a secret store, e.g. `postgres://${user}:${password}@host/db`.",
|
|
12
|
+
detect: (value) => Array.from(value.matchAll(URL_CREDENTIALS)).some((match) => !PLACEHOLDER.test(match[2])),
|
|
13
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createPiiRule } from "../internal/create-pii-rule.js";
|
|
2
|
+
// 17 chars, letters I, O and Q are never used in a VIN.
|
|
3
|
+
const VIN_CHARS = "[A-HJ-NPR-Z\\d]{17}";
|
|
4
|
+
const CANDIDATE = new RegExp(`(?<![A-Z\\d])${VIN_CHARS}(?![A-Z\\d])`, "g");
|
|
5
|
+
// European VINs are not required to have a valid check digit, so also accept
|
|
6
|
+
// any well-formed VIN that is explicitly labelled as one.
|
|
7
|
+
const LABELLED = new RegExp(`\\bVIN\\b[\\s:#=-]{0,3}(?<![A-Z\\d])(${VIN_CHARS})(?![A-Z\\d])`, "gi");
|
|
8
|
+
const WEIGHTS = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
9
|
+
const TRANSLITERATION = {
|
|
10
|
+
A: 1,
|
|
11
|
+
B: 2,
|
|
12
|
+
C: 3,
|
|
13
|
+
D: 4,
|
|
14
|
+
E: 5,
|
|
15
|
+
F: 6,
|
|
16
|
+
G: 7,
|
|
17
|
+
H: 8,
|
|
18
|
+
J: 1,
|
|
19
|
+
K: 2,
|
|
20
|
+
L: 3,
|
|
21
|
+
M: 4,
|
|
22
|
+
N: 5,
|
|
23
|
+
P: 7,
|
|
24
|
+
R: 9,
|
|
25
|
+
S: 2,
|
|
26
|
+
T: 3,
|
|
27
|
+
U: 4,
|
|
28
|
+
V: 5,
|
|
29
|
+
W: 6,
|
|
30
|
+
X: 7,
|
|
31
|
+
Y: 8,
|
|
32
|
+
Z: 9,
|
|
33
|
+
};
|
|
34
|
+
/** ISO 3779 / 49 CFR 565 check digit (position 9). */
|
|
35
|
+
function hasValidCheckDigit(vin) {
|
|
36
|
+
let sum = 0;
|
|
37
|
+
for (let i = 0; i < 17; i++) {
|
|
38
|
+
const char = vin[i];
|
|
39
|
+
sum += (TRANSLITERATION[char] ?? Number(char)) * WEIGHTS[i];
|
|
40
|
+
}
|
|
41
|
+
const remainder = sum % 11;
|
|
42
|
+
return vin[8] === (remainder === 10 ? "X" : String(remainder));
|
|
43
|
+
}
|
|
44
|
+
export default createPiiRule({
|
|
45
|
+
description: "enforces no VIN in comments or string literals",
|
|
46
|
+
subject: "VIN",
|
|
47
|
+
detect: (value) => Array.from(value.matchAll(LABELLED)).length > 0 ||
|
|
48
|
+
Array.from(value.matchAll(CANDIDATE), (m) => m[0]).some(
|
|
49
|
+
// Real VINs mix letters and digits; this drops long numeric IDs.
|
|
50
|
+
(vin) => /[A-Z]/.test(vin) && /\d/.test(vin) && hasValidCheckDigit(vin)),
|
|
51
|
+
});
|
package/llms.txt
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# eslint-plugin-no-pii
|
|
2
|
+
|
|
3
|
+
> ESLint plugin (ESLint >= 9, flat config) that reports PII hardcoded in source: values in comments and string literals, plus hardcoded values assigned to PII-named identifiers. Rule prefix: `pii/`.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// eslint.config.js
|
|
9
|
+
import pii from "eslint-plugin-no-pii";
|
|
10
|
+
export default [pii.configs.recommended];
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## If a rule reports
|
|
14
|
+
|
|
15
|
+
Messages start with "PII Violation:" and say what to do. To resolve a report:
|
|
16
|
+
|
|
17
|
+
- Do not keep the real value. Load it from an environment variable, secret store or test fixture, or replace it with a redaction token such as `"[REDACTED email address]"`.
|
|
18
|
+
- Do not suppress it. `eslint-disable-line` and `eslint-disable-next-line` do not silence the value-based rules, and block `eslint-disable` defeats their purpose.
|
|
19
|
+
- If a file legitimately needs such values, allow-list it in `eslint.config.js` with a `files` override that sets the rule to `"off"`, so the exception is visible in review. For `pii/no-pii-identifier`, add the exact name to its `ignore` option instead.
|
|
20
|
+
- Never print or copy the matched value into logs, commit messages or PR descriptions.
|
|
21
|
+
|
|
22
|
+
## Rules
|
|
23
|
+
|
|
24
|
+
Each rule has a docs page with examples and known limits.
|
|
25
|
+
|
|
26
|
+
- [pii/no-credit-card](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-credit-card.md): credit/debit card numbers (Luhn + issuer prefix). Recommended: error.
|
|
27
|
+
- [pii/no-crypto-wallet](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-crypto-wallet.md): Bitcoin and Ethereum wallet addresses. Recommended: error.
|
|
28
|
+
- [pii/no-dob](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-dob.md): birth dates near birth-related words. Recommended: warn.
|
|
29
|
+
- [pii/no-email](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-email.md): email addresses. Recommended: error.
|
|
30
|
+
- [pii/no-iban](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-iban.md): IBANs (length + mod-97). Recommended: error.
|
|
31
|
+
- [pii/no-imei](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-imei.md): IMEI numbers (Luhn). Recommended: error.
|
|
32
|
+
- [pii/no-ip](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-ip.md): IPv4 and IPv6 addresses. Recommended: error.
|
|
33
|
+
- [pii/no-jwt](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-jwt.md): JSON Web Tokens (structurally validated). Recommended: error.
|
|
34
|
+
- [pii/no-mac-address](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-mac-address.md): MAC addresses. Recommended: error.
|
|
35
|
+
- [pii/no-national-id](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-national-id.md): Polish PESEL/NIP, UK NINO, Spanish DNI/NIE. Recommended: error.
|
|
36
|
+
- [pii/no-phone-number](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-phone-number.md): phone numbers (FR, DE, UK, US formats). Recommended: error.
|
|
37
|
+
- [pii/no-pii-identifier](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-pii-identifier.md): hardcoded values assigned to PII-named identifiers (ssn, password, dateOfBirth, ...); options: identifiers, ignore. Recommended: error.
|
|
38
|
+
- [pii/no-ssn](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-ssn.md): US Social Security numbers. Recommended: error.
|
|
39
|
+
- [pii/no-url-credentials](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-url-credentials.md): scheme://user:password@host credentials. Recommended: error.
|
|
40
|
+
- [pii/no-vin](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/docs/rules/no-vin.md): vehicle identification numbers. Recommended: error.
|
|
41
|
+
|
|
42
|
+
## Optional
|
|
43
|
+
|
|
44
|
+
- [README](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/README.md): installation and configuration
|
|
45
|
+
- [AGENTS.md](https://github.com/lwojcik/eslint-plugin-no-pii/blob/main/AGENTS.md): contributing guide for coding agents (adding a rule, commands, conventions)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-no-pii",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "PII linting rules for ESLint 10",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eslint",
|
|
@@ -12,11 +12,12 @@
|
|
|
12
12
|
"homepage": "https://github.com/lwojcik/eslint-plugin-no-pii",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"prepare": "husky",
|
|
15
|
-
"commit": "
|
|
15
|
+
"commit": "czg",
|
|
16
16
|
"format": "prettier --write .",
|
|
17
17
|
"format:check": "prettier --check .",
|
|
18
18
|
"lint": "eslint .",
|
|
19
19
|
"test": "node --test \"tests/**/*.ts\"",
|
|
20
|
+
"test:coverage": "mkdir -p coverage && node --test --experimental-test-coverage --test-coverage-exclude=\"tests/**\" --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/lcov.info \"tests/**/*.ts\"",
|
|
20
21
|
"release": "HUSKY=0 GITHUB_TOKEN=$(gh auth token) release-it --no-increment --no-git.commit",
|
|
21
22
|
"release:prepare": "scripts/release-prepare.sh",
|
|
22
23
|
"build": "tsc -p tsconfig.build.json",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
"node": ">=20.19.0"
|
|
28
29
|
},
|
|
29
30
|
"repository": {
|
|
31
|
+
"type": "git",
|
|
30
32
|
"url": "git+https://github.com/lwojcik/eslint-plugin-no-pii.git"
|
|
31
33
|
},
|
|
32
34
|
"license": "ISC",
|
|
@@ -39,8 +41,7 @@
|
|
|
39
41
|
"@eslint/js": "^10.0.1",
|
|
40
42
|
"@release-it/conventional-changelog": "^12.0.2",
|
|
41
43
|
"@types/node": "^26.6.6",
|
|
42
|
-
"
|
|
43
|
-
"cz-conventional-changelog": "3.3.0",
|
|
44
|
+
"czg": "1.14.0",
|
|
44
45
|
"eslint": "^10.0.0",
|
|
45
46
|
"globals": "^17.14.0",
|
|
46
47
|
"husky": "9.1.7",
|
|
@@ -49,8 +50,12 @@
|
|
|
49
50
|
"typescript": "^6.0.3",
|
|
50
51
|
"typescript-eslint": "^8.73.0"
|
|
51
52
|
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
52
56
|
"files": [
|
|
53
|
-
"dist"
|
|
57
|
+
"dist",
|
|
58
|
+
"llms.txt"
|
|
54
59
|
],
|
|
55
60
|
"type": "module",
|
|
56
61
|
"exports": {
|
|
@@ -59,10 +64,5 @@
|
|
|
59
64
|
"default": "./dist/index.js"
|
|
60
65
|
}
|
|
61
66
|
},
|
|
62
|
-
"config": {
|
|
63
|
-
"commitizen": {
|
|
64
|
-
"path": "cz-conventional-changelog"
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
67
|
"types": "./dist/index.d.ts"
|
|
68
68
|
}
|