midnight-shield 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +68 -0
- package/LICENSE +21 -0
- package/README.md +382 -2
- package/action.yml +56 -0
- package/dist/src/analyzer.d.ts +97 -0
- package/dist/src/analyzer.js +895 -0
- package/dist/src/ast.d.ts +211 -0
- package/dist/src/ast.js +2 -0
- package/dist/src/browser.d.ts +3 -0
- package/dist/src/browser.js +5 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +336 -0
- package/dist/src/config.d.ts +26 -0
- package/dist/src/config.js +87 -0
- package/dist/src/fixes.d.ts +10 -0
- package/dist/src/fixes.js +45 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +10 -0
- package/dist/src/lexer.d.ts +18 -0
- package/dist/src/lexer.js +135 -0
- package/dist/src/linter.d.ts +67 -0
- package/dist/src/linter.js +137 -0
- package/dist/src/node.d.ts +13 -0
- package/dist/src/node.js +98 -0
- package/dist/src/parser.d.ts +2 -0
- package/dist/src/parser.js +791 -0
- package/dist/src/plugin-loader.d.ts +3 -0
- package/dist/src/plugin-loader.js +31 -0
- package/dist/src/plugins.d.ts +34 -0
- package/dist/src/plugins.js +59 -0
- package/dist/src/reporters.d.ts +22 -0
- package/dist/src/reporters.js +286 -0
- package/dist/src/rules.d.ts +23 -0
- package/dist/src/rules.js +104 -0
- package/dist/src/sdk.d.ts +6 -0
- package/dist/src/sdk.js +130 -0
- package/dist/src/walk.d.ts +28 -0
- package/dist/src/walk.js +110 -0
- package/docs/rules/MS000.md +7 -0
- package/docs/rules/MS001.md +46 -0
- package/docs/rules/MS002.md +42 -0
- package/docs/rules/MS003.md +41 -0
- package/docs/rules/MS004.md +41 -0
- package/docs/rules/MS005.md +32 -0
- package/docs/rules/MS006.md +17 -0
- package/docs/rules/MS007.md +44 -0
- package/docs/rules/MS008.md +59 -0
- package/docs/rules/MS009.md +45 -0
- package/docs/rules/MS010.md +46 -0
- package/docs/rules/MS101.md +32 -0
- package/docs/rules/MS102.md +31 -0
- package/package.json +67 -4
- package/skills/midnight-shield/SKILL.md +67 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Loads plugin modules from disk (Node.js only).
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { PluginError, validateRule } from "./plugins.js";
|
|
5
|
+
import { RULES_BY_ID } from "./rules.js";
|
|
6
|
+
/** Load plugin modules (paths relative to `baseDir`) and return their validated rules. */
|
|
7
|
+
export async function loadPlugins(paths, baseDir = process.cwd()) {
|
|
8
|
+
const rules = [];
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
for (const p of paths) {
|
|
11
|
+
const abs = resolve(baseDir, p);
|
|
12
|
+
let mod;
|
|
13
|
+
try {
|
|
14
|
+
mod = await import(pathToFileURL(abs).href);
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
throw new PluginError(`cannot load plugin ${p}: ${e.message}`);
|
|
18
|
+
}
|
|
19
|
+
const list = (mod.default ?? mod.rules);
|
|
20
|
+
if (!Array.isArray(list))
|
|
21
|
+
throw new PluginError(`${p}: export an array of rules as default (or as "rules")`);
|
|
22
|
+
for (const raw of list) {
|
|
23
|
+
const rule = validateRule(raw, p);
|
|
24
|
+
if (seen.has(rule.id) || RULES_BY_ID.has(rule.id))
|
|
25
|
+
throw new PluginError(`${p}: duplicate rule id ${rule.id}`);
|
|
26
|
+
seen.add(rule.id);
|
|
27
|
+
rules.push(rule);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return rules;
|
|
31
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Loc, Program } from "./ast.js";
|
|
2
|
+
import { type Confidence, type RuleMeta, type Severity } from "./rules.js";
|
|
3
|
+
import { type Visitor } from "./walk.js";
|
|
4
|
+
export interface PluginReport {
|
|
5
|
+
/** where the problem is; or give `line` (and optionally `col`) */
|
|
6
|
+
loc?: Loc;
|
|
7
|
+
line?: number;
|
|
8
|
+
col?: number;
|
|
9
|
+
message: string;
|
|
10
|
+
severity?: Severity;
|
|
11
|
+
confidence?: Confidence;
|
|
12
|
+
}
|
|
13
|
+
export interface RuleContext {
|
|
14
|
+
/** path of the file being checked, as shown in reports */
|
|
15
|
+
file: string;
|
|
16
|
+
source: string;
|
|
17
|
+
program: Program;
|
|
18
|
+
report(r: PluginReport): void;
|
|
19
|
+
/** walk every circuit, statement and expression in the file */
|
|
20
|
+
visit(visitor: Visitor): void;
|
|
21
|
+
}
|
|
22
|
+
export interface CustomRule extends RuleMeta {
|
|
23
|
+
check(ctx: RuleContext): void;
|
|
24
|
+
}
|
|
25
|
+
export declare class PluginError extends Error {
|
|
26
|
+
}
|
|
27
|
+
export declare function validateRule(raw: unknown, source: string): CustomRule;
|
|
28
|
+
export declare function runCustomRule(rule: CustomRule, file: string, source: string, program: Program): {
|
|
29
|
+
ruleId: string;
|
|
30
|
+
severity: Severity;
|
|
31
|
+
confidence: Confidence;
|
|
32
|
+
message: string;
|
|
33
|
+
loc: Loc;
|
|
34
|
+
}[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Custom rule API. A plugin is an ES module whose default export (or `rules`
|
|
2
|
+
// export) is an array of rules:
|
|
3
|
+
//
|
|
4
|
+
// export default [{
|
|
5
|
+
// id: "ACME001", name: "no-debug-ledger", severity: "low", summary: "...",
|
|
6
|
+
// check(ctx) {
|
|
7
|
+
// ctx.visit({ call(expr, callee, where) {
|
|
8
|
+
// if (callee === "debugLog") ctx.report({ loc: expr.loc, message: "..." });
|
|
9
|
+
// }});
|
|
10
|
+
// },
|
|
11
|
+
// }];
|
|
12
|
+
import { CONFIDENCE_ORDER, SEVERITY_ORDER } from "./rules.js";
|
|
13
|
+
import { walkProgram } from "./walk.js";
|
|
14
|
+
export class PluginError extends Error {
|
|
15
|
+
}
|
|
16
|
+
const ID_PATTERN = /^[A-Z][A-Z0-9]*\d{3}$/;
|
|
17
|
+
export function validateRule(raw, source) {
|
|
18
|
+
const r = raw;
|
|
19
|
+
const where = `${source}: rule ${r?.id ?? "(no id)"}`;
|
|
20
|
+
if (!r || typeof r !== "object")
|
|
21
|
+
throw new PluginError(`${source}: each rule must be an object`);
|
|
22
|
+
if (typeof r.id !== "string" || !ID_PATTERN.test(r.id))
|
|
23
|
+
throw new PluginError(`${where}: "id" must look like ACME001 (capital letters, then 3 digits)`);
|
|
24
|
+
if (r.id.startsWith("MS"))
|
|
25
|
+
throw new PluginError(`${where}: the "MS" prefix is reserved for built-in rules`);
|
|
26
|
+
if (typeof r.name !== "string" || !r.name)
|
|
27
|
+
throw new PluginError(`${where}: "name" is required`);
|
|
28
|
+
if (!SEVERITY_ORDER.includes(r.severity))
|
|
29
|
+
throw new PluginError(`${where}: "severity" must be one of ${SEVERITY_ORDER.join(", ")}`);
|
|
30
|
+
if (r.confidence !== undefined && !CONFIDENCE_ORDER.includes(r.confidence))
|
|
31
|
+
throw new PluginError(`${where}: "confidence" must be one of ${CONFIDENCE_ORDER.join(", ")}`);
|
|
32
|
+
if (typeof r.summary !== "string" || !r.summary)
|
|
33
|
+
throw new PluginError(`${where}: "summary" is required`);
|
|
34
|
+
if (typeof r.check !== "function")
|
|
35
|
+
throw new PluginError(`${where}: "check(ctx)" must be a function`);
|
|
36
|
+
return { ...r, confidence: r.confidence ?? "medium" };
|
|
37
|
+
}
|
|
38
|
+
export function runCustomRule(rule, file, source, program) {
|
|
39
|
+
const out = [];
|
|
40
|
+
const ctx = {
|
|
41
|
+
file,
|
|
42
|
+
source,
|
|
43
|
+
program,
|
|
44
|
+
report(r) {
|
|
45
|
+
const loc = r.loc ?? { line: r.line ?? 1, col: r.col ?? 1 };
|
|
46
|
+
out.push({ ruleId: rule.id, severity: r.severity ?? rule.severity, confidence: r.confidence ?? rule.confidence, message: r.message, loc });
|
|
47
|
+
},
|
|
48
|
+
visit(visitor) {
|
|
49
|
+
walkProgram(program, visitor);
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
rule.check(ctx);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
throw new PluginError(`rule ${rule.id} crashed on ${file}: ${e.message}`);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DisclosureEntry, Finding } from "./linter.js";
|
|
2
|
+
import { type RuleMeta } from "./rules.js";
|
|
3
|
+
export declare const VERSION = "0.4.1";
|
|
4
|
+
export interface TextOptions {
|
|
5
|
+
color?: boolean;
|
|
6
|
+
/** show the source line and a caret under each finding (default: true) */
|
|
7
|
+
codeFrame?: boolean;
|
|
8
|
+
/** show the rule documentation link (default: true) */
|
|
9
|
+
links?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function formatText(all: Finding[], fileCount: number, options?: TextOptions | boolean): string;
|
|
12
|
+
export declare function formatJson(findings: Finding[]): string;
|
|
13
|
+
export declare function formatSarif(findings: Finding[], extraRules?: RuleMeta[]): string;
|
|
14
|
+
/** A Markdown report for audits and pull requests. */
|
|
15
|
+
export declare function formatMarkdown(all: Finding[], fileCount: number, options?: {
|
|
16
|
+
title?: string;
|
|
17
|
+
date?: string;
|
|
18
|
+
}): string;
|
|
19
|
+
export declare function formatDisclosures(entries: DisclosureEntry[], options?: {
|
|
20
|
+
color?: boolean;
|
|
21
|
+
}): string;
|
|
22
|
+
export declare function formatDisclosuresMarkdown(entries: DisclosureEntry[]): string;
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { RULES, SEVERITY_ORDER, ruleDocUrl } from "./rules.js";
|
|
2
|
+
export const VERSION = "0.4.1";
|
|
3
|
+
const COLORS = {
|
|
4
|
+
critical: "\x1b[35m",
|
|
5
|
+
high: "\x1b[31m",
|
|
6
|
+
medium: "\x1b[33m",
|
|
7
|
+
low: "\x1b[36m",
|
|
8
|
+
info: "\x1b[90m",
|
|
9
|
+
};
|
|
10
|
+
const RESET = "\x1b[0m";
|
|
11
|
+
const DIM = "\x1b[2m";
|
|
12
|
+
const BOLD = "\x1b[1m";
|
|
13
|
+
function caretLength(snippet, col) {
|
|
14
|
+
const rest = snippet.slice(col - 1);
|
|
15
|
+
const m = rest.match(/^[A-Za-z_$][\w$]*(\(\))?/);
|
|
16
|
+
return m ? m[0].length : 1;
|
|
17
|
+
}
|
|
18
|
+
function plural(n, word) {
|
|
19
|
+
return `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
20
|
+
}
|
|
21
|
+
function severitySummary(findings, c) {
|
|
22
|
+
const counts = new Map();
|
|
23
|
+
for (const f of findings)
|
|
24
|
+
counts.set(f.severity, (counts.get(f.severity) ?? 0) + 1);
|
|
25
|
+
return SEVERITY_ORDER.filter((sv) => counts.has(sv))
|
|
26
|
+
.map((sv) => c(COLORS[sv], `${counts.get(sv)} ${sv}`))
|
|
27
|
+
.join(", ");
|
|
28
|
+
}
|
|
29
|
+
export function formatText(all, fileCount, options = {}) {
|
|
30
|
+
const opts = typeof options === "boolean" ? { color: options } : options;
|
|
31
|
+
const color = opts.color ?? true;
|
|
32
|
+
const codeFrame = opts.codeFrame ?? true;
|
|
33
|
+
const links = opts.links ?? true;
|
|
34
|
+
const c = (code, s) => (color ? code + s + RESET : s);
|
|
35
|
+
const findings = all.filter((f) => !f.suppressed);
|
|
36
|
+
const suppressed = all.filter((f) => f.suppressed);
|
|
37
|
+
const lines = [];
|
|
38
|
+
let currentFile = "";
|
|
39
|
+
for (const f of findings) {
|
|
40
|
+
if (f.file !== currentFile) {
|
|
41
|
+
if (currentFile)
|
|
42
|
+
lines.push("");
|
|
43
|
+
lines.push(c(BOLD + "\x1b[4m", f.file));
|
|
44
|
+
currentFile = f.file;
|
|
45
|
+
}
|
|
46
|
+
lines.push("");
|
|
47
|
+
const sev = c(BOLD + COLORS[f.severity], f.severity.toUpperCase().padEnd(8));
|
|
48
|
+
const conf = f.confidence === "high" ? "" : c(DIM, ` (${f.confidence} confidence)`);
|
|
49
|
+
const fix = f.fix ? c("\x1b[32m", " [fixable]") : "";
|
|
50
|
+
lines.push(` ${sev} ${c(BOLD, f.ruleId)} ${c(DIM, f.ruleName)} ${c(DIM, `${f.file}:${f.line}:${f.col}`)}${conf}${fix}`);
|
|
51
|
+
lines.push(` ${f.message}`);
|
|
52
|
+
if (codeFrame && f.snippet.trim()) {
|
|
53
|
+
const num = String(f.line);
|
|
54
|
+
const gutter = " ".repeat(num.length);
|
|
55
|
+
const text = f.snippet.replace(/\t/g, " ");
|
|
56
|
+
lines.push(` ${c(DIM, num + " │")} ${text}`);
|
|
57
|
+
lines.push(` ${c(DIM, gutter + " │")} ${" ".repeat(Math.max(0, f.col - 1))}${c(COLORS[f.severity], "^".repeat(caretLength(text, f.col)))}`);
|
|
58
|
+
}
|
|
59
|
+
if (links)
|
|
60
|
+
lines.push(` ${c(DIM, "→ " + f.docUrl)}`);
|
|
61
|
+
}
|
|
62
|
+
if (suppressed.length) {
|
|
63
|
+
if (lines.length)
|
|
64
|
+
lines.push("");
|
|
65
|
+
lines.push(c(BOLD, `Suppressed (${suppressed.length})`));
|
|
66
|
+
for (const f of suppressed) {
|
|
67
|
+
lines.push(` ${c(DIM, `${f.file}:${f.line}:${f.col}`)} ${f.ruleId} ${f.ruleName} ${c(DIM, `[${f.suppressed}]`)}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (lines.length)
|
|
71
|
+
lines.push("");
|
|
72
|
+
const files = plural(fileCount, "file");
|
|
73
|
+
const extra = suppressed.length ? c(DIM, `, ${suppressed.length} suppressed`) : "";
|
|
74
|
+
const fixable = findings.filter((f) => f.fix).length;
|
|
75
|
+
const fixNote = fixable ? c(DIM, `\n ${plural(fixable, "finding")} can be fixed with --fix.`) : "";
|
|
76
|
+
lines.push(findings.length
|
|
77
|
+
? `${c(BOLD, `✖ ${plural(findings.length, "finding")}`)} in ${files} (${severitySummary(findings, c)})${extra}${fixNote}`
|
|
78
|
+
: c("\x1b[32m", `✔ No findings in ${files}.`) + extra);
|
|
79
|
+
return lines.join("\n");
|
|
80
|
+
}
|
|
81
|
+
export function formatJson(findings) {
|
|
82
|
+
const active = findings.filter((f) => !f.suppressed);
|
|
83
|
+
return JSON.stringify({
|
|
84
|
+
tool: "midnight-shield",
|
|
85
|
+
version: VERSION,
|
|
86
|
+
summary: {
|
|
87
|
+
findings: active.length,
|
|
88
|
+
suppressed: findings.length - active.length,
|
|
89
|
+
bySeverity: Object.fromEntries(SEVERITY_ORDER.map((s) => [s, active.filter((f) => f.severity === s).length])),
|
|
90
|
+
},
|
|
91
|
+
findings,
|
|
92
|
+
}, null, 2);
|
|
93
|
+
}
|
|
94
|
+
const SARIF_LEVEL = {
|
|
95
|
+
critical: "error",
|
|
96
|
+
high: "error",
|
|
97
|
+
medium: "warning",
|
|
98
|
+
low: "note",
|
|
99
|
+
info: "note",
|
|
100
|
+
};
|
|
101
|
+
const SECURITY_SEVERITY = {
|
|
102
|
+
critical: "9.5",
|
|
103
|
+
high: "8.0",
|
|
104
|
+
medium: "5.5",
|
|
105
|
+
low: "3.0",
|
|
106
|
+
info: "0.0",
|
|
107
|
+
};
|
|
108
|
+
export function formatSarif(findings, extraRules = []) {
|
|
109
|
+
const rules = [...RULES, ...extraRules];
|
|
110
|
+
const sarif = {
|
|
111
|
+
$schema: "https://json.schemastore.org/sarif-2.1.0.json",
|
|
112
|
+
version: "2.1.0",
|
|
113
|
+
runs: [
|
|
114
|
+
{
|
|
115
|
+
tool: {
|
|
116
|
+
driver: {
|
|
117
|
+
name: "midnight-shield",
|
|
118
|
+
version: VERSION,
|
|
119
|
+
informationUri: "https://github.com/efforthye/midnight-shield",
|
|
120
|
+
rules: rules.map((r) => ({
|
|
121
|
+
id: r.id,
|
|
122
|
+
name: r.name,
|
|
123
|
+
shortDescription: { text: r.summary },
|
|
124
|
+
helpUri: ruleDocUrl(r.id, r),
|
|
125
|
+
defaultConfiguration: { level: SARIF_LEVEL[r.severity] },
|
|
126
|
+
properties: {
|
|
127
|
+
tags: ["security", "privacy", "midnight", "compact"],
|
|
128
|
+
precision: r.confidence,
|
|
129
|
+
"security-severity": SECURITY_SEVERITY[r.severity],
|
|
130
|
+
},
|
|
131
|
+
})),
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
results: findings.map((f) => ({
|
|
135
|
+
ruleId: f.ruleId,
|
|
136
|
+
level: SARIF_LEVEL[f.severity],
|
|
137
|
+
message: { text: f.message },
|
|
138
|
+
locations: [
|
|
139
|
+
{
|
|
140
|
+
physicalLocation: {
|
|
141
|
+
artifactLocation: { uri: f.file },
|
|
142
|
+
region: { startLine: f.line, startColumn: f.col },
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
partialFingerprints: { "midnightShield/v1": f.fingerprint },
|
|
147
|
+
properties: { confidence: f.confidence },
|
|
148
|
+
...(f.suppressed ? { suppressions: [{ kind: f.suppressed === "inline" ? "inSource" : "external" }] } : {}),
|
|
149
|
+
})),
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
};
|
|
153
|
+
return JSON.stringify(sarif, null, 2);
|
|
154
|
+
}
|
|
155
|
+
const SEVERITY_ICON = {
|
|
156
|
+
critical: "🟣",
|
|
157
|
+
high: "🔴",
|
|
158
|
+
medium: "🟡",
|
|
159
|
+
low: "🔵",
|
|
160
|
+
info: "⚪",
|
|
161
|
+
};
|
|
162
|
+
function mdEscape(s) {
|
|
163
|
+
return s.replace(/\|/g, "\\|");
|
|
164
|
+
}
|
|
165
|
+
/** A Markdown report for audits and pull requests. */
|
|
166
|
+
export function formatMarkdown(all, fileCount, options = {}) {
|
|
167
|
+
const findings = all.filter((f) => !f.suppressed);
|
|
168
|
+
const suppressed = all.filter((f) => f.suppressed);
|
|
169
|
+
const out = [];
|
|
170
|
+
out.push(`# ${options.title ?? "Midnight Shield report"}`);
|
|
171
|
+
out.push("");
|
|
172
|
+
out.push(`Generated by [Midnight Shield](https://github.com/efforthye/midnight-shield) ${VERSION}${options.date ? ` on ${options.date}` : ""}. Scanned ${plural(fileCount, "file")}.`);
|
|
173
|
+
out.push("");
|
|
174
|
+
out.push("## Summary");
|
|
175
|
+
out.push("");
|
|
176
|
+
out.push("| Severity | Findings |");
|
|
177
|
+
out.push("| --- | --- |");
|
|
178
|
+
for (const s of SEVERITY_ORDER)
|
|
179
|
+
out.push(`| ${SEVERITY_ICON[s]} ${s} | ${findings.filter((f) => f.severity === s).length} |`);
|
|
180
|
+
out.push(`| **Total** | **${findings.length}** |`);
|
|
181
|
+
if (suppressed.length)
|
|
182
|
+
out.push(`| Suppressed | ${suppressed.length} |`);
|
|
183
|
+
out.push("");
|
|
184
|
+
if (findings.length) {
|
|
185
|
+
out.push("## Findings");
|
|
186
|
+
out.push("");
|
|
187
|
+
out.push("| # | Severity | Rule | Location | Confidence |");
|
|
188
|
+
out.push("| --- | --- | --- | --- | --- |");
|
|
189
|
+
findings.forEach((f, i) => {
|
|
190
|
+
out.push(`| ${i + 1} | ${SEVERITY_ICON[f.severity]} ${f.severity} | [${f.ruleId}](${f.docUrl}) ${f.ruleName} | \`${f.file}:${f.line}\` | ${f.confidence} |`);
|
|
191
|
+
});
|
|
192
|
+
out.push("");
|
|
193
|
+
findings.forEach((f, i) => {
|
|
194
|
+
out.push(`### ${i + 1}. ${f.ruleId} ${f.ruleName}`);
|
|
195
|
+
out.push("");
|
|
196
|
+
out.push(`**${f.severity}** · ${f.confidence} confidence · \`${f.file}:${f.line}:${f.col}\``);
|
|
197
|
+
out.push("");
|
|
198
|
+
out.push(f.message);
|
|
199
|
+
out.push("");
|
|
200
|
+
if (f.snippet.trim()) {
|
|
201
|
+
out.push("```compact");
|
|
202
|
+
out.push(f.snippet.replace(/^\s+/, ""));
|
|
203
|
+
out.push("```");
|
|
204
|
+
out.push("");
|
|
205
|
+
}
|
|
206
|
+
out.push(`How to fix: [${f.ruleId}](${f.docUrl})`);
|
|
207
|
+
out.push("");
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
out.push("No findings. ✔");
|
|
212
|
+
out.push("");
|
|
213
|
+
}
|
|
214
|
+
if (suppressed.length) {
|
|
215
|
+
out.push("## Suppressed findings");
|
|
216
|
+
out.push("");
|
|
217
|
+
out.push("| Rule | Location | Reason |");
|
|
218
|
+
out.push("| --- | --- | --- |");
|
|
219
|
+
for (const f of suppressed)
|
|
220
|
+
out.push(`| ${f.ruleId} ${f.ruleName} | \`${f.file}:${f.line}\` | ${f.suppressed === "inline" ? "inline comment" : "baseline"} |`);
|
|
221
|
+
out.push("");
|
|
222
|
+
}
|
|
223
|
+
out.push("---");
|
|
224
|
+
out.push("");
|
|
225
|
+
out.push("_A static analyzer reduces risk but does not prove a contract is secure. Contracts that hold value should also get a manual review._");
|
|
226
|
+
return out.join("\n");
|
|
227
|
+
}
|
|
228
|
+
// ---------- disclosure map ----------
|
|
229
|
+
const PROTECTION_LABEL = {
|
|
230
|
+
raw: "RAW",
|
|
231
|
+
hash: "hash",
|
|
232
|
+
commit: "commitment",
|
|
233
|
+
public: "public data",
|
|
234
|
+
constant: "constant",
|
|
235
|
+
};
|
|
236
|
+
function describeVia(d) {
|
|
237
|
+
if (d.via === "return")
|
|
238
|
+
return "return value";
|
|
239
|
+
if (d.via === "ledger write")
|
|
240
|
+
return `ledger ${d.target ?? ""}`.trim();
|
|
241
|
+
return "disclose()";
|
|
242
|
+
}
|
|
243
|
+
function describeSources(d) {
|
|
244
|
+
return d.sources.length ? d.sources.map((s) => (s.startsWith("witness:") ? `${s.slice(8)}()` : s.slice(6))).join(", ") : "-";
|
|
245
|
+
}
|
|
246
|
+
export function formatDisclosures(entries, options = {}) {
|
|
247
|
+
const color = options.color ?? true;
|
|
248
|
+
const c = (code, s) => (color ? code + s + RESET : s);
|
|
249
|
+
const lines = [];
|
|
250
|
+
let currentFile = "";
|
|
251
|
+
const shown = entries.filter((e) => e.protection !== "constant");
|
|
252
|
+
for (const d of shown) {
|
|
253
|
+
if (d.file !== currentFile) {
|
|
254
|
+
if (currentFile)
|
|
255
|
+
lines.push("");
|
|
256
|
+
lines.push(c(BOLD + "\x1b[4m", d.file));
|
|
257
|
+
lines.push(c(DIM, ` ${"line".padEnd(6)}${"circuit".padEnd(18)}${"becomes public via".padEnd(26)}${"private inputs".padEnd(28)}protection`));
|
|
258
|
+
currentFile = d.file;
|
|
259
|
+
}
|
|
260
|
+
const prot = d.protection === "raw"
|
|
261
|
+
? c("\x1b[31m", `RAW${d.secretBits !== undefined ? ` (${plural(d.secretBits, "bit")})` : ""}`)
|
|
262
|
+
: d.protection === "hash" && d.secretBits !== undefined && d.secretBits <= 32
|
|
263
|
+
? c("\x1b[33m", `hash (${plural(d.secretBits, "bit")})`)
|
|
264
|
+
: d.protection === "commit"
|
|
265
|
+
? c("\x1b[32m", PROTECTION_LABEL.commit)
|
|
266
|
+
: PROTECTION_LABEL[d.protection];
|
|
267
|
+
const flags = d.findings.length ? c("\x1b[33m", ` ⚠ ${d.findings.join(", ")}`) : "";
|
|
268
|
+
lines.push(` ${String(d.loc.line).padEnd(6)}${d.circuit.slice(0, 17).padEnd(18)}${describeVia(d).slice(0, 25).padEnd(26)}${describeSources(d).slice(0, 27).padEnd(28)}${prot}${flags}`);
|
|
269
|
+
}
|
|
270
|
+
if (!shown.length)
|
|
271
|
+
lines.push("Nothing in these files becomes public.");
|
|
272
|
+
const raw = shown.filter((d) => d.protection === "raw").length;
|
|
273
|
+
lines.push("");
|
|
274
|
+
lines.push(`${plural(shown.length, "disclosure point")}, ${raw} of them publish private data without a hash or commitment.`);
|
|
275
|
+
return lines.join("\n");
|
|
276
|
+
}
|
|
277
|
+
export function formatDisclosuresMarkdown(entries) {
|
|
278
|
+
const out = ["# Disclosure map", "", "Every point where data becomes public: `disclose()`, ledger writes, and return values of exported circuits.", ""];
|
|
279
|
+
out.push("| File | Line | Circuit | Becomes public via | Private inputs | Protection | Findings |");
|
|
280
|
+
out.push("| --- | --- | --- | --- | --- | --- | --- |");
|
|
281
|
+
for (const d of entries.filter((e) => e.protection !== "constant")) {
|
|
282
|
+
const prot = PROTECTION_LABEL[d.protection] + (d.secretBits !== undefined && d.protection !== "public" ? ` (${plural(d.secretBits, "bit")})` : "");
|
|
283
|
+
out.push(`| \`${d.file}\` | ${d.loc.line} | ${mdEscape(d.circuit)} | ${mdEscape(describeVia(d))} | ${mdEscape(describeSources(d))} | ${prot} | ${d.findings.join(", ")} |`);
|
|
284
|
+
}
|
|
285
|
+
return out.join("\n");
|
|
286
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Severity = "critical" | "high" | "medium" | "low" | "info";
|
|
2
|
+
export declare const SEVERITY_ORDER: Severity[];
|
|
3
|
+
/**
|
|
4
|
+
* How sure the rule is that a finding is a real problem, independent of how bad
|
|
5
|
+
* it would be. Rules that rely on naming or on guessing the author's intent
|
|
6
|
+
* report lower confidence.
|
|
7
|
+
*/
|
|
8
|
+
export type Confidence = "high" | "medium" | "low";
|
|
9
|
+
export declare const CONFIDENCE_ORDER: Confidence[];
|
|
10
|
+
export interface RuleMeta {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
severity: Severity;
|
|
14
|
+
confidence: Confidence;
|
|
15
|
+
summary: string;
|
|
16
|
+
/** documentation link; built-in rules point to docs/rules/<id>.md */
|
|
17
|
+
docUrl?: string;
|
|
18
|
+
/** a safe automatic fix exists (see fixes.ts) */
|
|
19
|
+
fixable?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare const RULES: RuleMeta[];
|
|
22
|
+
export declare const RULES_BY_ID: Map<string, RuleMeta>;
|
|
23
|
+
export declare function ruleDocUrl(id: string, meta?: RuleMeta): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Rule catalog. Detection logic lives in analyzer.ts; this file is the single
|
|
2
|
+
// source of truth for IDs, names, severities, confidence and descriptions.
|
|
3
|
+
export const SEVERITY_ORDER = ["critical", "high", "medium", "low", "info"];
|
|
4
|
+
export const CONFIDENCE_ORDER = ["high", "medium", "low"];
|
|
5
|
+
export const RULES = [
|
|
6
|
+
{
|
|
7
|
+
id: "MS000",
|
|
8
|
+
name: "parse-error",
|
|
9
|
+
severity: "info",
|
|
10
|
+
confidence: "high",
|
|
11
|
+
summary: "Part of the file could not be parsed, so analysis of that part was skipped.",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: "MS001",
|
|
15
|
+
name: "own-public-key-auth",
|
|
16
|
+
severity: "critical",
|
|
17
|
+
confidence: "high",
|
|
18
|
+
summary: "ownPublicKey() is supplied by the prover, so comparing it to a stored key does not authenticate the caller.",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: "MS002",
|
|
22
|
+
name: "unconstrained-witness",
|
|
23
|
+
severity: "medium",
|
|
24
|
+
confidence: "medium",
|
|
25
|
+
summary: "A witness result reaches public state without any assert that constrains it.",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "MS003",
|
|
29
|
+
name: "low-entropy-hash",
|
|
30
|
+
severity: "high",
|
|
31
|
+
confidence: "medium",
|
|
32
|
+
summary: "A hash of only low-entropy private values is made public and can be reversed by brute force.",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "MS004",
|
|
36
|
+
name: "weak-commitment-randomness",
|
|
37
|
+
severity: "high",
|
|
38
|
+
confidence: "high",
|
|
39
|
+
summary: "A commitment uses constant, public, or reused randomness, so it does not hide the committed value.",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "MS005",
|
|
43
|
+
name: "missing-domain-separation",
|
|
44
|
+
severity: "low",
|
|
45
|
+
confidence: "medium",
|
|
46
|
+
summary: "A hash has no constant domain tag, so the same input hashed for different purposes gives linkable outputs.",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: "MS006",
|
|
50
|
+
name: "missing-pragma",
|
|
51
|
+
severity: "info",
|
|
52
|
+
confidence: "high",
|
|
53
|
+
summary: "No `pragma language_version` is declared.",
|
|
54
|
+
fixable: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: "MS007",
|
|
58
|
+
name: "disclosed-secret",
|
|
59
|
+
severity: "high",
|
|
60
|
+
confidence: "low",
|
|
61
|
+
summary: "The raw value of a secret-named witness or parameter (secret key, seed, password) is made public.",
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
id: "MS008",
|
|
65
|
+
name: "missing-nullifier",
|
|
66
|
+
severity: "medium",
|
|
67
|
+
confidence: "low",
|
|
68
|
+
summary: "A circuit authorizes with an anonymous Merkle membership proof and changes state without a nullifier check, so the action can be repeated.",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "MS009",
|
|
72
|
+
name: "unbound-merkle-path",
|
|
73
|
+
severity: "high",
|
|
74
|
+
confidence: "high",
|
|
75
|
+
summary: "A Merkle path's root is checked, but the path's leaf is never tied to the caller, so anyone can reuse a public path.",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
id: "MS010",
|
|
79
|
+
name: "linkable-ledger-update",
|
|
80
|
+
severity: "medium",
|
|
81
|
+
confidence: "medium",
|
|
82
|
+
summary: "In a contract with anonymous (Merkle) membership, the same identifier derived from a secret is published by several circuits, so observers can link those actions to one member.",
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: "MS101",
|
|
86
|
+
name: "remote-proof-server",
|
|
87
|
+
severity: "critical",
|
|
88
|
+
confidence: "medium",
|
|
89
|
+
summary: "TypeScript/JavaScript: the proof server URL points to a remote host, which would see every user's private inputs.",
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "MS102",
|
|
93
|
+
name: "secret-in-log",
|
|
94
|
+
severity: "high",
|
|
95
|
+
confidence: "medium",
|
|
96
|
+
summary: "TypeScript/JavaScript: a secret key, seed or password is passed to a log call.",
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
export const RULES_BY_ID = new Map(RULES.map((r) => [r.id, r]));
|
|
100
|
+
export function ruleDocUrl(id, meta) {
|
|
101
|
+
if (meta?.docUrl)
|
|
102
|
+
return meta.docUrl;
|
|
103
|
+
return `https://github.com/efforthye/midnight-shield/blob/main/docs/rules/${id}.md`;
|
|
104
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { RawFinding } from "./analyzer.js";
|
|
2
|
+
export declare const SDK_EXTENSIONS: string[];
|
|
3
|
+
export declare function isSdkFile(file: string): boolean;
|
|
4
|
+
/** Test files use fake secrets and fake remote servers on purpose, so SDK checks skip them. */
|
|
5
|
+
export declare function isTestFile(file: string): boolean;
|
|
6
|
+
export declare function lintSdkSource(source: string): RawFinding[];
|