"use strict"; // adpanel:legacy-fs-refusals — source-level pins for the legacy panel fs // routes' refusal guards that no behavioral test could reach (index.js boots // servers on require, so — same technique as // tests/node-proxy-create-async-setup.test.js — these are source assertions). // // What is pinned and why it must not regress: // 1. POST /create (legacy) refuses the name "." — path.posix.join(relPath, // ".") resolves to the server root itself, so without the refusal the // folder branch would drop a stray .keep marker at the server root and // the file branch would target the root. The refusal must sit AFTER the // auth/perm checks (401/403 before 400) and BEFORE resolveRemoteFsContext // (no daemon context is built for an invalid name). // 2. POST /rename (legacy) refuses the no-op (src === dest) and the // server-root src/dest — renaming (or over) the server root is reserved // for the dedicated admin server flows, exactly like deleting it. Both // refusals must test the RESOLVED paths (after safeJoinUnix, not the raw // body strings) and sit before the destination probe and the relay dial. const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const ROOT = path.join(__dirname, ".."); const INDEX_JS = path.join(ROOT, "index.js"); // Strip full-line comments so prose mentioning "." or the refusal strings // cannot satisfy the assertions. function stripFullLineComments(src) { return src .split("\n") .filter((line) => !/^\s*\/\//.test(line)) .join("\n"); } const indexSrc = stripFullLineComments(fs.readFileSync(INDEX_JS, "utf8")); function routeBody(startMarker, endMarker) { const start = indexSrc.indexOf(startMarker); assert.notStrictEqual(start, -1, `expected the legacy route ${startMarker} to exist`); const end = indexSrc.indexOf(endMarker, start); assert.notStrictEqual(end, -1, `expected the ${endMarker} marker after ${startMarker}`); return indexSrc.slice(start, end); } test("legacy /create refuses the \".\" name before any daemon context is built", () => { const body = routeBody('app.post("/create"', 'app.post("/rename"'); // The refusal line itself: bare "." (resolves to the server root) is // refused alongside ".." and any slash-bearing name, answering 400. const refusalLine = body .split("\n") .find((line) => line.includes('name === "."')); assert.ok(refusalLine, "the legacy /create route must keep the \".\" refusal"); assert.ok( refusalLine.includes('".."') && refusalLine.includes("Invalid name") && refusalLine.includes("400"), `the \".\" refusal must stay one line with the ".." check, the 400 status and the "Invalid name" body — got: ${refusalLine}` ); // Order: the refusal guards the filesystem namespace, so it must run before // the daemon context resolution (no node dial for a name that can never be // stored) — and after the perm gate (401/403 before 400). const refusalAt = body.indexOf(refusalLine); const permsAt = body.indexOf("perms.files_create"); const ctxAt = body.indexOf("resolveRemoteFsContext(bot)"); assert.ok(permsAt !== -1 && ctxAt !== -1, "expected the perm gate and the context resolution to both exist"); assert.ok( permsAt < refusalAt && refusalAt < ctxAt, "the \".\" refusal must sit after the perm check (401/403 before 400) and before resolveRemoteFsContext (no daemon context for an invalid name)" ); }); test("legacy /rename refuses the no-op and server-root renames on the RESOLVED paths", () => { const body = routeBody('app.post("/rename"', "\nconst nodeVersions"); // Both refusals with their exact wire strings. assert.match( body, /if \(src === dest\) return res\.status\(400\)\.send\("Source and destination are the same"\);/, "the legacy /rename route must keep the same-src no-op refusal" ); assert.match( body, /if \(isServerRootResolvedPath\(ctx\.baseDir, src\) \|\| isServerRootResolvedPath\(ctx\.baseDir, dest\)\) \{\s*\n\s*return res\.status\(400\)\.send\("Refusing to rename server root"\);/, "the legacy /rename route must keep the server-root refusal on both src and dest" ); // The refusals must test the safeJoinUnix-resolved paths, not the raw body // strings — a raw-string check would miss encoded/aliased traversal that // only resolves to the root after the join. const srcJoinAt = body.indexOf("const src = safeJoinUnix(ctx.baseDir, oldPath)"); const destJoinAt = body.indexOf("const dest = safeJoinUnix(ctx.baseDir, relative)"); const refusalAt = body.indexOf("src === dest"); const probeAt = body.indexOf("/v1/fs/list"); assert.ok(srcJoinAt !== -1 && destJoinAt !== -1 && refusalAt !== -1, "expected the resolved-path joins and the refusals to all exist"); assert.ok( refusalAt > srcJoinAt && refusalAt > destJoinAt, "the refusals must run after safeJoinUnix resolves src/dest — resolved-path discipline" ); assert.ok( probeAt === -1 || refusalAt < probeAt, "the refusals must run before the destination probe / relay dial" ); });