fix: consolidate boundary ownership and cleanup failure paths (#477)

This commit is contained in:
morluto
2026-09-08 10:59:13 +08:00
committed by GitHub
parent c490d85045
commit 2160d4fae5
138 changed files with 2220 additions and 1781 deletions
+4 -2
View File
@@ -1,6 +1,6 @@
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
import { err, ok } from "../domain/result.js";
import { traverseDirectory } from "./ReferenceSourceReaderEntries.js";
import { compareNames } from "./ReferenceSourceReaderPaths.js";
import {
type ReferenceSourceLimits,
type ReferenceSourceReaderOptions,
@@ -62,7 +62,9 @@ export const readReferenceSource = async (
};
const traversal = await traverse(state);
if (!traversal.ok) return traversal;
state.entries.sort((left, right) => compareNames(left.path, right.path));
state.entries.sort((left, right) =>
compareUnicodeCodePoints(left.path, right.path),
);
return ok({
root: canonicalRoot,
entries: state.entries,
@@ -1,12 +1,10 @@
import { readdir, readlink, realpath } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
import { entryFailure, safeSize } from "./ReferenceSourceReaderErrors.js";
import {
compareNames,
pathFromRoot,
withinRoot,
} from "./ReferenceSourceReaderPaths.js";
import { isPathWithinRoot } from "../domain/localPath.js";
import { pathFromRoot } from "./ReferenceSourceReaderPaths.js";
import { readStableFile } from "./ReferenceSourceReaderFile.js";
import {
isAborted,
@@ -97,7 +95,10 @@ const readDirectoryNames = async (
| { readonly ok: false; readonly message: string }
> => {
try {
return { ok: true, value: (await readdir(path)).sort(compareNames) };
return {
ok: true,
value: (await readdir(path)).sort(compareUnicodeCodePoints),
};
} catch {
return { ok: false, message: "Directory could not be read" };
}
@@ -236,7 +237,7 @@ const describeSymlink = async (
try {
const rawTarget = await readlink(absolute);
const lexicalTarget = resolve(dirname(absolute), rawTarget);
if (!withinRoot(root, lexicalTarget))
if (!isPathWithinRoot(root, lexicalTarget))
return {
status: "read",
kind: "symlink",
@@ -246,7 +247,7 @@ const describeSymlink = async (
};
try {
const canonicalTarget = await realpath(lexicalTarget);
return withinRoot(root, canonicalTarget)
return isPathWithinRoot(root, canonicalTarget)
? {
status: "read",
kind: "symlink",
+19 -29
View File
@@ -19,9 +19,7 @@ const READ_CHUNK_BYTES = 64 * 1024;
type PreparedFileRead =
| {
readonly status: "ready";
readonly handle: FileHandle;
readonly before: BigStats;
readonly parentBefore: { readonly ok: true; readonly stats: BigStats };
}
| { readonly status: "failed"; readonly entry: ReferenceSourceEntry };
@@ -43,30 +41,10 @@ type FinalizeFileReadRequest = {
const prepareFileRead = async (
request: StableFileRequest,
handle: FileHandle,
): Promise<PreparedFileRead> => {
const { root, rootIdentity, absolute, path, expected, signal } = request;
const parentBefore = await validateDirectory(
root,
rootIdentity,
dirname(absolute),
);
if (!parentBefore.ok)
return {
status: "failed",
entry: entryFailure(
path,
"file",
parentBefore.code,
parentBefore.message,
safeSize(expected.size),
),
};
const handle = await open(
absolute,
constants.O_RDONLY | constants.O_NOFOLLOW,
);
const { path, expected, signal } = request;
if (isAborted(signal)) {
await handle.close().catch(() => undefined);
return {
status: "failed",
entry: entryFailure(
@@ -80,7 +58,6 @@ const prepareFileRead = async (
}
const before = await handle.stat({ bigint: true });
if (!sameFile(expected, before)) {
await handle.close().catch(() => undefined);
return {
status: "failed",
entry: entryFailure(
@@ -92,7 +69,7 @@ const prepareFileRead = async (
),
};
}
return { status: "ready", handle, before, parentBefore };
return { status: "ready", before };
};
const readFileContents = async (request: {
@@ -211,9 +188,22 @@ export const readStableFile = async (
request;
let handle: FileHandle | undefined;
try {
const prepared = await prepareFileRead(request);
const parentBefore = await validateDirectory(
root,
rootIdentity,
dirname(absolute),
);
if (!parentBefore.ok)
return entryFailure(
path,
"file",
parentBefore.code,
parentBefore.message,
safeSize(expected.size),
);
handle = await open(absolute, constants.O_RDONLY | constants.O_NOFOLLOW);
const prepared = await prepareFileRead(request, handle);
if (prepared.status === "failed") return prepared.entry;
handle = prepared.handle;
const contents = await readFileContents({
handle,
path,
@@ -228,7 +218,7 @@ export const readStableFile = async (
path,
handle,
before: prepared.before,
parentBefore: prepared.parentBefore,
parentBefore,
chunks: contents.chunks,
total: contents.total,
});
+3 -20
View File
@@ -1,27 +1,10 @@
import { isAbsolute, relative, sep } from "node:path";
import { relative, sep } from "node:path";
export const withinRoot = (root: string, path: string): boolean => {
const value = relative(root, path);
return (
value === "" ||
(value !== ".." && !value.startsWith(`..${sep}`) && !isAbsolute(value))
);
};
import { isPathWithinRoot } from "../domain/localPath.js";
export const pathFromRoot = (root: string, path: string): string => {
const value = relative(root, path);
return withinRoot(root, path)
return isPathWithinRoot(root, path)
? value.split(sep).join("/") || "."
: "<outside-root>";
};
export const compareNames = (left: string, right: string): number => {
const leftPoints = [...left].map((value) => value.codePointAt(0) ?? 0);
const rightPoints = [...right].map((value) => value.codePointAt(0) ?? 0);
const length = Math.min(leftPoints.length, rightPoints.length);
for (let index = 0; index < length; index += 1) {
const difference = (leftPoints[index] ?? 0) - (rightPoints[index] ?? 0);
if (difference !== 0) return difference;
}
return leftPoints.length - rightPoints.length;
};
@@ -3,7 +3,7 @@ import { lstat, realpath } from "node:fs/promises";
import { err, ok } from "../domain/result.js";
import { cancelled, failure } from "./ReferenceSourceReaderErrors.js";
import { withinRoot } from "./ReferenceSourceReaderPaths.js";
import { isPathWithinRoot } from "../domain/localPath.js";
import {
type BigStats,
type ReferenceSourceFailureCode,
@@ -62,7 +62,7 @@ export const validateDirectory = async (
message: "Directory identity changed",
};
const canonical = await realpath(path);
if (canonical !== path || !withinRoot(root, canonical))
if (canonical !== path || !isPathWithinRoot(root, canonical))
return {
ok: false,
code: "changed",