Files
loki/scripts/test/preview-unpack.ts
CatoandClaude Sonnet 5.5 0c5ee9199b feat(previews): a preview is the whole of its own address, and a finished site can be brought in (#1193)
* feat(previews): a preview is the whole of its own address, and a finished site can be brought in

"create website for Murphy's Bar" now ends at murphys-bar.orangecat.ch, behind
the access code, with the widget on it.

- attach-preview-host.sh + lib.sh preview_vhost: a Caddy vhost per preview.
  Wildcard DNS already points every label at the box, so there is no record
  to set. "/" opens the entry file, everything else moves under /p/<label>/,
  and X-Preview-Root tells Loki the preview is the root
- handle.ts: with that header the cookie is scoped to "/", the gate posts to
  "/", and the widget loads from Loki's own origin
- createPreview takes an address (default: the name) and attaches the host,
  falling back to the /p/ path with the reason when the server cannot
- Build a preview takes a .zip of a finished site (unpack.ts: no unsafe paths,
  no symlinks, no .git, byte-counted) and commits it as it arrived
- Docs: prospect-previews.md ("Its own address", "A site that already exists",
  "Create website for <name>") and the standing flow in CLAUDE.md
- Tests: previews, previews-route (own-address cases), preview-unpack,
  test-preview-host.sh (in test:ops)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XpakL9C3TESU4H45fXMFfc

* fix(previews): read the base domain from its one source

address.ts read SITES_BASE_DOMAIN itself; the env-example test caught that
the variable Loki actually reads is LOKI_SITES_BASE_DOMAIN, owned by
site-cd.ts sitesBaseDomain(). Use that.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XpakL9C3TESU4H45fXMFfc

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-14 07:56:33 -07:00

96 lines
3.3 KiB
TypeScript

// Bringing a finished site into a preview folder from a zip: what is kept,
// what is refused. Needs python3 (as does the box). Run: npx tsx scripts/test/preview-unpack.ts
import { execFileSync } from "child_process";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "fs";
import os from "os";
import path from "path";
import { unpackArchive } from "../../src/lib/previews/unpack";
let pass = 0;
let fail = 0;
const ok = (cond: boolean, name: string) => {
if (cond) pass++;
else fail++;
console.log(`${cond ? "✓" : "✗"} ${name}`);
};
const tmp = mkdtempSync(path.join(os.tmpdir(), "unpack-test-"));
/** A zip built by python from {name: bytes-or-text} (symlink: name -> "@link:target"). */
function zipOf(entries: Record<string, string>): Uint8Array {
const out = path.join(tmp, `${Math.random().toString(36).slice(2)}.zip`);
execFileSync("python3", [
"-I",
"-c",
`
import json, sys, zipfile
entries = json.loads(sys.argv[2])
with zipfile.ZipFile(sys.argv[1], 'w') as z:
for name, body in entries.items():
if body.startswith('@link:'):
info = zipfile.ZipInfo(name); info.create_system = 3; info.external_attr = (0o120777 << 16)
z.writestr(info, body[6:])
else:
z.writestr(name, body)
`,
out,
JSON.stringify(entries),
]);
return new Uint8Array(readFileSync(out));
}
const fresh = () => path.join(mkdtempSync(path.join(tmp, "dest-")));
async function main() {
let dir = fresh();
let res = await unpackArchive(zipOf({ "index.html": "<h1>hi</h1>", "img/a.jpg": "JPEG" }), dir);
ok(res.ok && res.files === 2, "a plain site unpacks");
ok(readFileSync(path.join(dir, "img/a.jpg"), "utf8") === "JPEG", "…with its folders");
dir = fresh();
res = await unpackArchive(zipOf({ "site/index.html": "x", "site/css/s.css": "y" }), dir);
ok(res.ok && existsSync(path.join(dir, "index.html")), "one wrapping folder is stripped");
dir = fresh();
res = await unpackArchive(
zipOf({
"index.html": "x",
".git/config": "secret",
"__MACOSX/._index.html": "junk",
".DS_Store": "junk",
}),
dir,
);
ok(
res.ok && !existsSync(path.join(dir, ".git")) && !existsSync(path.join(dir, ".DS_Store")),
".git and macOS litter are skipped",
);
dir = fresh();
res = await unpackArchive(zipOf({ "index.html": "x", "../escape.txt": "no" }), dir);
ok(!res.ok && /unsafe/.test(res.ok ? "" : res.error), "a path that climbs out is refused");
ok(!existsSync(path.join(dir, "..", "escape.txt")), "…and nothing was written outside");
dir = fresh();
res = await unpackArchive(zipOf({ "index.html": "x", "/abs.txt": "no" }), dir);
ok(!res.ok, "an absolute path is refused");
dir = fresh();
res = await unpackArchive(zipOf({ "index.html": "x", link: "@link:/etc/passwd" }), dir);
ok(res.ok && !existsSync(path.join(dir, "link")), "a symlink is dropped, not followed");
dir = fresh();
res = await unpackArchive(zipOf({ "about.html": "x" }), dir);
ok(
!res.ok && /index\.html/.test(res.ok ? "" : res.error),
"no index.html: refused, and says why",
);
res = await unpackArchive(new TextEncoder().encode("not a zip"), fresh());
ok(!res.ok, "something that is not a zip is refused");
rmSync(tmp, { recursive: true, force: true });
console.log(`\npreview-unpack: ${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
}
void main();