Skip to content

@ogygia/federation

Keys and signing

Every call between apps is signed with the caller's Ed25519 private key and checked against the public keys the receiver lists. A signature is bound to the method, path, body, visitor claims, the receiving host and a moment in time, and a replay inside its window is refused, up to a limit.

On this page

Every server-to-server call is signed by the app that makes it, with an Ed25519 private key only that app holds. The app it calls lists the caller’s public key and checks the signature before it does anything else. There’s no shared secret to hand out, rotate or leak: a public key can’t sign anything.

An unsigned call, a bad signature or a key the receiver doesn’t list gets a 401 before any routing, widget or notice runs.

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';

export const { cms } = federate({
  name: 'shell',
key: env.SHELL_SIGNING_KEY, // private: signs what this app sends
  peers: {
    cms: {
      origin: 'https://cms.internal',
    key: env.CMS_PUBLIC_KEY
    } // public: checks what cms sends
  }
});

Make a key pair

npx ogygia keys <name> makes one pair and prints it as two environment lines. <NAME>_SIGNING_KEY is the private key: it stays with this app, as federate({ key }), in your secret store. <NAME>_PUBLIC_KEY goes to every app this one talks to, as peers.<name>.key. Key pairs covers the formats, the naming rule and where the output goes. Any Ed25519 pair in those formats works too.

npx ogygia keys shell
# export SHELL_SIGNING_KEY=MC4CAQAwBQYDK2Vw…
# export SHELL_PUBLIC_KEY=MCowBQYDK2VwAyEA…

Signed fields

A signature proves that the holder of one private key sent exactly this request, to this app, just now. It covers:

  • the method and the path with its query string;
  • a SHA-256 hash of the body (of no bytes for a GET);
  • the visitor claims, when the call carries any (Visitor identity);
  • the audience, the host the call is meant for;
  • the time it was signed, and a random nonce, new for every call.

Change any of them and the signature fails. Other headers aren’t covered. Claims are signed but not encrypted, so anyone who can read the traffic can read them: run the calls between apps over TLS.

Each call carries the signature twice, as an x-og-cap token and as the older x-og-ts, x-og-n and x-og-sig headers, with one nonce between them. A receiver that reads the token checks it alone. The two forms share a replay record, so a replay in either form is refused, within the limits under Replay and clocks. Non-ogygia hosts shows the exact bytes.

Audience

The audience stops a call signed for one app from being replayed at another. Say the cms and the dash both trust the shell’s key. A call the shell signed for cms.internal is refused at dash.internal, because its signature names the wrong host.

  • The caller signs each call for the peer’s audience option, or, when you don’t set one, the host (port included) of the origin it’s sending to. With replicas, that’s each replica’s own host, so set audience on a peer with several origins.
  • The receiver checks the call against its own federate({ audience }), a host or a list of hosts, or the host of its ORIGIN environment variable when you don’t set one. It never uses the Host the request names, since a caller could pick that.

With peers that have keys and neither audience nor ORIGIN, federate() warns with federation_audience_missing, and every signed call to the app gets a 401.

The two sides must name the same host. When the caller reaches the app under another name than the one it answers on, such as an internal host behind a proxy, either set the peer’s audience on the caller, or list both hosts in the receiver’s audience. Listing both is the safer choice today, since thaw notices ignore the peer’s audience. A producer that answers on a public ORIGIN while its callers reach it internally needs both hosts for the same reason.

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
import { cmsRouter } from '$lib/server/router';

federate({
  name: 'cms',
  key: env.CMS_SIGNING_KEY,
   audience: [
    'cms.example.com',
    'cms.internal:8080'
  ] ,
  peers: {
    shell: {
      origin: 'https://www.example.com',
      key: env.SHELL_PUBLIC_KEY
    }
  },
  expose: cmsRouter,
  base: '/cms'
});

Replay and clocks

A signature is fresh for skewMs either side of the receiver’s clock, 120 seconds by default. A call signed 121 seconds ago gets a 401, so keep the servers’ clocks in sync. Within that window, your app remembers the signatures it accepted and refuses a second delivery of the same call with a 401. That memory has limits, so treat it as a guard against casual replays, not a guarantee:

  • It’s per server process. A replay sent to another instance of the same app inside the window isn’t caught. Audience binding is what holds across instances, and the window bounds the rest.
  • It holds 100,000 signatures. The token form’s memory refuses new calls when it’s full of live ones, so with the default window, past about 100,000 signed calls per 120 seconds per process, roughly 830 a second across all peers, new calls get a 401 (#2311, item 18).
  • The older form forgets. The memory behind the x-og-ts headers drops its oldest entries once it holds 100,000, live ones included, and the record that ties a call’s two forms together lives there. So once up to 100,000 other signed calls arrive inside the window (fewer, down to about 50,000, when callers send only the older headers, since each of those takes two entries), someone who captured a call can drop its x-og-cap header, send the older headers again, and have it accepted a second time, claims included (#2312).

Until that’s fixed, keep skewMs short, rate-limit what each peer may send you, and treat a call that carries identity, or changes data, as one that could arrive twice: make mutations safe to repeat.

Key rotation

peers.X.key takes a list so that a new key can overlap the old one, and no valid call gets a 401 along the way:

  1. Make the new pair: npx ogygia keys shell-2026q4, which prints SHELL_2026Q4_SIGNING_KEY and SHELL_2026Q4_PUBLIC_KEY. Store them under those names, or rename them in your secret store.
  2. Add the new public key to the key list of every app that hears from the shell, keeping the old one. Deploy those apps.
  3. Switch the shell’s own key to the new private key, and deploy the shell.
  4. Remove the old public key from those apps, and deploy them.

You can stop after any step. To retire a leaked key fast, skip the overlap: switch the shell’s key and remove the old public key everywhere at once, and accept 401s from the old shell instances until they’re gone.

A key list is also how one peer name covers two callers, such as a second team’s service signing as the shell. Each key checks on its own. Thaw notices are told apart by the key that checks them, so a caller that sends notices needs its own peer entry (Cross-app thaw).

A key is trusted both ways. One list of every peer’s keys checks all inbound calls, so listing a peer’s public key, even only to take its thaw notices, also lets that peer call your exposed pages and widgets as any visitor it names (#2311). List only apps you’d trust to vouch for your visitors.

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
import { cmsRouter } from '$lib/server/router';

// new first, old kept; a variable that isn't set is left out
const shellKeys = [
  env.SHELL_2026Q4_PUBLIC_KEY,
  env.SHELL_PUBLIC_KEY
].filter((k) => k !== undefined);

federate({
  name: 'cms',
  key: env.CMS_SIGNING_KEY,
  audience: 'cms.internal',
  peers: {
    shell: {
      origin: 'https://www.example.com',
      key: shellKeys
    }
  },
  expose: cmsRouter,
  base: '/cms'
});

Open endpoints

open: true turns the signature check off for expose and widgets, for an app that sits behind mTLS or a service mesh that already checks who calls. Anyone who can reach the endpoint can then call it, so use it only behind such a check. Nothing proves who’s calling, so an open app ignores every claim a caller sends, even a signed one: user(c) and info.user are always undefined, and c.visitor falls back to your table’s own visitor.

/og/thaw stays signed on an open app, since a notice clears pages. Without open, an app that exposes something and lists no peer key refuses every call (Fail closed).