@platform-sec/eslint-plugin-sensitive-string 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +193 -2
- package/dist/ast.d.ts +172 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +418 -0
- package/dist/ast.js.map +1 -0
- package/dist/configs.d.ts +38 -0
- package/dist/configs.d.ts.map +1 -0
- package/dist/configs.js +131 -0
- package/dist/configs.js.map +1 -0
- package/dist/docs.d.ts +28 -0
- package/dist/docs.d.ts.map +1 -0
- package/dist/docs.js +33 -0
- package/dist/docs.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -0
- package/dist/log-calls.d.ts +28 -0
- package/dist/log-calls.d.ts.map +1 -0
- package/dist/log-calls.js +80 -0
- package/dist/log-calls.js.map +1 -0
- package/dist/options.d.ts +32 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +56 -0
- package/dist/options.js.map +1 -0
- package/dist/rules/explicit-class.d.ts +3 -0
- package/dist/rules/explicit-class.d.ts.map +1 -0
- package/dist/rules/explicit-class.js +93 -0
- package/dist/rules/explicit-class.js.map +1 -0
- package/dist/rules/no-allow-unaudited.d.ts +3 -0
- package/dist/rules/no-allow-unaudited.d.ts.map +1 -0
- package/dist/rules/no-allow-unaudited.js +92 -0
- package/dist/rules/no-allow-unaudited.js.map +1 -0
- package/dist/rules/no-credential-equality.d.ts +3 -0
- package/dist/rules/no-credential-equality.d.ts.map +1 -0
- package/dist/rules/no-credential-equality.js +146 -0
- package/dist/rules/no-credential-equality.js.map +1 -0
- package/dist/rules/no-log-reveal.d.ts +3 -0
- package/dist/rules/no-log-reveal.d.ts.map +1 -0
- package/dist/rules/no-log-reveal.js +67 -0
- package/dist/rules/no-log-reveal.js.map +1 -0
- package/dist/rules/no-network-sensitive.d.ts +3 -0
- package/dist/rules/no-network-sensitive.d.ts.map +1 -0
- package/dist/rules/no-network-sensitive.js +106 -0
- package/dist/rules/no-network-sensitive.js.map +1 -0
- package/dist/rules/no-password-fast-hash.d.ts +3 -0
- package/dist/rules/no-password-fast-hash.d.ts.map +1 -0
- package/dist/rules/no-password-fast-hash.js +84 -0
- package/dist/rules/no-password-fast-hash.js.map +1 -0
- package/dist/rules/no-stringify-sensitive.d.ts +3 -0
- package/dist/rules/no-stringify-sensitive.d.ts.map +1 -0
- package/dist/rules/no-stringify-sensitive.js +96 -0
- package/dist/rules/no-stringify-sensitive.js.map +1 -0
- package/dist/rules/prefer-safe-log.d.ts +3 -0
- package/dist/rules/prefer-safe-log.d.ts.map +1 -0
- package/dist/rules/prefer-safe-log.js +85 -0
- package/dist/rules/prefer-safe-log.js.map +1 -0
- package/dist/rules/require-reveal-context.d.ts +3 -0
- package/dist/rules/require-reveal-context.d.ts.map +1 -0
- package/dist/rules/require-reveal-context.js +149 -0
- package/dist/rules/require-reveal-context.js.map +1 -0
- package/dist/type-info.d.ts +134 -0
- package/dist/type-info.d.ts.map +1 -0
- package/dist/type-info.js +282 -0
- package/dist/type-info.js.map +1 -0
- package/dist/types.d.ts +137 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +23 -0
- package/dist/types.js.map +1 -0
- package/docs/rules/explicit-class.md +62 -0
- package/docs/rules/no-allow-unaudited.md +93 -0
- package/docs/rules/no-credential-equality.md +69 -0
- package/docs/rules/no-log-reveal.md +104 -0
- package/docs/rules/no-network-sensitive.md +74 -0
- package/docs/rules/no-password-fast-hash.md +95 -0
- package/docs/rules/no-stringify-sensitive.md +144 -0
- package/docs/rules/prefer-safe-log.md +131 -0
- package/docs/rules/require-reveal-context.md +128 -0
- package/package.json +49 -3
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# no-credential-equality
|
|
2
|
+
|
|
3
|
+
Disallow comparing a revealed sensitive value with an equality operator.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **error**. Offers suggestions.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
`===` on strings returns as soon as it finds a differing byte. The time it takes
|
|
10
|
+
is therefore a function of how long a shared prefix the two strings have, and an
|
|
11
|
+
attacker who can measure it can recover a credential a byte at a time.
|
|
12
|
+
|
|
13
|
+
`equals()` compares in constant time for the `credential` class, which is the
|
|
14
|
+
behavioural difference the class exists to express.
|
|
15
|
+
|
|
16
|
+
## Examples
|
|
17
|
+
|
|
18
|
+
Incorrect:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
if (password.reveal() === userInput) { grant(); }
|
|
22
|
+
if (token.reveal() == storedToken) { grant(); }
|
|
23
|
+
const differs = password.reveal() !== expected;
|
|
24
|
+
switch (token.reveal()) { case a: break; }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Correct:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
if (password.equals(userInput)) { grant(); }
|
|
31
|
+
if (token.equals(new SensitiveString(stored, 'credential'))) { grant(); }
|
|
32
|
+
if (password.equals(userInput, { name: 'login' })) { grant(); }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## `switch`
|
|
36
|
+
|
|
37
|
+
A `switch` compares its discriminant against each case in turn and stops at the
|
|
38
|
+
first match, which is the same leak in a different shape. It is reported under
|
|
39
|
+
its own message.
|
|
40
|
+
|
|
41
|
+
## Suggestions, not fixes
|
|
42
|
+
|
|
43
|
+
The rewrite is offered as an editor suggestion and never applied by `--fix`.
|
|
44
|
+
`--fix` rewrites files with nobody reading the diff, and the two forms are not
|
|
45
|
+
interchangeable: `equals()` can throw where `reveal()` did not, on an expired
|
|
46
|
+
value or under a policy that requires a context. That difference is the point of
|
|
47
|
+
the rule, and a person should accept it one call site at a time.
|
|
48
|
+
|
|
49
|
+
A context on the reveal moves into `equals()`, which takes it second:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
token.reveal({ name: 'login' }) === input;
|
|
53
|
+
// suggested: token.equals(input, { name: 'login' })
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
No suggestion is offered when:
|
|
57
|
+
|
|
58
|
+
- the call is optional-chained. `x?.reveal() === y` yields `false` when `x` is
|
|
59
|
+
nullish while `x?.equals(y)` yields `undefined`, and a suggestion must not
|
|
60
|
+
change which branch runs.
|
|
61
|
+
- a comment sits inside the comparison, which the replacement would delete.
|
|
62
|
+
- both sides reveal with a context each, since `equals()` has room for one.
|
|
63
|
+
|
|
64
|
+
## Known limits
|
|
65
|
+
|
|
66
|
+
- Only a reveal in operand position is reported. `password.reveal().length === 8`
|
|
67
|
+
is a comparison of a length, not of a value, and is deliberately not flagged;
|
|
68
|
+
the cost is that `password.reveal().trim() === input` is not flagged either.
|
|
69
|
+
- `[a.reveal()].includes(b)` and similar library comparisons are not reported.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# no-log-reveal
|
|
2
|
+
|
|
3
|
+
Disallow passing a revealed sensitive value to a logger.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **error**.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
`reveal()` returns an ordinary string, and logging an ordinary string is
|
|
10
|
+
ordinary code. No type can tell the two apart at the call site, so the one
|
|
11
|
+
mistake that defeats the whole library is also the one the compiler cannot see.
|
|
12
|
+
|
|
13
|
+
Log output is the widest distribution channel most services have: it goes to
|
|
14
|
+
disk, to a log aggregator, to a third-party vendor, and often to a lower
|
|
15
|
+
security tier than the process that produced it.
|
|
16
|
+
|
|
17
|
+
## Examples
|
|
18
|
+
|
|
19
|
+
Incorrect:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
console.log(secret.reveal());
|
|
23
|
+
logger.info({ key: secret.reveal() });
|
|
24
|
+
console.log(`token: ${token.reveal()}`);
|
|
25
|
+
console.log(secret.reveal().slice(0, 4));
|
|
26
|
+
console.log(secrets.map((s) => s.reveal()));
|
|
27
|
+
console.log(safeLog({ token: token.reveal() }));
|
|
28
|
+
console.log(token.serialize());
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Correct:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
console.log(secret); // redacts through inspect()
|
|
35
|
+
console.log(secret.toString()); // redacts
|
|
36
|
+
console.log(secret.mask()); // partial, under the policy's floor
|
|
37
|
+
console.log(safeLog({ secret })); // redacts the wrapper, not a raw string
|
|
38
|
+
logger.info('token present:', Boolean(token));
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## What it looks at
|
|
42
|
+
|
|
43
|
+
A call counts as a log call when either:
|
|
44
|
+
|
|
45
|
+
- its method name is in `logFunctions` **and** some name along its callee chain
|
|
46
|
+
is in `logObjects`, which covers `console.log`, `this.logger.info`,
|
|
47
|
+
`req.log.warn` and `logger.child({ id }).info`; or
|
|
48
|
+
- it is a bare call whose function name is in `logObjects`, as in `log(...)`.
|
|
49
|
+
|
|
50
|
+
Every argument is then searched for a call to one of `revealMethods`, at any
|
|
51
|
+
depth, through objects, arrays, template literals, concatenations, casts and
|
|
52
|
+
callbacks.
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
| Option | Default |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `logFunctions` | `log`, `info`, `warn`, `error`, `debug`, `trace`, `fatal`, `verbose`, `silly`, `write` |
|
|
59
|
+
| `logObjects` | `console`, `logger`, `log`, `winston`, `pino`, `bunyan`, `stdout`, `stderr` |
|
|
60
|
+
| `revealMethods` | `reveal`, `serialize` |
|
|
61
|
+
|
|
62
|
+
A configured list replaces the default rather than extending it.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
{
|
|
66
|
+
'@platform-sec/sensitive-string/no-log-reveal': [
|
|
67
|
+
'error',
|
|
68
|
+
{ logObjects: ['console', 'logger', 'report'] },
|
|
69
|
+
],
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Why `serialize` is in `revealMethods`
|
|
74
|
+
|
|
75
|
+
The serialized wire format carries the value in the clear; the README's first
|
|
76
|
+
security note says so. `console.log(token.serialize())` is the more dangerous of
|
|
77
|
+
the two forms precisely because it reads like the safe one. Drop it from
|
|
78
|
+
`revealMethods` if a project uses the name for something else.
|
|
79
|
+
|
|
80
|
+
### Why `safeLog()` does not help here
|
|
81
|
+
|
|
82
|
+
`safeLog` redacts `SensitiveString` instances. `secret.reveal()` has already
|
|
83
|
+
produced a plain string, and no redactor can tell that string from any other.
|
|
84
|
+
Wrapping a reveal in `safeLog` is reported for exactly that reason.
|
|
85
|
+
|
|
86
|
+
## Known limits
|
|
87
|
+
|
|
88
|
+
- A logger reached under a name in neither list is not recognised. Add it to
|
|
89
|
+
`logObjects`.
|
|
90
|
+
- A reveal inside a function that is passed to a logger but never called, as in
|
|
91
|
+
`console.log(() => secret.reveal())`, is reported. The walk descends into
|
|
92
|
+
function bodies because `console.log(list.map((s) => s.reveal()))` is a real
|
|
93
|
+
leak written the ordinary way, and that is worth a false positive on an
|
|
94
|
+
unused closure.
|
|
95
|
+
- A value revealed into a variable first, then logged, is not reported. Its
|
|
96
|
+
type is `string` by then, so type information does not help either: reveal at
|
|
97
|
+
the point of use, which keeps the raw string's lifetime as short as the
|
|
98
|
+
library can make it.
|
|
99
|
+
|
|
100
|
+
## Related
|
|
101
|
+
|
|
102
|
+
- [`prefer-safe-log`](prefer-safe-log.md), which asks the type-aware version of
|
|
103
|
+
the question: not "is a reveal being logged" but "does this structure carry a
|
|
104
|
+
sensitive value at all".
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# no-network-sensitive
|
|
2
|
+
|
|
3
|
+
Disallow sending an unaudited revealed value in a network request.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **error**.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
Sending a secret to another service is sometimes exactly right: that is what an
|
|
10
|
+
`Authorization` header is. What should never happen is sending one *by
|
|
11
|
+
accident*, with no record that it left the process.
|
|
12
|
+
|
|
13
|
+
So the rule does not forbid transmission. It forbids transmission that nothing
|
|
14
|
+
audited.
|
|
15
|
+
|
|
16
|
+
## Examples
|
|
17
|
+
|
|
18
|
+
Incorrect:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
fetch(url, { body: secret.reveal() });
|
|
22
|
+
axios.post(url, { token: token.reveal() });
|
|
23
|
+
fetch(`https://api.example/?token=${token.reveal()}`);
|
|
24
|
+
superagent.post(url).send({ token: token.reveal() });
|
|
25
|
+
|
|
26
|
+
// A placeholder is not an audit context.
|
|
27
|
+
fetch(url, { body: secret.reveal(undefined) });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Correct:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
// The wire format, which keeps the shape under the library's control.
|
|
34
|
+
fetch(url, { body: JSON.stringify({ token: token.serialize() }) });
|
|
35
|
+
|
|
36
|
+
// A deliberate, recorded transmission.
|
|
37
|
+
fetch(url, { headers: { Authorization: token.reveal({ name: 'auth-header' }) } });
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Options
|
|
41
|
+
|
|
42
|
+
| Option | Default |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `networkFunctions` | `fetch`, `$fetch`, `axios`, `got`, `ky`, `superagent`, `undici`, `http`, `https`, `XMLHttpRequest` |
|
|
45
|
+
| `allowWithContext` | `true` |
|
|
46
|
+
|
|
47
|
+
`allowWithContext: false` removes the exemption, so the only way a value leaves
|
|
48
|
+
the process is `serialize()`. The `strict` preset sets it.
|
|
49
|
+
|
|
50
|
+
`request` is deliberately absent from the defaults even though the library of
|
|
51
|
+
that name exists: Fastify calls its request object `request`, so the name
|
|
52
|
+
appears throughout handler code that sends nothing anywhere. Add it back through
|
|
53
|
+
`networkFunctions` in a project that uses the library.
|
|
54
|
+
|
|
55
|
+
## A resolved ambiguity
|
|
56
|
+
|
|
57
|
+
The roadmap's rule table describes this rule as catching any `reveal()` inside a
|
|
58
|
+
`fetch`, while the design document lists
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
fetch(url, { headers: { Authorization: token.reveal({ name: 'auth-header' }) } });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
among its examples of correct code. Both cannot hold. The design document has
|
|
65
|
+
the better of it, for the reason above, and `allowWithContext: false` is there
|
|
66
|
+
for a service that needs the stricter reading.
|
|
67
|
+
|
|
68
|
+
## Known limits
|
|
69
|
+
|
|
70
|
+
- A client reached under a name not in `networkFunctions` is not recognised. Add
|
|
71
|
+
it: `{ networkFunctions: ['fetch', 'apiClient'] }`.
|
|
72
|
+
- The rule cannot tell a URL argument from a body argument across libraries, so
|
|
73
|
+
an audited reveal interpolated into a URL is not reported even though a secret
|
|
74
|
+
in a URL also reaches proxy logs and `Referer` headers. Prefer a header.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# no-password-fast-hash
|
|
2
|
+
|
|
3
|
+
Disallow hashing a password with `hash()` or `hmac()`, which are fast by design.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **warn**. In `strict`: **error**.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
`hash()` exists for a lookup key, a deduplication key, a correlation token. It
|
|
10
|
+
is fast, which is the property those uses want and the one property a stored
|
|
11
|
+
password must not have: a wordlist runs through SHA-256 at billions of guesses a
|
|
12
|
+
second on hardware anybody can rent, and a salt does nothing about that for
|
|
13
|
+
whoever has the row the salt is stored in.
|
|
14
|
+
|
|
15
|
+
A password wants a function that is deliberately slow and memory-hard. That is
|
|
16
|
+
`hashForStorage()` from `@platform-sec/sensitive-string-kdf`, which is a
|
|
17
|
+
separate package because Argon2 and bcrypt are native modules and `core` has no
|
|
18
|
+
runtime dependencies.
|
|
19
|
+
|
|
20
|
+
`hmac()` is reported too. A key does not make a fast function slow. Peppering a
|
|
21
|
+
password with an HMAC is a real pattern and a defensible one *on top of* a KDF,
|
|
22
|
+
never instead of one.
|
|
23
|
+
|
|
24
|
+
## Examples
|
|
25
|
+
|
|
26
|
+
Incorrect:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const stored = password.hash('sha256');
|
|
30
|
+
const stored = user.password.hash('sha256', { salt });
|
|
31
|
+
const stored = newPassword.hmac('sha256', pepper);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Correct:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { hashForStorage } from '@platform-sec/sensitive-string-kdf';
|
|
38
|
+
|
|
39
|
+
const stored = await hashForStorage(password, { algorithm: 'argon2id' });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
// hash() on the values it is for.
|
|
44
|
+
const lookupKey = token.hash('sha256', {}, { name: 'session-lookup' });
|
|
45
|
+
const pseudonym = email.hmac('sha256', correlationKey, {}, ctx);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## How a password is recognised
|
|
49
|
+
|
|
50
|
+
By the name of the value being hashed, matched case-insensitively as a
|
|
51
|
+
fragment: `password`, `passwd`, `passphrase`, `pwd`. So `newPassword`,
|
|
52
|
+
`user.passwd` and `plainPWD` all count.
|
|
53
|
+
|
|
54
|
+
The name that matters is the value's own, not whatever holds it.
|
|
55
|
+
`user.password.hash()` reports on `password`; `passwordService.token.hash()`
|
|
56
|
+
reports on `token`, which is to say not at all.
|
|
57
|
+
|
|
58
|
+
`secret`, `token` and `key` are deliberately not in the list. Those are the
|
|
59
|
+
values `hash()` is *for*, and a rule that reported them would be reporting the
|
|
60
|
+
feature.
|
|
61
|
+
|
|
62
|
+
## Why a name, when the plugin says elsewhere that names are worse than nothing
|
|
63
|
+
|
|
64
|
+
`type-info.ts` rejects name matching for a different question. There it has to
|
|
65
|
+
decide whether a value carries a sensitive one, and a name test answers
|
|
66
|
+
`JSON.stringify(token)` while staying quiet on `JSON.stringify(user)`, which is
|
|
67
|
+
the call that actually holds the secret. Types answer that question properly.
|
|
68
|
+
|
|
69
|
+
Here the question is whether this particular value is a *password*, and no type
|
|
70
|
+
answers it: a password and an API key are both `SensitiveString<'credential'>`.
|
|
71
|
+
The name is the only signal anybody has, which is why this rule is a warning
|
|
72
|
+
rather than an error in `recommended`. A false positive costs a one-line
|
|
73
|
+
suppression or an entry in `passwordNames`; a false negative is a password store
|
|
74
|
+
that a wordlist opens.
|
|
75
|
+
|
|
76
|
+
## Options
|
|
77
|
+
|
|
78
|
+
| Option | Default |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `passwordNames` | `password`, `passwd`, `passphrase`, `pwd` |
|
|
81
|
+
|
|
82
|
+
A configured list replaces the default rather than extending it, which is the
|
|
83
|
+
convention across this plugin's name options.
|
|
84
|
+
|
|
85
|
+
## What it does not catch
|
|
86
|
+
|
|
87
|
+
`crypto.createHash('sha256').update(password)`, and the same written across two
|
|
88
|
+
statements. This plugin reads calls rather than following values, and by the
|
|
89
|
+
time an update is a statement of its own there is nothing at the call site
|
|
90
|
+
naming the algorithm that was chosen. A rule that tried would report the digest
|
|
91
|
+
of a file being read a chunk at a time.
|
|
92
|
+
|
|
93
|
+
It also says nothing about a password reaching `hashForStorage` with parameters
|
|
94
|
+
below the floor, because that call refuses at runtime and does not need a lint
|
|
95
|
+
rule to say so.
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# no-stringify-sensitive
|
|
2
|
+
|
|
3
|
+
Disallow `JSON.stringify` on a structure whose type carries a sensitive value.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended-type-checked`: **error**.
|
|
6
|
+
|
|
7
|
+
**Requires type information.** The rule needs a `ts.Program`, which means
|
|
8
|
+
`@typescript-eslint/parser` with `project` or `projectService` set. Without one
|
|
9
|
+
it reports its own misconfiguration rather than falling silent; see
|
|
10
|
+
[No type information](#no-type-information).
|
|
11
|
+
|
|
12
|
+
## Why
|
|
13
|
+
|
|
14
|
+
`JSON.stringify` does something to a `SensitiveString`, and does something else
|
|
15
|
+
to a `serialize()` result. Neither is what the call site says.
|
|
16
|
+
|
|
17
|
+
**A live wrapper becomes its redaction.** `SensitiveString` implements
|
|
18
|
+
`toJSON()`, so `JSON.stringify({ token })` produces
|
|
19
|
+
`{"token":"[REDACTED:credential]"}`. Nothing throws and nothing warns. If the
|
|
20
|
+
JSON was going to a log line, the substitution is what you wanted. If it was
|
|
21
|
+
going to another service, you have just sent the string `[REDACTED:credential]`
|
|
22
|
+
where a credential belonged, and you will find out at the far end, as an
|
|
23
|
+
authentication failure with no obvious cause.
|
|
24
|
+
|
|
25
|
+
**A `serialize()` result stays in the clear.** The wire format is
|
|
26
|
+
`{ __sensitive: true, klass, value }` with the raw value in it, and it is a
|
|
27
|
+
plain object, so `JSON.stringify` writes it out faithfully. This is the one
|
|
28
|
+
shape `toJSON()` cannot catch, and it is why `redactInStructure` recognises the
|
|
29
|
+
marker by hand.
|
|
30
|
+
|
|
31
|
+
The rule reports both, with different messages, because the fixes are
|
|
32
|
+
opposites: one call meant to redact and did not say so, the other meant to
|
|
33
|
+
transmit and did.
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
|
|
37
|
+
Incorrect:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
declare const payload: { token: SensitiveString };
|
|
41
|
+
JSON.stringify(payload); // writes "[REDACTED:credential]", silently
|
|
42
|
+
JSON.stringify(payload, replacer, 2); // the replacer changes nothing about this
|
|
43
|
+
JSON.stringify(token); // the value itself, same substitution
|
|
44
|
+
|
|
45
|
+
declare const wire: SerializedSensitiveString;
|
|
46
|
+
JSON.stringify(wire); // writes the raw value
|
|
47
|
+
JSON.stringify({ credentials: wire }); // and so does this
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Correct:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// Redacted JSON, said out loud. Also handles a BigInt, and never returns
|
|
54
|
+
// undefined the way JSON.stringify does for a value JSON cannot represent.
|
|
55
|
+
safeStringify(payload);
|
|
56
|
+
|
|
57
|
+
// The value has to arrive on the other side, so it travels as the wire format
|
|
58
|
+
// and the receiver deserializes it.
|
|
59
|
+
JSON.stringify({ token: token.serialize() });
|
|
60
|
+
|
|
61
|
+
// Already reduced to a string.
|
|
62
|
+
JSON.stringify({ token: token.toString() });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## What it looks at
|
|
66
|
+
|
|
67
|
+
The first argument of a `JSON.stringify(...)` call, and only the first: the
|
|
68
|
+
second is a replacer and the third an indent, and neither is written into the
|
|
69
|
+
output as a value.
|
|
70
|
+
|
|
71
|
+
Its type is then walked the way a serializer walks a value: union and
|
|
72
|
+
intersection members, array and tuple elements, base classes, object properties
|
|
73
|
+
and index signatures. A type is reported when its own name, or the name of the
|
|
74
|
+
alias it was written as, is in `sensitiveTypes` or `serializedTypes`.
|
|
75
|
+
|
|
76
|
+
## Options
|
|
77
|
+
|
|
78
|
+
| Option | Default |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `sensitiveTypes` | `SensitiveString` |
|
|
81
|
+
| `serializedTypes` | `SerializedSensitiveString` |
|
|
82
|
+
|
|
83
|
+
A configured list replaces the default rather than extending it.
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
{
|
|
87
|
+
'@platform-sec/sensitive-string/no-stringify-sensitive': [
|
|
88
|
+
'error',
|
|
89
|
+
{ sensitiveTypes: ['SensitiveString', 'ApiKey'] },
|
|
90
|
+
],
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Names are matched exactly, never by prefix. `SerializedSensitiveString` is the
|
|
95
|
+
shape `serialize()` produces and `deserialize()` consumes, and a rule that
|
|
96
|
+
reported every name containing "Sensitive" would report the recommended
|
|
97
|
+
transmission path at the point where it is correct.
|
|
98
|
+
|
|
99
|
+
## No type information
|
|
100
|
+
|
|
101
|
+
Applied to a file with no TypeScript program, the rule reports
|
|
102
|
+
`missingTypeInformation` once, on the file. That is deliberate: a rule that
|
|
103
|
+
reports nothing looks exactly like a rule that found nothing, and this one gets
|
|
104
|
+
read as "no sensitive value is being stringified here".
|
|
105
|
+
|
|
106
|
+
In a mixed project, layer `disable-type-checked` over the files a
|
|
107
|
+
`tsconfig.json` does not cover:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
export default [
|
|
111
|
+
sensitive.configs['recommended-type-checked'],
|
|
112
|
+
{ files: ['**/*.js', '**/*.mjs'], ...sensitive.configs['disable-type-checked'] },
|
|
113
|
+
];
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Known limits
|
|
117
|
+
|
|
118
|
+
- **The match is by name.** Any type called `SensitiveString` is treated as one,
|
|
119
|
+
wherever it came from, and a project's own wrapper is invisible until
|
|
120
|
+
`sensitiveTypes` names it. Resolving the declaration back to this library
|
|
121
|
+
would be more precise and would break under bundlers, path aliases and
|
|
122
|
+
re-exports, which is a worse trade for a rule that runs in an editor.
|
|
123
|
+
- **A cast to a weaker type hides the field**, because after
|
|
124
|
+
`payload as Record<string, unknown>` there is no longer a `SensitiveString` in
|
|
125
|
+
the type. This is the same hole every type-aware rule has.
|
|
126
|
+
- **`any` and `unknown` report nothing.** There is nothing to walk. A payload
|
|
127
|
+
arriving from an untyped module is invisible to this rule.
|
|
128
|
+
- **The call has to name `JSON`.** `JSON.stringify` and `globalThis.JSON.stringify`
|
|
129
|
+
are recognised; a destructured `const { stringify } = JSON` or an alias
|
|
130
|
+
`const J = JSON` is not. Requiring the name is what keeps `qs.stringify` and
|
|
131
|
+
every other library's serializer out of the rule, since those call
|
|
132
|
+
`toString()` and get the redaction.
|
|
133
|
+
- **A `Map` or a `Set` holding sensitive values is not walked.** Neither
|
|
134
|
+
survives `JSON.stringify` as anything but `{}`, so there is nothing to
|
|
135
|
+
report; a structure that converts one to an object first is walked normally.
|
|
136
|
+
- One search examines at most 1000 distinct types. This is a bound on the
|
|
137
|
+
pathological case, not a design limit, and a payload type that reaches it
|
|
138
|
+
without a sensitive value in it does not arise in practice.
|
|
139
|
+
|
|
140
|
+
## Related
|
|
141
|
+
|
|
142
|
+
- [`prefer-safe-log`](prefer-safe-log.md), the same question asked of a logger.
|
|
143
|
+
- [`no-log-reveal`](no-log-reveal.md), which catches `token.serialize()` handed
|
|
144
|
+
straight to a logger without needing types.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# prefer-safe-log
|
|
2
|
+
|
|
3
|
+
Prefer `safeLog()` when logging a structure whose type carries a sensitive
|
|
4
|
+
value.
|
|
5
|
+
|
|
6
|
+
Severity in `recommended-type-checked`: **warn**. `strict-type-checked` raises
|
|
7
|
+
it to error.
|
|
8
|
+
|
|
9
|
+
**Requires type information.** The rule needs a `ts.Program`, which means
|
|
10
|
+
`@typescript-eslint/parser` with `project` or `projectService` set. Without one
|
|
11
|
+
it reports its own misconfiguration rather than falling silent; see
|
|
12
|
+
[No type information](#no-type-information).
|
|
13
|
+
|
|
14
|
+
## Why
|
|
15
|
+
|
|
16
|
+
Two different things, under one rule because the fix is the same call.
|
|
17
|
+
|
|
18
|
+
**A `serialize()` result in a log line is a leak.** The wire format is a plain
|
|
19
|
+
object holding the raw value. `toString()` never runs on it, `toJSON()` never
|
|
20
|
+
runs on it, and `util.inspect` prints it field by field. Nothing in the library
|
|
21
|
+
redacts it except `safeLog`, which recognises the `__sensitive` marker by hand
|
|
22
|
+
for exactly this reason.
|
|
23
|
+
|
|
24
|
+
**A `SensitiveString` inside a logged structure is not a leak today, and the
|
|
25
|
+
rule is about not depending on that.** The class redacts itself through
|
|
26
|
+
`toString()`, `toJSON()` and `util.inspect`, so `console.log(user)` prints the
|
|
27
|
+
redaction under every transport in common use. What makes that true is the
|
|
28
|
+
transport reaching for one of those three. `safeLog(user)` produces a redacted
|
|
29
|
+
copy before the logger is handed anything, which is true regardless of what the
|
|
30
|
+
transport does with it.
|
|
31
|
+
|
|
32
|
+
That difference in kind is why the rule warns rather than errors by default,
|
|
33
|
+
and why the two cases carry different messages.
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
|
|
37
|
+
Incorrect:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
declare const user: { name: string; token: SensitiveString };
|
|
41
|
+
console.log(user);
|
|
42
|
+
logger.info({ token });
|
|
43
|
+
logger.info('sending request', payload);
|
|
44
|
+
|
|
45
|
+
declare const wire: SerializedSensitiveString;
|
|
46
|
+
console.log(wire); // nothing redacts this one
|
|
47
|
+
logger.warn({ credentials: wire });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Correct:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
console.log(safeLog(user)); // redacted copy, whatever the transport does
|
|
54
|
+
console.log(token); // the value itself: the class redacts
|
|
55
|
+
console.log(tokens); // and a list delegates to each element
|
|
56
|
+
console.log(token.toString());
|
|
57
|
+
logger.info('token present:', Boolean(token));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## What it looks at
|
|
61
|
+
|
|
62
|
+
A call counts as a log call under the same rules as
|
|
63
|
+
[`no-log-reveal`](no-log-reveal.md): a method whose name is in `logFunctions`
|
|
64
|
+
with some name along its callee chain in `logObjects`, or a bare call whose
|
|
65
|
+
function name is in `logObjects`. The two rules share one matcher, so a logger
|
|
66
|
+
configured for one is recognised by the other.
|
|
67
|
+
|
|
68
|
+
Every argument's type is then walked: union and intersection members, array and
|
|
69
|
+
tuple elements, base classes, object properties and index signatures.
|
|
70
|
+
|
|
71
|
+
**Logging the value itself is not reported.** `console.log(token)` and
|
|
72
|
+
`console.log(tokens)` are the documented way to log a sensitive value, and an
|
|
73
|
+
array delegates to each element, so both are left alone. What the rule reports
|
|
74
|
+
is a *structure* that holds one, where the redaction stops being the class's
|
|
75
|
+
business and becomes the transport's. A `serialize()` result is reported
|
|
76
|
+
wherever it sits, because nothing redacts one anywhere.
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
| Option | Default |
|
|
81
|
+
|---|---|
|
|
82
|
+
| `logFunctions` | `log`, `info`, `warn`, `error`, `debug`, `trace`, `fatal`, `verbose`, `silly`, `write` |
|
|
83
|
+
| `logObjects` | `console`, `logger`, `log`, `winston`, `pino`, `bunyan`, `stdout`, `stderr` |
|
|
84
|
+
| `sensitiveTypes` | `SensitiveString` |
|
|
85
|
+
| `serializedTypes` | `SerializedSensitiveString` |
|
|
86
|
+
|
|
87
|
+
A configured list replaces the default rather than extending it.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
{
|
|
91
|
+
'@platform-sec/sensitive-string/prefer-safe-log': [
|
|
92
|
+
'warn',
|
|
93
|
+
{ logObjects: ['console', 'logger', 'report'], sensitiveTypes: ['SensitiveString', 'ApiKey'] },
|
|
94
|
+
],
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Why there is no suggestion
|
|
99
|
+
|
|
100
|
+
Wrapping the argument in `safeLog(...)` is a safe rewrite, and it is still not
|
|
101
|
+
offered. `safeLog` has to be imported, and an editor suggestion that inserts a
|
|
102
|
+
call to a function the file does not import produces code that does not compile.
|
|
103
|
+
Nothing in this plugin rewrites code without a person reading the diff.
|
|
104
|
+
|
|
105
|
+
## No type information
|
|
106
|
+
|
|
107
|
+
Applied to a file with no TypeScript program, the rule reports
|
|
108
|
+
`missingTypeInformation` once, on the file, rather than falling silent. See
|
|
109
|
+
[`no-stringify-sensitive`](no-stringify-sensitive.md#no-type-information) for
|
|
110
|
+
the mixed-project configuration that turns it off where there is no program.
|
|
111
|
+
|
|
112
|
+
## Known limits
|
|
113
|
+
|
|
114
|
+
- **The match is by name.** Any type called `SensitiveString` is treated as one,
|
|
115
|
+
and a project's own wrapper is invisible until `sensitiveTypes` names it.
|
|
116
|
+
- **A cast to a weaker type hides the field**, and `any` or `unknown` gives the
|
|
117
|
+
rule nothing to walk.
|
|
118
|
+
- **A `Map` or a `Set` holding sensitive values is not walked.** Its entries
|
|
119
|
+
redact themselves under `util.inspect` and disappear under `JSON.stringify`,
|
|
120
|
+
so there is nothing this rule would add.
|
|
121
|
+
- **A revealed string is invisible**, because its type is `string`.
|
|
122
|
+
`no-log-reveal` is the rule for that, and it needs no types.
|
|
123
|
+
- `console.log(token.serialize())` is reported here *and* by `no-log-reveal`,
|
|
124
|
+
which sees the call rather than the type. The two findings agree on the fix.
|
|
125
|
+
|
|
126
|
+
## Related
|
|
127
|
+
|
|
128
|
+
- [`no-log-reveal`](no-log-reveal.md), the untyped rule for a revealed value
|
|
129
|
+
reaching a logger.
|
|
130
|
+
- [`no-stringify-sensitive`](no-stringify-sensitive.md), the same question asked
|
|
131
|
+
of `JSON.stringify`.
|