Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure case sensitive paths #1369

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {MarkdownPage} from "./markdown.js";
import {parseMarkdown} from "./markdown.js";
import {extractNodeSpecifier} from "./node.js";
import {extractNpmSpecifier, populateNpmCache, resolveNpmImport} from "./npm.js";
import {isAssetPath, isPathImport, relativePath, resolvePath} from "./path.js";
import {assertPathname, isAssetPath, isPathImport, relativePath, resolvePath} from "./path.js";
import {renderPage} from "./render.js";
import type {Resolvers} from "./resolvers.js";
import {getModuleResolver, getResolvers} from "./resolvers.js";
Expand Down Expand Up @@ -353,5 +353,6 @@ export class FileBuildEffects implements BuildEffects {
this.logger.log(destination);
await prepareOutput(destination);
await writeFile(destination, contents);
await assertPathname(destination, this.outputRoot, outputPath);
}
}
10 changes: 10 additions & 0 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {realpath} from "node:fs/promises";
import {dirname, join} from "node:path/posix";

/**
Expand Down Expand Up @@ -84,3 +85,12 @@ export function parseRelativeUrl(url: string): {pathname: string; search: string
else (search = url.slice(j)), (url = url.slice(0, j));
return {pathname: url, search, hash};
}

/**
* Checks that the actual filename under root/path has the same case as the
* target.
*/
export async function assertPathname(target: string, root: string, path: string): Promise<undefined> {
const cased = (await realpath(target)).slice((await realpath(root)).length + 1).replaceAll("\\", "/");
if (cased !== path.replace(/^\//, "")) throw new Error(`Incorrect case for ${target}: found ${cased} instead.`);
}
3 changes: 2 additions & 1 deletion src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {transpileJavaScript, transpileModule} from "./javascript/transpile.js";
import {parseMarkdown} from "./markdown.js";
import type {MarkdownCode, MarkdownPage} from "./markdown.js";
import {populateNpmCache} from "./npm.js";
import {isPathImport, resolvePath} from "./path.js";
import {assertPathname, isPathImport, resolvePath} from "./path.js";
import {renderPage} from "./render.js";
import type {Resolvers} from "./resolvers.js";
import {getResolvers} from "./resolvers.js";
Expand Down Expand Up @@ -155,6 +155,7 @@ export class PreviewServer {
const filepath = join(root, path);
try {
await access(filepath, constants.R_OK);
await assertPathname(filepath, root, path);
send(req, pathname.slice("/_file".length), {root}).pipe(res);
return;
} catch (error) {
Expand Down
18 changes: 17 additions & 1 deletion test/path-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import assert from "node:assert";
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "../src/path.js";
import {
assertPathname,
isPathImport,
parseRelativeUrl,
relativePath,
resolveLocalPath,
resolvePath
} from "../src/path.js";

describe("resolvePath(source, target)", () => {
it("returns the path to the specified target within the source root", () => {
Expand Down Expand Up @@ -148,3 +155,12 @@ describe("parseRelativeUrl(url)", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"});
});
});

describe("assertPathname(target, root, path)", () => {
it("throws an error on files with the wrong case", async () => {
await assert.rejects(assertPathname("./docs/HORSE.jpg", "./docs", "/HORSE.jpg"));
});
it("throws an error on paths with the wrong case", async () => {
await assert.rejects(assertPathname("./docs/DATA/alphabet.csv", "./docs", "/DATA/alphabet.csv"));
});
});