lintsure 0.3.14 → 0.3.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -21
- package/dist/src/agents.d.ts +11 -2
- package/dist/src/agents.js +91 -23
- package/dist/src/deps.d.ts +18 -0
- package/dist/src/deps.js +57 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +900 -65
- package/dist/src/menu.d.ts +15 -0
- package/dist/src/menu.js +89 -0
- package/dist/src/plugin.d.ts +28 -0
- package/dist/src/plugin.js +170 -0
- package/dist/src/skills.d.ts +18 -0
- package/dist/src/skills.js +86 -0
- package/package.json +54 -45
- package/skills/lintsure-check/SKILL.md +36 -0
- package/skills/lintsure-fix/SKILL.md +38 -0
- package/skills/lintsure-prelaunch/SKILL.md +37 -0
- package/skills/lintsure-rules/SKILL.md +54 -0
- package/skills/lintsure-rules/agents.mjs +75 -0
- package/skills/lintsure-rules/lib.mjs +60 -0
- package/skills/lintsure-rules/profile.mjs +129 -0
- package/skills/lintsure-rules/rules.mjs +197 -0
- package/skills/lintsure-setup/SKILL.md +34 -0
- package/skills/lintsure-stay-on-track/SKILL.md +39 -0
- package/skills/lintsure-triage/SKILL.md +105 -0
package/README.md
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
|
-
# lintsure
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
npx lintsure
|
|
9
|
-
npx lintsure
|
|
10
|
-
npx lintsure scan --
|
|
11
|
-
npx lintsure
|
|
12
|
-
npx lintsure
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
# lintsure
|
|
2
|
+
|
|
3
|
+
**Keep your AI coding agents on track and in context.** Lintsure checks the code Claude Code, Cursor, Codex and other agents write, and holds them to your project's rules. It finds secrets in source, databases left open, unsafe agent settings, request data reaching a query or a shell, and more, and maps each finding to OWASP, GDPR and the EU AI Act. It runs on your machine and never uploads your code.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx lintsure scan # check the project in this folder
|
|
7
|
+
npx lintsure setup --yes # connect it to the coding agents this project uses (run it in your project's folder)
|
|
8
|
+
npx lintsure setup --everywhere --yes # or connect it for every project on this machine
|
|
9
|
+
npx lintsure skills --yes # add the skills that tell the agent when to use it
|
|
10
|
+
npx lintsure scan --fail-on gap # fail a CI job on clear problems
|
|
11
|
+
npx lintsure baseline # then later scans show only what's new
|
|
12
|
+
npx lintsure scan --report --project "My app" # multi-page assessment to review and sign
|
|
13
|
+
npx lintsure status # check Node, config, rules and agents
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
! 3 open findings · 2 gap · 1 partial · 61 rules met · 30 outside code
|
|
18
|
+
Read 214 files
|
|
19
|
+
partial src/billing.ts:4 Data sent over plain HTTP
|
|
20
|
+
gap src/config.ts:2 Secret or private key committed to source
|
|
21
|
+
gap src/users.ts:5 Personal data written to logs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Every finding shows the file and line, a suggested fix, a plain note on why it matters, and the framework controls it may affect. It also says which files it couldn't read, so "nothing found" never means "checked everything".
|
|
25
|
+
|
|
26
|
+
In Claude Code, the [plugin](https://github.com/lintsure/plugin) adds guards that work while the agent edits: Drift Guard, Danger Guard, Secret Guard and Loop Guard.
|
|
27
|
+
|
|
28
|
+
With Docker: `docker run --rm -v "$PWD:/work" -w /work node:22-slim npx -y lintsure scan`
|
|
29
|
+
|
|
30
|
+
> **Lintsure is a readiness tool, not an audit, a certification or legal advice.** It checks code only, against our reading of the publicly available text of each framework. Work outside code still has to be done, and a qualified person should review the results before any audit or decision.
|
|
31
|
+
|
|
32
|
+
- Docs: https://lintsure.com/docs/cli
|
|
33
|
+
- Source: https://github.com/lintsure/lintsure
|
|
34
|
+
- Disclaimer: https://lintsure.com/disclaimer
|
|
35
|
+
|
|
36
|
+
If it helps you, please star the repo, share it, or [send a review](https://lintsure.com/review). Apache-2.0.
|
package/dist/src/agents.d.ts
CHANGED
|
@@ -2,11 +2,14 @@ export interface Agent {
|
|
|
2
2
|
id: string;
|
|
3
3
|
name: string;
|
|
4
4
|
file: string;
|
|
5
|
-
key: "mcpServers" | "servers";
|
|
5
|
+
key: "mcpServers" | "servers" | "mcp_servers";
|
|
6
6
|
marker: string;
|
|
7
7
|
}
|
|
8
8
|
/** Project-level MCP config files. `marker` is the folder that shows the project uses that agent. */
|
|
9
9
|
export declare const AGENTS: Agent[];
|
|
10
|
+
/** For agents with no config file in the project (they are set up in their own app or account): what to enter there. */
|
|
11
|
+
export declare const MANUAL: () => string;
|
|
12
|
+
export declare const tomlEntry: () => string;
|
|
10
13
|
export declare const serverEntry: (a: Agent) => {
|
|
11
14
|
command: string;
|
|
12
15
|
args: string[];
|
|
@@ -22,9 +25,15 @@ export declare function plan(root: string, a: Agent): Promise<{
|
|
|
22
25
|
}>;
|
|
23
26
|
/** JSON in the same style as the file it replaces: its indent (tabs or N spaces) and a leading byte-order mark. Pure. */
|
|
24
27
|
export declare function writeJson(original: string, value: unknown): string;
|
|
28
|
+
/** The agents this project already uses: the ones whose folder or config file is here. */
|
|
29
|
+
export declare function detected(root: string): Promise<Agent[]>;
|
|
30
|
+
/** What connecting does, in plain words, for the screens that offer it. */
|
|
31
|
+
export declare const CONNECT_NOTE = "Connecting adds one entry to the agent's config file in this project. It tells the agent how to start\nLintsure, so the agent can check code and explain findings itself. Installing the lintsure command does not\ndo this: an agent only knows Lintsure once it is connected.";
|
|
32
|
+
/** The agent running this command, from the environment it sets, so setup connects the one that asked. */
|
|
33
|
+
export declare function callingAgent(env?: Record<string, string | undefined>): Agent | undefined;
|
|
25
34
|
/** Agents to set up: the ones named, or the ones whose folder exists, with Claude Code as the default. */
|
|
26
35
|
export declare function pick(root: string, names?: string[]): Promise<Agent[] | string>;
|
|
27
|
-
export declare function setup(root: string, agents: Agent[], apply: boolean): Promise<number>;
|
|
36
|
+
export declare function setup(root: string, agents: Agent[], apply: boolean, scope?: string): Promise<number>;
|
|
28
37
|
/** Readiness check. Never prints secret values. Exit 2 only when the config has problems a scan would refuse. */
|
|
29
38
|
export declare function status(root: string): Promise<number>;
|
|
30
39
|
/** Takes Lintsure's own entry back out, leaving everything else in the file as it is now (later edits included).
|
package/dist/src/agents.js
CHANGED
|
@@ -5,13 +5,21 @@ import { access, chmod, copyFile, mkdir, readFile, rm, writeFile } from "node:fs
|
|
|
5
5
|
import { constants } from "node:fs";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
7
|
import { RULES_VERSION, readConfig } from "@lintsure/core";
|
|
8
|
+
import { installSkills, skills } from "./skills.js";
|
|
8
9
|
/** Project-level MCP config files. `marker` is the folder that shows the project uses that agent. */
|
|
9
10
|
export const AGENTS = [
|
|
10
11
|
{ id: "claude", name: "Claude Code", file: ".mcp.json", key: "mcpServers", marker: ".claude" },
|
|
11
12
|
{ id: "cursor", name: "Cursor", file: ".cursor/mcp.json", key: "mcpServers", marker: ".cursor" },
|
|
12
13
|
{ id: "vscode", name: "VS Code (Copilot)", file: ".vscode/mcp.json", key: "servers", marker: ".vscode" },
|
|
13
14
|
{ id: "gemini", name: "Gemini CLI", file: ".gemini/settings.json", key: "mcpServers", marker: ".gemini" },
|
|
15
|
+
// Codex keeps its config as TOML, one table per server. It reads a project's file only for projects marked as trusted.
|
|
16
|
+
{ id: "codex", name: "Codex", file: ".codex/config.toml", key: "mcp_servers", marker: ".codex" },
|
|
14
17
|
];
|
|
18
|
+
/** For agents with no config file in the project (they are set up in their own app or account): what to enter there. */
|
|
19
|
+
export const MANUAL = () => `Some agents are set up in their own settings, not by a file in the project: Devin, Windsurf, Zed, Cline and\nothers. Add an MCP server there with these values:\n\n name lintsure\n command npx\n args -y @lintsure/mcp@${RULES_VERSION}\n\nThen ask the agent, in its own chat: Check this project with Lintsure.`;
|
|
20
|
+
// Lintsure's own table in a Codex config: from its header to the next table. Only this table is ever written or removed.
|
|
21
|
+
const TOML_TABLE = /^\[mcp_servers\.lintsure\][^\n]*\n?(?:(?!\[)[^\n]*\n?)*/m;
|
|
22
|
+
export const tomlEntry = () => `[mcp_servers.lintsure]\ncommand = "npx"\nargs = ["-y", "@lintsure/mcp@${RULES_VERSION}"]\n`;
|
|
15
23
|
// Pinned to this version on purpose: a moving "latest" in an agent config is what agent.config-runs-remote-code warns about.
|
|
16
24
|
export const serverEntry = (a) => ({ ...(a.key === "servers" ? { type: "stdio" } : {}), command: "npx", args: ["-y", `@lintsure/mcp@${RULES_VERSION}`] });
|
|
17
25
|
const exists = (p) => access(p).then(() => true, () => false);
|
|
@@ -19,6 +27,12 @@ const exists = (p) => access(p).then(() => true, () => false);
|
|
|
19
27
|
export async function plan(root, a) {
|
|
20
28
|
const path = join(root, a.file);
|
|
21
29
|
const existed = await exists(path);
|
|
30
|
+
if (a.key === "mcp_servers") {
|
|
31
|
+
const toml = existed ? await readFile(path, "utf8") : "", now = TOML_TABLE.exec(toml)?.[0];
|
|
32
|
+
if (now?.replace(/\r/g, "").trim() === tomlEntry().trim())
|
|
33
|
+
return { agent: a, path, existed, skip: "already set up" };
|
|
34
|
+
return { agent: a, path, existed, text: now ? toml.replace(TOML_TABLE, () => tomlEntry()) : toml + (toml && !toml.endsWith("\n") ? "\n" : "") + (toml.trim() ? "\n" : "") + tomlEntry() };
|
|
35
|
+
}
|
|
22
36
|
let json = {}, raw = "";
|
|
23
37
|
if (existed) {
|
|
24
38
|
raw = await readFile(path, "utf8");
|
|
@@ -39,59 +53,91 @@ export function writeJson(original, value) {
|
|
|
39
53
|
const indent = /^\t/m.test(original) ? "\t" : (original.match(/^( +)"/m)?.[1].length ?? 2);
|
|
40
54
|
return (original.startsWith("") ? "" : "") + JSON.stringify(value, null, indent) + "\n";
|
|
41
55
|
}
|
|
56
|
+
/** The agents this project already uses: the ones whose folder or config file is here. */
|
|
57
|
+
export async function detected(root) {
|
|
58
|
+
const found = [];
|
|
59
|
+
for (const a of AGENTS)
|
|
60
|
+
if (await exists(join(root, a.marker)) || await exists(join(root, a.file)))
|
|
61
|
+
found.push(a);
|
|
62
|
+
return found;
|
|
63
|
+
}
|
|
64
|
+
/** What connecting does, in plain words, for the screens that offer it. */
|
|
65
|
+
export const CONNECT_NOTE = "Connecting adds one entry to the agent's config file in this project. It tells the agent how to start\nLintsure, so the agent can check code and explain findings itself. Installing the lintsure command does not\ndo this: an agent only knows Lintsure once it is connected.";
|
|
66
|
+
/** The agent running this command, from the environment it sets, so setup connects the one that asked. */
|
|
67
|
+
export function callingAgent(env = process.env) {
|
|
68
|
+
const id = env.CLAUDECODE ? "claude" : env.GEMINI_CLI ? "gemini" : env.CURSOR_AGENT || env.CURSOR_TRACE_ID ? "cursor" : Object.keys(env).some((k) => k.startsWith("CODEX_")) ? "codex" : undefined;
|
|
69
|
+
return AGENTS.find((a) => a.id === id);
|
|
70
|
+
}
|
|
42
71
|
/** Agents to set up: the ones named, or the ones whose folder exists, with Claude Code as the default. */
|
|
43
72
|
export async function pick(root, names) {
|
|
44
73
|
if (names) {
|
|
45
74
|
const bad = names.filter((n) => !AGENTS.some((a) => a.id === n));
|
|
46
75
|
return bad.length ? `Unknown agent: ${bad.join(", ")}. Use ${AGENTS.map((a) => a.id).join(", ")}.` : AGENTS.filter((a) => names.includes(a.id));
|
|
47
76
|
}
|
|
48
|
-
const found =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
found.push(a);
|
|
77
|
+
const found = await detected(root), me = callingAgent();
|
|
78
|
+
if (me && !found.includes(me))
|
|
79
|
+
found.push(me);
|
|
52
80
|
return found.length ? found : [AGENTS[0]];
|
|
53
81
|
}
|
|
54
|
-
export async function setup(root, agents, apply) {
|
|
82
|
+
export async function setup(root, agents, apply, scope = "") {
|
|
55
83
|
const plans = await Promise.all(agents.map((a) => plan(root, a)));
|
|
56
84
|
for (const p of plans) {
|
|
57
85
|
if (p.skip) {
|
|
58
86
|
console.log(`- ${p.agent.name}: ${p.skip}`);
|
|
59
87
|
continue;
|
|
60
88
|
}
|
|
61
|
-
console.log(`- ${p.agent.name}: ${p.existed ? "add to" : "create"} ${p.agent.file}\n "lintsure": ${JSON.stringify(serverEntry(p.agent))}`);
|
|
89
|
+
console.log(`- ${p.agent.name}: ${p.existed ? "add to" : "create"} ${p.agent.file}\n ${p.agent.key === "mcp_servers" ? tomlEntry().trim().replace(/\n/g, "\n ") : `"lintsure": ${JSON.stringify(serverEntry(p.agent))}`}`);
|
|
62
90
|
}
|
|
63
91
|
const todo = plans.filter((p) => p.text);
|
|
64
|
-
if (!todo.length)
|
|
92
|
+
if (!todo.length) {
|
|
93
|
+
console.log("\nNothing to write. Check the connection with: lintsure status");
|
|
65
94
|
return 0;
|
|
95
|
+
}
|
|
66
96
|
if (!apply) {
|
|
67
|
-
console.log(
|
|
97
|
+
console.log(`\nNothing written yet. To apply, run: lintsure setup${scope} --yes\nExisting files are backed up first (<file>.lintsure-backup).`);
|
|
68
98
|
return 0;
|
|
69
99
|
}
|
|
100
|
+
let wrote = 0;
|
|
70
101
|
for (const p of todo) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
102
|
+
try {
|
|
103
|
+
await mkdir(dirname(p.path), { recursive: true });
|
|
104
|
+
// The first backup is the user's original: never overwrite it on a later run. Owner-only, it may hold tokens.
|
|
105
|
+
if (p.existed) {
|
|
106
|
+
try {
|
|
107
|
+
await copyFile(p.path, p.path + ".lintsure-backup", constants.COPYFILE_EXCL);
|
|
108
|
+
await chmod(p.path + ".lintsure-backup", 0o600).catch(() => { });
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
if (e.code !== "EEXIST") {
|
|
112
|
+
console.error(`Couldn't back up ${p.agent.file}, so it was left alone.`);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
82
115
|
}
|
|
83
116
|
}
|
|
117
|
+
await writeFile(p.path, p.text);
|
|
118
|
+
wrote++;
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
console.error(`Couldn't write ${p.agent.file} (${e.code ?? "error"}), so it was left alone.`);
|
|
84
122
|
}
|
|
85
|
-
await writeFile(p.path, p.text);
|
|
86
123
|
}
|
|
87
|
-
|
|
124
|
+
// A sandboxed agent or a read-only folder: say plainly that nothing changed, so nobody thinks it is connected.
|
|
125
|
+
if (!wrote) {
|
|
126
|
+
console.error("\nNothing was written. This program isn't allowed to change files in that folder. Allow it there, or run the same command in your own terminal.");
|
|
127
|
+
return 1;
|
|
128
|
+
}
|
|
129
|
+
console.log(`Undo any time with: lintsure setup${scope} --undo`);
|
|
88
130
|
console.log(`\nWritten. Restart the agent so it starts the Lintsure MCP server. Run "lintsure status" to check.`);
|
|
89
|
-
console.log("
|
|
131
|
+
console.log("Then ask the agent, in its own chat: Check this project with Lintsure.");
|
|
132
|
+
if (!scope && agents.some((a) => a.id === "codex"))
|
|
133
|
+
console.log("Codex reads a project's .codex/config.toml only for projects you have marked as trusted.");
|
|
134
|
+
console.log("For guards that run while the agent works, add the plugin: lintsure plugin");
|
|
90
135
|
return 0;
|
|
91
136
|
}
|
|
92
137
|
/** Readiness check. Never prints secret values. Exit 2 only when the config has problems a scan would refuse. */
|
|
93
138
|
export async function status(root) {
|
|
94
|
-
|
|
139
|
+
let todo = 0;
|
|
140
|
+
const line = (ok, what, next) => (ok === false && todo++, console.log(`${ok === null ? "info" : ok ? "ok " : "todo"} ${what}${!ok && next ? `\n ${next}` : ""}`));
|
|
95
141
|
const major = Number(process.versions.node.split(".")[0]);
|
|
96
142
|
line(major >= 20, `Node.js ${process.versions.node}`, "Lintsure needs Node.js 20 or newer.");
|
|
97
143
|
line(true, `Lintsure rules ${RULES_VERSION}`);
|
|
@@ -109,12 +155,19 @@ export async function status(root) {
|
|
|
109
155
|
const p = await plan(root, a);
|
|
110
156
|
line(p.skip === "already set up", `${a.name}: Lintsure MCP server in ${a.file}`, p.skip && p.skip !== "already set up" ? p.skip : `Run: lintsure setup --agents ${a.id}`);
|
|
111
157
|
}
|
|
158
|
+
// Skills: are the ones in this project the ones this version ships? An older or edited copy teaches the agent old habits.
|
|
159
|
+
const steps = await installSkills(root, skills(), false), here = steps.filter((s) => s.state !== "add"), stale = here.filter((s) => s.state === "update");
|
|
160
|
+
if (!here.length)
|
|
161
|
+
line(null, "No Lintsure skills in this project: add them with lintsure skills");
|
|
162
|
+
else
|
|
163
|
+
line(!stale.length, `Lintsure skills: ${here.length} installed${stale.length ? `, ${stale.length} differ from this version (${stale.map((s) => s.folder).join(", ")})` : ", all current"}`, "Run: lintsure skills --yes (your copies are kept as backups)");
|
|
112
164
|
const mcp = await mcpRoundTrip();
|
|
113
165
|
if (!mcp)
|
|
114
166
|
line(null, "MCP server not installed next to this CLI: agents start it with npx, which downloads it on first use");
|
|
115
167
|
else
|
|
116
168
|
line(!mcp.problems.length, mcp.problems.length ? `MCP server: ${mcp.problems.join("; ")}` : `MCP server ${mcp.version} answers: ${mcp.tools} tools, test call ok`);
|
|
117
169
|
line(null, process.env.LINTSURE_API_KEY ? "LINTSURE_API_KEY is set: the plugin will use the hosted workspace" : "No LINTSURE_API_KEY: everything stays on this machine");
|
|
170
|
+
console.log(todo ? `\nAlmost there: ${todo} thing${todo === 1 ? "" : "s"} to do, marked todo above.` : "\nReady! Everything Lintsure needs is in place.");
|
|
118
171
|
return hasConfig && config.problems.length ? 2 : 0;
|
|
119
172
|
}
|
|
120
173
|
/** Takes Lintsure's own entry back out, leaving everything else in the file as it is now (later edits included).
|
|
@@ -122,6 +175,21 @@ export async function status(root) {
|
|
|
122
175
|
export async function undo(root, agents) {
|
|
123
176
|
for (const a of agents) {
|
|
124
177
|
const path = join(root, a.file);
|
|
178
|
+
if (a.key === "mcp_servers") {
|
|
179
|
+
const toml = await readFile(path, "utf8").catch(() => "");
|
|
180
|
+
if (!TOML_TABLE.test(toml))
|
|
181
|
+
continue;
|
|
182
|
+
const left = toml.replace(TOML_TABLE, "").replace(/\n{3,}/g, "\n\n"), kept = await exists(path + ".lintsure-backup");
|
|
183
|
+
if (!left.trim() && !kept) {
|
|
184
|
+
await rm(path);
|
|
185
|
+
console.log(`- ${a.name}: removed ${a.file} (Lintsure had created it)`);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
await writeFile(path, left);
|
|
189
|
+
console.log(`- ${a.name}: removed the Lintsure entry from ${a.file}${kept ? ` (your original is still in ${a.file}.lintsure-backup)` : ""}`);
|
|
190
|
+
}
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
125
193
|
let json, raw;
|
|
126
194
|
try {
|
|
127
195
|
raw = await readFile(path, "utf8");
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface Pinned {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
ecosystem: "npm" | "PyPI";
|
|
5
|
+
}
|
|
6
|
+
/** Every installed package in an npm lockfile (version 2 or 3), once per name and version. Pure. */
|
|
7
|
+
export declare function fromPackageLock(text: string): Pinned[];
|
|
8
|
+
/** Packages pinned to one exact version in a requirements file. Ranges and unpinned lines can't be looked up. Pure. */
|
|
9
|
+
export declare function fromRequirements(text: string): Pinned[];
|
|
10
|
+
/** The pinned packages of a project, and the files they came from.
|
|
11
|
+
* shortcut: reads package-lock.json and requirements.txt at the top of the folder only; add other lockfiles and nested
|
|
12
|
+
* folders when someone needs them. */
|
|
13
|
+
export declare function pinned(root: string): Promise<{
|
|
14
|
+
found: Pinned[];
|
|
15
|
+
files: string[];
|
|
16
|
+
}>;
|
|
17
|
+
/** Known advisory ids per package, from OSV. One request per 500 packages. Throws when the service can't be reached. */
|
|
18
|
+
export declare function advisories(pkgs: Pinned[], fetcher?: typeof fetch): Promise<Map<Pinned, string[]>>;
|
package/dist/src/deps.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// lintsure deps: asks a public database which of the project's pinned packages have known vulnerabilities.
|
|
2
|
+
// This is the one command that uses the network. It sends package names and versions, never code, to api.osv.dev,
|
|
3
|
+
// and only after it has said so and been told to go ahead. A scan never does this.
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
/** Every installed package in an npm lockfile (version 2 or 3), once per name and version. Pure. */
|
|
7
|
+
export function fromPackageLock(text) {
|
|
8
|
+
const packages = (JSON.parse(text).packages ?? {});
|
|
9
|
+
const out = new Map();
|
|
10
|
+
for (const [path, p] of Object.entries(packages)) {
|
|
11
|
+
const at = path.lastIndexOf("node_modules/");
|
|
12
|
+
if (at < 0 || p.link || !p.version)
|
|
13
|
+
continue; // the project itself, and links to its own workspaces
|
|
14
|
+
const name = path.slice(at + 13);
|
|
15
|
+
out.set(`${name}@${p.version}`, { name, version: p.version, ecosystem: "npm" });
|
|
16
|
+
}
|
|
17
|
+
return [...out.values()];
|
|
18
|
+
}
|
|
19
|
+
/** Packages pinned to one exact version in a requirements file. Ranges and unpinned lines can't be looked up. Pure. */
|
|
20
|
+
export function fromRequirements(text) {
|
|
21
|
+
return text.split(/\r?\n/).flatMap((l) => { const m = /^\s*([A-Za-z0-9._-]+)(?:\[[^\]]*\])?\s*==\s*([\w.!+-]+)/.exec(l); return m ? [{ name: m[1], version: m[2], ecosystem: "PyPI" }] : []; });
|
|
22
|
+
}
|
|
23
|
+
/** The pinned packages of a project, and the files they came from.
|
|
24
|
+
* shortcut: reads package-lock.json and requirements.txt at the top of the folder only; add other lockfiles and nested
|
|
25
|
+
* folders when someone needs them. */
|
|
26
|
+
export async function pinned(root) {
|
|
27
|
+
const found = [], files = [];
|
|
28
|
+
for (const [file, read] of [["package-lock.json", fromPackageLock], ["requirements.txt", fromRequirements]]) {
|
|
29
|
+
const text = await readFile(join(root, file), "utf8").catch(() => null);
|
|
30
|
+
if (text === null)
|
|
31
|
+
continue;
|
|
32
|
+
try {
|
|
33
|
+
const list = read(text);
|
|
34
|
+
if (list.length) {
|
|
35
|
+
found.push(...list);
|
|
36
|
+
files.push(file);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch { /* a lockfile that isn't JSON is skipped, not fatal */ }
|
|
40
|
+
}
|
|
41
|
+
return { found, files };
|
|
42
|
+
}
|
|
43
|
+
/** Known advisory ids per package, from OSV. One request per 500 packages. Throws when the service can't be reached. */
|
|
44
|
+
export async function advisories(pkgs, fetcher = fetch) {
|
|
45
|
+
const out = new Map();
|
|
46
|
+
for (let i = 0; i < pkgs.length; i += 500) {
|
|
47
|
+
const batch = pkgs.slice(i, i + 500);
|
|
48
|
+
const res = await fetcher("https://api.osv.dev/v1/querybatch", { method: "POST", headers: { "content-type": "application/json" }, signal: AbortSignal.timeout(20000),
|
|
49
|
+
body: JSON.stringify({ queries: batch.map((p) => ({ package: { name: p.name, ecosystem: p.ecosystem }, version: p.version })) }) });
|
|
50
|
+
if (!res.ok)
|
|
51
|
+
throw new Error(`api.osv.dev answered ${res.status}`);
|
|
52
|
+
const { results = [] } = (await res.json());
|
|
53
|
+
batch.forEach((p, k) => { const ids = (results[k]?.vulns ?? []).map((v) => v.id); if (ids.length)
|
|
54
|
+
out.set(p, ids); });
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
/** Whether a folder is a software project or sits inside one: a marker file in the folder itself, or a git repository
|
|
3
|
+
* around it. Folders above count only through .git, because a stray package.json in a temp or documents folder is
|
|
4
|
+
* common. The home folder and a drive root never count. */
|
|
5
|
+
export declare const looksLikeProject: (dir: string) => boolean;
|
|
6
|
+
/** The projects directly inside a folder that is not one itself: a workspace. Empty for a normal project. */
|
|
7
|
+
export declare function subProjects(dir: string): string[];
|
|
2
8
|
/** Changed line numbers per file from `git diff -U0` output. Pure. */
|
|
3
9
|
export declare function parseDiff(text: string): Record<string, number[]>;
|
|
4
10
|
/** Splits --flag=value into two tokens and checks every flag, so a CI gate can't silently fail open
|