@platform-sec/eslint-plugin-sensitive-string 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +193 -2
- 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 +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 AppSec Science, LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,194 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @platform-sec/eslint-plugin-sensitive-string
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lint rules for the mistakes
|
|
4
|
+
[`@platform-sec/sensitive-string-core`](../sensitive-core)'s type system cannot
|
|
5
|
+
catch.
|
|
6
|
+
|
|
7
|
+
A type keeps a secret wrapped. It cannot stop `console.log(secret.reveal())`,
|
|
8
|
+
because the reveal produces an ordinary string and logging a string is ordinary
|
|
9
|
+
code. The same goes for `password.reveal() === input`, which type-checks
|
|
10
|
+
perfectly and leaks the password to a timing attack. Nine rules read the call
|
|
11
|
+
instead: seven from its shape alone, two from the types of its arguments.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add -D @platform-sec/eslint-plugin-sensitive-string
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
ESLint 8.40 or newer is a peer dependency, and nothing else is. The seven
|
|
20
|
+
untyped rules work from the syntax alone, so they run under any parser and cost
|
|
21
|
+
nothing beyond the parse.
|
|
22
|
+
|
|
23
|
+
The other two ask what a value's type *contains*, which no syntax answers, so
|
|
24
|
+
they need a TypeScript program: `@typescript-eslint/parser` with `project` or
|
|
25
|
+
`projectService` set. They live only in the `-type-checked` presets, so a
|
|
26
|
+
project that has not configured one never meets them.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Flat config, `eslint.config.js`:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import sensitive from '@platform-sec/eslint-plugin-sensitive-string';
|
|
34
|
+
|
|
35
|
+
export default [sensitive.configs.recommended];
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
eslintrc, `.eslintrc.cjs`:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
module.exports = {
|
|
42
|
+
extends: ['plugin:@platform-sec/sensitive-string/legacy-recommended'],
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or set rules individually:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
{
|
|
50
|
+
plugins: { '@platform-sec/sensitive-string': sensitive },
|
|
51
|
+
rules: {
|
|
52
|
+
'@platform-sec/sensitive-string/no-log-reveal': 'error',
|
|
53
|
+
'@platform-sec/sensitive-string/no-credential-equality': 'error',
|
|
54
|
+
'@platform-sec/sensitive-string/no-network-sensitive': 'error',
|
|
55
|
+
'@platform-sec/sensitive-string/explicit-class': 'warn',
|
|
56
|
+
'@platform-sec/sensitive-string/require-reveal-context': 'warn',
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Those six are the untyped rules. `no-stringify-sensitive` and `prefer-safe-log`
|
|
62
|
+
are set the same way, under a config that gives them a program: see
|
|
63
|
+
[Type-aware linting](#type-aware-linting).
|
|
64
|
+
|
|
65
|
+
The plugin's package name is `eslint-plugin-sensitive-string` under the
|
|
66
|
+
`@platform-sec` scope, so ESLint addresses it as
|
|
67
|
+
`@platform-sec/sensitive-string`.
|
|
68
|
+
|
|
69
|
+
## Rules
|
|
70
|
+
|
|
71
|
+
Syntax only, in `recommended`:
|
|
72
|
+
|
|
73
|
+
| Rule | `recommended` | Catches |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| [no-log-reveal](docs/rules/no-log-reveal.md) | error | `console.log(x.reveal())`, `logger.info({ k: x.reveal() })` |
|
|
76
|
+
| [no-credential-equality](docs/rules/no-credential-equality.md) | error | `x.reveal() === y`, `switch (x.reveal())` |
|
|
77
|
+
| [no-network-sensitive](docs/rules/no-network-sensitive.md) | error | `fetch(url, { body: x.reveal() })` |
|
|
78
|
+
| [explicit-class](docs/rules/explicit-class.md) | warn | `new SensitiveString(v)` with no class |
|
|
79
|
+
| [require-reveal-context](docs/rules/require-reveal-context.md) | warn | `x.reveal()` with no context and no enclosing `withRevealContext`, and the same for `mask()`, `hash()`, `hmac()` and `serialize()` where types are available |
|
|
80
|
+
| [no-password-fast-hash](docs/rules/no-password-fast-hash.md) | warn | `password.hash('sha256')`, a fast digest where a KDF belongs |
|
|
81
|
+
| [no-allow-unaudited](docs/rules/no-allow-unaudited.md) | error | `fromWorkerPayload(d, { allowUnaudited: true })`, which drops an audit hook the sender asked for |
|
|
82
|
+
|
|
83
|
+
Type-aware, in `recommended-type-checked` only:
|
|
84
|
+
|
|
85
|
+
| Rule | `recommended-type-checked` | Catches |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| [no-stringify-sensitive](docs/rules/no-stringify-sensitive.md) | error | `JSON.stringify(payload)` where `payload` holds a `SensitiveString` or a `serialize()` result |
|
|
88
|
+
| [prefer-safe-log](docs/rules/prefer-safe-log.md) | warn | `console.log(user)` where `user` holds one |
|
|
89
|
+
|
|
90
|
+
## Presets
|
|
91
|
+
|
|
92
|
+
| Preset | Format | Rules |
|
|
93
|
+
|---|---|---|
|
|
94
|
+
| `configs.recommended` | flat | the seven untyped |
|
|
95
|
+
| `configs.strict` | flat | the seven untyped, harder |
|
|
96
|
+
| `configs['recommended-type-checked']` | flat | all nine |
|
|
97
|
+
| `configs['strict-type-checked']` | flat | all nine, harder |
|
|
98
|
+
| `configs['disable-type-checked']` | flat | turns the two type-aware rules off |
|
|
99
|
+
| `configs['legacy-recommended']` | eslintrc | the seven untyped |
|
|
100
|
+
| `configs['legacy-strict']` | eslintrc | the seven untyped, harder |
|
|
101
|
+
| `configs['legacy-recommended-type-checked']` | eslintrc | all nine |
|
|
102
|
+
| `configs['legacy-strict-type-checked']` | eslintrc | all nine, harder |
|
|
103
|
+
| `configs['legacy-disable-type-checked']` | eslintrc | turns the two type-aware rules off |
|
|
104
|
+
|
|
105
|
+
Both formats configure the same rules at the same severities, and a test holds
|
|
106
|
+
them to it.
|
|
107
|
+
|
|
108
|
+
`recommended` errors on the three rules that catch a value escaping, and on
|
|
109
|
+
`no-allow-unaudited`, which catches an audit control being switched off and is
|
|
110
|
+
never wrong about it. It warns on the three that reason about something they
|
|
111
|
+
cannot see: a context installed in a caller, a class supplied through a spread,
|
|
112
|
+
and a password recognised by what the variable holding it is called. A warning
|
|
113
|
+
is the honest severity for a check that can be wrong.
|
|
114
|
+
|
|
115
|
+
`strict` raises everything to error, requires a `reason` in every reveal
|
|
116
|
+
context, and sets `allowWithContext: false` on `no-network-sensitive`, so the
|
|
117
|
+
only way a value leaves the process is `serialize()`.
|
|
118
|
+
|
|
119
|
+
## Type-aware linting
|
|
120
|
+
|
|
121
|
+
The `-type-checked` presets add `no-stringify-sensitive` and `prefer-safe-log`
|
|
122
|
+
to their untyped namesakes. Both need a `ts.Program`:
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
import sensitive from '@platform-sec/eslint-plugin-sensitive-string';
|
|
126
|
+
import tseslint from 'typescript-eslint';
|
|
127
|
+
|
|
128
|
+
export default [
|
|
129
|
+
{
|
|
130
|
+
files: ['**/*.ts'],
|
|
131
|
+
languageOptions: {
|
|
132
|
+
parser: tseslint.parser,
|
|
133
|
+
parserOptions: { projectService: true, tsconfigRootDir: import.meta.dirname },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
sensitive.configs['recommended-type-checked'],
|
|
137
|
+
// Files no tsconfig.json covers have no types to read.
|
|
138
|
+
{ files: ['**/*.js'], ...sensitive.configs['disable-type-checked'] },
|
|
139
|
+
];
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Applied to a file with no program, both rules report that fact once, on the
|
|
143
|
+
file, rather than falling silent. Silence would be the worse failure: a rule
|
|
144
|
+
that reports nothing looks exactly like a rule that found nothing, and these
|
|
145
|
+
two get read as "no sensitive value is being stringified or logged here".
|
|
146
|
+
`disable-type-checked` is what turns them off where that is expected.
|
|
147
|
+
|
|
148
|
+
`require-reveal-context` is a third case: it works without a program and uses
|
|
149
|
+
one when it is there. `reveal()` is matched by name in every configuration;
|
|
150
|
+
`mask()`, `hash()`, `hmac()` and `serialize()` are checked only where the
|
|
151
|
+
receiver's type can be read, because `bcrypt` and `node:crypto` have a `hash`,
|
|
152
|
+
every ORM has a `serialize`, and a rule that reported those would be turned off
|
|
153
|
+
within the hour. It stays enabled under `disable-type-checked`, where it does
|
|
154
|
+
the half of its job that needs no types.
|
|
155
|
+
|
|
156
|
+
## Suggestions, not fixes
|
|
157
|
+
|
|
158
|
+
No rule here is auto-fixable. Two offer editor suggestions, which a person
|
|
159
|
+
accepts one at a time:
|
|
160
|
+
|
|
161
|
+
- `no-credential-equality` rewrites `a.reveal() === b` to `a.equals(b)`,
|
|
162
|
+
carrying any reveal context across into `equals()`'s second parameter.
|
|
163
|
+
- `explicit-class` offers each sensitivity class.
|
|
164
|
+
|
|
165
|
+
`--fix` rewrites files with nobody reading the diff, and every rewrite these
|
|
166
|
+
rules would make changes behaviour: `equals()` can throw where `reveal()` did
|
|
167
|
+
not, and a sensitivity class cannot be guessed from syntax at all.
|
|
168
|
+
|
|
169
|
+
## Precision
|
|
170
|
+
|
|
171
|
+
Every rule that asks for an argument treats an explicit `undefined` as the
|
|
172
|
+
absence of one: `reveal(undefined)` and `new SensitiveString(v, undefined)`
|
|
173
|
+
satisfy a signature and decide nothing, so accepting them would make passing
|
|
174
|
+
`undefined` the shortest way to silence a rule while looking like compliance
|
|
175
|
+
with it.
|
|
176
|
+
|
|
177
|
+
Beyond that, every rule has a shape it cannot see and a shape it reports too
|
|
178
|
+
eagerly. Each rule's documentation ends with a "Known limits" section saying
|
|
179
|
+
which. Three general points:
|
|
180
|
+
|
|
181
|
+
- **A logger or HTTP client under an unfamiliar name is invisible.** Every rule
|
|
182
|
+
that matches one takes an option naming the ones a project uses.
|
|
183
|
+
- **A value revealed into a variable and used later is not tracked.** Reveal at
|
|
184
|
+
the point of use, which is better practice anyway: it keeps the raw string's
|
|
185
|
+
lifetime as short as the library can make it.
|
|
186
|
+
- **The two type-aware rules match a type by its name**, so anything called
|
|
187
|
+
`SensitiveString` counts and a project's own wrapper does not until
|
|
188
|
+
`sensitiveTypes` names it. Resolving the declaration back to this library
|
|
189
|
+
would be more precise and would break under bundlers, path aliases and
|
|
190
|
+
re-exports.
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# explicit-class
|
|
2
|
+
|
|
3
|
+
Require an explicit sensitivity class when constructing a `SensitiveString`.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **warn**. Offers suggestions.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
The class decides how the value redacts and whether `equals()` compares in
|
|
10
|
+
constant time. A value constructed without one is not a weaker secret; it is a
|
|
11
|
+
broken object, since the constructor takes no default.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
Incorrect:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
new SensitiveString(value);
|
|
19
|
+
new core.SensitiveString(value);
|
|
20
|
+
|
|
21
|
+
// A placeholder names no class. The suggestion overwrites it rather than
|
|
22
|
+
// pushing it into the `label` position, where it is accepted and means nothing.
|
|
23
|
+
new SensitiveString(value, undefined);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Correct:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
new SensitiveString(value, 'credential');
|
|
30
|
+
new SensitiveString(value, 'sensitive', 'email');
|
|
31
|
+
new SensitiveString(value, klass);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Options
|
|
35
|
+
|
|
36
|
+
| Option | Default |
|
|
37
|
+
|---|---|
|
|
38
|
+
| `constructorNames` | `SensitiveString` |
|
|
39
|
+
|
|
40
|
+
Set it to cover a re-exported or renamed constructor.
|
|
41
|
+
|
|
42
|
+
## Relationship to the type checker
|
|
43
|
+
|
|
44
|
+
The TypeScript signature already requires the class, so in a checked TypeScript
|
|
45
|
+
file this rule is a second line rather than the only one. It earns its place in
|
|
46
|
+
JavaScript sources, at `any`-typed call sites where the checker is silent, and
|
|
47
|
+
in a project whose build and lint run at different times.
|
|
48
|
+
|
|
49
|
+
## Suggestions
|
|
50
|
+
|
|
51
|
+
Two are offered, one per class, and neither is applied by `--fix`. The right
|
|
52
|
+
class is a security decision about the value being wrapped, and nothing in the
|
|
53
|
+
syntax reveals it. `credential` is the one to pick for anything compared against
|
|
54
|
+
user input.
|
|
55
|
+
|
|
56
|
+
A construction with no arguments at all is reported without suggestions: it is
|
|
57
|
+
broken in a way no sensitivity class fixes.
|
|
58
|
+
|
|
59
|
+
## Known limits
|
|
60
|
+
|
|
61
|
+
A spread argument makes the argument count unknowable, so
|
|
62
|
+
`new SensitiveString(...args)` is not reported.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# no-allow-unaudited
|
|
2
|
+
|
|
3
|
+
Disallow `allowUnaudited` on `fromWorkerPayload()` and `withWorkerPayload()`.
|
|
4
|
+
|
|
5
|
+
Severity in `recommended`: **error**.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
A `SensitiveString` crosses a worker thread or a child process through
|
|
10
|
+
`toWorkerPayload()`, which carries the sending context's `name`, `reason` and
|
|
11
|
+
`tags` along with the values. It cannot carry the context's `auditReveal`,
|
|
12
|
+
because a function does not survive structured clone or JSON, so a payload built
|
|
13
|
+
under an audited context arrives with `audited: true` and no hook.
|
|
14
|
+
|
|
15
|
+
`fromWorkerPayload()` refuses such a payload unless the receiving side supplies
|
|
16
|
+
a hook of its own, either through the `auditReveal` option or from an ambient
|
|
17
|
+
`withRevealContext()`. `allowUnaudited: true` is the way past that refusal, and
|
|
18
|
+
what it means is precise: the sender asked for every reveal of this value to be
|
|
19
|
+
recorded, and reveals in this thread will not be.
|
|
20
|
+
|
|
21
|
+
That is sometimes the right call, which is why the option exists. It is never a
|
|
22
|
+
call to make by accident, which is why this rule reports every use of it and
|
|
23
|
+
why the severity is error rather than warn: the flag is always deliberate, and
|
|
24
|
+
an inline disable comment is the right place for the reason it was deliberate.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
Incorrect:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
fromWorkerPayload(workerData, { allowUnaudited: true });
|
|
32
|
+
withWorkerPayload(workerData, { allowUnaudited: true }, ({ token }) => sign(token));
|
|
33
|
+
fromWorkerPayload(workerData, { auditReveal, allowUnaudited: true });
|
|
34
|
+
fromWorkerPayload(workerData, { allowUnaudited: skipAudit });
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Correct:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
fromWorkerPayload(workerData, {
|
|
41
|
+
auditReveal: (value, details) => log.info('reveal', { klass: value.klass, ...details }),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
withWorkerPayload(workerData, { auditReveal }, ({ token }) => sign(token));
|
|
45
|
+
|
|
46
|
+
// Inside a context that already carries a hook, which the reverse direction
|
|
47
|
+
// of the handoff relies on: a parent handler registered inside
|
|
48
|
+
// withRevealContext() consumes a worker's reply with no options at all.
|
|
49
|
+
fromWorkerPayload(message);
|
|
50
|
+
|
|
51
|
+
// The default spelled out.
|
|
52
|
+
fromWorkerPayload(workerData, { allowUnaudited: false });
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
When the flag is the right answer:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
// eslint-disable-next-line @platform-sec/sensitive-string/no-allow-unaudited -- this worker only hashes the value and the digest is what gets recorded, downstream
|
|
59
|
+
fromWorkerPayload(workerData, { allowUnaudited: true });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Options
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"@platform-sec/sensitive-string/no-allow-unaudited": ["error", {
|
|
67
|
+
"payloadFunctions": ["fromWorkerPayload", "withWorkerPayload"]
|
|
68
|
+
}]
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`payloadFunctions` names the functions the flag is read on. A configured list
|
|
73
|
+
replaces the default, for a project that wraps the two library functions in one
|
|
74
|
+
of its own.
|
|
75
|
+
|
|
76
|
+
## Known limits
|
|
77
|
+
|
|
78
|
+
- The flag is read from an object literal in the call. An options object built
|
|
79
|
+
elsewhere and passed by name is invisible to syntax, and so is a spread that
|
|
80
|
+
contributes it.
|
|
81
|
+
- A `false` literal and an explicit `undefined` are not reported. Any other
|
|
82
|
+
value is, a variable included, because `allowUnaudited: flag` is still a
|
|
83
|
+
deliberate decision to sometimes skip the audit.
|
|
84
|
+
- The functions are matched by name. A project that names something else
|
|
85
|
+
`fromWorkerPayload` is reported on that too, and `payloadFunctions` is the
|
|
86
|
+
fix.
|
|
87
|
+
- An aliased import is invisible for the same reason:
|
|
88
|
+
`import { fromWorkerPayload as fwp }` and then `fwp(workerData, { ... })` is
|
|
89
|
+
not reported, because resolving the alias needs types this rule does not
|
|
90
|
+
take. Add the local name to `payloadFunctions`.
|
|
91
|
+
- `allowUnaudited: 0`, `null` or `''` are reported although the runtime reads
|
|
92
|
+
anything but `true` as unset. That is deliberate: each is a confusing way to
|
|
93
|
+
write the default, and the rule would rather ask for `false`.
|
|
@@ -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`.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# require-reveal-context
|
|
2
|
+
|
|
3
|
+
Require an audit context on `reveal()`, and on `mask()`, `hash()`, `hmac()` and
|
|
4
|
+
`serialize()` where types are available, from an argument or an enclosing
|
|
5
|
+
`withRevealContext()`.
|
|
6
|
+
|
|
7
|
+
Severity in `recommended`: **warn**.
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
The audit hook records that a value was read. Without a context it cannot record
|
|
12
|
+
*who* read it or *why*, which is the part an incident review needs.
|
|
13
|
+
|
|
14
|
+
Four other methods are read the same way by the library. `mask()` returns a
|
|
15
|
+
partial view as a plain string and `serialize()` returns the raw value in a wire
|
|
16
|
+
object; `hash()` and `hmac()` keep the result wrapped but reach the value
|
|
17
|
+
through the same gate, charge the same reveal budget, and write the same audit
|
|
18
|
+
record with `operation: 'hash'` on it. A record saying a credential was read,
|
|
19
|
+
with nothing about who read it, is the gap this rule exists to close, whichever
|
|
20
|
+
method produced it.
|
|
21
|
+
|
|
22
|
+
What is not covered is the transforms, `substring` and `map` and the rest. They
|
|
23
|
+
audit too, and requiring a context on `token.trim()` would make the rule
|
|
24
|
+
unusable long before it made an audit log better.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
Incorrect:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const raw = secret.reveal();
|
|
32
|
+
secret.reveal(undefined);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
// Only where types are available; see below.
|
|
37
|
+
const lookupKey = token.hash('sha256');
|
|
38
|
+
const pseudonym = email.hmac('sha256', key);
|
|
39
|
+
const shown = card.mask({ showLast: 4 });
|
|
40
|
+
const payload = token.serialize();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Correct:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
const raw = secret.reveal({ name: 'payment', reason: 'charging the card' });
|
|
47
|
+
|
|
48
|
+
await withRevealContext({ name: 'handler' }, async () => {
|
|
49
|
+
const raw = secret.reveal(); // context comes from AsyncLocalStorage
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
const lookupKey = token.hash('sha256', {}, { name: 'session-lookup' });
|
|
55
|
+
const pseudonym = email.hmac('sha256', key, {}, ctx);
|
|
56
|
+
const shown = card.mask({ showLast: 4 }, { name: 'support-screen' });
|
|
57
|
+
const payload = token.serialize({ name: 'transfer' });
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The context is the argument each method actually takes it in: the first for
|
|
61
|
+
`reveal()` and `serialize()`, the second for `mask()`, the third for `hash()`,
|
|
62
|
+
the fourth for `hmac()`. An options object in the position before it is not a
|
|
63
|
+
context, and is reported as one missing.
|
|
64
|
+
|
|
65
|
+
## What needs type information
|
|
66
|
+
|
|
67
|
+
`reveal()` is matched by its name, in every configuration. The other four are
|
|
68
|
+
matched only when the receiver's type can be read and is a `SensitiveString` or
|
|
69
|
+
something extending one. `reveal` by name and everything else by type is the
|
|
70
|
+
whole rule in a line.
|
|
71
|
+
|
|
72
|
+
That asymmetry is deliberate. `magician.reveal()` being reported is the standing
|
|
73
|
+
cost of an untyped rule, and `.reveal()` is rare enough for that to be a fair
|
|
74
|
+
trade. The others are not: `bcrypt` and `node:crypto` have a `hash`, every ORM
|
|
75
|
+
and form library has a `serialize`, `mask` belongs to half the input libraries
|
|
76
|
+
on npm, and a rule that reported those in a codebase would be turned off within
|
|
77
|
+
the hour.
|
|
78
|
+
|
|
79
|
+
In a file with no program the rule stays on and checks `reveal()` alone. It does
|
|
80
|
+
not report the missing type information the way `no-stringify-sensitive` does,
|
|
81
|
+
because it is not reduced to silence: most of its job needs no types.
|
|
82
|
+
|
|
83
|
+
The same goes for a receiver the checker knows nothing about. A value typed
|
|
84
|
+
`any` is not a wrapper as far as this rule can tell, so a `.hash()` on one is
|
|
85
|
+
not reported. That is the direction to miss in: the alternative is reporting
|
|
86
|
+
every `.hash()` call in a file that lost its types to one `any`.
|
|
87
|
+
|
|
88
|
+
## Options
|
|
89
|
+
|
|
90
|
+
| Option | Default |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `requireReason` | `false` |
|
|
93
|
+
| `contextFunctions` | `withRevealContext`, `withAuditedReveals` |
|
|
94
|
+
| `sensitiveTypes` | `SensitiveString` |
|
|
95
|
+
|
|
96
|
+
`requireReason: true` additionally reports a context object literal with no
|
|
97
|
+
`reason` key. An object built elsewhere, or one carrying a spread, is left
|
|
98
|
+
alone: reading what it contributes means following a value rather than naming a
|
|
99
|
+
type, which is not what the receiver check above does, and warning on a context
|
|
100
|
+
that is very likely complete would train people to disable the rule. The `strict` preset sets it.
|
|
101
|
+
|
|
102
|
+
## Why this is a warning
|
|
103
|
+
|
|
104
|
+
The enclosing-context check is lexical. It sees
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
withRevealContext(ctx, () => secret.reveal());
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
and misses a reveal in a function that is merely *called* from inside one:
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
withRevealContext(ctx, () => readSecret());
|
|
114
|
+
function readSecret() {
|
|
115
|
+
return secret.reveal(); // reported, though the context is present at runtime
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Following that would need a call graph. The rule is therefore wrong in the
|
|
120
|
+
direction of a needless warning, never a missed one, and a warning is the honest
|
|
121
|
+
severity for a check with that shape.
|
|
122
|
+
|
|
123
|
+
`reveal` is also matched by name, so an unrelated `magician.reveal()` is
|
|
124
|
+
reported. That is the standing cost of a rule that reads syntax rather than
|
|
125
|
+
types, and the reason the other four are not matched that way.
|
|
126
|
+
|
|
127
|
+
A spread ahead of the context argument, `secret.hash(...args)`, is left alone:
|
|
128
|
+
which argument is the context stops being a question syntax can answer.
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platform-sec/eslint-plugin-sensitive-string",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "ESLint rules that catch the mistakes SensitiveString's type system cannot: logged reveals, timing-unsafe comparisons, and unaudited transmission",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"docs"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"test": "vitest run --root ../.. packages/sensitive-eslint-plugin/",
|
|
21
|
+
"test:watch": "vitest --root ../.. packages/sensitive-eslint-plugin/"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"eslint",
|
|
25
|
+
"eslintplugin",
|
|
26
|
+
"eslint-plugin",
|
|
27
|
+
"security",
|
|
28
|
+
"sensitive-data",
|
|
29
|
+
"redaction",
|
|
30
|
+
"static-analysis",
|
|
31
|
+
"typescript"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/platform-sec/sensitive-string-js.git",
|
|
37
|
+
"directory": "packages/sensitive-eslint-plugin"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22.12.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"eslint": ">=8.40.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^22.20.1",
|
|
47
|
+
"@typescript-eslint/parser": "^8.69.0",
|
|
48
|
+
"eslint": "^9.39.5",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"vitest": "^4.1.11"
|
|
51
|
+
}
|
|
52
|
+
}
|