Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/cli/src/result-export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileReferenceIdentity } from "@hypit/result/node";
import { randomUUID } from "node:crypto";
import { lstat, mkdir, rename, rm, writeFile } from "node:fs/promises";
import { basename, dirname, join, relative, resolve, sep } from "node:path";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";

import type {
BuildResultFileRef,
Expand Down Expand Up @@ -35,7 +35,9 @@ async function pathExists(path: string): Promise<boolean> {
function containedPath(root: string, path: string): string {
const target = resolve(root, path);
const relation = relative(root, target);
assert(relation.length > 0 && relation !== ".." && !relation.startsWith(`..${sep}`),
// A cross-drive or UNC target makes relative() return an absolute path on Windows,
// which cannot start with ".." — reject it the same way as an escaping one.
assert(relation.length > 0 && relation !== ".." && !relation.startsWith(`..${sep}`) && !isAbsolute(relation),
`Result file ${path} leaves export directory ${root}`);
return target;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/src/build-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export function assertOrderedBuildId(value: string): void {
export function assertBuildId(value: string): void {
assert(value.length > 0 && value === value.trim(), "Build id must be a non-empty unpadded name");
assert(value !== "." && value !== "..", "Build id must not be a dot directory");
assert(!value.includes("/") && !value.includes("\\") && !value.includes("\0"),
assert(!value.includes("/") && !value.includes("\\") && !value.includes("\0") && !value.includes(":"),
"Build id must be one path-free name");
}
2 changes: 1 addition & 1 deletion packages/protocol/test/canonical.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("Resource ids retain their narrow execution-reference syntax", () => {

test("Build ids cannot address a parent or nested path", () => {
assert.doesNotThrow(() => assertBuildId("build-19"));
for (const value of ["..", ".", "episode/19", "episode\\19", " padded "]) {
for (const value of ["..", ".", "episode/19", "episode\\19", " padded ", "c:", "d:build"]) {
assert.throws(() => assertBuildId(value), /Build id/u);
}
});
Expand Down
6 changes: 4 additions & 2 deletions packages/result/src/node/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ export function buildResultDirectory(root: string, build: string): string {
const bucket = buildResultDateBucket(build)!;
const target = resolve(directory, bucket, build);
const relation = relative(directory, target);
assert(relation.length > 0 && relation !== ".." && !relation.startsWith(`..${sep}`),
// Cross-drive and UNC targets make relative() return an absolute path, which
// cannot start with ".." — reject it the same way as an escaping one.
assert(relation.length > 0 && relation !== ".." && !relation.startsWith(`..${sep}`) && !isAbsolute(relation),
`Build ${build} leaves Build Result root ${directory}`);
return target;
}
Expand All @@ -228,7 +230,7 @@ function containedResultPath(directory: string, path: string): string {
const root = resolve(directory);
const absolute = resolve(root, path);
const relation = relative(root, absolute);
assert(relation !== ".." && !relation.startsWith(`..${sep}`),
assert(relation !== ".." && !relation.startsWith(`..${sep}`) && !isAbsolute(relation),
`Build Result path ${path} leaves ${root}`);
return absolute;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-local/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileExecutionLogs } from "./log.js";
import { constants } from "node:fs";
import { access, readFile, rm, stat } from "node:fs/promises";
import { dirname, relative, resolve, sep } from "node:path";
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";

import {
loadNodePackageSelection,
Expand Down Expand Up @@ -883,7 +883,7 @@ function buildWorkPath(root: string, build: string): string {
const work = resolve(root, "work");
const target = resolve(work, build);
const relation = relative(work, target);
if (relation.length === 0 || relation === ".." || relation.startsWith(`..${sep}`)) {
if (relation.length === 0 || relation === ".." || relation.startsWith(`..${sep}`) || isAbsolute(relation)) {
throw new Error(`Build ${build} leaves Runtime work root ${work}`);
}
return target;
Expand Down