Skip to content
Merged
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
43 changes: 42 additions & 1 deletion scripts/sync-hive-docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, it, expect } from "vitest";
import { rewriteLinkTarget, rewriteLinks } from "./sync-hive-docs";
import {
resolveSyncTarget,
rewriteLinkTarget,
rewriteLinks,
} from "./sync-hive-docs";

// The source file being synced, expressed as its real repo path. Links inside
// the content are resolved relative to this file's directory.
Expand Down Expand Up @@ -203,3 +207,40 @@ describe("sync-hive-docs.ts — sync-root prefix drift guard", () => {
expect(SOURCE).not.toContain("v2/docs");
});
});

describe("resolveSyncTarget", () => {
const root = "/site/docs/content/hive";

it("joins a plain target under the root", () => {
expect(resolveSyncTarget(root, "architecture.md")).toBe(
"/site/docs/content/hive/architecture.md"
);
});

it("joins a nested target under the root", () => {
expect(resolveSyncTarget(root, "adr/0001.md")).toBe(
"/site/docs/content/hive/adr/0001.md"
);
});

it.each(["../escape.md", "adr/../../escape.md"])(
"refuses %s, which escapes the root",
target => {
expect(() => resolveSyncTarget(root, target)).toThrow(
`refusing to write outside Hive docs root: ${target}`
);
}
);

it("refuses a sibling directory that shares the root prefix", () => {
expect(() => resolveSyncTarget(root, "../hive-evil/x.md")).toThrow(
"refusing to write outside Hive docs root: ../hive-evil/x.md"
);
});

it.each(["", "."])("refuses the root itself (%j)", target => {
expect(() => resolveSyncTarget(root, target)).toThrow(
"refusing to write outside Hive docs root"
);
});
});
14 changes: 10 additions & 4 deletions scripts/sync-hive-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,20 @@ async function fetchText(url: string): Promise<string> {
return response.text();
}

/** Resolve a sync target under docsRoot, refusing anything that escapes it. */
export function resolveSyncTarget(root: string, target: string): string {
const targetPath = path.join(root, target);
if (!targetPath.startsWith(root + path.sep)) {
throw new Error(`refusing to write outside Hive docs root: ${target}`);
}
return targetPath;
}

async function main() {
for (const file of files) {
const target = file.target || file.source;
const sourceURL = `${rawBase}/${file.source}`;
const targetPath = path.join(docsRoot, target);
if (!targetPath.startsWith(docsRoot + path.sep)) {
throw new Error(`refusing to write outside Hive docs root: ${target}`);
}
const targetPath = resolveSyncTarget(docsRoot, target);
const content = await fetchText(sourceURL);
// Rewrite GitHub-relative links so they resolve on the docs site.
const overlaid = applyLocalHiveOverlays(content, file.source);
Expand Down
Loading