fix: 收口模板契约与安全门禁
统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
This commit is contained in:
1 parent
456171aa85
commit
4f0b50c7f1
84 files changed
+7367
-5180
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import crypto from "node:crypto";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import ts from "typescript";
|
||||
|
||||
// 静态检查不推断运行时成功;未知表达式保留为 review。
|
||||
@@ -409,3 +412,68 @@ export function applyManualReview(reports, reviews, reviewScope) {
|
||||
}
|
||||
return reports;
|
||||
}
|
||||
|
||||
export function applyCompletionEvidence(reports, registry, root) {
|
||||
if (registry === undefined) {
|
||||
for (const report of reports) report.completionStatus = "unverified";
|
||||
return { completionStatus: "unverified", reports };
|
||||
}
|
||||
if (
|
||||
!registry ||
|
||||
registry.schemaVersion !== 1 ||
|
||||
registry.scope !== "模板完成证据登记,保留命令、日志与产物哈希" ||
|
||||
!registry.templates ||
|
||||
typeof registry.templates !== "object" ||
|
||||
Array.isArray(registry.templates)
|
||||
) {
|
||||
throw new Error("Completion evidence registry has an unsupported schema");
|
||||
}
|
||||
|
||||
const expectedTemplates = new Set(reports.map((report) => report.template));
|
||||
const registeredTemplates = new Set(Object.keys(registry.templates));
|
||||
const rootPath = path.resolve(root);
|
||||
const insideRoot = (value) => {
|
||||
const resolved = path.resolve(rootPath, value);
|
||||
if (resolved !== rootPath && !resolved.startsWith(rootPath + path.sep)) return null;
|
||||
return resolved;
|
||||
};
|
||||
const validEvidence = (evidence) => {
|
||||
if (!evidence || evidence.status !== "passed" || !Array.isArray(evidence.commands)) return null;
|
||||
if (!evidence.commands.length || evidence.commands.some((command) => !command.trim()))
|
||||
return null;
|
||||
if (!Array.isArray(evidence.artifacts) || !evidence.artifacts.length) return null;
|
||||
return evidence;
|
||||
};
|
||||
|
||||
let complete = true;
|
||||
for (const report of reports) {
|
||||
const evidence = registry.templates[report.template] ?? {};
|
||||
const completionIssues = [];
|
||||
for (const key of report.requiredEvidence) {
|
||||
const item = validEvidence(evidence[key]);
|
||||
if (!item) {
|
||||
completionIssues.push(`${key}: missing or invalid passed evidence`);
|
||||
continue;
|
||||
}
|
||||
for (const artifact of item.artifacts) {
|
||||
if (!artifact?.path || !/^[a-f0-9]{64}$/.test(artifact.sha256 ?? "")) {
|
||||
completionIssues.push(`${key}: artifact path or sha256 is invalid`);
|
||||
continue;
|
||||
}
|
||||
const filePath = insideRoot(artifact.path);
|
||||
if (!filePath || !fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
|
||||
completionIssues.push(`${key}: artifact does not exist: ${artifact.path}`);
|
||||
continue;
|
||||
}
|
||||
const hash = crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
|
||||
if (hash !== artifact.sha256)
|
||||
completionIssues.push(`${key}: artifact checksum mismatch: ${artifact.path}`);
|
||||
}
|
||||
}
|
||||
report.evidence = evidence;
|
||||
report.completionIssues = completionIssues;
|
||||
report.completionStatus = completionIssues.length ? "incomplete" : "complete";
|
||||
if (completionIssues.length) complete = false;
|
||||
}
|
||||
return { completionStatus: complete ? "complete" : "incomplete", reports };
|
||||
}
|
||||
@@ -8,7 +8,6 @@ const root = path.resolve(__dirname, "..");
|
||||
const packagesRoot = path.join(root, "packages");
|
||||
const configRoot = path.join(packagesRoot, "config/src");
|
||||
const industrySource = fs.readFileSync(path.join(configRoot, "brand-templates.ts"), "utf8");
|
||||
const visualSource = fs.readFileSync(path.join(configRoot, "visual-templates.ts"), "utf8");
|
||||
const capabilitySource = fs.readFileSync(path.join(configRoot, "capability-contracts.ts"), "utf8");
|
||||
const platformPackage = JSON.parse(
|
||||
fs.readFileSync(path.join(root, "apps/taro/package.json"), "utf8"),
|
||||
@@ -57,10 +56,8 @@ function stringArray(expression) {
|
||||
: [];
|
||||
}
|
||||
const brandTemplateObject = exportedObject(industrySource, "brandTemplates");
|
||||
const visualTemplateObject = exportedObject(visualSource, "visualTemplates");
|
||||
const capabilityObject = exportedObject(capabilitySource, "capabilityRegistry");
|
||||
const industries = objectKeys(brandTemplateObject);
|
||||
const visualKeys = objectKeys(visualTemplateObject);
|
||||
const capabilityKeys = objectKeys(capabilityObject);
|
||||
const industryCapabilities = Object.fromEntries(
|
||||
(ts.isObjectLiteralExpression(brandTemplateObject) ? brandTemplateObject.properties : [])
|
||||
@@ -91,6 +88,9 @@ const packages = fs
|
||||
}))
|
||||
.sort((left, right) => left.manifest.packageKey.localeCompare(right.manifest.packageKey));
|
||||
|
||||
const visualKeys = [
|
||||
...new Set(packages.map((item) => item.manifest.visualTemplateKey).filter(Boolean)),
|
||||
];
|
||||
const results = [];
|
||||
const errors = [];
|
||||
const versionMajorMinor = (value) =>
|
||||
@@ -110,7 +110,7 @@ for (const { directory, manifest, package: packageMetadata } of packages) {
|
||||
? manifest.supportedIndustryTemplates
|
||||
: [];
|
||||
const visualKey = manifest.visualTemplateKey;
|
||||
const visualRegistered = typeof visualKey === "string" && visualKeys.includes(visualKey);
|
||||
const visualRegistered = visualKey === key && visualKeys.includes(visualKey);
|
||||
const rendererPath = `${directory}/taro/index.tsx`;
|
||||
const rendererExists = fs.existsSync(path.join(packagesRoot, rendererPath));
|
||||
const rendererExport = rendererExists
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { applyManualReview, diagnoseTemplate } from "./lib/template-doctor.mjs";
|
||||
import {
|
||||
applyCompletionEvidence,
|
||||
applyManualReview,
|
||||
diagnoseTemplate,
|
||||
} from "./lib/template-doctor.mjs";
|
||||
|
||||
try {
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
@@ -31,14 +35,30 @@ try {
|
||||
);
|
||||
const registry = JSON.parse(fs.readFileSync(registryPath, "utf8"));
|
||||
applyManualReview(reports, registry.items, registry.scope);
|
||||
const report = { schemaVersion: 1, completionStatus: "unverified", templates: reports };
|
||||
const evidencePath = path.join(
|
||||
root,
|
||||
".codex-tasks/template-audit-execution/raw/completion-evidence.json",
|
||||
);
|
||||
const evidence = fs.existsSync(evidencePath)
|
||||
? JSON.parse(fs.readFileSync(evidencePath, "utf8"))
|
||||
: undefined;
|
||||
const completion = applyCompletionEvidence(reports, evidence, root);
|
||||
const report = {
|
||||
schemaVersion: 1,
|
||||
completionStatus: completion.completionStatus,
|
||||
templates: reports,
|
||||
};
|
||||
const output = JSON.stringify(report, null, 2) + "\n";
|
||||
if (options["--output"]) {
|
||||
fs.mkdirSync(path.dirname(path.resolve(options["--output"])), { recursive: true });
|
||||
fs.writeFileSync(options["--output"], output);
|
||||
}
|
||||
process.stdout.write(output);
|
||||
process.exitCode = reports.some((r) => r.staticStatus === "fail") ? 1 : 0;
|
||||
process.exitCode = reports.some(
|
||||
(r) => r.staticStatus === "fail" || r.completionStatus === "incomplete",
|
||||
)
|
||||
? 1
|
||||
: 0;
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
process.exitCode = 2;
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { applyManualReview, diagnoseTemplate } from "../lib/template-doctor.mjs";
|
||||
import {
|
||||
applyCompletionEvidence,
|
||||
applyManualReview,
|
||||
diagnoseTemplate,
|
||||
} from "../lib/template-doctor.mjs";
|
||||
|
||||
test("真实模板自动发现并读取完整声明,生成可追溯区域报告", () => {
|
||||
const root = new URL("../../packages/", import.meta.url);
|
||||
@@ -70,6 +77,63 @@ test("CLI 自动发现全部模板,问题退出 1,未知模板退出 2", ()
|
||||
assert.equal(missing.status, 2);
|
||||
});
|
||||
|
||||
test("完成证据登记必须覆盖全部要求并校验产物哈希", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "template-doctor-evidence-"));
|
||||
const artifactPath = path.join(root, "integration.log");
|
||||
fs.writeFileSync(artifactPath, "passed\n");
|
||||
const sha256 = createHash("sha256").update(fs.readFileSync(artifactPath)).digest("hex");
|
||||
const report = {
|
||||
template: "fixture",
|
||||
staticStatus: "pass",
|
||||
completionStatus: "unverified",
|
||||
requiredEvidence: ["real-manifest-interaction", "visual-reference"],
|
||||
};
|
||||
const evidence = (items) => ({
|
||||
schemaVersion: 1,
|
||||
scope: "模板完成证据登记,保留命令、日志与产物哈希",
|
||||
templates: {
|
||||
fixture: Object.fromEntries(
|
||||
report.requiredEvidence.map((key) => [
|
||||
key,
|
||||
items.includes(key)
|
||||
? {
|
||||
status: "passed",
|
||||
commands: ["pnpm test"],
|
||||
artifacts: [{ path: "integration.log", sha256 }],
|
||||
}
|
||||
: undefined,
|
||||
]),
|
||||
),
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const complete = applyCompletionEvidence(
|
||||
[structuredClone(report)],
|
||||
evidence(report.requiredEvidence),
|
||||
root,
|
||||
);
|
||||
assert.equal(complete.completionStatus, "complete");
|
||||
assert.equal(complete.reports[0].completionStatus, "complete");
|
||||
|
||||
const incomplete = applyCompletionEvidence(
|
||||
[structuredClone(report)],
|
||||
evidence(["real-manifest-interaction"]),
|
||||
root,
|
||||
);
|
||||
assert.equal(incomplete.completionStatus, "incomplete");
|
||||
assert.match(incomplete.reports[0].completionIssues.join("\n"), /visual-reference/);
|
||||
|
||||
const invalid = evidence(report.requiredEvidence);
|
||||
invalid.templates.fixture["visual-reference"].artifacts[0].sha256 = "0".repeat(64);
|
||||
const checksumMismatch = applyCompletionEvidence([structuredClone(report)], invalid, root);
|
||||
assert.equal(checksumMismatch.completionStatus, "incomplete");
|
||||
assert.match(checksumMismatch.reports[0].completionIssues.join("\n"), /checksum mismatch/);
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("人工审阅登记只能匹配真实 review,新问题仍会失败", () => {
|
||||
const report = {
|
||||
template: "fixture",
|
||||
|
||||
Reference in new issue
Block a user