@fungi.computer/computer 0.14.3 → 0.14.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,8 @@ export type PortAccessPrincipal = Readonly<{
9
9
  }>;
10
10
  /** Visibility belongs to an existing exposed port, never a second catalog. */
11
11
  export type PortVisibility = "private" | "unlisted" | "public";
12
+ /** A host's registrable site, or null when it has none (IPs, single labels). */
13
+ export type PortSite = (url: URL) => string | null;
12
14
  /** Refusal at the host's issuance or configuration boundary. */
13
15
  export declare class PortAccessError extends Error {
14
16
  readonly reason: "unexposed" | "public_disabled" | "not_unlisted" | "capacity";
@@ -35,5 +37,8 @@ export declare class ComputerPortAccess {
35
37
  private exchange;
36
38
  private admits;
37
39
  /** Mount on one target host. Forwarding remains responsible for live exposure. */
38
- gate(portInput: number, entryUrl: string, forward: (request: Request) => Response | Promise<Response>): Hono;
40
+ gate(portInput: number, policy: Readonly<{
41
+ entryUrl: string;
42
+ site: PortSite;
43
+ }>, forward: (request: Request) => Response | Promise<Response>): Hono;
39
44
  }
@@ -46,6 +46,23 @@ async function hash(value) {
46
46
  const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value));
47
47
  return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
48
48
  }
49
+ /**
50
+ * Sibling hosts on one site share Lax cookies, so a present Origin or Referer
51
+ * must be this exact origin or another site. A value the URL parser refuses,
52
+ * including the opaque `null` Origin, is refused too. Absent headers pass.
53
+ */
54
+ function fromSibling(request, target, site) {
55
+ const siteOf = (url) => site(url) ?? url.hostname;
56
+ return ["origin", "referer"].some((name) => {
57
+ const value = request.headers.get(name);
58
+ if (value === null)
59
+ return false;
60
+ if (!URL.canParse(value))
61
+ return true;
62
+ const source = new URL(value);
63
+ return source.origin !== target.origin && siteOf(source) === siteOf(target);
64
+ });
65
+ }
49
66
  /** One Computer's port credentials; the host owns membership and routing. */
50
67
  export class ComputerPortAccess {
51
68
  storage;
@@ -165,8 +182,9 @@ export class ComputerPortAccess {
165
182
  .toArray().length === 1);
166
183
  }
167
184
  /** Mount on one target host. Forwarding remains responsible for live exposure. */
168
- gate(portInput, entryUrl, forward) {
185
+ gate(portInput, policy, forward) {
169
186
  const port = computerPortSchema.parse(portInput);
187
+ const { entryUrl, site } = policy;
170
188
  const app = new Hono();
171
189
  app.all("*", async (c) => {
172
190
  c.header("Cache-Control", "no-store");
@@ -179,9 +197,11 @@ export class ComputerPortAccess {
179
197
  : c.body(null, 401);
180
198
  // Sibling preview hosts are same-site but must never use each other's
181
199
  // cookies. WebSockets bypass CORS, so require their exact origin too.
200
+ const target = new URL(c.req.url);
182
201
  const upgrade = c.req.raw.headers.has("upgrade");
183
202
  if (c.req.header("sec-fetch-site") === "same-site" ||
184
- (upgrade && c.req.header("origin") !== new URL(c.req.url).origin))
203
+ fromSibling(c.req.raw, target, site) ||
204
+ (upgrade && c.req.header("origin") !== target.origin))
185
205
  return denied();
186
206
  const visibility = this.visibility(port);
187
207
  if (visibility === undefined ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/computer",
3
- "version": "0.14.3",
3
+ "version": "0.14.4",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Cloudflare Computer host for one durable Fungi Computer",
6
6
  "type": "module",