@sanity-labs/secret-scan 0.1.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 +25 -0
- package/README.md +125 -0
- package/dist/index.cjs +3142 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +3142 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sanity Labs
|
|
4
|
+
|
|
5
|
+
This software includes rules derived from gitleaks
|
|
6
|
+
(https://github.com/gitleaks/gitleaks), which is MIT licensed:
|
|
7
|
+
Copyright (c) 2019 Zachary Rice
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @sanity-labs/secret-scan
|
|
2
|
+
|
|
3
|
+
Detect and redact secrets in strings. Works in browser and Node.js. Zero runtime dependencies.
|
|
4
|
+
|
|
5
|
+
Rules derived from [gitleaks](https://github.com/gitleaks/gitleaks) (MIT licensed) — 221 rules covering API keys, tokens, passwords, and credentials from 100+ providers.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @sanity-labs/secret-scan
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### `scan` — find secrets
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { scan } from '@sanity-labs/secret-scan'
|
|
19
|
+
|
|
20
|
+
const secrets = scan('OPENAI_API_KEY=sk-proj-abc123...\nMODE=production')
|
|
21
|
+
// [
|
|
22
|
+
// {
|
|
23
|
+
// rule: 'openai-api-key',
|
|
24
|
+
// label: 'OpenAI API Key',
|
|
25
|
+
// text: 'sk-proj-abc123...',
|
|
26
|
+
// confidence: 'high',
|
|
27
|
+
// start: 15,
|
|
28
|
+
// end: 32
|
|
29
|
+
// }
|
|
30
|
+
// ]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### `redact` — find and replace secrets
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { redact } from '@sanity-labs/secret-scan'
|
|
37
|
+
|
|
38
|
+
const secrets = new Map()
|
|
39
|
+
let nextId = 0
|
|
40
|
+
|
|
41
|
+
const redacted = redact(pastedText, (secret) => {
|
|
42
|
+
const key = `[secret:${nextId++}]`
|
|
43
|
+
secrets.set(key, secret)
|
|
44
|
+
return key
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// redacted: "OPENAI_API_KEY=[secret:0]\nSTRIPE_KEY=[secret:1]"
|
|
48
|
+
// secrets: Map { '[secret:0]' => { text: 'sk-proj-...' }, ... }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
### `scan(input: string): Secret[]`
|
|
54
|
+
|
|
55
|
+
Returns an array of every secret found in the input.
|
|
56
|
+
|
|
57
|
+
### `redact(input: string, replacer: (secret: Secret) => string): string`
|
|
58
|
+
|
|
59
|
+
Calls `replacer` for each detected secret. The return value replaces the secret in the output string. The caller owns all state — `redact` just does string replacement.
|
|
60
|
+
|
|
61
|
+
### `Secret`
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
interface Secret {
|
|
65
|
+
rule: string // gitleaks rule ID, e.g. 'openai-api-key'
|
|
66
|
+
label: string // human-readable, e.g. 'OpenAI API Key'
|
|
67
|
+
text: string // the matched secret value
|
|
68
|
+
confidence: 'high' | 'medium' // provider pattern vs entropy-based
|
|
69
|
+
start: number // start index in input
|
|
70
|
+
end: number // end index (exclusive) in input
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `shannonEntropy(s: string): number`
|
|
75
|
+
|
|
76
|
+
Shannon entropy calculation. Exported for advanced use cases.
|
|
77
|
+
|
|
78
|
+
## How it works
|
|
79
|
+
|
|
80
|
+
1. **Keyword pre-filter** — Each rule has keywords. Before running a regex, we check if the input contains any of its keywords (case-insensitive). This keeps scanning fast with 221 rules — most regexes are skipped for any given input.
|
|
81
|
+
|
|
82
|
+
2. **Regex matching** — Rules are compiled from [gitleaks.toml](https://github.com/gitleaks/gitleaks/blob/master/config/gitleaks.toml) with Go→JS regex conversion (named groups, inline flags, dotall).
|
|
83
|
+
|
|
84
|
+
3. **Entropy filtering** — Many rules have Shannon entropy thresholds. Low-entropy matches (like `KEY=aaaaaaa`) are filtered out.
|
|
85
|
+
|
|
86
|
+
4. **Allowlist filtering** — Global and per-rule allowlists filter false positives. Includes 1,446 stopwords for the generic-api-key rule.
|
|
87
|
+
|
|
88
|
+
## Updating rules
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm run update-rules
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Fetches the latest `gitleaks.toml` from GitHub, converts Go regex → JS regex, and writes `src/rules.ts`. Run this whenever gitleaks updates their rules.
|
|
95
|
+
|
|
96
|
+
### Go → JS regex conversion
|
|
97
|
+
|
|
98
|
+
| Go pattern | JS equivalent | Notes |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| `(?P<name>...)` | `(?<name>...)` | Named groups |
|
|
101
|
+
| `(?i)` at start | `i` flag | Case-insensitive |
|
|
102
|
+
| `(?i:...)` mid-pattern | `(?:...)` + `i` flag | Promoted to global flag |
|
|
103
|
+
| `(?-i:...)` | `(?:...)` | Groups already enumerate cases |
|
|
104
|
+
| `(?s:.)` | `[\s\S]` | Dotall |
|
|
105
|
+
| `\z` | `$` | End of string |
|
|
106
|
+
|
|
107
|
+
## Rules coverage
|
|
108
|
+
|
|
109
|
+
221 rules from gitleaks covering:
|
|
110
|
+
|
|
111
|
+
- **Cloud providers**: AWS, GCP, Azure, DigitalOcean, Heroku, Fly.io, etc.
|
|
112
|
+
- **AI/ML**: OpenAI, Anthropic, Cohere, HuggingFace, Perplexity
|
|
113
|
+
- **Payment**: Stripe, Square, Plaid, Coinbase, Flutterwave
|
|
114
|
+
- **DevOps**: GitHub, GitLab, Bitbucket, CircleCI, Travis CI, Jenkins
|
|
115
|
+
- **Communication**: Slack, Discord, Telegram, Twilio, SendGrid
|
|
116
|
+
- **Databases**: PlanetScale, MongoDB Atlas, ClickHouse
|
|
117
|
+
- **And 80+ more providers**
|
|
118
|
+
|
|
119
|
+
Plus the `generic-api-key` rule which catches `KEY=value` patterns with entropy thresholds and 1,446 stopwords.
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT — includes gitleaks copyright notice per their license terms.
|
|
124
|
+
|
|
125
|
+
This package uses rules derived from [gitleaks](https://github.com/gitleaks/gitleaks), which is also MIT licensed. Copyright (c) 2019 Zachary Rice.
|