Repository navigation
Expand file tree
/
Copy pathbyte-audit.mjs
More file actions
292 lines (254 loc) · 8.72 KB
/
Copy pathbyte-audit.mjs
File metadata and controls
292 lines (254 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { execFileSync } from "node:child_process";
const GENERATED_AUDIT_FILES = new Set([
"audit-output.json",
"AUDIT-MANIFEST.sha256",
"AUDIT-TRAIL.ndjson"
]);
const TEXT_EXTENSIONS = new Set([
".js", ".mjs", ".cjs", ".json", ".md", ".txt", ".yml", ".yaml",
".toml", ".cmake", ".c", ".h", ".cpp", ".hpp", ".ts", ".tsx",
".jsx", ".html", ".css", ".gitignore", ".gitattributes"
]);
function fail(message) {
console.error(JSON.stringify({ status: "FAILED", reason: message }, null, 2));
process.exit(1);
}
function sha256Bytes(bytes) {
return crypto.createHash("sha256").update(bytes).digest("hex");
}
function readBytes(filePath) {
return fs.readFileSync(filePath);
}
function hasBom(bytes) {
return bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf;
}
function readJsonNoBom(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf8").replace(/^\uFEFF/, ""));
}
function stable(value) {
if (value === null || typeof value !== "object") {
return JSON.stringify(value);
}
if (Array.isArray(value)) {
return "[" + value.map(stable).join(",") + "]";
}
const keys = Object.keys(value).sort();
return "{" + keys.map((key) => JSON.stringify(key) + ":" + stable(value[key])).join(",") + "}";
}
function runGit(repoPath, args, allowFailure = false) {
try {
return execFileSync("git", args, {
cwd: repoPath,
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"]
});
} catch (error) {
if (allowFailure) {
return "";
}
throw error;
}
}
function parseGitStatus(raw) {
return raw
.split(/\r?\n/)
.map((line) => line.trimEnd())
.filter(Boolean)
.map((line) => {
const status = line.slice(0, 2);
const filePath = line.slice(3).replace(/^"|"$/g, "");
return { status, path: filePath.replaceAll("\\", "/") };
})
.filter((entry) => !GENERATED_AUDIT_FILES.has(entry.path));
}
function gitTrackedFiles(repoPath) {
const raw = runGit(repoPath, ["ls-files", "-z"], true);
return raw
.split("\0")
.filter(Boolean)
.map((file) => file.replaceAll("\\", "/"))
.filter((file) => !GENERATED_AUDIT_FILES.has(file))
.sort();
}
function isTextCandidate(filePath) {
const base = path.basename(filePath);
const ext = path.extname(filePath).toLowerCase();
return TEXT_EXTENSIONS.has(ext) || TEXT_EXTENSIONS.has(base);
}
function safeVerifyOutput(repoPath) {
const verifyPath = path.join(repoPath, "verify-output.json");
if (!fs.existsSync(verifyPath)) {
return {
present: false,
parse_ok: false,
status: "UNAUDITED",
sha256: null,
failure_codes: ["VERIFY_OUTPUT_MISSING"]
};
}
const bytes = readBytes(verifyPath);
try {
const parsed = readJsonNoBom(verifyPath);
return {
present: true,
parse_ok: true,
status: parsed.status || "STATUS_MISSING",
sha256: sha256Bytes(bytes),
bom_present: hasBom(bytes),
failure_codes: Array.isArray(parsed.failure_codes) ? parsed.failure_codes : []
};
} catch (error) {
return {
present: true,
parse_ok: false,
status: "FAILED",
sha256: sha256Bytes(bytes),
bom_present: hasBom(bytes),
failure_codes: ["VERIFY_OUTPUT_PARSE_FAILED"],
error: error.message
};
}
}
function auditRepo(repo, rootDir) {
const repoPath = path.resolve(rootDir, "..", repo.name);
const exists = fs.existsSync(repoPath);
const result = {
name: repo.name,
ring: repo.ring,
class: repo.class || null,
repo_path: repoPath,
repo_exists: exists,
git_branch: null,
git_commit: null,
dirty: false,
dirty_entries: [],
required_files: Array.isArray(repo.required_files) ? repo.required_files.slice().sort() : [],
missing_required_files: [],
verify_output: null,
tracked_file_count: 0,
tracked_files: [],
bom_failures: [],
byte_status: "UNAUDITED"
};
if (!exists) {
result.byte_status = "REPO_MISSING";
return result;
}
result.git_branch = runGit(repoPath, ["branch", "--show-current"], true).trim() || null;
result.git_commit = runGit(repoPath, ["rev-parse", "HEAD"], true).trim() || null;
result.dirty_entries = parseGitStatus(runGit(repoPath, ["status", "--porcelain=v1"], true));
result.dirty = result.dirty_entries.length > 0;
result.missing_required_files = result.required_files.filter((file) => {
return !fs.existsSync(path.join(repoPath, file));
});
result.verify_output = safeVerifyOutput(repoPath);
const tracked = gitTrackedFiles(repoPath);
result.tracked_file_count = tracked.length;
for (const relPath of tracked) {
const absolutePath = path.join(repoPath, relPath);
if (!fs.existsSync(absolutePath) || !fs.statSync(absolutePath).isFile()) {
continue;
}
const bytes = readBytes(absolutePath);
const bomPresent = hasBom(bytes);
const fileRecord = {
path: relPath,
size_bytes: bytes.length,
sha256: sha256Bytes(bytes),
bom_present: bomPresent
};
result.tracked_files.push(fileRecord);
if (bomPresent && isTextCandidate(relPath)) {
result.bom_failures.push(relPath);
}
}
const failures = [];
if (result.missing_required_files.length > 0) failures.push("REQUIRED_FILES_MISSING");
if (!result.verify_output.present) failures.push("VERIFY_OUTPUT_MISSING");
if (result.verify_output.present && !result.verify_output.parse_ok) failures.push("VERIFY_OUTPUT_PARSE_FAILED");
if (result.verify_output.bom_present) failures.push("VERIFY_OUTPUT_BOM_PRESENT");
if (result.verify_output.status !== "VERIFIED") failures.push("VERIFY_OUTPUT_NOT_VERIFIED");
if (result.bom_failures.length > 0) failures.push("BOM_PRESENT");
if (result.dirty) failures.push("DIRTY_WORKTREE");
result.failure_codes = failures;
result.byte_status = failures.length === 0 ? "BYTE_VERIFIED" : "BYTE_BLOCKED";
return result;
}
const rootDir = process.cwd();
const manifestPath = path.join(rootDir, "cluster-manifest-v2.json");
if (!fs.existsSync(manifestPath)) {
fail("MISSING_CLUSTER_MANIFEST");
}
const manifestBytes = readBytes(manifestPath);
const manifest = readJsonNoBom(manifestPath);
const repos = Array.isArray(manifest.repos) ? manifest.repos : [];
const ringIndex = process.argv.indexOf("--ring");
const targetRing = ringIndex >= 0 ? Number(process.argv[ringIndex + 1]) : null;
if (ringIndex >= 0 && !Number.isInteger(targetRing)) {
fail("INVALID_RING_ARGUMENT");
}
const selectedRepos = targetRing === null ? repos : repos.filter((repo) => repo.ring === targetRing);
const auditedRepos = selectedRepos.map((repo) => auditRepo(repo, rootDir));
const blocked = auditedRepos.filter((repo) => repo.byte_status !== "BYTE_VERIFIED");
const summary = {
target_ring: targetRing,
repo_count: auditedRepos.length,
byte_verified_count: auditedRepos.length - blocked.length,
byte_blocked_count: blocked.length,
byte_gate_status: blocked.length === 0 ? "BYTE_GATE_PASS_VERIFIED" : "BYTE_GATE_BLOCKED"
};
const report = {
auditor: "Riverbraid Byte Floor Auditor",
report_version: "1.0.0",
claim_boundary: "byte-level-local-audit-trail",
manifest: {
path: "cluster-manifest-v2.json",
sha256: sha256Bytes(manifestBytes),
schema: manifest._schema || null,
version: manifest._version || null,
constellation_root: manifest._constellation_root || null,
cluster_version: manifest._cluster_version || null,
full_constellation_claimed: manifest.full_constellation_claimed === true
},
summary,
repos: auditedRepos
};
const preimage = stable(report);
report.audit_preimage_sha256 = sha256Bytes(Buffer.from(preimage, "utf8"));
const finalJson = stable(report) + "\n";
const finalDigest = sha256Bytes(Buffer.from(finalJson, "utf8"));
fs.writeFileSync(path.join(rootDir, "audit-output.json"), finalJson, "utf8");
fs.writeFileSync(path.join(rootDir, "AUDIT-MANIFEST.sha256"), finalDigest + " audit-output.json\n", "utf8");
const trailRecord = {
generated_at_utc: new Date().toISOString(),
auditor: report.auditor,
report_version: report.report_version,
target_ring: targetRing,
byte_gate_status: summary.byte_gate_status,
audit_output_sha256: finalDigest,
blocked_repos: blocked.map((repo) => ({
name: repo.name,
failure_codes: repo.failure_codes
}))
};
fs.appendFileSync(
path.join(rootDir, "AUDIT-TRAIL.ndjson"),
JSON.stringify(trailRecord) + "\n",
"utf8"
);
console.log(JSON.stringify({
status: summary.byte_gate_status,
target_ring: targetRing,
repo_count: summary.repo_count,
byte_verified_count: summary.byte_verified_count,
byte_blocked_count: summary.byte_blocked_count,
audit_output_sha256: finalDigest,
blocked_repos: trailRecord.blocked_repos
}, null, 2));
if (blocked.length > 0) {
process.exit(1);
}
process.exit(0);