From 38f74f566f48f84d73f2a8a4e275da1fab4e47e8 Mon Sep 17 00:00:00 2001 From: Michiel de Jong Date: Fri, 25 Sep 2026 16:05:25 +0200 Subject: [PATCH] Require v2 signatures on state-changing endpoints, each proof once (#1700) Piece 6 and answer 7 on #1700. A v1 signature covers only "{url} {timestamp}", and a session cookie is a reusable v1 proof, so whoever captured either could send another body to the same endpoint for five minutes; a v2 proof could be replayed byte for byte in that window. Server: - `require_v2` middleware (server/src/require_v2.rs), wrapped on each route below. On POST/DELETE it refuses anything but `x-atomic-signature-version: 2` (v1, bearer tokens, cookies: 401 with REQUIRES_V2), reads the raw body, checks the signature over method, full URL and exactly those bytes, then hands the same bytes to the handler, so handlers keep their `web::Json` extractors and the hash always matches what is parsed. A body with Content-Encoding is refused. The verified agent goes into the request extensions; `helpers::get_client_agent_of` and `get_client_agent_for_request` return it. GET/HEAD/OPTIONS pass through. Refusals are rendered as responses, so CORS still applies. - Routes: /app-agent, /app-write, /plugin-view-token, /plugin-secret, /plugin-run, /plugin-schedule, /plugin-resume, /plugin-auto-apply, /plugin-trigger, /plugin-release, /plugin-release-package, /plugin-release-pin, /plugin-sync-{preview,apply,schedule,status}, /plugin-connection-{state,checkpoint}, /plugin-external-{read,status,confirm,apply}, the 13 /integration-action* routes, /bind-drive, /forget-peer, /iroh-sync, and the /website-hosting POSTs. Not /commit, PUT /blob, /upload, or POST on other resources. - `ReplayCache` (server/src/replay_cache.rs), one per node in AppState: keyed on the SHA-256 of the decoded signature bytes, each entry kept until timestamp + AUTH_MAX_AGE_MS (the last moment the proof is fresh), pruned in expiry order. Bounded at 100,000; when full of live entries it refuses (429) rather than evict one, which would make it replayable. Browser: `@tomic/lib` gets `signedRequestInit(url, agent, {method, body, headers})`. Every data-browser and @tomic/lib caller of these routes signs its writes with it: plugin-connection, integration-actions, plugin-server, plugin-install (release package), app agent/write/view token, plugin schedule/secret/trigger/automation, website hosting and assets, forget-peer, iroh-sync. Reads keep v1. Tests: replay_cache unit tests (replay refused, forgotten after the window, clock skew, full cache); app_endpoints_test sends every listed route v1, a cookie and v2 through the real router; a captured v2 request replayed is refused; GET keeps v1. Existing tests moved to v2 (app_endpoints_test, website, it/iroh_pairing). vitest for signedRequestInit and the touched callers. Docs in authentication.md. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 16 + TESTING_COVERAGE.md | 16 + browser/CHANGELOG.md | 1 + .../src/chunks/AppPage/AppFrame.tsx | 20 +- .../src/chunks/AppPage/appAgent.ts | 23 +- .../src/chunks/AppPage/hostStore.test.ts | 14 +- .../src/chunks/AppPage/hostStore.ts | 17 +- .../PluginRuns/CreateEventAutomation.tsx | 30 +- .../src/chunks/PluginRuns/PluginSchedule.tsx | 31 +- .../src/chunks/PluginRuns/PluginSecrets.tsx | 25 +- .../src/chunks/PluginRuns/PluginTrigger.tsx | 36 +- .../src/chunks/PluginRuns/runScript.ts | 24 +- .../src/chunks/Website/hostingClient.test.ts | 22 +- .../src/chunks/Website/hostingClient.ts | 28 +- .../src/chunks/Website/websiteAssets.ts | 23 +- .../src/helpers/managedServer.test.ts | 17 + .../data-browser/src/helpers/managedServer.ts | 14 +- .../data-browser/src/helpers/pairing.test.ts | 4 + browser/data-browser/src/helpers/pairing.ts | 20 +- browser/data-browser/src/routes/SyncRoute.tsx | 22 +- browser/lib/src/authentication-v2.test.ts | 50 ++ browser/lib/src/authentication.ts | 35 ++ browser/lib/src/integration-actions.test.ts | 16 +- browser/lib/src/integration-actions.ts | 18 +- browser/lib/src/plugin-connection.test.ts | 14 +- browser/lib/src/plugin-connection.ts | 18 +- browser/lib/src/plugin-install.test.ts | 2 + browser/lib/src/plugin-install.ts | 18 +- browser/lib/src/plugin-server.test.ts | 17 +- browser/lib/src/plugin-server.ts | 21 +- docs/src/authentication.md | 16 +- server/src/appstate.rs | 4 + server/src/bin.rs | 2 + server/src/handlers/app_endpoints_test.rs | 495 +++++++++++++++--- server/src/handlers/app_write.rs | 4 +- server/src/handlers/forget_peer.rs | 4 +- server/src/handlers/plugin_run.rs | 5 +- server/src/handlers/plugin_schedule.rs | 4 +- server/src/handlers/plugin_secret.rs | 4 +- server/src/handlers/plugin_trigger.rs | 4 +- server/src/handlers/post_resource.rs | 4 +- server/src/handlers/website.rs | 77 +-- server/src/helpers.rs | 24 + server/src/lib.rs | 2 + server/src/replay_cache.rs | 195 +++++++ server/src/require_v2.rs | 159 ++++++ server/src/routes.rs | 50 +- server/tests/it/iroh_pairing.rs | 68 ++- 48 files changed, 1410 insertions(+), 323 deletions(-) create mode 100644 server/src/replay_cache.rs create mode 100644 server/src/require_v2.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index c9f1a4233f..d8e3d82648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,22 @@ See [STATUS.md](server/STATUS.md) to learn more about which features will remain ## UNRELEASED +- State-changing endpoints require a version 2 request signature, and accept + each one once (#1700, piece 6 and answer 7). `POST`/`DELETE` on + `/app-agent`, `/app-write`, `/plugin-view-token`, `/plugin-secret`, + `/plugin-run`, `/plugin-schedule`, `/plugin-resume`, `/plugin-auto-apply`, + `/plugin-trigger`, the `/plugin-release*`, `/plugin-sync-*`, + `/plugin-connection-*` and `/plugin-external-*` routes, the 13 + `/integration-action*` routes, `/bind-drive`, `/forget-peer`, `/iroh-sync` + and the `/website-hosting` writes refuse version 1 signatures, bearer + tokens and session cookies with a `401`. A middleware reads the body before + the handler, checks the signature over exactly those bytes, and hands them + on. An in-memory replay cache per node remembers every accepted v2 + signature (by the SHA-256 of its bytes) until it can no longer be fresh, + and refuses a second use; it holds at most 100,000 and answers `429` when + full rather than forget a live one. `GET` on the same paths still takes + version 1. `/commit`, `PUT /blob`, `/upload` and `POST` on other resources + are unchanged. See docs/src/authentication.md. - wasip2 class extenders do not reach the integration proxy (#1700, answer 5). They were already refused, because the host has no installation agent to sign as for them; the refusal now comes first, whether the extender uses diff --git a/TESTING_COVERAGE.md b/TESTING_COVERAGE.md index e00009b792..9a17bf3974 100644 --- a/TESTING_COVERAGE.md +++ b/TESTING_COVERAGE.md @@ -256,6 +256,22 @@ A wasip2 class extender is refused at the integration proxy, by URL and by `atomic-proxy:`, even when its manifest lists the proxy origin (#1700, answer 5): `host_core::tests::a_class_extender_is_refused_at_the_proxy_and_told_why`. +Version 2 required on state-changing endpoints, with replay protection +(#1700, piece 6 and answer 7): `app_endpoints_test::state_changing_routes_refuse_v1_and_cookies_and_accept_v2` +sends every route on the list (including all 13 `/integration-action*` +routes and the `DELETE`s) a v1 signature, a session cookie and a v2 +signature through the real router and middleware; `a_replayed_v2_request_is_refused` +replays a captured v2 request byte for byte; `reads_on_v2_routes_still_accept_v1` +covers `GET`. `replay_cache::tests` cover the window edges, a proof signed +ahead of the server clock, and a full cache. `server/tests/it/iroh_pairing.rs` +posts `/iroh-sync` with v2 between two real servers. The browser callers are +covered with mocked signing (`plugin-server`, `plugin-install`, +`plugin-connection`, `integration-actions`, `hostingClient`, `hostStore`, +`managedServer`, `pairing`), and `authentication-v2.test.ts` verifies what +`signedRequestInit` produces. Not covered: a real browser against a real +server for these writes (the Playwright suite exercises some of them +end-to-end), and a restart forgetting the cache. + Issues view: `TablePage/Issues/issueStatus.test.ts` covers reading open/closed status tags and booleans, picking close/reopen targets, and title/`#number` filtering; `browser/e2e/tests/issues-view.spec.ts` covers the Issues view for diff --git a/browser/CHANGELOG.md b/browser/CHANGELOG.md index 318307ee61..821c911e1f 100644 --- a/browser/CHANGELOG.md +++ b/browser/CHANGELOG.md @@ -4,6 +4,7 @@ This changelog covers all five packages, as they are (for now) updated as a whol ## UNRELEASED +- `@tomic/lib`: `signedRequestInit(url, agent, { method, body, headers })` returns the `fetch` options for a version 2 signed request, over exactly the body it sends. AtomicServer now requires version 2 on its state-changing endpoints and accepts each signature once (#1700), so the data browser and `@tomic/lib` sign every write to them with it: `/app-agent`, `/app-write`, `/plugin-view-token`, `/plugin-secret`, `/plugin-run`, `/plugin-schedule`, `/plugin-resume`, `/plugin-auto-apply`, `/plugin-trigger`, `/plugin-release*`, `/plugin-sync-*`, `/plugin-connection-*`, `/plugin-external-*`, `/integration-action*`, `/forget-peer`, `/iroh-sync` and the `/website-hosting` writes. Reads on those paths still sign version 1. - `@tomic/lib`: `signRequest(url, agent, headers, { method, body })` makes a version 2 request signature (ontola/atomic-plugins#54), which also covers the method and the SHA-256 of the body and sends `x-atomic-signature-version: 2`. Without `method` it signs version 1 as before, and a string fourth argument is still the legacy subject. New exports `requestSignatureMessageV2`, `sha256Hex` and `SIGNATURE_VERSION_HEADER`. `Agent.generateNonExtractable()` makes an agent whose Ed25519 key is a non-extractable WebCrypto key, with subject `atomic:agent:`; where WebCrypto has no Ed25519 it throws instead of falling back to an extractable key. - Plugins moved to [atomic-plugins](https://github.com/ontola/atomic-plugins). The Integrations page lists what that catalog publishes (`catalog.json`, fetched at runtime; the URL is configurable under Settings > Integration) instead of plugins compiled into the app and server. The bundled GitHub issues, Notion, Pets, MT940/camt.053, Clockify and Google Calendar (Devonian) integrations, the `devonian` dependency and the `/app/devonian-demo` page are removed. The Todoist lens and the Moneybird administration picker are removed too. LocalThought connect and sync, and the "Show API plugins" toggle, are removed from the app: plugins will run in their own iframe and make proxy calls through the host. - Tracker tables get an Issues view: open and closed issues split by the status column (or a boolean column), filtering by title or `#number`, New issue, and Close/Reopen per row. The Issue Tracker template opens it as a tab. diff --git a/browser/data-browser/src/chunks/AppPage/AppFrame.tsx b/browser/data-browser/src/chunks/AppPage/AppFrame.tsx index ffee449358..13fbbd7400 100644 --- a/browser/data-browser/src/chunks/AppPage/AppFrame.tsx +++ b/browser/data-browser/src/chunks/AppPage/AppFrame.tsx @@ -2,7 +2,11 @@ import { isViewRequest } from '@tomic/plugin'; import { viewSession } from '@helpers/extensions/viewSession'; import { useEffect, useRef, useState } from 'react'; import { styled } from 'styled-components'; -import { errorMessageFromResponse, signRequest, useStore } from '@tomic/react'; +import { + errorMessageFromResponse, + signedRequestInit, + useStore, +} from '@tomic/react'; import { findSchema, pluginSchema } from '@tomic/lib'; import { FrameBridge } from '@helpers/extensions/FrameBridge'; import { handleRequest, isHostRequest, type HostReply } from './hostStore'; @@ -455,12 +459,14 @@ async function mintViewToken( const url = `${store.getServerUrl()}/plugin-view-token`; try { - const headers = await signRequest(url, agent, {}); - const response = await fetch(url, { - method: 'POST', - headers: { ...headers, 'Content-Type': 'application/json' }, - body: JSON.stringify({ drive, plugin }), - }); + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ drive, plugin }), + }), + ); if (!response.ok) { return { diff --git a/browser/data-browser/src/chunks/AppPage/appAgent.ts b/browser/data-browser/src/chunks/AppPage/appAgent.ts index 6f5861c1f8..2a735eb6f4 100644 --- a/browser/data-browser/src/chunks/AppPage/appAgent.ts +++ b/browser/data-browser/src/chunks/AppPage/appAgent.ts @@ -1,6 +1,6 @@ import { errorMessageFromResponse, - signRequest, + signedRequestInit, type Store, } from '@tomic/react'; @@ -25,17 +25,18 @@ export async function handOverAppKey( if (!agent) throw new Error('Sign in to give an app its key'); const url = `${store.getServerUrl()}/app-agent`; - const headers = await signRequest(url, agent, {}); - - const response = await fetch(url, { - method: 'POST', - headers: { ...headers, 'Content-Type': 'application/json' }, - body: JSON.stringify({ - drive: options.drive, - app: options.app, - secret: options.secret, + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + drive: options.drive, + app: options.app, + secret: options.secret, + }), }), - }); + ); if (!response.ok) { throw new Error( diff --git a/browser/data-browser/src/chunks/AppPage/hostStore.test.ts b/browser/data-browser/src/chunks/AppPage/hostStore.test.ts index 1e3fe57570..c440bcf11a 100644 --- a/browser/data-browser/src/chunks/AppPage/hostStore.test.ts +++ b/browser/data-browser/src/chunks/AppPage/hostStore.test.ts @@ -9,7 +9,19 @@ vi.mock('@tomic/react', async () => { // Signing needs a real key and a real agent; what these tests are about is // which requests leave and which are refused before they do. - return { ...actual, signRequest: async () => ({}) }; + return { + ...actual, + signRequest: async () => ({}), + signedRequestInit: async ( + _url: string, + _agent: unknown, + request: { method: string; headers?: object; body?: string }, + ) => ({ + method: request.method, + headers: { ...request.headers }, + body: request.body, + }), + }; }); const APP = 'did:ad:app'; diff --git a/browser/data-browser/src/chunks/AppPage/hostStore.ts b/browser/data-browser/src/chunks/AppPage/hostStore.ts index ccaa0cf91a..fbf054da7a 100644 --- a/browser/data-browser/src/chunks/AppPage/hostStore.ts +++ b/browser/data-browser/src/chunks/AppPage/hostStore.ts @@ -7,7 +7,7 @@ import { errorMessageFromResponse, findSchema, pluginSchema, - signRequest, + signedRequestInit, } from '@tomic/react'; /** @@ -243,13 +243,14 @@ async function writeAsApp( if (!agent) throw new Error('Sign in to use this app'); const url = `${store.getServerUrl()}/app-write`; - const headers = await signRequest(url, agent, {}); - - const response = await fetch(url, { - method: 'POST', - headers: { ...headers, 'Content-Type': 'application/json' }, - body: JSON.stringify({ drive, app, ...request }), - }); + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ drive, app, ...request }), + }), + ); if (!response.ok) { throw new Error( diff --git a/browser/data-browser/src/chunks/PluginRuns/CreateEventAutomation.tsx b/browser/data-browser/src/chunks/PluginRuns/CreateEventAutomation.tsx index a49ca68ed5..49d8ee3b9b 100644 --- a/browser/data-browser/src/chunks/PluginRuns/CreateEventAutomation.tsx +++ b/browser/data-browser/src/chunks/PluginRuns/CreateEventAutomation.tsx @@ -1,7 +1,7 @@ import { useState } from 'react'; import { dataBrowser, - signRequest, + signedRequestInit, useStore, ensureSchema, pluginSchema, @@ -66,21 +66,21 @@ export function CreateEventAutomation({ await script.set(dataBrowser.properties.emoji, '⚡'); await script.save(); const url = `${store.getServerUrl()}/plugin-trigger`; - const response = await fetch(url, { - method: 'POST', - headers: { - ...(await signRequest(url, store.getAgent()!, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - drive, - plugin: subject, - filters: selected.filters, - onEnter: true, - onLeave: false, - autoApply: false, + const response = await fetch( + url, + await signedRequestInit(url, store.getAgent()!, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + drive, + plugin: subject, + filters: selected.filters, + onEnter: true, + onLeave: false, + autoApply: false, + }), }), - }); + ); if (!response.ok) throw new Error(await response.text()); navigate(constructOpenURL(subject)); if (withAssistant) diff --git a/browser/data-browser/src/chunks/PluginRuns/PluginSchedule.tsx b/browser/data-browser/src/chunks/PluginRuns/PluginSchedule.tsx index 4fa2d1944d..27262ee9c0 100644 --- a/browser/data-browser/src/chunks/PluginRuns/PluginSchedule.tsx +++ b/browser/data-browser/src/chunks/PluginRuns/PluginSchedule.tsx @@ -2,7 +2,12 @@ import { useCallback, useEffect, useState } from 'react'; import { styled } from 'styled-components'; import toast from 'react-hot-toast'; import { FaClock } from 'react-icons/fa6'; -import { errorMessageFromResponse, signRequest, useStore } from '@tomic/react'; +import { + errorMessageFromResponse, + signedRequestInit, + signRequest, + useStore, +} from '@tomic/react'; import { setPluginSchedule } from './runScript'; import { Button } from '@components/Button'; import { Column, Row } from '@components/Row'; @@ -276,11 +281,25 @@ async function request( if (!agent) return { ok: false, error: 'Not signed in' }; try { - const headers = await signRequest(url, agent, {}); - const response = await fetch(url, { - ...init, - headers: { ...headers, 'Content-Type': 'application/json' }, - }); + const method = (init.method ?? 'GET').toUpperCase(); + // Reads sign with version 1; every write is a v2-only route, signed over + // the method, URL and exactly this body. + const response = await fetch( + url, + method === 'GET' + ? { + ...init, + headers: { + ...(await signRequest(url, agent, {})), + 'Content-Type': 'application/json', + }, + } + : await signedRequestInit(url, agent, { + method, + headers: { 'Content-Type': 'application/json' }, + body: init.body as string | undefined, + }), + ); if (!response.ok) { return { diff --git a/browser/data-browser/src/chunks/PluginRuns/PluginSecrets.tsx b/browser/data-browser/src/chunks/PluginRuns/PluginSecrets.tsx index fe2a390749..6cb5045ca9 100644 --- a/browser/data-browser/src/chunks/PluginRuns/PluginSecrets.tsx +++ b/browser/data-browser/src/chunks/PluginRuns/PluginSecrets.tsx @@ -4,6 +4,7 @@ import toast from 'react-hot-toast'; import { FaCheck, FaKey, FaTrash } from 'react-icons/fa6'; import { errorMessageFromResponse, + signedRequestInit, signRequest, useStore, type DeclaredSecret, @@ -422,11 +423,25 @@ async function request( if (!agent) return { ok: false, error: 'Not signed in' }; try { - const headers = await signRequest(url, agent, {}); - const response = await fetch(url, { - ...init, - headers: { ...headers, 'Content-Type': 'application/json' }, - }); + const method = (init.method ?? 'GET').toUpperCase(); + // Reads sign with version 1; every write is a v2-only route, signed over + // the method, URL and exactly this body. + const response = await fetch( + url, + method === 'GET' + ? { + ...init, + headers: { + ...(await signRequest(url, agent, {})), + 'Content-Type': 'application/json', + }, + } + : await signedRequestInit(url, agent, { + method, + headers: { 'Content-Type': 'application/json' }, + body: init.body as string | undefined, + }), + ); if (!response.ok) { return { diff --git a/browser/data-browser/src/chunks/PluginRuns/PluginTrigger.tsx b/browser/data-browser/src/chunks/PluginRuns/PluginTrigger.tsx index a63cf0ab89..ae4bd4bd48 100644 --- a/browser/data-browser/src/chunks/PluginRuns/PluginTrigger.tsx +++ b/browser/data-browser/src/chunks/PluginRuns/PluginTrigger.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; -import { signRequest, useStore } from '@tomic/react'; +import { signedRequestInit, signRequest, useStore } from '@tomic/react'; import { Button } from '@components/Button'; import { Column, Row } from '@components/Row'; import { Card } from '@components/Card'; @@ -59,24 +59,24 @@ export function PluginTrigger({ setError(undefined); try { - const response = await fetch(endpoint, { - method: 'POST', - headers: { - ...(await signRequest(endpoint, store.getAgent()!, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - drive, - plugin, - filters: trigger.query.filters.map(f => ({ - property: f.property, - value: f.value, - })), - onEnter: trigger.onEnter, - onLeave: trigger.onLeave, - autoApply: enabled, + const response = await fetch( + endpoint, + await signedRequestInit(endpoint, store.getAgent()!, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + drive, + plugin, + filters: trigger.query.filters.map(f => ({ + property: f.property, + value: f.value, + })), + onEnter: trigger.onEnter, + onLeave: trigger.onLeave, + autoApply: enabled, + }), }), - }); + ); if (!response.ok) throw new Error(await response.text()); setTrigger(await response.json()); } catch (e) { diff --git a/browser/data-browser/src/chunks/PluginRuns/runScript.ts b/browser/data-browser/src/chunks/PluginRuns/runScript.ts index 239c2b5144..d82d94de92 100644 --- a/browser/data-browser/src/chunks/PluginRuns/runScript.ts +++ b/browser/data-browser/src/chunks/PluginRuns/runScript.ts @@ -29,7 +29,7 @@ import { // only the entry file, leaving its relative imports missing in production. import PluginWorker from '@tomic/lib/plugin-run.worker.js?worker'; import { useEffect, useState } from 'react'; -import { signRequest } from '@tomic/react'; +import { signedRequestInit, signRequest } from '@tomic/react'; /** * Running a plugin and applying what it proposed are separate calls on purpose. @@ -629,18 +629,18 @@ export async function setPluginSchedule( if (!agent) throw new Error('Not signed in'); const url = `${store.getServerUrl()}/plugin-schedule`; - const response = await fetch(url, { - method: 'POST', - headers: { - ...(await signRequest(url, agent, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - drive: target.drive, - plugin: target.plugin, - intervalSeconds, + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + drive: target.drive, + plugin: target.plugin, + intervalSeconds, + }), }), - }); + ); if (!response.ok) { throw new Error( diff --git a/browser/data-browser/src/chunks/Website/hostingClient.test.ts b/browser/data-browser/src/chunks/Website/hostingClient.test.ts index 7e671ece9c..e2597dea32 100644 --- a/browser/data-browser/src/chunks/Website/hostingClient.test.ts +++ b/browser/data-browser/src/chunks/Website/hostingClient.test.ts @@ -1,8 +1,19 @@ import { describe, it, expect, vi, afterEach } from 'vitest'; import { hostingRequest, sameWebsiteOutput } from './hostingClient'; -import { signRequest, type Store } from '@tomic/lib'; +import { signedRequestInit, type Store } from '@tomic/lib'; vi.mock('@tomic/lib', () => ({ signRequest: vi.fn(async () => ({ 'x-atomic-signature': 'proof' })), + signedRequestInit: vi.fn( + async ( + _url: string, + _agent: unknown, + request: { method: string; headers?: object; body?: string }, + ) => ({ + method: request.method, + headers: { ...request.headers, 'x-atomic-signature': 'proof' }, + body: request.body, + }), + ), errorMessageFromResponse: (body: string) => body, })); afterEach(() => { @@ -28,10 +39,15 @@ describe('hosting control requests', () => { const [url, options] = fetch.mock.calls[0] as [URL, RequestInit]; expect(url.origin).toBe('https://atomic.example'); expect(url.searchParams.get('drive')).toBe('did:ad:drive'); - expect(signRequest).toHaveBeenCalledWith( + // A POST here requires a version 2 signature over exactly the body sent. + expect(signedRequestInit).toHaveBeenCalledWith( url.toString(), store.getAgent(), - {}, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: options.body, + }, ); expect(options.credentials).toBe('omit'); expect(options.redirect).toBe('error'); diff --git a/browser/data-browser/src/chunks/Website/hostingClient.ts b/browser/data-browser/src/chunks/Website/hostingClient.ts index cd630e94fc..4e2c745e65 100644 --- a/browser/data-browser/src/chunks/Website/hostingClient.ts +++ b/browser/data-browser/src/chunks/Website/hostingClient.ts @@ -1,5 +1,10 @@ // @wc-ignore-file -import { signRequest, errorMessageFromResponse, type Store } from '@tomic/lib'; +import { + signRequest, + signedRequestInit, + errorMessageFromResponse, + type Store, +} from '@tomic/lib'; export interface WebsitePackage { version: 1; @@ -33,13 +38,26 @@ export async function hostingRequest( const url = new URL(`/website-hosting${path}`, store.getServerUrl()); url.searchParams.set('project', project); url.searchParams.set('drive', drive); - const headers = await signRequest(url.toString(), agent, {}); + // Reads sign with version 1; a POST here requires version 2, over exactly + // the JSON sent. + const signed = + body === undefined + ? { + method: 'GET', + headers: { + ...(await signRequest(url.toString(), agent, {})), + 'Content-Type': 'application/json', + }, + } + : await signedRequestInit(url.toString(), agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }); const result = await fetch(url, { - method: body === undefined ? 'GET' : 'POST', - headers: { ...headers, 'Content-Type': 'application/json' }, + ...signed, credentials: 'omit', redirect: 'error', - body: body === undefined ? undefined : JSON.stringify(body), }); if (!result.ok) throw new Error( diff --git a/browser/data-browser/src/chunks/Website/websiteAssets.ts b/browser/data-browser/src/chunks/Website/websiteAssets.ts index 3e66aee82a..3c41e1ba1a 100644 --- a/browser/data-browser/src/chunks/Website/websiteAssets.ts +++ b/browser/data-browser/src/chunks/Website/websiteAssets.ts @@ -1,5 +1,10 @@ // @wc-ignore-file -import { hexToBytes, signRequest, type Store } from '@tomic/lib'; +import { + hexToBytes, + signedRequestInit, + signRequest, + type Store, +} from '@tomic/lib'; export interface WebsiteAsset { hash: string; @@ -35,13 +40,21 @@ async function assetRequest( const url = new URL(`/website-hosting/assets/${hash}`, store.getServerUrl()); url.searchParams.set('project', project); url.searchParams.set('drive', drive); - const headers = await signRequest(url.toString(), agent, {}); + // A read signs with version 1; an upload requires version 2, over exactly + // these bytes. + const signed = body + ? await signedRequestInit(url.toString(), agent, { + method: 'POST', + body: new Uint8Array(await body.arrayBuffer()), + }) + : { + method: 'GET', + headers: await signRequest(url.toString(), agent, {}), + }; const response = await fetch(url, { - method: body ? 'POST' : 'GET', - headers, + ...signed, credentials: 'omit', redirect: 'error', - body, }); if (!response.ok) throw new Error( diff --git a/browser/data-browser/src/helpers/managedServer.test.ts b/browser/data-browser/src/helpers/managedServer.test.ts index 2e7a848909..d68bdf4eeb 100644 --- a/browser/data-browser/src/helpers/managedServer.test.ts +++ b/browser/data-browser/src/helpers/managedServer.test.ts @@ -12,6 +12,21 @@ import { // URL to verify the signature. vi.mock('@tomic/react', () => ({ signRequest: vi.fn(async (url: string) => ({ 'x-atomic-signed-url': url })), + signedRequestInit: vi.fn( + async ( + url: string, + _agent: unknown, + request: { method: string; headers?: object; body?: string }, + ) => ({ + method: request.method, + headers: { + ...request.headers, + 'x-atomic-signed-url': url, + 'x-atomic-signature-version': '2', + }, + body: request.body, + }), + ), })); describe('accountCreationTarget', () => { @@ -159,6 +174,8 @@ describe('forgetServerPeer', () => { // Signed over the full URL including the query string, or the server's // rebuild will not match. expect(init.headers['x-atomic-signed-url']).toBe(url); + // `/forget-peer` changes state, so it takes only version 2. + expect(init.headers['x-atomic-signature-version']).toBe('2'); }); it('reports failure rather than success when the server refuses', async () => { diff --git a/browser/data-browser/src/helpers/managedServer.ts b/browser/data-browser/src/helpers/managedServer.ts index f3dbecc316..ca11242599 100644 --- a/browser/data-browser/src/helpers/managedServer.ts +++ b/browser/data-browser/src/helpers/managedServer.ts @@ -1,4 +1,4 @@ -import { signRequest, type Agent } from '@tomic/react'; +import { signedRequestInit, signRequest, type Agent } from '@tomic/react'; import { serverProps, peerProps } from './serverOntology'; import { rememberManagedPortalUrl, safePortalUrl } from './managed/api'; import { isRunningInTauri } from './tauri'; @@ -307,10 +307,14 @@ export async function forgetServerPeer( try { // Sign the exact URL being fetched (path + query), same scheme as // fetchNodeDriveUsage — the server rebuilds and verifies it. - const headers = await signRequest(url.toString(), agent, { - Accept: 'application/json', - }); - const res = await fetch(url.toString(), { method: 'POST', headers }); + // A state-changing route: version 2 only, over the (empty) body. + const res = await fetch( + url.toString(), + await signedRequestInit(url.toString(), agent, { + method: 'POST', + headers: { Accept: 'application/json' }, + }), + ); return res.ok; } catch { diff --git a/browser/data-browser/src/helpers/pairing.test.ts b/browser/data-browser/src/helpers/pairing.test.ts index 9da72609d8..069f28616f 100644 --- a/browser/data-browser/src/helpers/pairing.test.ts +++ b/browser/data-browser/src/helpers/pairing.test.ts @@ -23,6 +23,7 @@ const AGENT = { subject: 'did:ad:agent:abc', getPublicKey: async () => 'pk', createSignature: async () => 'sig', + sign: async () => 'sig', } as unknown as Agent; const AUTH_HEADERS = [ @@ -106,6 +107,9 @@ describe('pairAndSync', () => { } expect(init.headers['x-atomic-agent']).toBe(AGENT.subject); + // `/iroh-sync` changes state, so it takes only version 2. + expect(init.headers['x-atomic-signature-version']).toBe('2'); + expect(init.method).toBe('POST'); }); it('goes out unsigned without an agent, so the node’s refusal is what shows', async () => { diff --git a/browser/data-browser/src/helpers/pairing.ts b/browser/data-browser/src/helpers/pairing.ts index 76b103cd2d..0071d492ed 100644 --- a/browser/data-browser/src/helpers/pairing.ts +++ b/browser/data-browser/src/helpers/pairing.ts @@ -10,7 +10,7 @@ import { decodePairingEnvelope, PairingEnvelopeError, - signRequest, + signedRequestInit, type Agent, } from '@tomic/lib'; import { upsertKnownPeer } from './knownPeers'; @@ -52,15 +52,17 @@ export async function pairAndSync( // server, inside the desktop/mobile webview. const url = `${getLocalServerOrigin()}/iroh-sync`; const baseHeaders = { 'Content-Type': 'application/json' }; - const headers = agent - ? await signRequest(url, agent, baseHeaders) - : baseHeaders; + const body = JSON.stringify({ nodeId: nodeDid, drive }); + // `/iroh-sync` takes only a version 2 signature, over exactly this body. + const init: RequestInit = agent + ? await signedRequestInit(url, agent, { + method: 'POST', + headers: baseHeaders, + body, + }) + : { method: 'POST', headers: baseHeaders, body }; - const response = await fetch(url, { - method: 'POST', - headers, - body: JSON.stringify({ nodeId: nodeDid, drive }), - }); + const response = await fetch(url, init); // A refusal (401/403) may not carry JSON; still say what happened. const data = await response.json().catch(() => ({ error: `${response.status} ${response.statusText}`.trim(), diff --git a/browser/data-browser/src/routes/SyncRoute.tsx b/browser/data-browser/src/routes/SyncRoute.tsx index 274a5f5379..2d6e29d439 100644 --- a/browser/data-browser/src/routes/SyncRoute.tsx +++ b/browser/data-browser/src/routes/SyncRoute.tsx @@ -121,7 +121,7 @@ import { decodePairingEnvelope, PairingEnvelopeError, PAIRING_URI_PREFIX, - signRequest, + signedRequestInit, } from '@tomic/lib'; import { isClientDbEnabled, setClientDbEnabled } from '../helpers/clientDbMode'; import { PRODUCT_NAME } from '../helpers/managed/product'; @@ -1455,14 +1455,18 @@ function SyncPage() { try { const syncUrl = `${getLocalServerOrigin()}/iroh-sync`; - const headers = await signRequest(syncUrl, agent, { - 'Content-Type': 'application/json', - }); - const res = await fetch(syncUrl, { - method: 'POST', - headers, - body: JSON.stringify({ nodeId: canonicalNodeDid, drive: status.drive }), - }); + // `/iroh-sync` takes only a version 2 signature, over exactly this body. + const res = await fetch( + syncUrl, + await signedRequestInit(syncUrl, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + nodeId: canonicalNodeDid, + drive: status.drive, + }), + }), + ); // A refusal (401/403) may not carry JSON; still say what happened. const data = await res .json() diff --git a/browser/lib/src/authentication-v2.test.ts b/browser/lib/src/authentication-v2.test.ts index 3ed61e7840..99f98406e9 100644 --- a/browser/lib/src/authentication-v2.test.ts +++ b/browser/lib/src/authentication-v2.test.ts @@ -4,6 +4,7 @@ import { Agent } from './agent.js'; import { requestSignatureMessageV2, sha256Hex, + signedRequestInit, signRequest, } from './authentication.js'; import { decodeB64 } from './base64.js'; @@ -220,6 +221,55 @@ describe('version 2 request signatures', () => { expect(headers['x-atomic-signature']).toBe(v.signature); }); + it('builds fetch options that send exactly what was signed', async ({ + expect, + }) => { + const agent = await Agent.generateNonExtractable(); + const publicKey = await agent.getPublicKey(); + const url = 'https://server.example/plugin-secret?drive=d'; + const body = JSON.stringify({ drive: 'd', plugin: 'p', name: 'n' }); + const init = await signedRequestInit(url, agent, { + method: 'POST', + body, + headers: { 'Content-Type': 'application/json' }, + }); + + expect(init.method).toBe('POST'); + expect(init.body).toBe(body); + expect(init.headers['Content-Type']).toBe('application/json'); + expect(init.headers['x-atomic-signature-version']).toBe('2'); + const timestamp = Number(init.headers['x-atomic-timestamp']); + expect( + await verifies( + publicKey, + requestSignatureMessageV2('POST', url, timestamp, sha256Hex(body)), + init.headers['x-atomic-signature'], + ), + ).toBe(true); + }); + + it('signs a request without a body over the empty body', async ({ + expect, + }) => { + const agent = await Agent.generateNonExtractable(); + const url = 'https://server.example/plugin-secret?drive=d&name=n'; + const init = await signedRequestInit(url, agent, { method: 'DELETE' }); + + expect('body' in init).toBe(false); + expect( + await verifies( + await agent.getPublicKey(), + requestSignatureMessageV2( + 'DELETE', + url, + Number(init.headers['x-atomic-timestamp']), + sha256Hex(undefined), + ), + init.headers['x-atomic-signature'], + ), + ).toBe(true); + }); + it('leaves version 1 unchanged', async ({ expect }) => { const v = vectors[0]; const agent = new Agent(new JSCryptoProvider(v.private_key), v.agent); diff --git a/browser/lib/src/authentication.ts b/browser/lib/src/authentication.ts index 6f9b153d0c..d5817c87ca 100644 --- a/browser/lib/src/authentication.ts +++ b/browser/lib/src/authentication.ts @@ -214,6 +214,41 @@ export async function signRequest( return newHeaders; } +/** A request body {@link signedRequestInit} can hash and send as it is. */ +export type SignedRequestBody = string | Uint8Array | ArrayBuffer; + +/** + * The `fetch` options for a state-changing request to an AtomicServer + * endpoint, signed with version 2 over the method, the full `url` and exactly + * the `body` that is sent. Those endpoints (`/plugin-run`, `/app-write`, + * `/plugin-secret`, and the others listed in `docs/src/authentication.md`) + * refuse a version 1 signature and a session cookie, and accept each + * signature once, so sign every request anew rather than reusing the result. + * + * Pass the body as the string or bytes to send, not an object: it is hashed + * as given. + */ +export async function signedRequestInit( + url: string, + agent: Agent, + request: { + method: string; + body?: SignedRequestBody; + headers?: HeadersObject; + }, +): Promise { + const headers = await signRequest(url, agent, request.headers ?? {}, { + method: request.method, + body: request.body, + }); + + return { + method: request.method, + headers, + ...(request.body === undefined ? {} : { body: request.body as BodyInit }), + }; +} + /** * How long a signed authentication proof stays valid, mirroring the server's * `AUTH_MAX_AGE_MS` in `lib/src/authentication.rs`. Before 0.41 a proof never diff --git a/browser/lib/src/integration-actions.test.ts b/browser/lib/src/integration-actions.test.ts index 84e8ddafe0..50ef8d60ac 100644 --- a/browser/lib/src/integration-actions.test.ts +++ b/browser/lib/src/integration-actions.test.ts @@ -10,7 +10,19 @@ import { type ActionStore, } from './integration-actions.js'; vi.mock('./authentication.js', () => ({ - signRequest: async () => ({ 'x-test-signature': 'signed' }), + signedRequestInit: async ( + _url: string, + _agent: unknown, + request: { method: string; body?: string; headers?: object }, + ) => ({ + method: request.method, + headers: { + ...request.headers, + 'x-test-signature': 'signed', + 'x-atomic-signature-version': '2', + }, + body: request.body, + }), })); afterEach(() => vi.unstubAllGlobals()); it('MCP discovery and calls use the signed host API and never approve writes', async () => { @@ -34,6 +46,8 @@ it('MCP discovery and calls use the signed host API and never approve writes', a vi.fn(async (url, init) => { seen.push({ url: String(url), body: JSON.parse(init.body) }); expect(init.headers['x-test-signature']).toBe('signed'); + // Every integration-action route requires a version 2 signature. + expect(init.headers['x-atomic-signature-version']).toBe('2'); return new Response( JSON.stringify( diff --git a/browser/lib/src/integration-actions.ts b/browser/lib/src/integration-actions.ts index 80763f1fb8..7f26c3d11d 100644 --- a/browser/lib/src/integration-actions.ts +++ b/browser/lib/src/integration-actions.ts @@ -1,5 +1,5 @@ /** One host API for the UI, Atomic assistant and MCP hosts. */ -import { signRequest } from './authentication.js'; +import { signedRequestInit } from './authentication.js'; import type { Store } from './store.js'; import type { DeclaredAction } from './plugin-manifest.js'; import type { @@ -39,14 +39,14 @@ async function post( const agent = store.getAgent(); if (!agent) throw new Error('Sign in to use an integration'); const url = `${store.getServerUrl()}/${endpoint}`; - const response = await fetch(url, { - method: 'POST', - headers: { - ...(await signRequest(url, agent, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify(body), - }); + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }), + ); if (!response.ok) throw new Error(await response.text()); return response.json(); diff --git a/browser/lib/src/plugin-connection.test.ts b/browser/lib/src/plugin-connection.test.ts index 9d21e6e9c0..8019cd3c21 100644 --- a/browser/lib/src/plugin-connection.test.ts +++ b/browser/lib/src/plugin-connection.test.ts @@ -7,7 +7,19 @@ import { inspectExternalOperation, } from './plugin-connection.js'; vi.mock('./authentication.js', () => ({ - signRequest: async () => ({ authorization: 'signed' }), + signedRequestInit: async ( + _url: string, + _agent: unknown, + request: { method: string; body?: string; headers?: object }, + ) => ({ + method: request.method, + headers: { + ...request.headers, + authorization: 'signed', + 'x-atomic-signature-version': '2', + }, + body: request.body, + }), })); describe('external approval transport', () => { diff --git a/browser/lib/src/plugin-connection.ts b/browser/lib/src/plugin-connection.ts index 295b84f9b0..5c5b7a9d76 100644 --- a/browser/lib/src/plugin-connection.ts +++ b/browser/lib/src/plugin-connection.ts @@ -1,4 +1,4 @@ -import { signRequest } from './authentication.js'; +import { signedRequestInit } from './authentication.js'; import type { Store } from './store.js'; import type { PluginManifest } from './plugin-manifest.js'; @@ -36,14 +36,14 @@ async function post( const agent = store.getAgent(); if (!agent) throw new Error('sign in before changing a plugin connection'); const url = `${store.getServerUrl()}${path}`; - const response = await transport(url, { - method: 'POST', - headers: { - ...(await signRequest(url, agent, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify(body), - }); + const response = await transport( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }), + ); if (!response.ok) throw new Error(await response.text()); return response.json() as Promise; diff --git a/browser/lib/src/plugin-install.test.ts b/browser/lib/src/plugin-install.test.ts index 61860c1359..65b64ba10e 100644 --- a/browser/lib/src/plugin-install.test.ts +++ b/browser/lib/src/plugin-install.test.ts @@ -411,6 +411,8 @@ describe('publishZipRelease', () => { const headers = init.headers as Record; expect(headers['Content-Type']).toBe('application/zip'); expect(headers['x-atomic-signature']).toBeTruthy(); + // The route requires version 2, over exactly these bytes. + expect(headers['x-atomic-signature-version']).toBe('2'); expect(new Uint8Array(init.body as ArrayBuffer)).toEqual(bytes); }); diff --git a/browser/lib/src/plugin-install.ts b/browser/lib/src/plugin-install.ts index 78f4a75288..112dadc9f1 100644 --- a/browser/lib/src/plugin-install.ts +++ b/browser/lib/src/plugin-install.ts @@ -9,7 +9,7 @@ * `planning/plugin-runtime-convergence.md`, "One install path and a * marketplace". */ -import { signRequest } from './authentication.js'; +import { signedRequestInit } from './authentication.js'; import { generateKeyPair } from './CryptoProvider.js'; import { Datatype } from './datatypes.js'; import { core } from './ontologies/core.js'; @@ -396,14 +396,14 @@ export async function publishZipRelease( url.searchParams.set('drive', drive); if (options.world) url.searchParams.set('world', options.world); if (options.public) url.searchParams.set('public', 'true'); - const response = await transport(url.toString(), { - method: 'POST', - headers: { - ...(await signRequest(url.toString(), agent, {})), - 'Content-Type': 'application/zip', - }, - body: await file.arrayBuffer(), - }); + const response = await transport( + url.toString(), + await signedRequestInit(url.toString(), agent, { + method: 'POST', + headers: { 'Content-Type': 'application/zip' }, + body: new Uint8Array(await file.arrayBuffer()), + }), + ); if (!response.ok) throw new Error(await response.text()); return response.json() as Promise<{ diff --git a/browser/lib/src/plugin-server.test.ts b/browser/lib/src/plugin-server.test.ts index f2736f6bf6..534351fc0a 100644 --- a/browser/lib/src/plugin-server.test.ts +++ b/browser/lib/src/plugin-server.test.ts @@ -1,7 +1,19 @@ import { afterEach, expect, it, vi } from 'vitest'; import { executeServerPlugin } from './plugin-server.js'; vi.mock('./authentication.js', () => ({ - signRequest: async () => ({ 'x-test-signature': 'signed' }), + signedRequestInit: async ( + _url: string, + _agent: unknown, + request: { method: string; body?: string; headers?: object }, + ) => ({ + method: request.method, + headers: { + ...request.headers, + 'x-test-signature': 'signed', + 'x-atomic-signature-version': '2', + }, + body: request.body, + }), })); afterEach(() => vi.unstubAllGlobals()); const store = { @@ -17,6 +29,9 @@ const request = { it('uses the signed shared runtime endpoint and preserves plugin output', async () => { const fetch = vi.fn(async (_url, init) => { expect(init.headers['x-test-signature']).toBe('signed'); + // `/plugin-run` requires a version 2 signature over this body. + expect(init.headers['x-atomic-signature-version']).toBe('2'); + expect(init.method).toBe('POST'); expect(JSON.parse(init.body)).toEqual({ ...request, input: JSON.stringify(request.input), diff --git a/browser/lib/src/plugin-server.ts b/browser/lib/src/plugin-server.ts index 1aba8fe2f5..160f031b9b 100644 --- a/browser/lib/src/plugin-server.ts +++ b/browser/lib/src/plugin-server.ts @@ -1,4 +1,4 @@ -import { signRequest } from './authentication.js'; +import { signedRequestInit } from './authentication.js'; import { errorMessageFromResponse } from './error.js'; import type { Store } from './store.js'; @@ -10,14 +10,17 @@ export async function executeServerPlugin( const agent = store.getAgent(); if (!agent) throw new Error('Not signed in'); const url = `${store.getServerUrl()}/plugin-run`; - const response = await fetch(url, { - method: 'POST', - headers: { - ...(await signRequest(url, agent, {})), - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ ...request, input: JSON.stringify(request.input) }), - }); + const response = await fetch( + url, + await signedRequestInit(url, agent, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + ...request, + input: JSON.stringify(request.input), + }), + }), + ); if (!response.ok) throw new Error( errorMessageFromResponse(await response.text(), response.status), diff --git a/docs/src/authentication.md b/docs/src/authentication.md index 5b3c4356c3..94d2f224ee 100644 --- a/docs/src/authentication.md +++ b/docs/src/authentication.md @@ -146,7 +146,21 @@ In `@tomic/lib`: `signRequest(url, agent, headers, { method, body })`. In `atomic_lib`: `client::get_authentication_headers_v2(method, url, body, agent)`. Shared test vectors: `lib/src/authentication_v2_vectors.json`. -A server that sees `x-atomic-signature-version: 2` checks only the version 2 message and never falls back to version 1. AtomicServer accepts version 2 on `/app-agent` and `/plugin-view-token`, and refuses it with a `401` on endpoints that do not yet check the body. Cookies and WebSocket `AUTH` are always version 1. The integration proxy accepts only version 2 (see ontola/atomic-plugins#54). +A server that sees `x-atomic-signature-version: 2` checks only the version 2 message and never falls back to version 1. Cookies and WebSocket `AUTH` are always version 1. The integration proxy accepts only version 2 (see ontola/atomic-plugins#54). + +AtomicServer requires version 2 on the endpoints that change state (`POST`, `DELETE`), and refuses a version 1 signature, a bearer token and a session cookie there with a `401`: + +- `/app-agent`, `/app-write`, `/plugin-view-token`, `/plugin-secret`; +- `/plugin-run`, `/plugin-schedule`, `/plugin-resume`, `/plugin-auto-apply`, `/plugin-trigger`; +- `/plugin-release`, `/plugin-release-package`, `/plugin-release-pin`; +- `/plugin-sync-preview`, `-apply`, `-schedule`, `-status`, `/plugin-connection-state`, `-checkpoint`, `/plugin-external-read`, `-status`, `-confirm`, `-apply`; +- `/integration-actions` and the twelve `/integration-action-*` routes; +- `/bind-drive`, `/forget-peer`, `/iroh-sync`; +- `/website-hosting/assets/{hash}`, `/website-hosting/deployments`, `/website-hosting/activate`. + +A `GET` on the same paths still takes version 1. Each version 2 signature on these endpoints is accepted once: the server remembers every one it accepted until it can no longer be fresh (five minutes after its timestamp), and answers a second use with a `401`. Sign every request anew; two identical requests signed in the same millisecond carry the same signature, since Ed25519 is deterministic. The memory is per server process and bounded; when it is full the server answers `429` until older signatures age out. The request body is signed as sent, so send it without `Content-Encoding`. In `@tomic/lib`, `signedRequestInit(url, agent, { method, body, headers })` returns the `fetch` options for such a request. + +`/commit` (commits carry their own signature), `PUT /blob/{hash}` (content-addressed), `/upload` and `POST` on other resources still take version 1, and refuse version 2 with a `401`. ## Verifying an Authentication diff --git a/server/src/appstate.rs b/server/src/appstate.rs index acaa6c2663..0766c112c0 100644 --- a/server/src/appstate.rs +++ b/server/src/appstate.rs @@ -43,6 +43,9 @@ pub struct AppState { /// `crate::rate_limit`. Sized from `--write-rate-limit` and /// `--anonymous-write-rate-limit`. pub write_rate_limiter: Arc, + /// The version 2 request signatures this node accepted and that are still + /// fresh, so none is accepted twice; see `crate::replay_cache`. + pub replay_cache: Arc, } impl AppState { @@ -282,6 +285,7 @@ impl AppState { managed: server_info.managed, managed_dashboard_url: server_info.managed_dashboard_url, view_tokens: Arc::new(Default::default()), + replay_cache: Arc::new(Default::default()), }) } diff --git a/server/src/bin.rs b/server/src/bin.rs index 06da6c1ee5..07485bcb1a 100644 --- a/server/src/bin.rs +++ b/server/src/bin.rs @@ -22,6 +22,8 @@ mod metrics; mod node_key; pub mod plugins; mod rate_limit; +mod replay_cache; +mod require_v2; mod routes; pub mod serve; pub mod vector_search; diff --git a/server/src/handlers/app_endpoints_test.rs b/server/src/handlers/app_endpoints_test.rs index da1df1c8d4..5dbe31acc9 100644 --- a/server/src/handlers/app_endpoints_test.rs +++ b/server/src/handlers/app_endpoints_test.rs @@ -46,32 +46,6 @@ fn signed(path: &str, appstate: &AppState) -> TestRequest { request } -/// Signs as someone other than this node's own agent — a collaborator. -fn signed_as(path: &str, appstate: &AppState, agent: &Agent) -> TestRequest { - let origin = appstate.config.get_origin(); - let url = format!("{origin}{path}"); - let headers = - atomic_lib::client::get_authentication_headers(&url, agent).expect("auth headers"); - - let mut request = TestRequest::with_uri(path); - - for (key, value) in headers { - request = request.insert_header((key, value)); - } - - if let Ok(parsed) = url::Url::parse(&origin) { - if let Some(host) = parsed.host_str() { - let authority = match parsed.port() { - Some(port) => format!("{host}:{port}"), - None => host.to_string(), - }; - request = request.insert_header(("Host", authority)); - } - } - - request -} - /// Publishes an agent so the server can verify its signatures, and gives it /// `right` on `target` — which is exactly what the Share dialog does. async fn share(fixture: &Fixture, target: &str, agent: &Agent, right: &str) { @@ -212,14 +186,15 @@ async fn a_view_is_served_only_to_something_holding_a_token() { let minted = test::call_service( &service, - signed("/plugin-view-token", &fixture.appstate) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2( + "/plugin-view-token", + &fixture.appstate, + format!( r#"{{"drive":{:?},"plugin":{:?}}}"#, fixture.drive, fixture.plugin, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -253,14 +228,15 @@ async fn a_token_does_not_open_another_plugin() { let minted = test::call_service( &service, - signed("/plugin-view-token", &fixture.appstate) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2( + "/plugin-view-token", + &fixture.appstate, + format!( r#"{{"drive":{:?},"plugin":{:?}}}"#, fixture.drive, fixture.plugin, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -294,16 +270,17 @@ async fn an_app_writes_its_own_data_as_itself() { let response = test::call_service( &service, - signed("/app-write", &fixture.appstate) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2( + "/app-write", + &fixture.appstate, + format!( r#"{{"drive":{:?},"app":{:?},"op":"create","propVals":{{{:?}:"A note"}}}}"#, fixture.drive, app, urls::NAME, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -365,14 +342,15 @@ async fn an_app_cannot_write_outside_itself() { // app has an identity of its own. let response = test::call_service( &service, - signed("/app-write", &fixture.appstate) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2( + "/app-write", + &fixture.appstate, + format!( r#"{{"drive":{:?},"app":{:?},"op":"create","parent":{:?},"propVals":{{}}}}"#, fixture.drive, app, fixture.drive, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -400,14 +378,15 @@ async fn an_app_with_no_key_is_told_so() { let response = test::call_service( &service, - signed("/app-write", &fixture.appstate) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2( + "/app-write", + &fixture.appstate, + format!( r#"{{"drive":{:?},"app":{:?},"op":"create","propVals":{{}}}}"#, fixture.drive, fixture.drive, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -435,14 +414,16 @@ async fn someone_the_app_was_shared_with_can_use_it() { // has to be shared twice. let opened = test::call_service( &service, - signed_as("/plugin-view-token", &fixture.appstate, &collaborator) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(format!( + post_v2_as( + "/plugin-view-token", + &fixture.appstate, + &collaborator, + format!( r#"{{"drive":{:?},"plugin":{:?}}}"#, fixture.drive, fixture.plugin, - )) - .to_request(), + ), + ) + .to_request(), ) .await; @@ -450,11 +431,13 @@ async fn someone_the_app_was_shared_with_can_use_it() { let response = test::call_service( &service, - signed_as("/app-write", &fixture.appstate, &collaborator) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(create_payload(&fixture, &app, "Added by a collaborator")) - .to_request(), + post_v2_as( + "/app-write", + &fixture.appstate, + &collaborator, + create_payload(&fixture, &app, "Added by a collaborator"), + ) + .to_request(), ) .await; @@ -515,11 +498,13 @@ async fn read_only_means_look_not_touch() { let response = test::call_service( &service, - signed_as("/app-write", &fixture.appstate, &onlooker) - .method(actix_web::http::Method::POST) - .insert_header(("Content-Type", "application/json")) - .set_payload(create_payload(&fixture, &app, "Should not exist")) - .to_request(), + post_v2_as( + "/app-write", + &fixture.appstate, + &onlooker, + create_payload(&fixture, &app, "Should not exist"), + ) + .to_request(), ) .await; @@ -568,9 +553,78 @@ fn signed_v2( request } +/// A version 2 request, signed by `agent`, with `body` as JSON. +fn signed_v2_as( + path: &str, + appstate: &AppState, + agent: &Agent, + method: actix_web::http::Method, + body: &str, +) -> TestRequest { + let origin = appstate.config.get_origin(); + let url = format!("{origin}{path}"); + let headers = atomic_lib::client::get_authentication_headers_v2( + method.as_str(), + &url, + body.as_bytes(), + agent, + ) + .expect("auth headers"); + + let mut request = TestRequest::with_uri(path) + .method(method) + .insert_header(("Content-Type", "application/json")) + .set_payload(body.to_string()); + for (key, value) in headers { + request = request.insert_header((key, value)); + } + with_host(request, appstate) +} + +fn with_host(mut request: TestRequest, appstate: &AppState) -> TestRequest { + if let Ok(parsed) = url::Url::parse(&appstate.config.get_origin()) { + if let Some(host) = parsed.host_str() { + let authority = match parsed.port() { + Some(port) => format!("{host}:{port}"), + None => host.to_string(), + }; + request = request.insert_header(("Host", authority)); + } + } + request +} + +/// A JSON `POST` with a version 2 signature, as this node's own agent. +fn post_v2(path: &str, appstate: &AppState, body: impl AsRef) -> TestRequest { + let agent = appstate.store.get_default_agent().unwrap(); + signed_v2_as( + path, + appstate, + &agent, + actix_web::http::Method::POST, + body.as_ref(), + ) +} + +/// A JSON `POST` with a version 2 signature, as `agent`. +fn post_v2_as( + path: &str, + appstate: &AppState, + agent: &Agent, + body: impl AsRef, +) -> TestRequest { + signed_v2_as( + path, + appstate, + agent, + actix_web::http::Method::POST, + body.as_ref(), + ) +} + /// `/plugin-view-token` and `/app-agent` accept a version 2 signature, and /// check the body with it: the same headers on a different body are refused. -/// Version 1 keeps working on both (not required yet). +/// Version 1 is refused on both: they require version 2 (#1700, piece 6). #[actix_rt::test] async fn v2_signatures_bind_the_body() { use actix_web::http::Method; @@ -688,7 +742,7 @@ async fn v2_signatures_bind_the_body() { .agent; assert_eq!(before, after, "nothing was stored"); - // Version 1 still works where v2 is accepted. + // Version 1 no longer works here: v2 is required (#1700, piece 6). let v1 = test::call_service( &service, signed("/plugin-view-token", &fixture.appstate) @@ -698,7 +752,8 @@ async fn v2_signatures_bind_the_body() { .to_request(), ) .await; - assert_eq!(v1.status(), 200); + assert_eq!(v1.status(), 401); + assert!(body_of(v1).contains(crate::require_v2::REQUIRES_V2)); } /// An endpoint that does not bind method and body yet refuses a v2 @@ -716,17 +771,12 @@ async fn v2_is_never_downgraded_to_v1() { ) .await; - let payload = create_payload(&fixture, &app, "v2"); + let _ = app; + // A POST on an ordinary resource reads the headers alone, with no body + // to bind (`/upload` and POST on any resource are not v2 routes yet). let unbound = test::call_service( &service, - signed_v2( - "/app-write", - &fixture.appstate, - Method::POST, - &payload, - &payload, - ) - .to_request(), + signed_v2("/not-an-endpoint", &fixture.appstate, Method::POST, "", "").to_request(), ) .await; assert_eq!(unbound.status(), 401); @@ -833,3 +883,280 @@ async fn an_active_installation_reports_its_agent_on_this_node() { // Its own agent, not the server's. assert_ne!(stored, db.get_default_agent().unwrap().subject.to_string()); } + +/// A session cookie holding a version 1 proof for this server, as +/// `setCookieAuthentication` writes it. +fn session_cookie(appstate: &AppState, agent: &Agent) -> String { + use base64::Engine; + let origin = appstate.config.get_origin(); + let timestamp = atomic_lib::utils::now(); + let signature = atomic_lib::agents::sign_message( + format!("{origin} {timestamp}").as_bytes(), + agent.private_key.as_ref().unwrap(), + ) + .unwrap(); + let proof = serde_json::json!({ + "https://atomicdata.dev/properties/auth/agent": agent.subject.to_string(), + "https://atomicdata.dev/properties/auth/requestedSubject": origin, + "https://atomicdata.dev/properties/auth/publicKey": agent.public_key, + "https://atomicdata.dev/properties/auth/timestamp": timestamp, + "https://atomicdata.dev/properties/auth/signature": signature, + }); + format!( + "atomic_session={}", + base64::engine::general_purpose::STANDARD.encode(proof.to_string()) + ) +} + +/// Every state-changing route that requires a version 2 signature (#1700, +/// piece 6), with a request body shaped roughly like what it takes. A body +/// the handler cannot use is fine: what is checked is whether the request +/// gets past authentication. +fn v2_required_routes( + fixture: &Fixture, + app: &str, +) -> Vec<(actix_web::http::Method, String, String)> { + use actix_web::http::Method; + let target = format!( + r#"{{"drive":{:?},"plugin":{:?}}}"#, + fixture.drive, fixture.plugin + ); + let drive = urlencoding::encode(&fixture.drive).to_string(); + let plugin = urlencoding::encode(&fixture.plugin).to_string(); + let app_query = format!("drive={drive}&app={}", urlencoding::encode(app)); + let plugin_query = format!("drive={drive}&plugin={plugin}"); + let hosting = format!("project=p1&drive={drive}"); + let post = |path: &str, body: &str| (Method::POST, path.to_string(), body.to_string()); + let mut routes = vec![ + post( + "/app-agent", + &format!( + r#"{{"drive":{:?},"app":{:?},"secret":"x"}}"#, + fixture.drive, app + ), + ), + ( + Method::DELETE, + format!("/app-agent?{app_query}"), + String::new(), + ), + post("/plugin-view-token", &target), + post( + "/plugin-secret", + &format!( + r#"{{"drive":{:?},"plugin":{:?},"name":"n","value":"v"}}"#, + fixture.drive, fixture.plugin + ), + ), + ( + Method::DELETE, + format!("/plugin-secret?{plugin_query}&name=n"), + String::new(), + ), + post( + "/app-write", + &create_payload(fixture, app, "v2 route check"), + ), + post("/plugin-run", &target), + post("/plugin-schedule", &target), + ( + Method::DELETE, + format!("/plugin-schedule?{plugin_query}"), + String::new(), + ), + post("/plugin-resume", &target), + post("/plugin-auto-apply", &target), + post("/plugin-trigger", &target), + post("/plugin-release", &target), + post( + &format!("/plugin-release-package?drive={drive}"), + "not a zip", + ), + post("/plugin-release-pin", &target), + post("/plugin-sync-preview", &target), + post("/plugin-sync-apply", &target), + post("/plugin-sync-schedule", &target), + post("/plugin-sync-status", &target), + post("/plugin-connection-state", &target), + post("/plugin-connection-checkpoint", &target), + post("/plugin-external-read", &target), + post("/plugin-external-status", &target), + post("/plugin-external-confirm", &target), + post("/plugin-external-apply", &target), + post("/bind-drive", "{}"), + post("/forget-peer?node=unknown", ""), + post( + "/iroh-sync", + &format!(r#"{{"nodeId":"not-a-node","drive":{:?}}}"#, fixture.drive), + ), + post( + &format!("/website-hosting/assets/{}?{hosting}", "0".repeat(64)), + "bytes", + ), + post(&format!("/website-hosting/deployments?{hosting}"), "{}"), + post(&format!("/website-hosting/activate?{hosting}"), "{}"), + ]; + for action in [ + "approve", + "call", + "cancel", + "consumer-abandon", + "consumers", + "grant", + "grants", + "history", + "history-compact", + "proposals", + "recovery-confirm", + "recovery-inspect", + ] { + routes.push(post(&format!("/integration-action-{action}"), &target)); + } + routes.push(post("/integration-actions", &target)); + routes +} + +/// #1700, piece 6: each state-changing route refuses a version 1 signature +/// and a session cookie, and gets past authentication with version 2. +#[actix_rt::test] +async fn state_changing_routes_refuse_v1_and_cookies_and_accept_v2() { + let (fixture, app) = app_fixture("app_v2_required").await; + let service = test::init_service( + App::new() + .app_data(Data::new(fixture.appstate.clone())) + .configure(crate::routes::config_routes), + ) + .await; + let agent = fixture.appstate.store.get_default_agent().unwrap(); + let routes = v2_required_routes(&fixture, &app); + assert_eq!( + routes + .iter() + .filter(|(_, path, _)| path.starts_with("/integration-action")) + .count(), + 13, + "all 13 integration-action routes" + ); + + for (method, path, body) in routes { + let v1 = test::call_service( + &service, + signed(&path, &fixture.appstate) + .method(method.clone()) + .insert_header(("Content-Type", "application/json")) + .set_payload(body.clone()) + .to_request(), + ) + .await; + assert_eq!(v1.status(), 401, "{method} {path} with v1"); + assert!( + body_of(v1).contains(crate::require_v2::REQUIRES_V2), + "{method} {path} says why" + ); + + let cookie = test::call_service( + &service, + with_host(TestRequest::with_uri(&path), &fixture.appstate) + .method(method.clone()) + .insert_header(("Content-Type", "application/json")) + .insert_header(("Cookie", session_cookie(&fixture.appstate, &agent))) + .set_payload(body.clone()) + .to_request(), + ) + .await; + assert_eq!(cookie.status(), 401, "{method} {path} with a cookie"); + + let v2 = test::call_service( + &service, + signed_v2_as(&path, &fixture.appstate, &agent, method.clone(), &body).to_request(), + ) + .await; + let status = v2.status(); + let answer = body_of(v2); + assert!( + status != 401 && !answer.contains(crate::require_v2::REQUIRES_V2), + "{method} {path} with v2 got {status}: {answer}" + ); + } +} + +/// #1700, answer 7: the same v2 request sent twice is refused the second +/// time, even though it is still fresh and its signature verifies. +#[actix_rt::test] +async fn a_replayed_v2_request_is_refused() { + let (fixture, _app) = app_fixture("app_v2_replay").await; + let service = test::init_service( + App::new() + .app_data(Data::new(fixture.appstate.clone())) + .configure(crate::routes::config_routes), + ) + .await; + let body = format!( + r#"{{"drive":{:?},"plugin":{:?}}}"#, + fixture.drive, fixture.plugin, + ); + let agent = fixture.appstate.store.get_default_agent().unwrap(); + let request = || { + signed_v2_as( + "/plugin-view-token", + &fixture.appstate, + &agent, + actix_web::http::Method::POST, + &body, + ) + }; + let captured: Vec<(String, String)> = request() + .to_http_request() + .headers() + .iter() + .map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string())) + .collect(); + let replay = || { + let mut replayed = TestRequest::with_uri("/plugin-view-token") + .method(actix_web::http::Method::POST) + .set_payload(body.clone()); + for (key, value) in &captured { + replayed = replayed.insert_header((key.as_str(), value.as_str())); + } + replayed.to_request() + }; + + let accepted = test::call_service(&service, replay()).await; + assert_eq!(accepted.status(), 200, "{}", body_of(accepted)); + let refused = test::call_service(&service, replay()).await; + assert_eq!(refused.status(), 401); + assert!(body_of(refused).contains("already used")); + + // A fresh signature over the same request is fine. (Signed in the same + // millisecond it would be the same signature: Ed25519 is deterministic.) + std::thread::sleep(std::time::Duration::from_millis(2)); + let again = test::call_service(&service, request().to_request()).await; + assert_eq!(again.status(), 200, "{}", body_of(again)); +} + +/// Reads on the same routes do not change state and keep accepting +/// version 1. +#[actix_rt::test] +async fn reads_on_v2_routes_still_accept_v1() { + let (fixture, app) = app_fixture("app_v2_reads").await; + let service = test::init_service( + App::new() + .app_data(Data::new(fixture.appstate.clone())) + .configure(crate::routes::config_routes), + ) + .await; + let read = test::call_service( + &service, + signed( + &format!( + "/app-agent?drive={}&app={}", + urlencoding::encode(&fixture.drive), + urlencoding::encode(&app), + ), + &fixture.appstate, + ) + .to_request(), + ) + .await; + assert_eq!(read.status(), 200, "{}", body_of(read)); +} diff --git a/server/src/handlers/app_write.rs b/server/src/handlers/app_write.rs index 4f9802ec92..cca8b586c6 100644 --- a/server/src/handlers/app_write.rs +++ b/server/src/handlers/app_write.rs @@ -25,7 +25,7 @@ use serde_json::Value as Json; use crate::{ appstate::AppState, errors::{AtomicServerError, AtomicServerResult}, - helpers::get_client_agent, + helpers::get_client_agent_of, plugins::apply::{ApplyHost, CreateRequest}, plugins::store_host::StoreApplyHost, }; @@ -83,7 +83,7 @@ pub async fn handle_app_write( .to_string(); let signed_subject = Subject::from_raw(&path_and_query, None).resolve(&context.origin); - let agent = get_client_agent(req.headers(), &appstate, &signed_subject).await?; + let agent = get_client_agent_of(&req, &appstate, &signed_subject).await?; check_write(store, &app_resource, &agent).await?; let mut host = StoreApplyHost::for_installation(store, &body.drive, &body.app, agent) diff --git a/server/src/handlers/forget_peer.rs b/server/src/handlers/forget_peer.rs index 131ee46c4c..72472f1e30 100644 --- a/server/src/handlers/forget_peer.rs +++ b/server/src/handlers/forget_peer.rs @@ -1,6 +1,6 @@ use crate::{ appstate::AppState, context::RequestContext, errors::AtomicServerResult, - helpers::get_client_agent, + helpers::get_client_agent_of, }; use actix_web::{web, HttpRequest, HttpResponse}; use atomic_lib::agents::ForAgent; @@ -94,7 +94,7 @@ pub async fn handle_forget_peer( // The client signs the full request URL (path + query); rebuild it exactly // so the signature check matches what it signed. let full_url = format!("{}{}", origin, req.uri()); - let for_agent = get_client_agent(req.headers(), &appstate, &full_url).await?; + let for_agent = get_client_agent_of(&req, &appstate, &full_url).await?; crate::helpers::enforce_write_rate_limit(&appstate, &req, &for_agent)?; let node = params.node.clone(); diff --git a/server/src/handlers/plugin_run.rs b/server/src/handlers/plugin_run.rs index 5aecd5f3b0..c35c0a5fbb 100644 --- a/server/src/handlers/plugin_run.rs +++ b/server/src/handlers/plugin_run.rs @@ -11,7 +11,8 @@ use actix_web::{web, HttpResponse}; use atomic_lib::{hierarchy::check_write, Storelike}; use crate::{ - appstate::AppState, errors::AtomicServerResult, helpers::get_client_agent, plugins::js_runtime, + appstate::AppState, errors::AtomicServerResult, helpers::get_client_agent_of, + plugins::js_runtime, }; #[derive(serde::Deserialize, Debug)] @@ -59,7 +60,7 @@ pub async fn handle_plugin_run( let signed_subject = atomic_lib::Subject::from_raw(&path_and_query, None).resolve(&context.origin); - let agent = get_client_agent(req.headers(), &appstate, &signed_subject).await?; + let agent = get_client_agent_of(&req, &appstate, &signed_subject).await?; check_write(store, &resource, &agent).await?; let runtime = js_runtime::embedded_runtime()?; diff --git a/server/src/handlers/plugin_schedule.rs b/server/src/handlers/plugin_schedule.rs index 621e727bce..7efcd36216 100644 --- a/server/src/handlers/plugin_schedule.rs +++ b/server/src/handlers/plugin_schedule.rs @@ -15,7 +15,7 @@ use atomic_lib::{ use crate::{ appstate::AppState, errors::{AtomicServerError, AtomicServerResult}, - helpers::get_client_agent, + helpers::get_client_agent_of, plugins::scheduler::drive_terms, }; @@ -55,7 +55,7 @@ pub(crate) async fn authorize( let signed_subject = atomic_lib::Subject::from_raw(&path_and_query, None).resolve(&context.origin); - let agent = get_client_agent(req.headers(), appstate, &signed_subject).await?; + let agent = get_client_agent_of(req, appstate, &signed_subject).await?; check_write(store, &resource, &agent).await?; Ok(agent) diff --git a/server/src/handlers/plugin_secret.rs b/server/src/handlers/plugin_secret.rs index 637283f259..b663b9feef 100644 --- a/server/src/handlers/plugin_secret.rs +++ b/server/src/handlers/plugin_secret.rs @@ -19,7 +19,7 @@ use atomic_lib::{ Storelike, }; -use crate::{appstate::AppState, errors::AtomicServerResult, helpers::get_client_agent}; +use crate::{appstate::AppState, errors::AtomicServerResult, helpers::get_client_agent_of}; #[derive(serde::Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -142,7 +142,7 @@ async fn authorize( let signed_subject = atomic_lib::Subject::from_raw(&path_and_query, None).resolve(&context.origin); - let agent = get_client_agent(req.headers(), appstate, &signed_subject).await?; + let agent = get_client_agent_of(req, appstate, &signed_subject).await?; check_write(store, &resource, &agent).await?; Ok(()) diff --git a/server/src/handlers/plugin_trigger.rs b/server/src/handlers/plugin_trigger.rs index 7ee1b6d447..391aaa62a3 100644 --- a/server/src/handlers/plugin_trigger.rs +++ b/server/src/handlers/plugin_trigger.rs @@ -19,7 +19,7 @@ use atomic_lib::{ use crate::{ appstate::AppState, errors::{AtomicServerError, AtomicServerResult}, - helpers::get_client_agent, + helpers::get_client_agent_of, }; #[derive(serde::Deserialize, Debug)] @@ -72,7 +72,7 @@ async fn authorize( let signed_subject = atomic_lib::Subject::from_raw(&path_and_query, None).resolve(&context.origin); - let agent = get_client_agent(req.headers(), appstate, &signed_subject).await?; + let agent = get_client_agent_of(req, appstate, &signed_subject).await?; check_write(store, &resource, &agent).await?; Ok(agent) diff --git a/server/src/handlers/post_resource.rs b/server/src/handlers/post_resource.rs index e75fbb57ea..e14f55bb36 100644 --- a/server/src/handlers/post_resource.rs +++ b/server/src/handlers/post_resource.rs @@ -3,7 +3,7 @@ use crate::{ content_types::get_accept, content_types::ContentType, errors::AtomicServerResult, - helpers::{get_client_agent, try_extension}, + helpers::{get_client_agent_of, try_extension}, }; use actix_web::{web, HttpResponse}; use atomic_lib::{Resource, Storelike}; @@ -51,7 +51,7 @@ pub async fn handle_post_resource( let store = &appstate.store; timer.add("parse_headers"); - let for_agent = get_client_agent(headers, &appstate, &full_subject).await?; + let for_agent = get_client_agent_of(&req, &appstate, &full_subject).await?; crate::helpers::enforce_write_rate_limit(&appstate, &req, &for_agent)?; timer.add("get_agent"); diff --git a/server/src/handlers/website.rs b/server/src/handlers/website.rs index b31f1f7710..970c7e08c1 100644 --- a/server/src/handlers/website.rs +++ b/server/src/handlers/website.rs @@ -3,7 +3,7 @@ use crate::{ appstate::AppState, context::RequestContext, errors::{AtomicServerError, AtomicServerResult}, - helpers::get_client_agent, + helpers::get_client_agent_of, }; use actix_web::{ guard, @@ -99,7 +99,7 @@ async fn authorize( None, ) .resolve(&ctx.origin); - let agent = get_client_agent(req.headers(), state, &signed).await?; + let agent = get_client_agent_of(req, state, &signed).await?; if matches!(agent, ForAgent::Public) { return Err(AtomicServerError::bad_request("Sign in to publish")); } @@ -257,6 +257,10 @@ pub async fn preview( pub fn control_routes(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/website-hosting") + // Its POSTs (assets, deployments, activate) require a v2 signature. + .wrap(actix_web::middleware::from_fn( + crate::require_v2::require_v2, + )) .app_data(web::JsonConfig::default().limit(MAX_BYTES)) .app_data(web::PayloadConfig::new(2_000_000)) .route("/assets/{hash}", web::post().to(asset_upload)) @@ -429,6 +433,29 @@ mod tests { } req } + /// A JSON POST with a version 2 signature, which these routes require. + /// Signed a millisecond apart at least, so resending the same body is a + /// new proof rather than a replay. + fn signed_post(state: &AppState, path: &str, json: &serde_json::Value) -> test::TestRequest { + std::thread::sleep(std::time::Duration::from_millis(2)); + let url = format!("{}{path}", state.config.get_origin()); + let body = json.to_string(); + let headers = atomic_lib::client::get_authentication_headers_v2( + "POST", + &url, + body.as_bytes(), + &state.store.get_default_agent().unwrap(), + ) + .unwrap(); + let mut req = test::TestRequest::post() + .uri(path) + .insert_header(("Content-Type", "application/json")) + .set_payload(body); + for (key, value) in headers { + req = req.insert_header((key, value)); + } + req + } #[actix_web::test] async fn hosting_requires_auth_and_separates_public_content_from_control() { let mut f = fixture("website_http").await; @@ -477,14 +504,8 @@ mod tests { ) .await; assert!(!forbidden.status().is_success()); - let result = test::call_service( - &app, - signed(&state, &upload) - .method(Method::POST) - .set_json(&package) - .to_request(), - ) - .await; + let result = + test::call_service(&app, signed_post(&state, &upload, &package).to_request()).await; if !result.status().is_success() { panic!("upload failed: {:?}", test::read_body(result).await); } @@ -518,24 +539,12 @@ mod tests { ); let body = serde_json::json!({"expectedRevision":uploaded["state"]["revision"],"deployment":id}); - let live = test::call_service( - &app, - signed(&state, &activate) - .method(Method::POST) - .set_json(&body) - .to_request(), - ) - .await; + let live = + test::call_service(&app, signed_post(&state, &activate, &body).to_request()).await; assert!(live.status().is_success()); let live: serde_json::Value = test::read_body_json(live).await; - let stale = test::call_service( - &app, - signed(&state, &activate) - .method(Method::POST) - .set_json(&body) - .to_request(), - ) - .await; + let stale = + test::call_service(&app, signed_post(&state, &activate, &body).to_request()).await; assert_eq!(stale.status(), 409); let page = test::call_service( &app, @@ -572,16 +581,12 @@ mod tests { } let disable = serde_json::json!({"expectedRevision":live["state"]["revision"],"deployment":null}); - assert!(test::call_service( - &app, - signed(&state, &activate) - .method(Method::POST) - .set_json(&disable) - .to_request() - ) - .await - .status() - .is_success()); + assert!( + test::call_service(&app, signed_post(&state, &activate, &disable).to_request()) + .await + .status() + .is_success() + ); assert_eq!( test::call_service( &app, diff --git a/server/src/helpers.rs b/server/src/helpers.rs index 277b48787a..fcb86e9a5c 100644 --- a/server/src/helpers.rs +++ b/server/src/helpers.rs @@ -256,6 +256,27 @@ pub async fn get_client_agent( get_client_agent_checked(headers, None, appstate, requested_subject).await } +/// The agent this request is from: the one [crate::require_v2::require_v2] +/// verified, on a route that requires a version 2 signature, and otherwise +/// what [get_client_agent] reads from the headers or cookie. +pub async fn get_client_agent_of( + req: &actix_web::HttpRequest, + appstate: &AppState, + requested_subject: &str, +) -> AtomicServerResult { + if let Some(verified) = verified_agent(req) { + return Ok(verified); + } + get_client_agent(req.headers(), appstate, requested_subject).await +} + +fn verified_agent(req: &actix_web::HttpRequest) -> Option { + use actix_web::HttpMessage; + req.extensions() + .get::() + .map(|verified| verified.0.clone()) +} + /// [get_client_agent] for a handler that knows the body it acts on, so it /// also accepts a version 2 request signature (`x-atomic-signature-version: /// 2`, ontola/atomic-plugins#54), which covers the method and that body. @@ -267,6 +288,9 @@ pub async fn get_client_agent_for_request( appstate: &AppState, requested_subject: &str, ) -> AtomicServerResult { + if let Some(verified) = verified_agent(req) { + return Ok(verified); + } let request = SignedRequest { method: req.method().as_str(), body, diff --git a/server/src/lib.rs b/server/src/lib.rs index 2a33d1fb65..9d14d07d3e 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -23,6 +23,8 @@ mod metrics; pub mod node_key; pub mod plugins; pub mod rate_limit; +mod replay_cache; +mod require_v2; pub mod routes; pub mod serve; pub mod vector_search; diff --git a/server/src/replay_cache.rs b/server/src/replay_cache.rs new file mode 100644 index 0000000000..daad9045a7 --- /dev/null +++ b/server/src/replay_cache.rs @@ -0,0 +1,195 @@ +//! Each version 2 request signature is accepted once (#1700, answer 7). +//! +//! A v2 proof is bound to its method, full URL and body, but on its own it +//! stays valid for its whole freshness window: from +//! [`AUTH_MAX_AGE_MS`] after its timestamp back to a few seconds before it. +//! Whoever captured a request (a log, a proxy, a browser extension) could +//! send it again, byte for byte, within that window, and a `POST +//! /plugin-secret` or `/plugin-run` would happen twice. +//! +//! This cache remembers, per node and in memory, the digest of every v2 +//! signature it accepted until that signature could no longer be fresh, and +//! refuses a second use. It lives in memory on purpose: a restart forgets it, +//! which re-opens at most one window for proofs captured before the restart, +//! and a node never has to write for a read-only check. +//! +//! Keyed on the SHA-256 of the decoded signature bytes. v2 verification is +//! strict Ed25519, so the same message cannot be signed into a second valid +//! encoding, and re-encoding the same bytes in the other base64 alphabet gives +//! the same key. +//! +//! Bounded: when it holds [`ReplayCache::DEFAULT_CAPACITY`] live entries it +//! refuses new proofs rather than forget live ones, since forgetting would +//! make those replayable. Reaching it takes that many signed state-changing +//! requests within five minutes, which the write rate limiter already stops +//! for any one agent. + +use std::collections::{BTreeSet, HashMap}; +use std::sync::Mutex; + +use atomic_lib::authentication::AUTH_MAX_AGE_MS; + +type Digest = [u8; 32]; + +/// Why [`ReplayCache::record`] refused a signature. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Refusal { + /// This signature was accepted before, and it is still within its window. + Replayed, + /// The cache is full of live entries; nothing new is accepted until some + /// of them age out. + Full, +} + +impl std::fmt::Display for Refusal { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Refusal::Replayed => write!( + f, + "This request signature was already used. A version 2 request signature is accepted once; sign the request again." + ), + Refusal::Full => write!( + f, + "This node has accepted too many signed requests in the last five minutes to remember another one. Try again shortly." + ), + } + } +} + +#[derive(Default)] +struct Entries { + /// Digest to the last millisecond its signature can still be fresh. + expiry_of: HashMap, + /// The same entries in expiry order, so aging out is cheap. + by_expiry: BTreeSet<(i64, Digest)>, +} + +impl Entries { + fn forget_expired(&mut self, now: i64) { + while let Some(&(expiry, digest)) = self.by_expiry.first() { + if expiry >= now { + break; + } + self.by_expiry.pop_first(); + self.expiry_of.remove(&digest); + } + } +} + +pub struct ReplayCache { + entries: Mutex, + capacity: usize, +} + +impl Default for ReplayCache { + fn default() -> Self { + Self::new(Self::DEFAULT_CAPACITY) + } +} + +impl ReplayCache { + /// About 10 MB at most. + pub const DEFAULT_CAPACITY: usize = 100_000; + + pub fn new(capacity: usize) -> Self { + ReplayCache { + entries: Mutex::new(Entries::default()), + capacity, + } + } + + /// Accepts `signature` (the decoded signature bytes), signed at + /// `timestamp`, if it was not accepted before, and remembers it until it + /// can no longer be fresh. `now` is Unix ms. + /// + /// Call it only for a signature that verified and is fresh: a forged one + /// must not take a slot. + pub fn record(&self, signature: &[u8], timestamp: i64, now: i64) -> Result<(), Refusal> { + let mut digest: Digest = [0; 32]; + hex::decode_to_slice( + atomic_lib::authentication::sha256_hex(signature), + &mut digest, + ) + .expect("a SHA-256 hex digest is 32 bytes"); + // Fresh while `now <= timestamp + AUTH_MAX_AGE_MS`; see + // `atomic_lib::utils::check_timestamp_fresh`. + let expiry = timestamp.saturating_add(AUTH_MAX_AGE_MS); + + let mut entries = self + .entries + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + entries.forget_expired(now); + if entries.expiry_of.contains_key(&digest) { + return Err(Refusal::Replayed); + } + if entries.expiry_of.len() >= self.capacity { + return Err(Refusal::Full); + } + entries.expiry_of.insert(digest, expiry); + entries.by_expiry.insert((expiry, digest)); + Ok(()) + } + + /// How many signatures are remembered, including any that aged out since + /// the last [`ReplayCache::record`]. + #[cfg(test)] + pub fn len(&self) -> usize { + self.entries.lock().unwrap().expiry_of.len() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const T: i64 = 1_800_000_000_000; + + #[test] + fn a_signature_is_accepted_once() { + let cache = ReplayCache::default(); + assert_eq!(cache.record(b"sig-a", T, T), Ok(())); + assert_eq!(cache.record(b"sig-a", T, T + 1), Err(Refusal::Replayed)); + // Another signature is not affected. + assert_eq!(cache.record(b"sig-b", T, T + 1), Ok(())); + } + + #[test] + fn a_replay_is_refused_for_the_whole_window_and_forgotten_after_it() { + let cache = ReplayCache::default(); + assert_eq!(cache.record(b"sig", T, T), Ok(())); + // Up to and including the last millisecond the proof is fresh. + assert_eq!( + cache.record(b"sig", T, T + AUTH_MAX_AGE_MS), + Err(Refusal::Replayed) + ); + // After that the signature check refuses it as too old anyway, so the + // cache lets go of it. + assert_eq!(cache.record(b"other", T, T + AUTH_MAX_AGE_MS + 1), Ok(())); + assert_eq!(cache.len(), 1, "the expired entry is forgotten"); + assert_eq!(cache.record(b"sig", T, T + AUTH_MAX_AGE_MS + 1), Ok(())); + } + + #[test] + fn a_proof_signed_ahead_of_this_clock_is_kept_until_its_own_window_ends() { + let cache = ReplayCache::default(); + let ahead = T + 9_000; + assert_eq!(cache.record(b"sig", ahead, T), Ok(())); + assert_eq!( + cache.record(b"sig", ahead, T + AUTH_MAX_AGE_MS + 1), + Err(Refusal::Replayed) + ); + } + + #[test] + fn a_full_cache_refuses_new_proofs_rather_than_forget_live_ones() { + let cache = ReplayCache::new(2); + assert_eq!(cache.record(b"one", T, T), Ok(())); + assert_eq!(cache.record(b"two", T, T), Ok(())); + assert_eq!(cache.record(b"three", T, T), Err(Refusal::Full)); + // Still remembered, so still refused. + assert_eq!(cache.record(b"one", T, T), Err(Refusal::Replayed)); + // Once they age out there is room again. + assert_eq!(cache.record(b"three", T, T + AUTH_MAX_AGE_MS + 1), Ok(())); + } +} diff --git a/server/src/require_v2.rs b/server/src/require_v2.rs new file mode 100644 index 0000000000..038e11585e --- /dev/null +++ b/server/src/require_v2.rs @@ -0,0 +1,159 @@ +//! State-changing endpoints that accept only a version 2 request signature, +//! each proof once (#1700, piece 6 and answer 7). +//! +//! A version 1 signature covers `"{url} {timestamp}"` and nothing else, and a +//! session cookie is a reusable v1 proof: whoever captures either can send a +//! different body to the same URL for five minutes. On the routes wrapped with +//! [`require_v2`], a write goes through only with `x-atomic-signature-version: +//! 2` over its method, full URL and the exact body bytes, and only the first +//! time: [`crate::replay_cache`] refuses the same signature again. +//! +//! Reading the body here, before any handler, is what makes the hash match +//! what is parsed: the middleware hashes the bytes it received and gives the +//! handler exactly those bytes back, so a handler keeps its `web::Json` +//! extractor. It sets [`VerifiedAgent`] on the request, which +//! [`crate::helpers::get_client_agent_of`] returns instead of reading the +//! headers again. +//! +//! `GET`, `HEAD` and `OPTIONS` on the same routes pass through unchanged: they +//! do not change state, and a CORS preflight carries no signature. + +use actix_web::{ + body::{EitherBody, MessageBody}, + dev::{Payload, ServiceRequest, ServiceResponse}, + http::{header, Method}, + middleware::Next, + web, HttpMessage, +}; +use atomic_lib::{agents::ForAgent, authentication::SIGNATURE_VERSION_HEADER, AtomicError}; +use futures::StreamExt; + +use crate::{ + appstate::AppState, + errors::{AppErrorType, AtomicServerError, AtomicServerResult}, + helpers::SignedRequest, +}; + +/// Why a request without a version 2 signature is refused. +pub const REQUIRES_V2: &str = "This endpoint changes state, so it accepts only a version 2 request signature: send x-atomic-signature-version: 2 with the x-atomic-* headers, signed over the method, the full URL and the body. Version 1 signatures and session cookies are not accepted here."; + +/// The agent a version 2 signature on this request proved, set by +/// [`require_v2`] once the signature verified, was fresh and was not a replay. +#[derive(Clone, Debug)] +pub struct VerifiedAgent(pub ForAgent); + +/// Refuses a state-changing request that does not carry a fresh, unused +/// version 2 signature over its method, full URL and body. +/// +/// A refusal is answered here as an error response rather than returned as an +/// error, so the middleware around it (CORS in particular) still sees a +/// response and a browser can read why. +pub async fn require_v2( + mut req: ServiceRequest, + next: Next, +) -> Result>, actix_web::Error> { + let changes_state = !matches!(*req.method(), Method::GET | Method::HEAD | Method::OPTIONS); + if changes_state { + let Some(appstate) = req.app_data::>().cloned() else { + let error = AtomicServerError::from("the server has no state for this request"); + return Ok(req.error_response(error).map_into_right_body()); + }; + // In public mode nobody is authenticated: everyone is the public agent. + if !appstate.config.opts.public_mode { + match verify(&mut req, &appstate).await { + Ok(agent) => { + req.extensions_mut().insert(VerifiedAgent(agent)); + } + Err(error) => return Ok(req.error_response(error).map_into_right_body()), + } + } + } + next.call(req) + .await + .map(ServiceResponse::map_into_left_body) +} + +async fn verify(req: &mut ServiceRequest, appstate: &AppState) -> AtomicServerResult { + let asks_for_v2 = req + .headers() + .get(SIGNATURE_VERSION_HEADER) + .and_then(|v| v.to_str().ok()) + .is_some_and(|v| v.trim() == atomic_lib::authentication::SIGNATURE_VERSION_2); + if !asks_for_v2 { + return Err(AtomicError::unauthorized(REQUIRES_V2.into()).into()); + } + // The signature covers the bytes on the wire, and the handler parses what + // it is given back; a compressed body would be decompressed in between. + if req + .headers() + .get(header::CONTENT_ENCODING) + .is_some_and(|v| v.as_bytes() != b"identity") + { + return Err(AtomicServerError::bad_request( + "A request with a version 2 signature is sent without Content-Encoding", + )); + } + + let body = read_body(req).await?; + req.set_payload(Payload::from(body.clone())); + + let origin = crate::context::RequestContext::new(req.request(), appstate).origin; + let url = format!("{origin}{}", req.uri()); + let signed = SignedRequest { + method: req.method().as_str(), + body: &body, + }; + let auth = crate::helpers::get_auth_headers_for_request(req.headers(), &url, Some(signed)) + .map_err(as_unauthorized)? + .ok_or_else(|| AtomicServerError::from(AtomicError::unauthorized(REQUIRES_V2.into())))?; + let signature = atomic_lib::agents::decode_base64(&auth.signature) + .map_err(|e| as_unauthorized(e.into()))?; + let timestamp = auth.timestamp; + + let agent = atomic_lib::authentication::get_agent_from_auth_values_and_check( + Some(auth), + &appstate.store, + ) + .await + .map_err(|e| as_unauthorized(e.into()))?; + + appstate + .replay_cache + .record(&signature, timestamp, atomic_lib::utils::now()) + .map_err(|refusal| AtomicServerError { + message: refusal.to_string(), + error_type: match refusal { + crate::replay_cache::Refusal::Replayed => AppErrorType::Unauthorized, + crate::replay_cache::Refusal::Full => AppErrorType::TooManyRequests, + }, + error_resource: None, + })?; + Ok(agent) +} + +/// The whole body, up to what the server accepts at all. The handler still +/// applies its own, usually smaller, limit when it parses it. +async fn read_body(req: &mut ServiceRequest) -> AtomicServerResult { + let mut payload = req.take_payload(); + let mut body = web::BytesMut::new(); + while let Some(chunk) = payload.next().await { + let chunk = chunk.map_err(|e| { + AtomicServerError::bad_request(format!("could not read the request body: {e}")) + })?; + if body.len() + chunk.len() > crate::serve::PAYLOAD_MAX { + return Err(AtomicServerError::bad_request(format!( + "the request body is larger than {} bytes", + crate::serve::PAYLOAD_MAX + ))); + } + body.extend_from_slice(&chunk); + } + Ok(body.freeze()) +} + +fn as_unauthorized(error: AtomicServerError) -> AtomicServerError { + AtomicServerError { + error_type: AppErrorType::Unauthorized, + ..error + } +} diff --git a/server/src/routes.rs b/server/src/routes.rs index c1e3492866..b41a5ac7d2 100644 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -1,7 +1,9 @@ //! Contains routing logic, sends the client to the correct handler. //! We should try to minimize what happens in here, since most logic should be defined in Atomic Data - not in the server itself. +use crate::require_v2::require_v2; use crate::{content_types, handlers}; +use actix_web::middleware::from_fn; use actix_web::{ guard, http::{header, Method}, @@ -135,7 +137,7 @@ async fn iroh_sync_handler( // The client signs the full request URL; rebuild it exactly. let origin = crate::context::RequestContext::new(&req, &appstate).origin; let full_url = format!("{}{}", origin, req.uri()); - let for_agent = crate::helpers::get_client_agent(req.headers(), &appstate, &full_url).await?; + let for_agent = crate::helpers::get_client_agent_of(&req, &appstate, &full_url).await?; crate::helpers::enforce_write_rate_limit(&appstate, &req, &for_agent)?; if matches!(for_agent, atomic_lib::agents::ForAgent::Public) { return Err(atomic_lib::errors::AtomicError::unauthorized( @@ -203,102 +205,128 @@ fn configure_wasm_plugin_routes(app: &mut actix_web::web::ServiceConfig) { app.service(web::resource("/plugin-ui").to(handlers::plugin_ui::handle_plugin_ui)) .service( web::resource("/plugin-view-token") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_ui::handle_mint_view_token)), ) .service( web::resource("/plugin-release-pin") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_release::pin)), ) .service( web::resource("/plugin-external-read") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_external::read)), ) .service(web::resource("/plugin-list").to(handlers::plugin_ui::handle_plugin_list)) .service( web::resource("/integration-action-history-compact") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::compact_history)), ) .service( web::resource("/integration-action-consumers") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::consumers)), ) .service( web::resource("/integration-action-consumer-abandon") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::abandon_consumer)), ) .service( web::resource("/integration-action-history") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::history)), ) .service( web::resource("/integration-action-cancel") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::cancel)), ) .service( web::resource("/integration-action-grant") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::grant)), ) .service( web::resource("/integration-action-grants") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::grants)), ) .service( web::resource("/integration-action-recovery-inspect") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::inspect_recovery)), ) .service( web::resource("/integration-action-recovery-confirm") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::confirm_recovery)), ) .service( web::resource("/integration-actions") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::list)), ) .service( web::resource("/integration-action-call") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::invoke)), ) .service( web::resource("/integration-action-proposals") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::proposals)), ) .service( web::resource("/integration-action-approve") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::integration_action::approve)), ) .service( web::resource("/plugin-sync-preview") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_sync::preview)), ) .service( - web::resource("/plugin-sync-apply").route(web::post().to(handlers::plugin_sync::apply)), + web::resource("/plugin-sync-apply") + .wrap(from_fn(require_v2)) + .route(web::post().to(handlers::plugin_sync::apply)), ) .service( web::resource("/plugin-sync-schedule") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_sync::schedule)), ) .service( web::resource("/plugin-sync-status") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_sync::status)), ) .service( web::resource("/plugin-connection-state") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_connection::read)), ) .service( web::resource("/plugin-connection-checkpoint") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_connection::checkpoint)), ) .service( web::resource("/plugin-external-status") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_external::status)), ) .service( web::resource("/plugin-external-confirm") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_external::confirm)), ) .service( web::resource("/plugin-external-apply") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_external::apply)), ) .service( @@ -315,36 +343,44 @@ fn configure_wasm_plugin_routes(app: &mut actix_web::web::ServiceConfig) { ) .service( web::resource("/plugin-release") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_release::publish)), ) .service( web::resource("/plugin-release-package") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_release::publish_package)), ) .service( web::resource("/plugin-run") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_run::handle_plugin_run)), ) .service( web::resource("/plugin-schedule") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_schedule::handle_set_schedule)) .route(web::get().to(handlers::plugin_schedule::handle_get_schedule)) .route(web::delete().to(handlers::plugin_schedule::handle_clear_pending)), ) .service( web::resource("/plugin-resume") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_schedule::handle_resume)), ) .service( web::resource("/plugin-auto-apply") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_schedule::handle_set_auto_apply)), ) .service( web::resource("/app-write") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::app_write::handle_app_write)), ) .service( web::resource("/plugin-trigger") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_trigger::handle_set_trigger)) .route(web::get().to(handlers::plugin_trigger::handle_get_trigger)), ); @@ -370,6 +406,7 @@ pub fn config_routes(app: &mut actix_web::web::ServiceConfig) { ) .service( web::resource("/bind-drive") + .wrap(from_fn(require_v2)) .guard(guard::Method(Method::POST)) .to(handlers::post_resource::handle_post_resource), ) @@ -381,20 +418,27 @@ pub fn config_routes(app: &mut actix_web::web::ServiceConfig) { ) .service( web::resource("/forget-peer") + .wrap(from_fn(require_v2)) .guard(guard::Method(Method::POST)) .to(handlers::forget_peer::handle_forget_peer), ) - .service(web::resource("/iroh-sync").route(web::post().to(iroh_sync_handler))) + .service( + web::resource("/iroh-sync") + .wrap(from_fn(require_v2)) + .route(web::post().to(iroh_sync_handler)), + ) .service(web::resource("/export").to(handlers::export::handle_export)) .configure(configure_wasm_plugin_routes) .service( web::resource("/app-agent") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::app_agent::handle_set_app_agent)) .route(web::get().to(handlers::app_agent::handle_get_app_agent)) .route(web::delete().to(handlers::app_agent::handle_delete_app_agent)), ) .service( web::resource("/plugin-secret") + .wrap(from_fn(require_v2)) .route(web::post().to(handlers::plugin_secret::handle_set_secret)) .route(web::get().to(handlers::plugin_secret::handle_list_secrets)) .route(web::delete().to(handlers::plugin_secret::handle_delete_secret)), diff --git a/server/tests/it/iroh_pairing.rs b/server/tests/it/iroh_pairing.rs index f028d4e035..4abc7d82e1 100644 --- a/server/tests/it/iroh_pairing.rs +++ b/server/tests/it/iroh_pairing.rs @@ -199,6 +199,26 @@ async fn spawn_server_process(tag: &str) -> (String, std::process::Child) { panic!("peer server never reported its port within 180s\nstderr:\n{stderr}"); } +/// A `POST` signed with version 2 over `body`, as `/iroh-sync` and +/// `/forget-peer` require (#1700): they change state, so they refuse version 1. +fn post_signed( + url: &str, + agent: &atomic_lib::agents::Agent, + body: String, +) -> reqwest::RequestBuilder { + let headers = + atomic_lib::client::get_authentication_headers_v2("POST", url, body.as_bytes(), agent) + .expect("auth headers"); + let mut request = reqwest::Client::new() + .post(url) + .header("Content-Type", "application/json") + .body(body); + for (key, value) in headers { + request = request.header(key, value); + } + request +} + /// Ask a server to pair with `node_did` and pull `drive`. This is byte-for-byte /// the request `pairAndSync` in the data-browser sends. /// `POST /iroh-sync`, signed as a fresh agent created on that server. The @@ -217,15 +237,11 @@ async fn post_iroh_sync( .await .unwrap(); let url = format!("{base_url}/iroh-sync"); - let headers = - atomic_lib::client::get_authentication_headers(&url, &agent).expect("auth headers"); - let mut request = reqwest::Client::new() - .post(&url) - .json(&serde_json::json!({ "nodeId": node_did, "drive": drive })); - for (key, value) in headers { - request = request.header(key, value); - } - let response = request.send().await.expect("iroh-sync request"); + let body = serde_json::json!({ "nodeId": node_did, "drive": drive }).to_string(); + let response = post_signed(&url, &agent, body) + .send() + .await + .expect("iroh-sync request"); let status = response.status(); let body = response.json().await.unwrap_or(serde_json::Value::Null); @@ -404,7 +420,13 @@ async fn iroh_sync_requires_both_a_node_and_a_drive() { let port = start_server("iroh_pair_fields"); wait_for_server(port).await; let url = format!("http://localhost:{port}"); - let http = reqwest::Client::new(); + // Signed, so the refusal is about the fields and not about who asks. + let agent = Client::new(&url) + .await + .unwrap() + .new_agent("Pairer") + .await + .unwrap(); let valid_node = format!("did:ad:node:{}", "a".repeat(64)); @@ -416,9 +438,7 @@ async fn iroh_sync_requires_both_a_node_and_a_drive() { ("no drive", serde_json::json!({ "nodeId": valid_node })), ("neither", serde_json::json!({})), ] { - let response = http - .post(format!("{url}/iroh-sync")) - .json(&payload) + let response = post_signed(&format!("{url}/iroh-sync"), &agent, payload.to_string()) .send() .await .expect("iroh-sync request"); @@ -528,13 +548,10 @@ async fn a_paired_device_can_be_forgotten() { .await .unwrap(); let url = forget_peer_url(&peer.base_url, &node_a); - let headers = - atomic_lib::client::get_authentication_headers(&url, &stranger).expect("auth headers"); - let mut request = reqwest::Client::new().post(&url); - for (key, value) in headers { - request = request.header(key, value); - } - let response = request.send().await.expect("forget-peer request"); + let response = post_signed(&url, &stranger, String::new()) + .send() + .await + .expect("forget-peer request"); assert!( !response.status().is_success(), "a stranger must not be able to forget a peer, got {}", @@ -547,13 +564,10 @@ async fn a_paired_device_can_be_forgotten() { // Alice may write the drive this peer was paired for, so Alice may undo // the pairing. - let headers = - atomic_lib::client::get_authentication_headers(&url, &agent).expect("auth headers"); - let mut request = reqwest::Client::new().post(&url); - for (key, value) in headers { - request = request.header(key, value); - } - let response = request.send().await.expect("forget-peer request"); + let response = post_signed(&url, &agent, String::new()) + .send() + .await + .expect("forget-peer request"); assert!( response.status().is_success(), "a forget-peer signed by the drive's writer must be accepted, got {}",