Public: anyone can read this; the crew can change it.

Access 5a: a visitor principal is minted only when a visitor creates something, signed, with a role in may() and per-visitor limits #18174

opened this

Merged 3h 47m ago, live in production

A visitor with no account now becomes a principal only when they make something. The one place a visitor makes something today is an app note sent from the forge button, and that is now wired to it. What changed New src/core/visitors.ts.

Notes

opened this

A visitor with no account now becomes a principal only when they make something. The one place a visitor makes something today is an app note sent from the forge button, and that is now wired to it.

What changed

  • New src/core/visitors.ts. The id is signed and lives in its own forge_maker cookie (HttpOnly, SameSite=Lax, Secure follows the https rule). The signing key is derived only for this cookie (HKDF, "forge visitor principal v1"), and ids start with v.
    • heldVisitor reads the cookie and never mints.
    • admitVisitor asks may for visitor.make, checks the caps, and mints an id only if the request holds none.
    • countMake records a thing once it has actually been made.
  • Principal and may(). Principal gains the kind visitor; userOf returns nothing for it. may() gains the action visitor.make: anonymous and visitors may do it, and a visitor may do nothing else beyond what anonymous may. resolveAuth sets Auth.visitorId only when there is no credential.
  • Limits. VISITOR_CAPS in limits.ts: 200 things a day per visitor and 5000 a day for all visitors together. The per-visitor cap is twice what one app takes in a day (PER_APP_DAY), so the app's own limit refuses first.
  • App notes (src/apps/feedback.ts). This door used to give every request, including GET /__forge/notes, an unsigned forge_visitor cookie with the same name as the analytics cookie. Now only the 201 answer to a filed note sets forge_maker, and listing notes mints nothing. With DNT: 1 or Sec-GPC: 1 the note is still filed and no id is kept.
  • Docs. docs/ROLES.md and docs/features/notes.md say all of this. There is no terminal side: a note sent from the terminal is always the SSH user's, never a visitor's.
  • Events. There is no new bus.append kind, so event_audience.ts needed no entry.

Tests I changed because they pinned the old behaviour

  • test/may.test.ts: anonymous capabilities now include visitor.make.
  • test/cookie-secure.test.ts: posts a note instead of reading the list.
  • test/apps-feedback.test.ts: two list reads now see the name "visitor" with no minted suffix.
  • test/cookie-flags.test.ts and test/cookie-floor.test.ts: list the new cookie site.

test/visitors.test.ts is new and covers:

  • a minted id verifies and resolves to the visitor principal, not a user;
  • an unsigned id, an altered id or MAC, a wrong key, or an analytics value signed by this forge is refused;
  • a read mints nothing;
  • DNT and GPC mint no id;
  • the per-visitor cap, then the per-forge cap, refuses the next make.

tsc passes, the gate shows no failures, and the 19 related test files I ran pass. I did not run the full suite.

Threat note

  • Mixing the id up with analytics: the two cookies differ in name, key, id prefix and table. An analytics value signed by this forge fails verification, and the test checks that.
  • Treating a visitor as a user: userOf returns nothing for a visitor, so every repository and forge rule treats one as anonymous (for example, commenting is refused as "unauthorized").
  • Forging ids: ids are HMAC-signed and compared in constant time. Minting fresh ids gets around the per-visitor cap only until the forge-wide cap is spent.
  • Cookie scope: the cookie lives on the app's host, so each app host has its own visitor id.

Left out

  • Shared tab-forge apps and linked game players are allowed kinds of thing but are not wired yet; no route for them exists on the server.
  • The cap is a day's count stored in SQLite, not a setting an admin can change.
  • Notes filed under the old unsigned cookie stay with the builders, but they no longer show in that visitor's own list.

Reuse: the lazily created table follows run_secret.ts and client_cloud.ts. mint is deliberately not perf/visitor_id.ts mintId (the gate flags them as near-identical): sharing it would put the two ids in one namespace, which is exactly the mix-up this issue rules out.

Try: open a public app's page on the preview and open the forge button's list; no cookie is set. Send a note; the 201 answer sets forge_maker, and the list then shows that note.

[{"go":"a public app's page on the preview","look":"open the forge button's notes list, then the response headers","expect":"no Set-Cookie on GET /__forge/notes"},{"go":"the same page","look":"send a note from the forge button","expect":"201 with Set-Cookie forge_maker=v…; HttpOnly; the list now shows the note"}]

🤖 Generated with Claude Code

Closes #18112

3 earlier comments
commented

A visitor with no account now becomes a principal only when they make something. The one place a visitor makes something today is an app note sent from the forge button, and that is now wired to it.

What changed

  • New src/core/visitors.ts. The id is signed and lives in its own forge_maker cookie (HttpOnly, SameSite=Lax, Secure follows the https rule). The signing key is derived only for this cookie (HKDF, "forge visitor principal v1"), and ids start with v.
    • heldVisitor reads the cookie and never mints.
    • admitVisitor asks may for visitor.make, checks the caps, and mints an id only if the request holds none.
    • countMake records a thing once it has actually been made.
  • Principal and may(). Principal gains the kind visitor; userOf returns nothing for it. may() gains the action visitor.make: anonymous and visitors may do it, and a visitor may do nothing else beyond what anonymous may. resolveAuth sets Auth.visitorId only when there is no credential.
  • Limits. VISITOR_CAPS in limits.ts: 200 things a day per visitor and 5000 a day for all visitors together. The per-visitor cap is twice what one app takes in a day (PER_APP_DAY), so the app's own limit refuses first.
  • App notes (src/apps/feedback.ts). This door used to give every request, including GET /__forge/notes, an unsigned forge_visitor cookie with the same name as the analytics cookie. Now only the 201 answer to a filed note sets forge_maker, and listing notes mints nothing. With DNT: 1 or Sec-GPC: 1 the note is still filed and no id is kept.
  • Docs. docs/ROLES.md and docs/features/notes.md say all of this. There is no terminal side: a note sent from the terminal is always the SSH user's, never a visitor's.
  • Events. There is no new bus.append kind, so event_audience.ts needed no entry.

Tests I changed because they pinned the old behaviour

  • test/may.test.ts: anonymous capabilities now include visitor.make.
  • test/cookie-secure.test.ts: posts a note instead of reading the list.
  • test/apps-feedback.test.ts: two list reads now see the name "visitor" with no minted suffix.
  • test/cookie-flags.test.ts and test/cookie-floor.test.ts: list the new cookie site.

test/visitors.test.ts is new and covers:

  • a minted id verifies and resolves to the visitor principal, not a user;
  • an unsigned id, an altered id or MAC, a wrong key, or an analytics value signed by this forge is refused;
  • a read mints nothing;
  • DNT and GPC mint no id;
  • the per-visitor cap, then the per-forge cap, refuses the next make.

tsc passes, the gate shows no failures, and the 19 related test files I ran pass. I did not run the full suite.

Threat note

  • Mixing the id up with analytics: the two cookies differ in name, key, id prefix and table. An analytics value signed by this forge fails verification, and the test checks that.
  • Treating a visitor as a user: userOf returns nothing for a visitor, so every repository and forge rule treats one as anonymous (for example, commenting is refused as "unauthorized").
  • Forging ids: ids are HMAC-signed and compared in constant time. Minting fresh ids gets around the per-visitor cap only until the forge-wide cap is spent.
  • Cookie scope: the cookie lives on the app's host, so each app host has its own visitor id.

Left out

  • Shared tab-forge apps and linked game players are allowed kinds of thing but are not wired yet; no route for them exists on the server.
  • The cap is a day's count stored in SQLite, not a setting an admin can change.
  • Notes filed under the old unsigned cookie stay with the builders, but they no longer show in that visitor's own list.

Reuse: the lazily created table follows run_secret.ts and client_cloud.ts. mint is deliberately not perf/visitor_id.ts mintId (the gate flags them as near-identical): sharing it would put the two ids in one namespace, which is exactly the mix-up this issue rules out.

Try: open a public app's page on the preview and open the forge button's list; no cookie is set. Send a note; the 201 answer sets forge_maker, and the list then shows that note.

[{"go":"a public app's page on the preview","look":"open the forge button's notes list, then the response headers","expect":"no Set-Cookie on GET /__forge/notes"},{"go":"the same page","look":"send a note from the forge button","expect":"201 with Set-Cookie forge_maker=v…; HttpOnly; the list now shows the note"}]

🤖 Generated with Claude Code

commented

Access 5a adds a visitor principal: a person with no account gets a signed forge_maker id only when they make something, and the id is counted against a cap per visitor and one per forge. It is checked through may as visitor.make (src/core/visitors.ts), and app notes are the first thing that mints it.

This push fixes the check's 500 in test/outsiders-member.test.ts. resolveAuth read the secret key on every request with no credential, just to look for the maker cookie. That test writes secret.key with mode 644 on purpose, and from then on loadKey threw on every such request. heldVisitor now takes the key as a function and reads it only once a well-formed forge_maker cookie is there, so a request with no cookie never touches the key file. A new test in test/visitors.test.ts makes sure the key isn't read for a missing cookie, someone else's cookie or a malformed one.

Test changed: in test/visitors.test.ts, the caps test's setup of about 5,000 makes now runs in one transaction. It ran just past bun's 5-second timeout in my sandbox, whether or not this push's fix was applied, and did not fail in the check.

Not reproduced: the other failure in the check, "plan carries the template only when it is not blank" in test/spawn-template-api.test.ts, passes on its own and alongside outsiders-member. It runs no visitor code, so I made no change for it; the next check will show whether it was related.

Checks run: tsc --noEmit and the gate pass, and so do the eight test files nearest the change (visitors, outsiders-member, spawn-template-api, apps-feedback, cookie-flags, cookie-floor, cookie-secure, may). The full suite is left to the forge's check, since scripts/fast.ts named 4,162 files.

Try it: with no credential, / answers 200 even when secret.key has the wrong mode. Make a note in an app without signing in: the answer sets forge_maker, and later requests from that browser resolve to a visitor principal.

🤖 Generated with Claude Code

commented

Access 5a adds a visitor principal: a person with no account becomes someone the forge knows only when they make something. For now that means filing a note on an app's page. They get a signed id in their own forge_maker cookie. It uses its own key (HKDF, "forge visitor principal v1"), is checked in constant time, and is never the analytics forge_visitor id. Nothing is minted when the browser sends DNT: 1 or Sec-GPC: 1 (src/core/visitors.ts).

A request with a valid id is the principal visitor. userOf returns no user for it, and may lets it do what anonymous may plus visitor.make. A request whose credential matched nothing gets no visitor.make.

VISITOR_CAPS in src/http/limits.ts sets the daily limits on what visitors can make:

  • 200 per visitor id.
  • 200 per connection (clientBucket). A client that drops its cookie gets a fresh id each time, so the per-visitor cap never binds it; this one does. Without it, one client could spend the whole forge's cap and lock every other visitor out.
  • 5000 for all visitors on the forge together.

Only notes that were actually filed count toward the caps.

What to look at:

  • Key file reads: the secret key is read only when a well-formed maker cookie is present, in resolveAuth and in the notes list, picture and send. Before this, every request with no credential read the key file, so a key with the wrong mode made those requests fail with 500. That was the cause of the earlier red check in test/outsiders-member.test.ts.
  • Stored data: the visitor_made table holds the visitor id, the socket's client and the time. It has no kind column, because nothing read it.
  • Old cookie: a visitor holding only the old unsigned forge_visitor note cookie no longer sees their earlier notes in the list. The notes are still filed for the builders. docs/features/notes.md says so; the old cookie is not accepted for reads because it is unsigned.

Threat note:

  • Mixing ids: a visitor id can't be mixed up with the analytics id: they share no key, no format prefix and no table.
  • Treated as a user: a visitor is never treated as a user, because userOf returns none.
  • Forged or altered ids: these are refused, and a request carrying one is admitted as a new visitor, never as the one it copies.

Not done here: an app made and shared in the tab forge (#18188) and a linked game player (#18189) do not mint the principal yet. Both are open issues waiting on this one.

Tests:

  • test/visitors.test.ts:
    • a minted id verifies;
    • unsigned, altered or analytics ids are refused;
    • a read mints nothing;
    • DNT/GPC mint nothing;
    • each of the three caps refuses the next make;
    • the key is read only when a maker cookie is present.
  • test/apps-feedback.test.ts: a notes list read with no maker cookie never calls loadKey; this test fails without the fix.

In test/visitors.test.ts, the caps test builds its thousands of setup rows in one transaction, because separate commits ran past bun's 5-second timeout on a slow disk. The test file has tsc --noEmit, the gate, the unused-exports and module-state guards, and the eight test files nearest the change all pass. The full suite is left to the forge's check.

The other failure in that earlier check, "plan carries the template only when it is not blank" in test/spawn-template-api.test.ts, passes on its own and together with outsiders-member. It runs no visitor code, so it is unchanged.

How to try it: open an app's page without signing in and send a note from the forge button. That answer, and no earlier one, sets forge_maker, and the button's list then shows the note. The 201st note in a day from one connection is answered with 429.

🤖 Generated with Claude Code

commented

Adds a visitor principal: a person with no account can now make something as a visitor through the new visitor.make action. This merges current main, which had added forge.write to the permission registry.

What changed

  • src/core/visitors.ts creates the visitor ID. src/core/may.ts gives visitor.make to visitors and to anonymous callers who haven't been refused. Signed-in accounts don't get it.
  • src/http/limits.ts caps visitors by connection as well as by ID.
  • The secret key is read only when a maker cookie is present, so a request with no credential never touches it.
  • The cookie floor test names the visitor ID line that the note response is allowed to set.

Merge conflict: test/may.test.ts conflicted on what each caller may do. The resolution keeps both sides: no account gives ["forge.read", "visitor.make"], and signed-in accounts now also list forge.write, as on main.

Checks: bun x tsc --noEmit passes, and the gate passes. scripts/fast.ts selected 4,167 test files, far over its 5-minute budget, so I ran only the 59 files around may, limits, visitors, access and cookies: 401 pass, 0 fail, 4 skipped. The full check runs on this push.

Left out: the gate notes that core/visitors.ts#mint is identical to perf/visitor_id.ts#mintId, and that core/visitors.ts#table is close to core/client_cloud.ts#tables. The notes are advisory and I didn't merge either pair here.

To try it: with no session, GET /api/v1/user on the preview should list visitor.make. A signed-in token should not have it.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs