@cachly-dev/audit-rules 0.0.0-stage → 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 +181 -0
- package/README.md +175 -2
- package/action.yml +59 -0
- package/bin/audit-rules.mjs +185 -0
- package/examples/vulnerable-demo/.cursorrules +12 -0
- package/examples/vulnerable-demo/CLAUDE.md +18 -0
- package/package.json +49 -4
- package/src/formatters.mjs +183 -0
- package/src/rules.mjs +619 -0
- package/src/scanner.mjs +209 -0
package/src/scanner.mjs
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { RULE_CATALOG, checkFileContent } from "./rules.mjs";
|
|
4
|
+
|
|
5
|
+
const SKIP_DIRS = new Set([
|
|
6
|
+
".git",
|
|
7
|
+
"node_modules",
|
|
8
|
+
"dist",
|
|
9
|
+
"build",
|
|
10
|
+
"out",
|
|
11
|
+
".next",
|
|
12
|
+
".nuxt",
|
|
13
|
+
".svelte-kit",
|
|
14
|
+
"coverage",
|
|
15
|
+
"vendor",
|
|
16
|
+
"target",
|
|
17
|
+
"__pycache__",
|
|
18
|
+
".venv",
|
|
19
|
+
"venv",
|
|
20
|
+
".tox",
|
|
21
|
+
".gradle",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Identifies whether a repo-relative POSIX path is an AI coding rule file
|
|
26
|
+
* and returns its tool family name (or null if not a rule file).
|
|
27
|
+
*/
|
|
28
|
+
export function classifyRuleFile(relPosixPath) {
|
|
29
|
+
const base = path.posix.basename(relPosixPath);
|
|
30
|
+
const lower = relPosixPath.toLowerCase();
|
|
31
|
+
const baseLower = base.toLowerCase();
|
|
32
|
+
|
|
33
|
+
if (baseLower === "claude.md" || baseLower === "claude.local.md") {
|
|
34
|
+
return "Claude Code";
|
|
35
|
+
}
|
|
36
|
+
if (baseLower === "agents.md") {
|
|
37
|
+
return "AGENTS.md / Codex";
|
|
38
|
+
}
|
|
39
|
+
if (baseLower === "skill.md") {
|
|
40
|
+
return "Agent Skill";
|
|
41
|
+
}
|
|
42
|
+
if (baseLower === ".cursorrules" || /^(\.cursor\/rules\/).+\.(?:mdc|md)$/i.test(relPosixPath)) {
|
|
43
|
+
return "Cursor";
|
|
44
|
+
}
|
|
45
|
+
if (
|
|
46
|
+
lower === ".github/copilot-instructions.md" ||
|
|
47
|
+
/^\.github\/instructions\/.+\.instructions\.md$/i.test(relPosixPath)
|
|
48
|
+
) {
|
|
49
|
+
return "GitHub Copilot";
|
|
50
|
+
}
|
|
51
|
+
if (baseLower === ".windsurfrules" || /^\.windsurf\/rules\/.+\.md$/i.test(relPosixPath)) {
|
|
52
|
+
return "Windsurf";
|
|
53
|
+
}
|
|
54
|
+
if (baseLower === ".clinerules" || /^\.clinerules\/.+\.md$/i.test(relPosixPath)) {
|
|
55
|
+
return "Cline";
|
|
56
|
+
}
|
|
57
|
+
if (baseLower === ".rules") {
|
|
58
|
+
return "Zed / Generic Rules";
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function toPosix(p) {
|
|
64
|
+
return p.split(path.sep).join("/");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function walkRuleFiles(rootDir, extraIncludes = []) {
|
|
68
|
+
const found = [];
|
|
69
|
+
const normalizedExtras = extraIncludes.map((item) => toPosix(item).replace(/^\.\//, ""));
|
|
70
|
+
|
|
71
|
+
function visit(currentDir) {
|
|
72
|
+
let entries;
|
|
73
|
+
try {
|
|
74
|
+
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
75
|
+
} catch {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
80
|
+
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
if (entry.isSymbolicLink()) continue;
|
|
83
|
+
|
|
84
|
+
const absPath = path.join(currentDir, entry.name);
|
|
85
|
+
const relPosix = toPosix(path.relative(rootDir, absPath));
|
|
86
|
+
|
|
87
|
+
if (entry.isDirectory()) {
|
|
88
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
89
|
+
visit(absPath);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!entry.isFile()) continue;
|
|
94
|
+
|
|
95
|
+
const family = classifyRuleFile(relPosix);
|
|
96
|
+
const extraMatch = normalizedExtras.some(
|
|
97
|
+
(inc) => relPosix === inc || path.posix.basename(relPosix) === inc
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (family || extraMatch) {
|
|
101
|
+
found.push({
|
|
102
|
+
absPath,
|
|
103
|
+
relativePath: relPosix,
|
|
104
|
+
family: family || "Custom Rule",
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const stat = fs.statSync(rootDir);
|
|
111
|
+
if (stat.isFile()) {
|
|
112
|
+
const base = path.basename(rootDir);
|
|
113
|
+
return {
|
|
114
|
+
repoRoot: path.dirname(rootDir),
|
|
115
|
+
files: [
|
|
116
|
+
{
|
|
117
|
+
absPath: rootDir,
|
|
118
|
+
relativePath: base,
|
|
119
|
+
family: classifyRuleFile(base) || "Custom Rule",
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
visit(rootDir);
|
|
126
|
+
return { repoRoot: rootDir, files: found };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Scans a directory or single rule file and returns a structured audit report.
|
|
131
|
+
* Makes zero network calls.
|
|
132
|
+
*/
|
|
133
|
+
export function scanTarget(targetPath, options = {}) {
|
|
134
|
+
const startedAt = performance.now();
|
|
135
|
+
const resolvedTarget = path.resolve(targetPath);
|
|
136
|
+
|
|
137
|
+
if (!fs.existsSync(resolvedTarget)) {
|
|
138
|
+
throw new Error(`Target path does not exist: ${targetPath}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const { repoRoot, files } = walkRuleFiles(resolvedTarget, options.include || []);
|
|
142
|
+
|
|
143
|
+
const existingPathCheck = (relCandidate) => {
|
|
144
|
+
const candidateAbs = path.resolve(repoRoot, relCandidate);
|
|
145
|
+
// Keep checks strictly inside repoRoot
|
|
146
|
+
const relCheck = path.relative(repoRoot, candidateAbs);
|
|
147
|
+
if (relCheck.startsWith("..") || path.isAbsolute(relCheck)) return true;
|
|
148
|
+
return fs.existsSync(candidateAbs);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const findings = [];
|
|
152
|
+
const toolFamilies = new Set();
|
|
153
|
+
|
|
154
|
+
for (const fileInfo of files) {
|
|
155
|
+
if (fileInfo.family !== "Agent Skill" && fileInfo.family !== "Custom Rule") {
|
|
156
|
+
toolFamilies.add(fileInfo.family);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const content = fs.readFileSync(fileInfo.absPath, "utf8");
|
|
160
|
+
const fileFindings = checkFileContent({
|
|
161
|
+
relativePath: fileInfo.relativePath,
|
|
162
|
+
content,
|
|
163
|
+
existingPathCheck: options.skipDeadRefs ? null : existingPathCheck,
|
|
164
|
+
});
|
|
165
|
+
findings.push(...fileFindings);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Repo-wide rule fragmentation check (HYG-002)
|
|
169
|
+
if (toolFamilies.size >= 2) {
|
|
170
|
+
const familyList = Array.from(toolFamilies).sort().join(", ");
|
|
171
|
+
findings.push({
|
|
172
|
+
ruleId: "HYG-002",
|
|
173
|
+
severity: RULE_CATALOG["HYG-002"].severity,
|
|
174
|
+
file: files[0]?.relativePath || ".",
|
|
175
|
+
line: 1,
|
|
176
|
+
message: `Found rule files for ${toolFamilies.size} different tools (${familyList}); rules can drift out of sync across teammates.`,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Sort deterministically: errors first, then file, then line, then ruleId
|
|
181
|
+
const severityRank = { error: 0, warn: 1, info: 2 };
|
|
182
|
+
findings.sort((a, b) => {
|
|
183
|
+
const sr = (severityRank[a.severity] ?? 9) - (severityRank[b.severity] ?? 9);
|
|
184
|
+
if (sr !== 0) return sr;
|
|
185
|
+
const fr = a.file.localeCompare(b.file);
|
|
186
|
+
if (fr !== 0) return fr;
|
|
187
|
+
const lr = (a.line || 0) - (b.line || 0);
|
|
188
|
+
if (lr !== 0) return lr;
|
|
189
|
+
return a.ruleId.localeCompare(b.ruleId);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const summary = {
|
|
193
|
+
filesScanned: files.length,
|
|
194
|
+
errors: findings.filter((f) => f.severity === "error").length,
|
|
195
|
+
warnings: findings.filter((f) => f.severity === "warn").length,
|
|
196
|
+
info: findings.filter((f) => f.severity === "info").length,
|
|
197
|
+
durationMs: Math.max(1, Math.round(performance.now() - startedAt)),
|
|
198
|
+
networkCalls: 0,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
tool: "@cachly-dev/audit-rules",
|
|
203
|
+
version: options.version || "0.1.0",
|
|
204
|
+
target: toPosix(resolvedTarget),
|
|
205
|
+
files: files.map((f) => ({ path: f.relativePath, family: f.family })),
|
|
206
|
+
findings,
|
|
207
|
+
summary,
|
|
208
|
+
};
|
|
209
|
+
}
|