Skip to content
Draft
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
4 changes: 1 addition & 3 deletions apps/reference/site.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export default defineShellsiteConfig({
files: {
work: "work",
writing: "writing.yaml",
records: "records.yaml",
profile: "profile.yaml",
pageCopy: "page-copy.yaml",
profile: "profile/profile.yaml",
},
policy: "./src/lib/edit-policy.ts",
},
Expand Down
4 changes: 3 additions & 1 deletion docs/ARE_YOU_INSANE.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ CORS is a browser rule, and it is a different one. Vite 6.4 defaults `server.cor

Copying the marquee's `server.host: true` onto a host that mounts the handler exposes the desk on every interface that process listens on.

By default, `@shellside/site` does not mount `/admin` or `/api/dev-content` when Astro's `server.host` is not loopback-only (`server.host: true`, `0.0.0.0`, and the like). It logs one line at setup and mounts nothing unless `edit.allowNonLoopback: true` is set in `site.config`.

## What the path grammar holds

`parseContentPath` (`packages/edit-protocol/src/paths.ts`) accepts `kind:id:field` up to 200 characters. An id must match `[a-z0-9][a-z0-9._-]*`. A field must be dotted identifiers (`title`, `links.0.href`). A slash, a space, or an id of `..` fails the parse, and the handler returns 400. Work files are `<work dir>/<id>.md` with that id. YAML lists are rewritten with `yaml.dump`, so a value is a datum in the document.
Expand All @@ -68,7 +70,7 @@ The portfolio, the console, `list.txt`, and `llms.txt` are supposed to read one
## If you run one

- Mount `createDevContentHandler` only where `import.meta.env.DEV` is the real dev flag, and pass that flag as `isDev`.
- Leave the dev server on loopback. `server.host: true` listens on every interface, and the desk's host check will not notice.
- Leave the dev server on loopback. `server.host: true` listens on every interface; the request `Host` check will not notice. The kit's mount gate refuses non-loopback binds by default — do not set `edit.allowNonLoopback: true` unless you mean to expose the desk on every interface the process listens on.
- Build the VFS from `isPublicItem`. Run `leakAudit` on `dist`.
- Pass a real allowlist. An omitted policy allows every well-formed path.
- Escape anything a console command sends to `out()`.
Expand Down
7 changes: 7 additions & 0 deletions packages/site/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ export type ShellsiteConfig = {
* Omit for a content-only desk.
*/
look?: string;
/**
* When false (default), the host refuses to mount the dev desk in
* `astro dev` unless Astro's `server.host` is loopback-only. Set
* `true` only if you accept exposing the write API on every address
* the dev server binds to (`server.host: true` and the like).
*/
allowNonLoopback?: boolean;
};
};

Expand Down
38 changes: 36 additions & 2 deletions packages/site/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { defineShellsiteConfig, shellsite } from "./index.js";
import { defineShellsiteConfig, isLoopbackServerBind, shellsite } from "./index.js";

test("config is plain data (serializable)", () => {
const c = defineShellsiteConfig({
Expand Down Expand Up @@ -45,6 +45,40 @@ test("virtual config plugin emits the config", () => {
assert.ok(plugin.load(id)!.includes('"name":"T"'));
});

test("isLoopbackServerBind matches Astro server.host shapes", () => {
assert.equal(isLoopbackServerBind(undefined), true);
assert.equal(isLoopbackServerBind(false), true);
assert.equal(isLoopbackServerBind(true), false);
assert.equal(isLoopbackServerBind("0.0.0.0"), false);
assert.equal(isLoopbackServerBind("127.0.0.1"), true);
assert.equal(isLoopbackServerBind("localhost"), true);
});

test("devDesk skips mount when server.host is true unless allowNonLoopback", () => {
const edit = {
files: { work: "work" },
policy: "./src/policy.ts",
};
const run = (allowNonLoopback?: boolean) => {
const routes: string[] = [];
const i = shellsite({
identity: { name: "T" },
collections: {},
surfaces: { devDesk: true },
edit: { ...edit, ...(allowNonLoopback ? { allowNonLoopback: true } : {}) },
});
(i.hooks as Record<string, Function>)["astro:config:setup"]({
injectRoute: (r: { pattern: string }) => routes.push(r.pattern),
updateConfig: () => {},
command: "dev",
config: { root: new URL("file:///site/"), server: { host: true } },
});
return routes;
};
assert.equal(run().includes("/api/dev-content"), false);
assert.equal(run(true).includes("/api/dev-content"), true);
});

test("devDesk injects /api/dev-content only in dev and only with edit", () => {
const mk = (devDesk: boolean, withEdit: boolean) =>
shellsite({
Expand All @@ -67,7 +101,7 @@ test("devDesk injects /api/dev-content only in dev and only with edit", () => {
plugins = u.vite?.plugins ?? [];
},
command,
config: { root: new URL("file:///site/") },
config: { root: new URL("file:///site/"), server: { host: "127.0.0.1" } },
});
const want = command === "dev" && withEdit;
assert.equal(
Expand Down
23 changes: 22 additions & 1 deletion packages/site/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ export { isPublicItem } from "@shellside/content-model";
export const SHELLSITE_CONFIG_ID = "virtual:shellsite/config";
const RESOLVED_CONFIG_ID = "\0" + SHELLSITE_CONFIG_ID;

const LOOPBACK_BIND_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);

/** Whether Astro's dev `server.host` binds only on loopback (default safe). */
export function isLoopbackServerBind(host?: string | boolean): boolean {
if (host === undefined || host === false) return true;
if (host === true) return false;
const h = host.trim().toLowerCase();
if (LOOPBACK_BIND_HOSTS.has(h)) return true;
if (h === "0.0.0.0" || h === "::" || h === "*") return false;
if (/^127(?:\.\d{1,3}){3}$/.test(h)) return true;
return false;
}

function configPlugin(config: ShellsiteConfig): Plugin {
return {
name: "shellsite-config",
Expand All @@ -55,8 +68,16 @@ export function shellsite(config: ShellsiteConfig): AstroIntegration {
"astro:config:setup": ({ injectRoute, updateConfig, command, config: astroConfig }: any) => {
// The desk mounts only in `astro dev` — a build never sees the
// route, the virtual module, or the site's edit modules.
const deskOn =
const deskWanted =
Boolean(config.surfaces.devDesk && config.edit) && command === "dev";
const bindLoopback = isLoopbackServerBind(astroConfig?.server?.host);
const allowWideBind = config.edit?.allowNonLoopback === true;
const deskOn = deskWanted && (bindLoopback || allowWideBind);
if (deskWanted && !deskOn) {
console.log(
"@shellside/site: dev desk not mounted because Astro server.host is not loopback (set edit.allowNonLoopback: true to opt in)",
);
}
if (deskOn) {
injectRoute({
pattern: "/api/dev-content",
Expand Down
Loading