Skip to content

Commit 0abc12a

Browse files
committed
fix(flows): the writer stages under a short name beside the destination and keeps the file's mode; the guards compare every name the write lands on
Findings on the shared writer and the two guards over it: - The staging name was the destination's leaf plus a suffix, so a leaf at NAME_MAX made the write fail; it is now `.gsac-<pid>-<hex>.tmp` in the destination's directory, spelled as the caller spelled it (a `link/..` segment stays the OS's to resolve, the same way for both names, so the rename never crosses a filesystem). - Replacing through a new file dropped an existing 0600 file to the umask; the staging file takes the existing regular destination's permission bits before the rename (ownership cannot be kept without privileges and is not). - The merge guard compared the destination's referent alone, refusing `out.yml -> layer.yml` though the rename replaces the link and leaves the layer intact; the snapshot claim reserved a destination link's referent too, refusing `old/r` after `alice/r` whose leaf linked to old/r.yml. Both now use landingNames(): the rename target (the directory as the filesystem names it plus the leaf as spelled) and, unless the leaf is a link, the referent as the filesystem names it now, which on a case-insensitive filesystem is the existing file's own spelling. The snapshot claims the rename target and looks up every landing name, in either target order.
1 parent 5bf2a80 commit 0abc12a

5 files changed

Lines changed: 261 additions & 54 deletions

File tree

‎src/flows/merge.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Problem, ProblemOf } from "../problem.js";
1010
import type { FinishedMerge } from "./deliver.js";
1111
import { foldLayers, readLayerFiles } from "./layers.js";
1212
import { renderMergedYaml } from "./library.js";
13-
import { canonicalPath, writeReplacing } from "./settings-write.js";
13+
import { landingNames, writeReplacing } from "./settings-write.js";
1414

1515
export interface MergeConfig {
1616
settingsFiles: string[];
@@ -21,13 +21,17 @@ export interface MergeConfig {
2121
const MERGED_LABEL = "the merged settings document";
2222

2323
/**
24-
* Paths are compared as the filesystem names them, so "./a.yml", "a.yml", and a spelling through a symlinked directory
25-
* (macOS's /tmp for /private/tmp) all collide. Guarded beside the write: the next run would fold the merged document
26-
* as if it were a layer.
24+
* The destination's landing names against each layer's (the rename target, and the referent unless the leaf is a
25+
* link), so "./a.yml", "a.yml", a spelling through a symlinked directory (macOS's /tmp for /private/tmp), a case
26+
* alias of an existing layer, and a layer that IS the link at the destination collide, while `out.yml -> layer.yml`
27+
* with `layer.yml` as the layer does not: the write replaces the link and leaves the layer intact. Guarded beside the
28+
* write: the next run would fold the merged document as if it were a layer.
2729
*/
2830
function mergedFileCollision(cfg: MergeConfig): Result<void, ProblemOf<"merged-file-is-layer">> {
29-
const landing = canonicalPath(cfg.mergedFile);
30-
const index = cfg.settingsFiles.findIndex((layer) => canonicalPath(layer) === landing);
31+
const landings = new Set(landingNames(cfg.mergedFile));
32+
const index = cfg.settingsFiles.findIndex((layer) =>
33+
landingNames(layer).some((name) => landings.has(name)),
34+
);
3135
const layer = cfg.settingsFiles[index];
3236
return layer === undefined
3337
? ok()

‎src/flows/settings-write.ts‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
import { randomBytes } from "node:crypto";
1111
import {
1212
closeSync,
13+
fchmodSync,
14+
lstatSync,
1315
mkdirSync,
1416
openSync,
1517
realpathSync,
1618
renameSync,
1719
rmSync,
20+
statSync,
1821
writeFileSync,
1922
} from "node:fs";
20-
import { dirname, join, parse, resolve, sep } from "node:path";
23+
import { basename, dirname, join, parse, resolve, sep } from "node:path";
2124
import { err, ok, type Result } from "neverthrow";
2225

2326
/**
@@ -53,17 +56,66 @@ function realOrSpelled(path: string): string {
5356
}
5457
}
5558

56-
/** The error is the filesystem's own reason; each caller names the input that chose the path. */
59+
/** The bits a replaced regular file keeps; a link at `path` is stat-followed, since the write replaces the link with a regular file of its referent's mode. */
60+
function regularFileMode(path: string): number | undefined {
61+
try {
62+
const stat = statSync(path);
63+
return stat.isFile() ? stat.mode & 0o7777 : undefined;
64+
} catch {
65+
return undefined;
66+
}
67+
}
68+
69+
/**
70+
* The name the write reaches: the directory as the filesystem names it plus the leaf as spelled, since a rename
71+
* replaces a link at the leaf rather than following it.
72+
*/
73+
export function renameTarget(path: string): string {
74+
return join(canonicalPath(dirname(path)), basename(path));
75+
}
76+
77+
/**
78+
* Every name a write to `path` lands on, for a flow comparing its destination against its inputs: the rename target,
79+
* and, when the leaf is not a link (a link is replaced, its referent untouched), the referent as the filesystem names
80+
* it now, which on a case-insensitive filesystem is the existing file's own spelling.
81+
*/
82+
export function landingNames(path: string): string[] {
83+
const names = [renameTarget(path)];
84+
if (!isSymlink(path)) {
85+
names.push(canonicalPath(path));
86+
}
87+
return [...new Set(names)];
88+
}
89+
90+
function isSymlink(path: string): boolean {
91+
try {
92+
return lstatSync(path).isSymbolicLink();
93+
} catch {
94+
return false;
95+
}
96+
}
97+
98+
/**
99+
* The error is the filesystem's own reason; each caller names the input that chose the path. The staging file sits in
100+
* the destination's directory, spelled as the caller spelled it (a `link/..` segment is the OS's to resolve, the same
101+
* way for both names), under a short name of its own (the destination's leaf may already be at NAME_MAX), and takes
102+
* an existing regular destination's mode, so a replaced 0600 file stays 0600.
103+
*/
57104
export function writeReplacing(path: string, text: string): Result<void, string> {
58-
const staging = `${path}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
105+
const directory = path.slice(0, path.length - basename(path).length);
106+
const staging = `${directory}.gsac-${process.pid}-${randomBytes(4).toString("hex")}.tmp`;
59107
// Set once the exclusive open succeeded: only a staging file THIS write made is removed on failure, never one
60108
// another writer got there first with (`wx` fails on it, and that failure is the one reported).
61109
let created = false;
62110
let fd: number | undefined;
63111
try {
64112
mkdirSync(dirname(path), { recursive: true });
113+
const mode = regularFileMode(path);
65114
fd = openSync(staging, "wx");
66115
created = true;
116+
if (mode !== undefined) {
117+
fchmodSync(fd, mode);
118+
}
67119
writeFileSync(fd, text);
68120
closeSync(fd);
69121
fd = undefined;

‎src/flows/snapshot.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* lands on disk while nothing about it is printed.
1010
*/
1111

12-
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
12+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
1313
import { err, ok, type Result, ResultAsync } from "neverthrow";
1414
import type { RepoRef } from "../discovery/targets.js";
1515
import type { SectionSelection } from "../engine/section-selection.js";
@@ -33,7 +33,7 @@ import {
3333
type TargetOutcome,
3434
toPublicView,
3535
} from "./redact.js";
36-
import { canonicalPath, writeReplacing } from "./settings-write.js";
36+
import { canonicalPath, landingNames, renameTarget, writeReplacing } from "./settings-write.js";
3737
import { openSingleRepoChannel } from "./single.js";
3838
import { writeSnapshotDirSummary, writeSummary } from "./summary.js";
3939

@@ -197,15 +197,10 @@ async function snapshotTarget(ctx: {
197197
* authored ground in a way the two inputs cannot show: a link under either
198198
* directory that leads into the other, or an owner spelled ".github". Two
199199
* targets can land on ONE file the same way (a link `out/bob -> out/alice`
200-
* with targets alice/r and bob/r), so every landing is claimed in `claimed`
201-
* for the run and a second claim is refused. Two names are claimed per file:
202-
* the referent as the filesystem names it now, and the file the RENAME
203-
* reaches (the directory as the filesystem names it plus the leaf as spelled,
204-
* since the rename replaces a link at the leaf rather than following it). The
205-
* first catches a leaf spelled in another case on a case-insensitive
206-
* filesystem once the file exists; the second, a leaf that was a link. The
207-
* refusal names the earlier target through `display`, so a redacted one stays
208-
* sealed, and never the landing, whose spelling is the operator's.
200+
* with targets alice/r and bob/r), so every file this run writes is claimed
201+
* in `claimed` and a second target reaching it is refused. The refusal names
202+
* the earlier target through `display`, so a redacted one stays sealed, and
203+
* never the landing, whose spelling is the operator's.
209204
*/
210205
function snapshotFilePath(
211206
cfg: Extract<SnapshotConfig, { form: "dir" }>,
@@ -231,8 +226,13 @@ function snapshotFilePath(
231226
error: `cannot write the snapshot to ${path}: the filesystem carries it to ${landing}, inside the "repos-dir" input "${cfg.reposDir}". Write the snapshots to a directory that leads to no central file`,
232227
};
233228
}
234-
const names = [landing, join(canonicalPath(dirname(path)), basename(path))];
235-
const earlier = names.map((name) => claimed.get(name)).find((slug) => slug !== undefined);
229+
// The claim is the file the rename reaches; a later target is refused when any of its landing names is claimed
230+
// (its own rename target for a leaf that was a link, its referent for a leaf spelled in another case on a
231+
// case-insensitive filesystem once the first file exists).
232+
const written = renameTarget(path);
233+
const earlier = landingNames(path)
234+
.map((name) => claimed.get(name))
235+
.find((slug) => slug !== undefined);
236236
if (earlier !== undefined) {
237237
return {
238238
error:
@@ -241,9 +241,7 @@ function snapshotFilePath(
241241
"each target has a file of its own",
242242
};
243243
}
244-
for (const name of names) {
245-
claimed.set(name, repo.slug);
246-
}
244+
claimed.set(written, repo.slug);
247245
return { path };
248246
}
249247

‎test/flows/merge.test.ts‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { describe, expect, test } from "bun:test";
2-
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
2+
import {
3+
chmodSync,
4+
existsSync,
5+
lstatSync,
6+
mkdirSync,
7+
readdirSync,
8+
readFileSync,
9+
statSync,
10+
symlinkSync,
11+
writeFileSync,
12+
} from "node:fs";
313
import { join } from "node:path";
414
import { err, ok } from "neverthrow";
515
import { collectingIo, concludeMerge, type MergeConfig, runMerge } from "../../src/index.js";
@@ -82,3 +92,106 @@ describe("runMerge", () => {
8292
expect(readdirSync(dir).sort()).toEqual(["fleet.yml", "merged.yml", "merged.yml.tmp"]);
8393
}));
8494
});
95+
96+
describe("runMerge writes through the shared writer", () => {
97+
const twoLayers = (dir: string): string[] => {
98+
writeFileSync(join(dir, "fleet.yml"), FLEET);
99+
writeFileSync(join(dir, "repo.yml"), REPO);
100+
return [join(dir, "fleet.yml"), join(dir, "repo.yml")];
101+
};
102+
const MERGED = "repository:\n has_wiki: false\n has_issues: true\n";
103+
const merge = (layers: string[], mergedFile: string) =>
104+
runMerge({ settingsFiles: layers, mergedFile, layering: "merge" }, collectingIo().io);
105+
106+
test("a merged-file that is a link to a layer is admitted: the rename replaces the link and the layer stands", () =>
107+
withTempDir("run-merge-", (dir) => {
108+
const layers = twoLayers(dir);
109+
const mergedFile = join(dir, "out.yml");
110+
symlinkSync("repo.yml", mergedFile);
111+
expect(merge(layers, mergedFile)).toEqual(ok({ layers, mergedFile }));
112+
expect(lstatSync(mergedFile).isSymbolicLink()).toBe(false);
113+
expect(readFileSync(mergedFile, "utf8")).toBe(MERGED);
114+
expect(readFileSync(join(dir, "repo.yml"), "utf8")).toBe(REPO);
115+
}));
116+
117+
test("a layer that IS the link at the destination is refused: the rename would replace the layer's own entry", () =>
118+
withTempDir("run-merge-", (dir) => {
119+
twoLayers(dir);
120+
const link = join(dir, "out.yml");
121+
symlinkSync("repo.yml", link);
122+
const layers = [join(dir, "fleet.yml"), link];
123+
expect(merge(layers, link)).toEqual(
124+
err({ code: "merged-file-is-layer" as const, mergedFile: link, index: 1, layer: link }),
125+
);
126+
expect(lstatSync(link).isSymbolicLink()).toBe(true);
127+
}));
128+
129+
test("an existing merged file keeps its permission bits through the replace", () =>
130+
withTempDir("run-merge-", (dir) => {
131+
const layers = twoLayers(dir);
132+
const mergedFile = join(dir, "merged.yml");
133+
writeFileSync(mergedFile, "stale\n");
134+
chmodSync(mergedFile, 0o600);
135+
// A permissive umask, so a writer that forgot the mode would land at 0644 and fail the pin.
136+
const previous = process.umask(0o022);
137+
try {
138+
expect(merge(layers, mergedFile)).toEqual(ok({ layers, mergedFile }));
139+
} finally {
140+
process.umask(previous);
141+
}
142+
expect(readFileSync(mergedFile, "utf8")).toBe(MERGED);
143+
expect(statSync(mergedFile).mode & 0o777).toBe(0o600);
144+
}));
145+
146+
test("a merged-file spelled through a link and .. stages beside the destination the OS resolves, not beside cwd", () =>
147+
withTempDir("run-merge-", (dir) => {
148+
// `link/../out.yml`: the OS follows the link and steps up, so the file lands beside `inner`; a staging name
149+
// built with join() would collapse `link/..` lexically and stage under `dir`. `dir` is made read-only for the
150+
// write, so a staging file placed there fails the run instead of quietly renaming across.
151+
mkdirSync(join(dir, "elsewhere", "inner"), { recursive: true });
152+
symlinkSync(join("elsewhere", "inner"), join(dir, "link"));
153+
const layers = twoLayers(dir);
154+
const mergedFile = `${dir}/link/../out.yml`;
155+
chmodSync(dir, 0o555);
156+
try {
157+
expect(merge(layers, mergedFile)).toEqual(ok({ layers, mergedFile }));
158+
} finally {
159+
chmodSync(dir, 0o755);
160+
}
161+
expect(readFileSync(join(dir, "elsewhere", "out.yml"), "utf8")).toBe(MERGED);
162+
expect(readdirSync(join(dir, "elsewhere")).sort()).toEqual(["inner", "out.yml"]);
163+
expect(readdirSync(dir).filter((name) => name.startsWith(".gsac-"))).toEqual([]);
164+
}));
165+
166+
test("a merged-file that spells an existing layer in another case is refused on a case-insensitive filesystem", () =>
167+
withTempDir("run-merge-", (dir) => {
168+
writeFileSync(join(dir, "Probe"), "");
169+
const caseInsensitive = existsSync(join(dir, "probe"));
170+
const layers = twoLayers(dir);
171+
const mergedFile = join(dir, "REPO.YML");
172+
const merged = merge(layers, mergedFile);
173+
if (caseInsensitive) {
174+
expect(merged).toEqual(
175+
err({
176+
code: "merged-file-is-layer" as const,
177+
mergedFile,
178+
index: 1,
179+
layer: join(dir, "repo.yml"),
180+
}),
181+
);
182+
expect(readFileSync(join(dir, "repo.yml"), "utf8")).toBe(REPO);
183+
} else {
184+
expect(merged).toEqual(ok({ layers, mergedFile }));
185+
expect(readFileSync(mergedFile, "utf8")).toBe(MERGED);
186+
}
187+
}));
188+
189+
test("a merged-file leaf at the filesystem's name limit is written: the staging name is short and its own", () =>
190+
withTempDir("run-merge-", (dir) => {
191+
const layers = twoLayers(dir);
192+
const mergedFile = join(dir, `${"m".repeat(251)}.yml`);
193+
expect(merge(layers, mergedFile)).toEqual(ok({ layers, mergedFile }));
194+
expect(readFileSync(mergedFile, "utf8")).toBe(MERGED);
195+
expect(readdirSync(dir).filter((name) => name.startsWith(".gsac-"))).toEqual([]);
196+
}));
197+
});

0 commit comments

Comments
 (0)