Skip to content
53 changes: 30 additions & 23 deletions docs/contributing/architecture/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ Hosted package apps run on their own registrable domain in production
(`PACKAGE_APP_BASE_URL`) so author-supplied code is cross-site from the app
origin — see
[Hosted package app origin isolation](../security.md#hosted-package-app-origin-isolation)
for why. That means `kody_session` never reaches them, so the package-app origin
needs its own, deliberately smaller credential.
for why. Production serves each owner's apps on a per-user subdomain of that
domain (`https://{username}.kodyapps.dev/packages/{kodyId}/...`); the apex only
redirects. That means `kody_session` never reaches them, so the package-app
subdomain needs its own, deliberately smaller credential.

**Handoff token** (`packages/worker/src/app/package-app-handoff.ts`). When a
signed-in owner requests `/@{username}/packages/{kodyId}/...` on the app origin,
Expand All @@ -368,7 +370,7 @@ the app origin mints `<base64url payload>.<HMAC-SHA256>`:
(`kody-package-app-handoff:v2`), so it is not interchangeable with any other
signed value
- payload binds `{ stableUserId, username, kodyId, exp, jti }`; the package-app
origin rejects a token whose `username`/`kodyId` do not match the requested
subdomain rejects a token whose `username`/`kodyId` do not match the requested
path
- 60 second lifetime
- single use: `jti` is burned in `BUNDLE_ARTIFACTS_KV` for 60 seconds on first
Expand All @@ -379,17 +381,18 @@ the app origin mints `<base64url payload>.<HMAC-SHA256>`:
the binding is missing; signature, expiry, and the path binding always fail
closed.

It travels in the `__kody_handoff` query parameter of a cross-origin redirect,
which is why it is deliberately this weak. A token in a URL is exposed to
browser history, referrers, and anything that logs URLs; the package-app origin
redirects straight to the same URL without it, which reduces that exposure but
cannot eliminate it. The 60-second expiry and the single-use burn are what bound
the damage when a token does leak. A request that still carries the parameter is
rewritten without it before package code sees it.
It travels in the `__kody_handoff` query parameter of a cross-origin redirect to
the owner's package-app subdomain, which is why it is deliberately this weak. A
token in a URL is exposed to browser history, referrers, and anything that logs
URLs; the subdomain redirects straight to the same URL without it, which reduces
that exposure but cannot eliminate it. The 60-second expiry and the single-use
burn are what bound the damage when a token does leak. A request that still
carries the parameter is rewritten without it before package code sees it.

**Package-app session cookie**
(`packages/worker/src/app/package-app-session.ts`). Exchanging a valid token
sets `kody_pkg_session` on the package-app origin:
(`packages/worker/src/app/package-app-session.ts`). Exchanging a valid token on
the owner's subdomain sets `__Host-kody_pkg_session` on secure requests (plain
`kody_pkg_session` on insecure local HTTP only):

- `httpOnly: true`, `sameSite: 'Lax'`, `path: '/'`, `secure` per request
- 12 hour max age
Expand All @@ -399,17 +402,21 @@ sets `kody_pkg_session` on the package-app origin:
- payload is `{ v, stableUserId, pkgUsername, issuedAt }` — a shape the app
session schema rejects, so the two cannot be confused even by name
substitution

It authorizes hosted package-app serving for one account and nothing else: the
app origin has no code path that reads it, and the package-app origin has no
first-party routes. Every request re-resolves the account from D1
(`resolvePackageAppOwnerByStableUserId`) and fails closed for unknown, deleting,
or suspended accounts, and for sessions issued at or before
`users.password_changed_at` — the same rules browser sessions follow. Confirmed
local, preview, and test runtimes with `PACKAGE_APP_BASE_URL` unset never mint
either credential; they serve package apps inline behind `kody_session`.
Production requires a separate registrable package-app origin and returns `500`
instead of falling back inline when that configuration is missing or unsafe.
- the `__Host-` prefix on secure requests forbids a `Domain` attribute, so
sibling subdomains cannot plant a shadow cookie under this name

It authorizes hosted package-app serving for one account on that account's
subdomain and nothing else: the app origin has no code path that reads it, and
the package-app domain has no first-party routes. Every request re-resolves the
account from D1 (`resolvePackageAppOwnerByStableUserId`) and fails closed for
unknown, deleting, or suspended accounts, for sessions issued at or before
`users.password_changed_at` — the same rules browser sessions follow — and when
the session account's username does not match the subdomain label and requested
package path. Confirmed local, preview, and test runtimes with
`PACKAGE_APP_BASE_URL` unset never mint either credential; they serve package
apps inline behind `kody_session`. Production requires a separate registrable
package-app origin and returns `500` instead of falling back inline when that
configuration is missing or unsafe.

## Account secret reveal

Expand Down
14 changes: 9 additions & 5 deletions docs/contributing/architecture/request-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ Requests are handled in this order:

0. Package-app host isolation (before everything else — see
[Hosted package app origin isolation](../security.md#hosted-package-app-origin-isolation)):
- On the **package-app origin**, only `/@{username}/packages/*` is served
(plus the handoff-token exchange on those same paths). `/` redirects to the
app origin; every other path is `404`.
- On the **package-app apex**, no package code runs: `/` redirects to the app
origin; legacy `/@{username}/packages/*` redirects to the owning user's
subdomain; everything else is `404`.
- On a **per-user package-app subdomain** (`{username}.<package-app host>`),
only `/packages/{kodyId}/*` for that hostname's username is served (plus
the handoff-token exchange on those same paths). `/` redirects to the app
origin; every other path is `404`.
- On the **app origin**, `/@{username}/packages/*` never executes package
code: safe methods redirect to the package-app origin with a handoff token,
other methods get a `307` to the same path there.
code: safe methods redirect to the owner's package-app subdomain with a
handoff token, other methods get a `307` to that subdomain.
- Production package-app requests fail with `500` when `PACKAGE_APP_BASE_URL`
is missing or is not on a separate registrable domain from `APP_BASE_URL`;
package code never executes inline.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 0017: Per-user subdomains for hosted package apps

- **Status:** accepted
- **Date:** 2026-08-11

## Context

Hosted package apps execute author-supplied HTML and JavaScript. Production
already isolates them from the first-party app origin via a separate registrable
domain (`PACKAGE_APP_BASE_URL`, `kodyapps.dev`) and credential stripping, but
every owner's apps still shared one origin on that domain. One user's package
could reach another user's `kody_pkg_session`-authorized endpoints from the
browser — a smaller blast radius than first-party access, but not zero.

Alternatives considered:

- **Stay on one shared package-app origin.** Simpler DNS and routing, but
user-to-user browser isolation remains incomplete.
- **Per-package subdomains** (`{kodyId}.kodyapps.dev`). Stronger same-owner
isolation, but multiplies DNS/certificate surface and complicates the handoff
and `packageContext` contract for every package.
- **Path-only isolation with stricter CSP on package apps.** Would still leave
same-origin cookie and storage sharing; CSP cannot be applied to author code
without breaking real apps.

Usernames had allowed underscores, which are invalid in DNS labels and would
block `{username}.kodyapps.dev` for affected accounts.

## Decision

Serve production hosted package apps on **per-user subdomains** of the
package-app apex:

- Public URL shape: `https://{username}.kodyapps.dev/packages/{kodyId}/{path}`
- The apex (`kodyapps.dev`) serves no package code: `/` redirects to the app
origin; legacy `/@user/packages/...` paths redirect to the owning subdomain;
everything else `404`s.
- The app-origin path `/@{username}/packages/...` remains the authenticated
entry point and redirects into the handoff on the owner's subdomain.
- Package-app session cookies on secure requests use the
`__Host-kody_pkg_session` name so browsers reject any variant with a `Domain`
attribute (cookie-tossing defense before Public Suffix List entry). Insecure
local HTTP keeps plain `kody_pkg_session`.
- Serving requires session account username == subdomain label == path owner
(fixation defense).
- New and changed usernames are strict DNS labels (lowercase alphanumeric +
hyphens; underscores banned). Recognition of stored usernames stays lenient
(two-tier validation) so legacy underscore accounts keep display names, public
lookup, and inbound email routing; they must rename before hosted apps work
(the app origin answers their package-app entry with a `409` rename prompt).
- `packageContext.appBasePath` on a subdomain is `/packages/{kodyId}` (no
`/@{username}` prefix); inline non-production serving keeps the path-based
mount. Well-behaved packages that use `hostedUrl` / `appBasePath` stay
transparent.
- Untrusted markdown refuses `/packages/...` links on any host in addition to
`/@...`.
- **Same-user package-to-package isolation is deliberately deferred:** two
packages of one owner still share that owner's subdomain origin.
- **Operational follow-up:** submit `kodyapps.dev` to the Public Suffix List
(`_psl` TXT record + PR to publicsuffix/list) for defense-in-depth.

## Consequences

- User-to-user browser isolation for hosted apps is complete; same-owner
cross-package reachability remains an accepted residual risk (documented in
[`security.md`](../security.md)).
- Production deploys require wildcard DNS and a `*.kodyapps.dev/*` Worker route
(see [`setup-manifest.md`](../setup-manifest.md)).
- Username renames become mandatory for underscore holders before subdomain
hosting works.
- `parsePackageSearchIdentity`, status probes, and author docs must recognize
both subdomain URLs and legacy path shapes during transition.
- Revisit per-package subdomains only if same-owner isolation becomes a reported
abuse vector or a product requirement.
1 change: 1 addition & 0 deletions docs/contributing/decisions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ behavior (see [documentation principles](../documentation.md)).
- [0014 — Platform scopes resolve live in package imports](./0014-platform-live-packages.md)
- [0015 — Wait on Skills over MCP (SEP-2640); serve skill content via packages](./0015-skills-over-mcp-wait.md)
- [0016 — Extract the package runtime and jobs lanes into separate workers](./0016-mono-worker-extraction.md)
- [0017 — Per-user subdomains for hosted package apps](./0017-per-user-package-app-subdomains.md)
30 changes: 17 additions & 13 deletions docs/contributing/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ Wrangler `var` (public and non-secret; required in production, optional in
confirmed non-production runtimes; see `packages/worker/src/app-base-url.ts` and
`packages/worker/src/app/package-app-origin.ts`):

- `PACKAGE_APP_BASE_URL` — the origin that hosted package apps are served from.
Production sets `https://kodyapps.dev` in `packages/worker/wrangler.jsonc`,
and the deploy derives a Workers `custom_domain` route from it so Cloudflare
provisions DNS and the edge certificate (see
[setup-manifest.md](./setup-manifest.md)). It **must be a separate registrable
domain** from `APP_BASE_URL`: that is what makes author-supplied package code
cross-site, so the `SameSite=Lax` `kody_session` cookie never reaches it.
Production origin validation also requires `APP_BASE_URL` so this relationship
can be checked at runtime. Production returns `500` for package-app requests
when this value is missing, invalid, equal to `APP_BASE_URL`, or on the same
registrable domain; it never falls back to inline serving. Preview, tests, and
E2E may leave it unset and keep serving package apps inline on the app origin
at `/@{username}/packages/*`.
- `PACKAGE_APP_BASE_URL` — the **apex** origin of the package-app domain that
hosted package apps are served from. Production sets `https://kodyapps.dev` in
`packages/worker/wrangler.jsonc`, and the deploy derives Workers
`custom_domain` routes for the apex and a wildcard for per-user subdomains so
Cloudflare provisions DNS and edge certificates (see
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
[setup-manifest.md](./setup-manifest.md)). Each owner's apps are addressed at
`https://{username}.<apex-host>/packages/{kodyId}/...`; the apex itself serves
only redirects (legacy `/@user/packages/...` paths to the owning subdomain,
`/` to the app origin). It **must be a separate registrable domain** from
`APP_BASE_URL`: that is what makes author-supplied package code cross-site, so
the `SameSite=Lax` `kody_session` cookie never reaches it. Production origin
validation also requires `APP_BASE_URL` so this relationship can be checked at
runtime. Production returns `500` for package-app requests when this value is
missing, invalid, equal to `APP_BASE_URL`, or on the same registrable domain;
it never falls back to inline serving. Preview, tests, and E2E may leave it
unset and keep serving package apps inline on the app origin at
`/@{username}/packages/*`.

`npm run dev` runs the **production** Wrangler environment, so the committed
production value reaches local dev too; `getPackageAppBaseUrl` ignores an
Expand Down
Loading
Loading