@betterleaks/sdk 0.0.0-stage → 2.0.0-rc.13
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 +71 -2
- package/index.d.ts +66 -0
- package/index.js +53 -0
- package/package.json +20 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zachary Rice
|
|
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,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @betterleaks/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Scan text and local files for leaked credentials from a Node.js application. npm installs the prebuilt native addon for your OS and CPU. Scans run in the Node process, off the JavaScript thread. The SDK installation includes no CLI executable.
|
|
4
|
+
|
|
5
|
+
## Try it in a new project
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
mkdir secret-scanner
|
|
9
|
+
cd secret-scanner
|
|
10
|
+
npm init -y
|
|
11
|
+
npm install @betterleaks/sdk@next
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Save the following as `scan.mjs`.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { scanFile, scanText } from "@betterleaks/sdk";
|
|
18
|
+
|
|
19
|
+
const path = process.argv[2];
|
|
20
|
+
const report = path ? await scanFile(path) : await scanText("hello world");
|
|
21
|
+
console.log(`Findings: ${report.summary.findings}`);
|
|
22
|
+
for (const { rule_id, location } of report.findings) {
|
|
23
|
+
console.log(rule_id, location.path ?? "<text>", location.start_line);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Run `node scan.mjs` for an empty result, or `node scan.mjs settings.env` to scan an existing file. Findings can contain credentials in `match.full` and `match.value`. The example logs only rule IDs and locations.
|
|
28
|
+
|
|
29
|
+
CommonJS supports `const { scanText, scanFile, createScanner } = require("@betterleaks/sdk")`. TypeScript declarations are included. Both scan functions return a promise. The SDK allows up to 8 MiB of input and 10,000 findings per scan. A file error or an exceeded limit rejects the promise.
|
|
30
|
+
|
|
31
|
+
## Use your own rules
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { createScanner } from "@betterleaks/sdk";
|
|
35
|
+
|
|
36
|
+
const scanner = await createScanner({ configPath: "./betterleaks.toml" });
|
|
37
|
+
try {
|
|
38
|
+
const report = await scanner.scanFile("settings.env");
|
|
39
|
+
console.log(report.summary.findings);
|
|
40
|
+
} finally {
|
|
41
|
+
scanner.close();
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
You can also pass TOML directly. This example adds a rule to the built-in rules.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
const scanner = await createScanner({
|
|
49
|
+
configToml: `
|
|
50
|
+
[extend]
|
|
51
|
+
useDefault = true
|
|
52
|
+
|
|
53
|
+
[[rules]]
|
|
54
|
+
id = "internal-token"
|
|
55
|
+
regex = 'INTERNAL_[A-Z]{12}'
|
|
56
|
+
`,
|
|
57
|
+
});
|
|
58
|
+
try {
|
|
59
|
+
const report = await scanner.scanText("token=INTERNAL_ABCDEFGHIJKL");
|
|
60
|
+
console.log(report.findings.map(finding => finding.rule_id));
|
|
61
|
+
} finally {
|
|
62
|
+
scanner.close();
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`createScanner` reads and compiles the configuration once, off the JavaScript thread. A missing file or invalid configuration rejects its promise. If you pass both options, the scanner reads `configToml` and uses `configPath` as its source name for diagnostics. Use configuration you trust, since TOML extensions can read local files.
|
|
67
|
+
|
|
68
|
+
Reuse a scanner for concurrent text and file scans. Call `close()` when finished to release the native scanner as soon as pending scans complete. Each scanner has its own rules. The top-level `scanText` and `scanFile` functions always use the built-in rules.
|
|
69
|
+
|
|
70
|
+
The native addon supports macOS 12+ and glibc Linux 2.35+ on x64 and arm64. Keep optional dependencies enabled and install them for the platform where Node will run. Installation works with `--ignore-scripts`.
|
|
71
|
+
|
|
72
|
+
For Git history, directories, archives, or the `betterleaks` command, install the [combined CLI and SDK package](https://www.npmjs.com/package/betterleaks).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface ScanResult {
|
|
2
|
+
schema_version: 1;
|
|
3
|
+
engine_version: string;
|
|
4
|
+
config_hash: string;
|
|
5
|
+
findings: Finding[];
|
|
6
|
+
summary: { bytes_inspected: number; findings: number };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Match {
|
|
10
|
+
full: string;
|
|
11
|
+
value: string;
|
|
12
|
+
entropy?: number;
|
|
13
|
+
fingerprint?: string;
|
|
14
|
+
captures?: Record<string, string>;
|
|
15
|
+
context?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Lines and byte columns are one-based. */
|
|
19
|
+
export interface Location {
|
|
20
|
+
path?: string;
|
|
21
|
+
start_line?: number;
|
|
22
|
+
end_line?: number;
|
|
23
|
+
start_column?: number;
|
|
24
|
+
end_column?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Detection {
|
|
28
|
+
rule_id: string;
|
|
29
|
+
rule_hash?: string;
|
|
30
|
+
match: Match;
|
|
31
|
+
location: Location;
|
|
32
|
+
encodings?: string[];
|
|
33
|
+
decode_depth?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Finding extends Detection {
|
|
37
|
+
description: string;
|
|
38
|
+
confidence: string;
|
|
39
|
+
tags?: string[];
|
|
40
|
+
attributes?: Record<string, string>;
|
|
41
|
+
component_sets?: { components: (Detection & { optional?: boolean })[] }[];
|
|
42
|
+
component_sets_truncated?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Scan up to 8 MiB of UTF-8 text with the built-in rules. */
|
|
46
|
+
export function scanText(text: string): Promise<ScanResult>;
|
|
47
|
+
|
|
48
|
+
/** Scan one local regular file with filesystem rules and path metadata. */
|
|
49
|
+
export function scanFile(path: string): Promise<ScanResult>;
|
|
50
|
+
|
|
51
|
+
export interface ScannerOptions {
|
|
52
|
+
/** Read TOML from this path. With configToml, use it as the inline config's source name. */
|
|
53
|
+
configPath?: string;
|
|
54
|
+
/** Supply TOML directly. It can extend the built-in rules. */
|
|
55
|
+
configToml?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface Scanner {
|
|
59
|
+
scanText(text: string): Promise<ScanResult>;
|
|
60
|
+
scanFile(path: string): Promise<ScanResult>;
|
|
61
|
+
/** Stop accepting scans. Already submitted scans finish normally. Safe to call repeatedly. */
|
|
62
|
+
close(): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Load and compile rules once for this scanner. Omit options to use the built-in rules. */
|
|
66
|
+
export function createScanner(options?: ScannerOptions): Promise<Scanner>;
|
package/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
let addon;
|
|
4
|
+
function native() {
|
|
5
|
+
if (addon) return addon;
|
|
6
|
+
const { platform, arch } = process;
|
|
7
|
+
if (!["darwin", "linux"].includes(platform) || !["x64", "arm64"].includes(arch)) {
|
|
8
|
+
throw new Error(`Betterleaks bindings support macOS and glibc Linux on x64 and arm64 (running ${platform}/${arch})`);
|
|
9
|
+
}
|
|
10
|
+
const name = `@betterleaks/sdk-${platform}-${arch}`;
|
|
11
|
+
const version = require("./package.json").version;
|
|
12
|
+
let manifest;
|
|
13
|
+
try {
|
|
14
|
+
manifest = require(`${name}/package.json`);
|
|
15
|
+
} catch (cause) {
|
|
16
|
+
throw new Error(`Install @betterleaks/sdk with optional dependencies enabled to load ${name}@${version}`, { cause });
|
|
17
|
+
}
|
|
18
|
+
if (manifest.version !== version) throw new Error(`Expected ${name}@${version}, found ${manifest.version}`);
|
|
19
|
+
addon = require(`${name}/betterleaks.node`);
|
|
20
|
+
return addon;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.scanText = async function scanText(text) {
|
|
24
|
+
return JSON.parse(await native().scanText(text));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.scanFile = async function scanFile(path) {
|
|
28
|
+
return JSON.parse(await native().scanFile(path));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
exports.createScanner = async function createScanner(options = {}) {
|
|
32
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
33
|
+
throw new TypeError("Expected a scanner options object");
|
|
34
|
+
}
|
|
35
|
+
const config = Object.create(null);
|
|
36
|
+
for (const [key, value] of Object.entries(options)) {
|
|
37
|
+
if (key !== "configPath" && key !== "configToml") throw new TypeError(`Unknown scanner option: ${key}`);
|
|
38
|
+
if (value === undefined) continue;
|
|
39
|
+
if (typeof value !== "string") throw new TypeError(`${key} must be a string`);
|
|
40
|
+
if (value.length > (8 << 20)) throw new RangeError("Scanner options exceed 8 MiB");
|
|
41
|
+
if (key === "configPath" && (!value || value.includes("\0"))) {
|
|
42
|
+
throw new TypeError("configPath must be a nonempty path without NUL");
|
|
43
|
+
}
|
|
44
|
+
config[key === "configPath" ? "config_path" : "config_toml"] = value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const scanner = await native().createScanner(JSON.stringify(config));
|
|
48
|
+
return {
|
|
49
|
+
async scanText(text) { return JSON.parse(await scanner.scanText(text)); },
|
|
50
|
+
async scanFile(path) { return JSON.parse(await scanner.scanFile(path)); },
|
|
51
|
+
close() { scanner.close(); },
|
|
52
|
+
};
|
|
53
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@betterleaks/sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0-rc.13",
|
|
4
|
+
"description": "Scan text and files for leaked secrets from Node.js",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/betterleaks/betterleaks.git",
|
|
9
|
+
"directory": "bindings/node/sdk"
|
|
10
|
+
},
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"types": "index.d.ts",
|
|
13
|
+
"optionalDependencies": {
|
|
14
|
+
"@betterleaks/sdk-darwin-x64": "2.0.0-rc.13",
|
|
15
|
+
"@betterleaks/sdk-darwin-arm64": "2.0.0-rc.13",
|
|
16
|
+
"@betterleaks/sdk-linux-x64": "2.0.0-rc.13",
|
|
17
|
+
"@betterleaks/sdk-linux-arm64": "2.0.0-rc.13"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
}
|
|
22
|
+
}
|