Skip to content

Commit e9afd0c

Browse files
noreplyclaude
andcommitted
fork: schema check runs from the base branch and reads whole files
Review round 1 of the schema check: - The workflow runs on pull_request_target, so its file and the check script come from the default branch; a pull request can no longer weaken, skip or delete the check that judges it. The pull request's commits are only fetched and read as blobs, never checked out or run. - Changes to the check's own files or to any .gitattributes are refused once the base has the check; only the owner can merge such a change. - Files the migrations import (transitively, computed from both trees) count as schema files, and a shipped file whose text holds a schema statement before or after the change is refused; statements match across lines. - Paths come from `git diff --raw -z` and texts from the blobs, so quoted non-ASCII paths and .gitattributes cannot hide a change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2146267 commit e9afd0c

3 files changed

Lines changed: 499 additions & 88 deletions

File tree

‎.github/scripts/fork-schema-check.cjs‎

Lines changed: 190 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,200 @@
11
// Refuses fork changes to the database schema: T3 has no down-migrations, so a fork migration
2-
// strands installs that return to upstream. Fork state goes in existing JSON fields instead.
3-
// Usage: node .github/scripts/fork-schema-check.cjs <base> <head> (checks <base>...<head>).
2+
// strands installs that return to upstream. Usage: fork-schema-check.cjs <base> <head>.
3+
// Refused: migrations, schema setup and what they import; shipped files holding a schema statement
4+
// (matched across lines); this check itself and .gitattributes. Files are read as git blobs.
45

56
const { execFileSync } = require("node:child_process");
7+
const path = require("node:path");
68

7-
// Files that define or apply the schema. Any change to them is refused.
8-
const SCHEMA_PATHS = [
9+
// Files that define or apply the schema. Any change to them, or to what they import, is refused.
10+
const SCHEMA_ROOTS = [
911
/^apps\/server\/src\/persistence\/Migrations(\/|\.ts$)/,
1012
/^apps\/server\/src\/persistence\/databaseMigrationHead\.ts$/,
1113
/^apps\/server\/src\/persistence\/initializeV2Database\.ts$/,
1214
/^apps\/server\/src\/persistence\/reconcileV2PreviewMigration\.ts$/,
1315
];
1416

15-
// Schema statements added anywhere else in shipped source (tests may build their own tables).
17+
// The check's own files. Changing them would let a PR judge itself, so it is never allowed.
18+
const CHECK_SCRIPT = ".github/scripts/fork-schema-check.cjs";
19+
const CHECK_FILES = [
20+
CHECK_SCRIPT,
21+
".github/scripts/fork-schema-check.test.cjs",
22+
".github/workflows/fork-checks.yml",
23+
];
24+
const GITATTRIBUTES = /(^|\/)\.gitattributes$/;
25+
26+
// Schema statements in shipped source (tests may build their own tables). \s spans line breaks.
1627
const SCHEMA_STATEMENT =
1728
/\b(CREATE\s+(UNIQUE\s+)?(TABLE|INDEX|VIEW|TRIGGER|VIRTUAL\s+TABLE)|ALTER\s+TABLE|DROP\s+(TABLE|INDEX|VIEW|TRIGGER))\b/i;
18-
const SHIPPED_SOURCE = /^(apps|packages)\/.+\.(ts|tsx|js|mjs|cjs|sql)$/;
19-
const TEST_SOURCE = /(\.test\.|\.spec\.|\/testkit\/|\/__tests__\/|\/test\/)/;
29+
const SHIPPED_SOURCE = /^(apps|packages)\/.+\.(ts|tsx|mts|cts|js|mjs|cjs|sql)$/;
30+
const TEST_SOURCE = /(\.test\.|\.spec\.|\.testkit\.|\/testkit\/|\/__tests__\/|\/test\/)/;
31+
const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".mts", ".js", "/index.ts", "/index.tsx"];
32+
const PACKAGE_MANIFEST = /^(apps|packages)\/[^/]+\/package\.json$/;
33+
const IMPORT_SPECIFIER = /\b(?:from|import|require)\s*\(?\s*["']([^"'\n]+)["']/g;
34+
35+
const isTest = (file) => TEST_SOURCE.test(file);
36+
const isShippedSource = (file) => SHIPPED_SOURCE.test(file) && !isTest(file);
37+
38+
/**
39+
* Files the schema roots import, transitively: relative and package subpath imports. Package
40+
* roots (`@t3tools/contracts`) re-export the whole package, so they are not followed.
41+
* @param {{ files: string[]; has(file: string): boolean; read(file: string): string }} tree
42+
* @returns {Set<string>}
43+
*/
44+
function schemaClosure(tree) {
45+
const packages = packageDirectories(tree);
46+
const queue = tree.files.filter(
47+
(file) => SCHEMA_ROOTS.some((pattern) => pattern.test(file)) && !isTest(file),
48+
);
49+
const closure = new Set(queue);
50+
while (queue.length > 0) {
51+
const file = queue.pop();
52+
const text = tree.read(file);
53+
for (const match of text.matchAll(IMPORT_SPECIFIER)) {
54+
const target = resolveImport(tree, packages, file, match[1]);
55+
if (target && !isTest(target) && !closure.has(target)) {
56+
closure.add(target);
57+
queue.push(target);
58+
}
59+
}
60+
}
61+
return closure;
62+
}
63+
64+
function packageDirectories(tree) {
65+
const packages = new Map();
66+
for (const file of tree.files) {
67+
if (!PACKAGE_MANIFEST.test(file)) continue;
68+
try {
69+
const manifest = JSON.parse(tree.read(file));
70+
if (typeof manifest.name === "string") {
71+
packages.set(manifest.name, { dir: path.posix.dirname(file), exports: manifest.exports });
72+
}
73+
} catch {
74+
// A broken package.json fails the build elsewhere; it defines no schema imports here.
75+
}
76+
}
77+
return packages;
78+
}
79+
80+
function resolveImport(tree, packages, from, specifier) {
81+
if (specifier.startsWith(".")) {
82+
return resolveFile(tree, path.posix.join(path.posix.dirname(from), specifier));
83+
}
84+
const scoped = specifier.match(/^(@[^/]+\/[^/]+)\/(.+)$/);
85+
const pkg = scoped && packages.get(scoped[1]);
86+
if (!pkg || !pkg.exports || typeof pkg.exports !== "object") return undefined;
87+
const entry = pkg.exports[`./${scoped[2]}`];
88+
const target =
89+
typeof entry === "string" ? entry : entry && (entry.import ?? entry.default ?? entry.types);
90+
return typeof target === "string"
91+
? resolveFile(tree, path.posix.join(pkg.dir, target))
92+
: undefined;
93+
}
94+
95+
function resolveFile(tree, file) {
96+
const candidates = SOURCE_EXTENSIONS.map((extension) => file + extension);
97+
if (file.endsWith(".js")) candidates.unshift(file.replace(/\.js$/, ".ts"));
98+
return candidates.find((candidate) => tree.has(candidate));
99+
}
20100

21101
/**
22-
* @param {ReadonlyArray<{ path: string; addedLines: ReadonlyArray<string> }>} files
102+
* @param {ReadonlyArray<{ path: string; before?: string; after?: string }>} changes
103+
* one entry per changed path; `before`/`after` are the file texts (undefined when absent)
104+
* @param {{ closure: ReadonlySet<string>; baseHasCheck: boolean }} context
23105
* @returns {string[]} one reason per refused file
24106
*/
25-
function findSchemaChanges(files) {
107+
function findSchemaChanges(changes, { closure, baseHasCheck }) {
26108
const reasons = [];
27-
for (const file of files) {
28-
if (SCHEMA_PATHS.some((pattern) => pattern.test(file.path))) {
29-
reasons.push(`${file.path}: changes the database schema or its migrations`);
109+
for (const change of changes) {
110+
const file = change.path;
111+
if (CHECK_FILES.includes(file)) {
112+
if (baseHasCheck) {
113+
reasons.push(
114+
`${file}: changes the schema check itself. Only the owner may do that: the check stays ` +
115+
"red, and the owner merges past it.",
116+
);
117+
}
30118
continue;
31119
}
32-
if (!SHIPPED_SOURCE.test(file.path) || TEST_SOURCE.test(file.path)) continue;
33-
const statement = file.addedLines.find((line) => SCHEMA_STATEMENT.test(line));
120+
if (GITATTRIBUTES.test(file)) {
121+
reasons.push(
122+
`${file}: changes how git treats files, which can hide changes from review. Only the ` +
123+
"owner may do that.",
124+
);
125+
continue;
126+
}
127+
if (SCHEMA_ROOTS.some((pattern) => pattern.test(file)) || closure.has(file)) {
128+
reasons.push(`${file}: changes the database schema, its migrations or code they import`);
129+
continue;
130+
}
131+
if (!isShippedSource(file)) continue;
132+
const statement = [change.after, change.before]
133+
.map((text) => text?.match(SCHEMA_STATEMENT)?.[0])
134+
.find(Boolean);
34135
if (statement) {
35-
reasons.push(`${file.path}: adds a schema statement: ${statement.trim().slice(0, 120)}`);
136+
reasons.push(
137+
`${file}: holds a schema statement (${statement.replace(/\s+/g, " ")}), so any change ` +
138+
"to it may change the schema",
139+
);
36140
}
37141
}
38142
return reasons;
39143
}
40144

41-
/** Parses `git diff --unified=0` output into changed files and their added lines. */
42-
function parseDiff(diff) {
43-
const files = [];
44-
let oldPath = "";
45-
let current;
46-
for (const line of diff.split("\n")) {
47-
if (line.startsWith("diff --git ")) {
48-
current = undefined;
49-
oldPath = "";
50-
} else if (current === undefined && line.startsWith("--- ")) {
51-
oldPath = line.slice(4).replace(/^a\//, "");
52-
} else if (current === undefined && line.startsWith("+++ ")) {
53-
// A deleted file's new side is /dev/null; it keeps its old path, so deleting is refused too.
54-
const target = line.slice(4);
55-
current = {
56-
path: target === "/dev/null" ? oldPath : target.replace(/^b\//, ""),
57-
addedLines: [],
58-
};
59-
files.push(current);
60-
} else if (current && line.startsWith("+")) {
61-
current.addedLines.push(line.slice(1));
145+
/**
146+
* Parses `git diff --raw -z --no-abbrev` output into { path, oldBlob, newBlob } entries. A side
147+
* that is absent or a submodule (no blob) is undefined; the path is still listed.
148+
*/
149+
function parseRawDiff(raw) {
150+
const fields = raw.split("\0");
151+
const entries = [];
152+
for (let index = 0; index + 1 < fields.length; index += 2) {
153+
const header = fields[index];
154+
if (!header.startsWith(":")) break;
155+
const [oldMode, newMode, oldSha, newSha, status] = header.slice(1).split(" ");
156+
if (/^[RC]/.test(status)) throw new Error(`unexpected rename in diff: ${header}`);
157+
const blob = (mode, sha) => (/^10|^12/.test(mode) && !/^0+$/.test(sha) ? sha : undefined);
158+
entries.push({
159+
path: fields[index + 1],
160+
oldBlob: blob(oldMode, oldSha),
161+
newBlob: blob(newMode, newSha),
162+
});
163+
}
164+
return entries;
165+
}
166+
167+
function git(args) {
168+
return execFileSync("git", ["-c", "core.quotePath=false", ...args], {
169+
encoding: "utf8",
170+
maxBuffer: 512 * 1024 * 1024,
171+
});
172+
}
173+
174+
/** Source files and package manifests of a commit; texts are read only when the walk asks. */
175+
function readTree(rev) {
176+
const blobs = new Map();
177+
for (const record of git(["ls-tree", "-r", "-z", "--full-tree", rev]).split("\0")) {
178+
const match = record.match(/^\d+ blob ([0-9a-f]+)\t(.+)$/s);
179+
if (match && (SHIPPED_SOURCE.test(match[2]) || PACKAGE_MANIFEST.test(match[2]))) {
180+
blobs.set(match[2], match[1]);
62181
}
63182
}
64-
return files;
183+
return {
184+
files: [...blobs.keys()],
185+
has: (file) => blobs.has(file),
186+
read: (file) => (blobs.has(file) ? readBlob(blobs.get(file)) : ""),
187+
};
188+
}
189+
190+
const blobCache = new Map();
191+
function readBlob(sha) {
192+
if (!blobCache.has(sha)) blobCache.set(sha, git(["cat-file", "blob", sha]));
193+
return blobCache.get(sha);
194+
}
195+
196+
function treeHas(rev, file) {
197+
return git(["ls-tree", "--full-tree", "--name-only", rev, "--", file]).trim() === file;
65198
}
66199

67200
function main(argv) {
@@ -70,22 +203,35 @@ function main(argv) {
70203
console.error("Usage: fork-schema-check.cjs <base> <head>");
71204
return 2;
72205
}
73-
const diff = execFileSync(
74-
"git",
75-
["diff", "--unified=0", "--no-renames", "--no-color", `${base}...${head}`],
76-
{ encoding: "utf8", maxBuffer: 256 * 1024 * 1024 },
206+
const mergeBase = git(["merge-base", base, head]).trim();
207+
const entries = parseRawDiff(
208+
git(["diff", "--raw", "-z", "--no-abbrev", "--no-renames", "--no-ext-diff", mergeBase, head]),
77209
);
78-
const reasons = findSchemaChanges(parseDiff(diff));
210+
// Only shipped source is searched for statements, so only its texts are read.
211+
const text = (entry, sha) => (sha && isShippedSource(entry.path) ? readBlob(sha) : undefined);
212+
const changes = entries.map((entry) => ({
213+
path: entry.path,
214+
before: text(entry, entry.oldBlob),
215+
after: text(entry, entry.newBlob),
216+
}));
217+
const closure = new Set([
218+
...schemaClosure(readTree(mergeBase)),
219+
...schemaClosure(readTree(head)),
220+
]);
221+
const reasons = findSchemaChanges(changes, {
222+
closure,
223+
baseHasCheck: treeHas(base, CHECK_SCRIPT),
224+
});
79225
if (reasons.length === 0) {
80-
console.log("No database schema changes.");
226+
console.log(`No database schema changes (${changes.length} changed files checked).`);
81227
return 0;
82228
}
83229
console.error("Fork changes must not change the database schema (no down-migrations exist):");
84230
for (const reason of reasons) console.error(`- ${reason}`);
85231
return 1;
86232
}
87233

88-
module.exports = { findSchemaChanges, parseDiff };
234+
module.exports = { findSchemaChanges, parseRawDiff, schemaClosure, CHECK_FILES };
89235

90236
if (require.main === module) {
91237
process.exitCode = main(process.argv.slice(2));

0 commit comments

Comments
 (0)