Skip to content
Merged
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
168 changes: 168 additions & 0 deletions docs/adr/0003-public-sharing-export-reader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# ADR 0003: Public sharing through Hub exports and an isolated reader

Status: accepted on November 29, 2026, with the [policy decisions](#accepted-policy-decisions) recorded below.

## Context

A signed-in Hub user can create a public link to one successful Build. Anyone with the link can open that Build without signing in until the link expires or is revoked.

Today's links ([Build share links](../../hub/docs/share-links.md)) are `ShareLink` rows with a random UUID, a label, an expiry and a revocation tombstone. Hub serves them through the legacy Build-specific preview origin, which needs wildcard DNS and TLS that the Azure reference installation does not provision. Rails used `/share/:public_id` with the same idea and no password. No Rails share data was imported.

The [private prototype service](../../hub/docs/build-previews.md#private-prototype-service) now serves eligible Builds at `/previews/github/<repositoryId>/<runId>/`. These Builds are compiled for that exact path, so they do not work under a different path.

## Accepted requirements

- Hub stores share records and writes or revokes exports in a separate public export storage, using a container-scoped managed identity.
- A separate read-only public reader serves exports. It has no access to the Hub database, Hub secrets, private storage, or Hub APIs.
- The same Terraform and setup wizard install both. There is no separate lifecycle API.
- Normal multi-file prototypes with large asset sets work without a second bundle.
- Sharing never deletes or rewrites stored private Builds.

## Decision

```mermaid
flowchart LR
user["Hub user"] --> hub["Hub API and worker"]
hub --> db[("Hub database: share records")]
hub -- "reads Build files" --> private[("Private Build storage")]
hub -- "writes and revokes" --> exports[("Public export storage")]
hubId["Hub identity: export contributor"] -.-> exports
viewer["Link viewer"] -- "/s/shareId" --> reader["Public reader"]
reader -- "reads by exact key" --> exports
readerId["Reader identity: read only, no listing"] -.-> exports
reader -. "no route" .-> hub
reader -. "no grant" .-> private
setup["Terraform and setup wizard"] -. "provisions" .-> exports
setup -. "provisions" .-> reader
```

### Components and permissions

| Component | Can do | Cannot do |
| --- | --- | --- |
| Hub API and on-demand worker | Read Build files; create, overwrite and delete objects in the export container | Nothing new outside that container |
| Public export storage | Separate storage account and container; shared keys and anonymous access off | Serve anything without the reader |
| Public reader | Read exact export objects with its own identity | List, write or delete blobs; reach Hub, its database, Key Vault or private storage |

The reader runs from the Hub image with its own start command, like the private reader, but its modules import only Node built-ins, Express, zod, the Azure SDK, Hub's dependency-free start-page and private-preview address rules, and other public-share modules. A test enforces that import boundary.

### Storage layout and manifest

Each share is a self-contained copy. Two shares of one Build are two copies, so revoking or deleting one never affects the other and no reference counting is needed.

| Object | Written | Purpose |
| --- | --- | --- |
| `shares/<shareId>/files/<sha256>` | First, create-only | One served `dist/` file, named by its SHA-256 |
| `shares/<shareId>/manifest.json` | After every file | Immutable file index |
| `shares/<shareId>/policy.json` | Last; overwritten for changes | Activation, expiry, password and revocation |

The share is live only once `policy.json` exists, so a viewer never sees a partial export.

`manifest.json`, version 1:

```json
{
"version": 1,
"shareId": "6f2c…",
"mount": "/previews/github/901/1201/",
"entry": "/",
"files": { "index.html": { "sha256": "…", "size": 812 }, "assets/app.js": { "sha256": "…", "size": 90211 } }
}
```

`policy.json`, version 1:

```json
{
"version": 1,
"shareId": "6f2c…",
"state": "active",
"expiresAt": "2026-12-06T08:00:00.000Z",
"password": { "algorithm": "scrypt", "cost": 16384, "blockSize": 8, "parallelization": 1, "salt": "…", "hash": "…" }
}
```

`state` is `active` or `revoked`. `password` is `null` when the link has none. Hub never stores or exports a plaintext password.

### URL contract

Each link is an opaque entry that leads into the Build's own mount on the public origin.

1. Hub gives out `https://<public-origin>/s/<shareId>`. The share ID is the existing random `publicId` UUID.
2. The reader checks the policy. For a password link, it asks for the password first.
3. The reader sets a host-only, HTTP-only cookie named `__Secure-replica-share-<shareId>` with `Path=<mount>` and redirects to `<mount><entry>`.
4. Every file request under the mount reads the share cookies the browser sent, rechecks each share's policy, and serves from the first active share whose mount matches.

Two shares of one Build use two cookies with the same path. Revoking one leaves the other working only for viewers who opened the other link. A password link's cookie holds an HMAC keyed by the stored password hash, so changing or removing the password ends existing password sessions.

Only Builds that the private prototype service can already serve are eligible, because their mount is verified. Hub shows "This Build can't be shared publicly" for every other Build. This rules out automatic rebuilds, response rewriting, framework internals and per-share wildcard DNS.

Accepted trade-offs:

- After opening, the address bar shows the mount, which includes the numeric GitHub repository and workflow run IDs. These are not secret, but today's links reveal neither.
- All shares share one origin and its browser storage. A shared prototype's script can read storage of other shares and fetch other shares' files only within a browser that has already opened those shares. The private reader accepts the same trade-off.
- That includes password prompts. A shared prototype's script can open another share's password page in a same-origin window and read a password the viewer types there. A password therefore protects a link from people who only have the URL, not from other prototypes the same viewer opens.
- Same-origin script can also set many share cookies and hide real ones. This only breaks viewing in that browser.
- Public shares open top level. They cannot be framed and do not include Hub's Controls panel or review bridge.

Rejected alternatives: serving the mount without an entry cannot tell two shares apart and leaks repository identity in the link itself. Accepting only root-relative packages would exclude every current `proto/*` Build.

### Public hostname

The reader serves links on its generated Azure Container Apps hostname in every installation, including Production. An installation can add a custom domain later as an optional setup wizard setting.

- The reader accepts exactly two HTTPS origins: its generated origin and, when configured, the custom one. It refuses every other host.
- Hub builds new links on the custom origin when one is configured, and on the generated origin otherwise.
- Adding a custom domain does not break existing links, because the generated hostname keeps working.

Public sharing shares no cookies with Hub. In Production, Hub uses `replica.coreai.design` and the private reader uses `preview.replica.coreai.design`, so the reader's generated hostname is a different site from both. Three caveats remain:

- Links on the generated hostname break if the reader's app or Container Apps environment is recreated without a custom domain.
- `azurecontainerapps.io` is not a public suffix, so browsers treat every generated Container Apps hostname as one site, including apps in other environments and other tenants. Those apps can send same-site requests to the reader and set cookies it receives. The reader serves nothing secret to such requests: responses are same-origin only, and a forged share cookie can open only a passwordless share whose link the sender already has, or hide real cookies in that browser.
- The same applies in reverse to a Hub on its generated hostname: shared prototypes would be same-site with it. By default, Hub and the setup wizard refuse to enable public sharing while Hub uses a generated hostname. Production uses a custom domain, so the default holds there; Hub's cookies are host-only on `replica.coreai.design`.

A non-production installation, such as Staging, can opt in anyway with `HUB_PUBLIC_SHARE_ALLOW_SHARED_SITE=true`. The setup wizard offers this only for non-production installations and warns about the shared-site trade-off first. The risk is accepted there because a Hub on a generated hostname is already same-site with every Container App, shared or not. Its real protections are its host-only `__Host-` cookies and exact-origin request checks, which sharing does not change.

### Request checks

The reader runs these checks for every request, including assets and `HEAD`:

- The request uses HTTPS on one of the reader's allowed hostnames.
- The share ID, mount and file path are valid; files are read only by manifest key, never by request path, and storage is never listed.
- The policy is `active` and `expiresAt` is in the future, checked again after the file read.
- A password link has a valid cookie proof.
- File, manifest and policy reads are size-bounded and time out. Files share a 100 MiB in-memory allowance per process, and policies and manifests share a separate 40 MiB one. A file's reservation lasts until both its read and its response end, and a stalled download is dropped after 30 seconds without progress. Manifests may list up to 2,000 files, matching a Build's limit.
- Password attempts are limited to 5 per caller address and link in 10 minutes, as a temporary cooldown that never locks or turns off the link, counted before each check, with at most 4 checks running at once. Each replica counts separately, so the limit grows with the number of replicas. A shared count would need the reader to write storage or another service, so the per-replica limit is accepted; passwords need at least 8 characters and scrypt keeps each guess slow.

Exact files are served first, then a directory's `index.html`; a directory requested without its trailing slash redirects to add it. The mount root without its slash redirects to the mount before any share is read, because share cookies cover only the slash-terminated mount. An extensionless document navigation falls back to the export's `index.html`. Missing assets return 404.

Responses send a strict content security policy, `X-Robots-Tag: noindex, nofollow, noarchive`, `Referrer-Policy: no-referrer`, `frame-ancestors 'none'` and `Cache-Control: private, no-cache` with a content-hash ETag, so browsers revalidate and policy checks still run. The reader sets only its own share cookies and ignores all others.

### Revocation

Hub revokes by overwriting `policy.json` with `state: "revoked"` during the user's request; a worker retries if the write fails and the UI shows the revocation as pending. Each reader replica caches a policy for at most 5 seconds, so revocation reaches every replica within 5 seconds of the write. Expiry is exact because it is compared on each request. Bytes already sent cannot be recalled. A worker then deletes the copied files within 24 hours of revocation or expiry. Private Builds and Hub share records are never deleted.

## Pull requests

| PR | Scope | Depends on |
| --- | --- | --- |
| 1. Public reader and export contract | Reader, start command, manifest and policy schemas, storage keys, password hashing, adapters, tests and docs. Not deployed or enabled. | None |
| 2. Hub export and share records | Additive `ShareLink` columns for backend, export state and password flag; export and revoke jobs; share API and **Share links** UI; eligibility message; refusal to enable while Hub's own hostname is a generated `azurecontainerapps.io` one, unless `HUB_PUBLIC_SHARE_ALLOW_SHARED_SITE=true`; legacy links unchanged | PR 1 contract |
| 3. Terraform and setup wizard | Export storage account and container, reader Container App and identity, container-scoped grants, the reader's generated hostname with an optional custom domain setting, settings on Hub and reader, disabled by default and refused while Hub uses a generated hostname; the wizard sets the shared-site override only for non-production installations | PR 1 start command and settings |

PR 2 and PR 3 can proceed in parallel after PR 1. Enabling public sharing needs all three, plus a non-production end-to-end check with a real framework-built `proto/*` Build.

## Accepted policy decisions

The Replica maintainers accepted these on November 29, 2026:

| Decision | Result |
| --- | --- |
| Extending an active link's expiry | Not allowed. Create a new link. Deadlines never grow. |
| Passwords | Optional per link, at least 8 characters, set at creation and replaceable or removable later. The shared-origin exposure above is accepted. |
| Copied files after revocation or expiry | Deleted within 24 hours. The Hub record stays for history. Sharing again makes a new copy. |
| Viewing during a Hub outage | Active links stay viewable. Revocations made during an outage take effect when Hub can write the policy again. |
| Existing `ShareLink` rows | Left unchanged on the legacy backend. They are not converted or redirected to exports. |
| Mount path in the address bar | Accepted. Hiding it would need response rewriting or rebuilds. |
| Public hostname | The reader's generated Container Apps hostname, with an optional custom domain later. See [public hostname](#public-hostname). |
| Hub on a generated hostname | Sharing is refused by default. Non-production installations, such as Staging, may opt in with an explicit shared-site override. |
1 change: 1 addition & 0 deletions hub/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Hub is being rebuilt. The [Hub overview](../README.md) describes what the baseli
- [GitHub Actions Builds](github-actions-builds.md): workflow events, dist and screenshot artifact contracts, and idempotent import.
- [Build previews](build-previews.md): authenticated access, isolated Build origins, file behavior, screenshots, and bridge injection.
- [Build share links](share-links.md): local and hosted opt-in, Build-pinned public links, revocation, and the separate public preview session.
- [Public share reader](public-share-reader.md): export contract, per-request checks, and runtime settings for the isolated read-only reader.
- [Database](database.md): Prisma schema conventions, migrations, and the shared client.
- [Authentication and Hub sessions](authentication.md): provider-neutral proof, reusable administrator enrollment, admission, browser sessions, and existing-user administrator management.
- [Copilot connection](copilot.md): operator token configuration, fresh administrator authorization, durable connection state, and credential safety.
Expand Down
Loading
Loading