From 93bc161f203c5c4f2750e2ea52df80c9b3cd026a Mon Sep 17 00:00:00 2001 From: vorotamoroz Date: Tue, 15 Sep 2026 16:16:46 +0000 Subject: [PATCH] Add optional Cloudflare TURN credentials and secure profile sharing --- devs.md | 2 + .../2026_08_p2p_transport_compatibility.md | 10 +- .../design_docs/renewable_turn_credentials.md | 508 ++++++++++++++++++ docs/p2p.md | 42 +- docs/settings.md | 22 +- eslint.community.config.mjs | 7 + eslint.config.mjs | 7 + package-lock.json | 8 +- package.json | 2 +- .../BrowserP2PTransportSettings.svelte | 144 ++--- src/apps/cli/commands/runCommand.ts | 4 +- src/apps/cli/commands/runCommand.unit.spec.ts | 27 +- src/apps/cli/main.ts | 5 +- src/apps/webapp/WebAppRuntime.ts | 5 +- src/apps/webpeer/src/WebPeerRuntime.ts | 6 +- .../messages/LiveSyncProvisionalMessages.ts | 31 +- src/common/reportTool.ts | 2 + src/common/reportTool.unit.spec.ts | 41 ++ src/common/turnSettingsPrivacy.ts | 52 ++ src/common/turnSettingsPrivacy.unit.spec.ts | 71 +++ src/common/types.ts | 2 +- src/features/P2PSync/TurnConfiguration.svelte | 83 +++ .../cloudflare/iceServerSource.ts | 384 +++++++++++++ .../cloudflare/iceServerSource.unit.spec.ts | 164 ++++++ src/integrations/cloudflare/settings.ts | 87 +++ src/integrations/iceServerSources.ts | 85 +++ .../iceServerSources.unit.spec.ts | 39 ++ src/main.ts | 4 +- .../ModuleObsidianSettingAsMarkdown.ts | 13 + .../SettingDialogue/PaneRemoteConfig.ts | 11 + .../SetupWizard/dialogs/SetupRemoteP2P.svelte | 55 +- .../SetupWizard/dialogs/UseSetupURI.svelte | 14 +- src/serviceFeatures/setupObsidian/qrCode.ts | 10 +- .../setupObsidian/qrCode.unit.spec.ts | 14 + .../setupObsidian/setupProtocol.ts | 11 +- .../setupObsidian/setupProtocol.unit.spec.ts | 14 + src/serviceFeatures/setupObsidian/setupUri.ts | 9 +- src/serviceFeatures/useIceServerSources.ts | 17 + test/apps/webapp/WebAppRuntime.unit.spec.ts | 2 +- .../webpeer/browser-smoke.test.ts | 23 + 40 files changed, 1855 insertions(+), 182 deletions(-) create mode 100644 docs/design_docs/renewable_turn_credentials.md create mode 100644 src/common/reportTool.unit.spec.ts create mode 100644 src/common/turnSettingsPrivacy.ts create mode 100644 src/common/turnSettingsPrivacy.unit.spec.ts create mode 100644 src/features/P2PSync/TurnConfiguration.svelte create mode 100644 src/integrations/cloudflare/iceServerSource.ts create mode 100644 src/integrations/cloudflare/iceServerSource.unit.spec.ts create mode 100644 src/integrations/cloudflare/settings.ts create mode 100644 src/integrations/iceServerSources.ts create mode 100644 src/integrations/iceServerSources.unit.spec.ts create mode 100644 src/serviceFeatures/useIceServerSources.ts diff --git a/devs.md b/devs.md index 21b1f2e4..d95d0a68 100644 --- a/devs.md +++ b/devs.md @@ -189,6 +189,8 @@ steps required to add a built-in provider. Commonlib owns one stable `LiveSyncP2PService`, its `P2PRoomSessionOwner`, and the replaceable Trystero room session. Host commands, event handlers, and views consume the focused transport, connection-probe admission, directory, peer-admission, transfer, change-relay, configuration, and diagnostic views returned by the service feature. They must not retain the deprecated compatibility Replicator as an ordinary service locator, close Trystero-owned raw peers, or install another Trystero transport generation at the application root. The exact implemented ownership and shutdown boundaries are recorded in Commonlib's [P2P transport lifecycle](https://github.com/vrtmrz/livesync-commonlib/blob/main/docs/p2p-transport-lifecycle.md) design document. +The proposed [TURN credential sources design](docs/design_docs/renewable_turn_credentials.md) covers credential expiry in the existing room reuse decision, replication continuation after room replacement, persisted and shared provider tokens, report redaction, and optional integrations on the device. It records the Commonlib work and compatibility boundaries before implementation. + ### Conflict Merge Policy Markdown conflict auto-merge should behave like a conservative three-way merge. The guiding rule is to merge changes when they touch non-overlapping regions, and to keep a manual conflict when the edits overlap semantically. diff --git a/docs/adr/2026_08_p2p_transport_compatibility.md b/docs/adr/2026_08_p2p_transport_compatibility.md index c9691c28..004c6e5d 100644 --- a/docs/adr/2026_08_p2p_transport_compatibility.md +++ b/docs/adr/2026_08_p2p_transport_compatibility.md @@ -60,7 +60,15 @@ The first settings revision retains the existing storage and dialogue contract o A future interface may present the existing comma-separated value as ordered `turn:` and `turns:` URL rows without changing its serialised representation. A structured list of multiple credential profiles is deferred until a provider or self-hosted use case requires different credentials in the same P2P profile. -Static long-term credentials are the supported first stage. Managed providers may return short-lived credentials, but LiveSync must not store a provider API token or a Coturn shared authentication secret. A future managed-credential design needs a separately trusted HTTPS endpoint, expiry handling, refresh behaviour, failure reporting, and a clear Setup URI policy. It is not represented as another static password field. +Static long-term credentials are the supported first stage. Managed credentials use an optional source implementation on the device, behind a service-independent acquisition contract. Service-specific requests and settings belong under `src/integrations/`; Commonlib owns acquisition coordination and the P2P lifecycle. A separately operated HTTPS credential endpoint is an optional future source, not a prerequisite. + +A user-supplied provider API token is persisted as a sensitive P2P profile setting and included in encrypted Setup URI sharing, so that participating devices can use the same configuration without repeated token entry. Optional configuration encryption must cover every saved copy. Reports and logs redact the complete provider configuration and issued credentials, including inactive profiles and settings projections. Coturn's server-side shared authentication secret remains outside client settings. + +Issued short-lived TURN credentials and their expiry remain in memory. The existing room reuse decision checks both the effective connection settings and credential validity. When reconciliation finds expired credentials, it uses the normal room retirement and replacement path with newly acquired credentials. Replacement may cancel an in-progress transfer; the next replication attempt uses stored checkpoints and revision comparison to retain received progress. Whether that next attempt starts automatically follows the existing synchronisation policy. + +Time passing alone does not trigger acquisition or disconnection. This design adds no renewal timer, per-peer acquisition hook, configuration update on raw peers, or credential-driven ICE restart. Internal peer reconnection within an unchanged room does not guarantee fresh issuance. Acquisition failure is reported without changing the selected source or route policy. See [TURN credential sources](../design_docs/renewable_turn_credentials.md) for the proposed contract, persistence and sharing formats, room replacement, and verified replication continuation behaviour. + +When managed sources are introduced, relay-only validation accepts a valid managed TURN source configuration as well as the existing manual URL list. Failure to acquire usable TURN entries keeps relay-only mode selected and reports the connection failure; it does not restore `Automatic` silently. ### TURN allocation check and route diagnostics diff --git a/docs/design_docs/renewable_turn_credentials.md b/docs/design_docs/renewable_turn_credentials.md new file mode 100644 index 00000000..474142a0 --- /dev/null +++ b/docs/design_docs/renewable_turn_credentials.md @@ -0,0 +1,508 @@ +--- +date: 2026-09-15 +commonlib-version: "0.1.25-dev.turn-credentials.3" +self-hosted-livesync-version: "1.0.28" +status: unreleased +--- + +# TURN credential sources + +## Purpose and decisions + +This developer design addresses [Issue #1182](https://github.com/vrtmrz/obsidian-livesync/issues/1182) +through a service-independent interface for acquiring TURN credentials. +The [P2P transport compatibility ADR](../adr/2026_08_p2p_transport_compatibility.md) +records the accepted policy. The contract, lifecycle, settings, and host +integration are implemented locally. Real provider issuance, relay-only +Obsidian synchronisation, and synchronisation after explicit reconnection have +been verified. Expiry-driven TURN reconnection remains release validation work. + +The design uses these decisions: + +- Acquire credentials on the device through an optional service integration. +- Persist the user-supplied provider API token with the P2P profile and include + it in encrypted Setup URI sharing for additional devices. +- Redact provider configuration and issued credentials from reports and logs. +- Keep issued short-lived credentials in memory only. +- Keep a local expiry alongside issued credentials and check it in the + existing room reuse decision. +- When that decision finds expired credentials, acquire a new configuration + and use the existing room replacement lifecycle. Replacement may cancel + an in-progress transfer; the next replication attempt reuses stored progress. +- Check expiry when the room lifecycle is reconciled. Add no renewal timer, + per-peer acquisition hook, `setConfiguration()`, or credential-driven ICE + restart. + +Manual TURN configuration remains supported without a provider account. +Cloudflare is the first optional integration. A separate credential endpoint, +a general authentication framework, runtime extension loading, and migration +of existing service integrations are outside the first delivery. + +## Ownership and composition + +An **ICE server source**, represented by `IceServerSource`, supplies ICE server +URLs, access credentials, and their expiry. This is developer vocabulary for +the acquisition contract; it is separate from a Replicator provider. + +| Component | Responsibility | Owner | +| --- | --- | --- | +| Source contract | Acquisition result, validation, and safe failure categories | Commonlib | +| Credential state and room reuse | Memory cache, expiry check, acquisition, cancellation, and room replacement | Commonlib `P2PRoomSessionOwner` | +| Physical peer creation | Use the configuration supplied when joining the room | Existing Trystero implementation | +| Source catalogue and settings | Explicit source selection and host dependencies | LiveSync | +| Cloudflare source | Provider request, response conversion, and configuration validation | LiveSync `src/integrations/cloudflare/` | + +Implementation placement: + +```text +Commonlib + P2P source contract and private credential cache + Expiry check in the existing room owner and session construction + +LiveSync + src/integrations/iceServerSources.ts + src/integrations/cloudflare/iceServerSource.ts + src/integrations/cloudflare/settings.ts + src/serviceFeatures/useIceServerSources.ts +``` + +`integrations/` groups code which connects external services to the common +contract. It does not imply a hosted project service or a public extension +marketplace. The service feature composes a closed catalogue of source +factories with explicit dependencies, following +[Service feature and legacy Module boundaries](service_feature_and_legacy_module_boundaries.md). +An integration receives neither `LiveSyncBaseCore` nor ownership of replication. + +Supply the catalogue through an optional composition argument to +`useP2PReplicatorFeature`, preserving its manual-only default for existing +Commonlib consumers. Factories validate settings without network access; +acquisition runs only when requested by the P2P owner. Unsupported sources +produce an explicit configuration error. + +```mermaid +flowchart LR + R["Existing room lifecycle reconciliation"] --> D{"Same binding and valid credentials?"} + D -->|"Yes"| K["Keep current room"] + D -->|"No"| C["Retire current room, if present"] + C --> A["Reuse valid cached credentials or acquire"] + A --> O["Open room with resolved ICE configuration"] +``` + +## Settings and dependencies + +Present a `TURN configuration` choice with `Manual` and `Cloudflare`. +The catalogue supplies each integration's label and fields; the common P2P +engine does not branch on a service name. + +| Input | Manual | Cloudflare | +| --- | --- | --- | +| TURN server URLs | Existing field | Supplied by the API | +| TURN username and credential | Existing fields | Issued in memory | +| TURN Key ID | Unused | Required and persisted | +| TURN Key API Token | Unused | Required, masked in the dialogue, and persisted | + +The first Cloudflare implementation requests a 24-hour lifetime internally. +It needs no account ID, email address, custom endpoint URL, or renewal interval +setting. This lifetime is a design default, not a provider default. + +Dependencies are an injected HTTP operation, a clock, cancellation/deadline +handling, and the existing settings and P2P lifecycle services. No Cloudflare +SDK, credential broker, or new operating-system secret-store dependency is +required. + +Retain `P2P_turnServers`, `P2P_turnUsername`, and `P2P_turnCredential` for manual +configuration. An absent source selection means manual. Add a versioned P2P +profile descriptor, `P2P_iceServerSource`: + +```json +{ + "version": 1, + "id": "cloudflare", + "configuration": { + "turnKeyId": "user-supplied-key-id", + "apiToken": "user-supplied-turn-key-api-token" + } +} +``` + +Commonlib owns the JSON envelope; each source owns validation of its +configuration. Unsupported identifiers and versions remain preserved in +storage and produce an explicit unsupported result when selected. Loading an +inactive profile performs no acquisition. + +The selected source configuration, including token changes, participates in +the effective P2P configuration identity. Issued credentials and their expiry +are separate runtime state. Room reuse requires both a matching identity and +usable credentials. Under managed selection, unused manual credentials do +not affect that identity; manual selection preserves the existing projection. +Keep the identity opaque and absent from diagnostics. Apply source changes +and expired runtime credentials through the existing room replacement policy. + +## Persistence, sharing, and redaction + +The API token is an ordinary sensitive connection setting. Persist it with +the profile so that restarting a device and configuring another device do +not require re-entry. This does not claim operating-system keychain storage. +When optional configuration encryption is enabled, cover both the saved +profile URI and any top-level settings projection containing the source. +Failure to encrypt either copy must leave the prior saved settings intact +and report a safe error; it must not silently save a plaintext replacement. + +| Destination | Provider API token | Issued TURN username and credential | +| --- | --- | --- | +| Saved P2P profile | Included | Omitted | +| Encrypted Setup URI | Included with the source and Key ID | Omitted | +| Runtime room configuration | Available only to the source | Cached in memory and passed to WebRTC | +| General report or diagnostic log | Redacted | Redacted | + +Encrypted Setup URI sharing is the complete sharing route for managed +profiles. Preserve the independent main-remote and P2P selections and the +receiving device's own peer name. Raw profile and unencrypted QR copy actions +should offer encrypted Setup URI sharing when their output includes a managed +source, including one in an inactive profile. Never substitute a temporary +TURN password or silently export a profile missing its API token. + +Markdown settings export must not leak tokens through either the top-level +source or a profile URI. For this first delivery, omit the profile collection, +its selections, and the source projection together when managed profiles are +present, and explain that connection sharing uses the encrypted Setup URI. +Importing Markdown without that group preserves the local profiles and +selections rather than replacing them with a filtered collection. + +Reports expose only safe source labels and acquisition state. Redact the +entire opaque source configuration, including unknown source configurations, +and every stored or projected copy. Preserve the existing scheme-only +redaction of profile URIs in `src/common/reportTool.ts`. Do not log request +headers, raw API bodies, source identity values, or HTTP errors which embed +credentials. Use one redaction policy across report and diagnostic paths; +cover inactive profiles and encoded values in tests. + +## Acquisition contract + +Commonlib exports the acquisition contract from `/p2p`: + +```typescript +type IceServerConfiguration = { + iceServers: readonly RTCIceServer[]; + expiresAt: number | null; +}; + +declare class IceServerSourceError extends Error { + constructor( + code: "configuration" | "authentication" | "unavailable" | "invalid-response", + message: string, + retryable: boolean + ); +} + +interface IceServerSource { + acquire(signal: AbortSignal): Promise; +} +``` + +`expiresAt` is a local Unix timestamp in milliseconds. `null` represents +non-expiring manual configuration; managed results require a finite expiry. +Sources throw a typed, safe failure or propagate cancellation. The room owner +calls the same operation when it needs an initial or replacement credential +set. A source does not save settings, schedule renewal, mutate peers, or +start replication. + +Validate supported `stun:`, `stuns:`, `turn:`, and `turns:` URLs, complete TURN +credentials, bounded response size and entry count, and enough remaining +lifetime for connection establishment. A managed TURN source must return at +least one usable TURN entry. Copy the validated result before handing it to +WebRTC; unknown fields never become arbitrary `RTCConfiguration` options. +Preserve ordinary STUN behaviour and the selected connection-path policy. + +### Cloudflare request + +The source calls the fixed provider API: + +```http +POST https://rtc.live.cloudflare.com/v1/turn/keys/{TURN_KEY_ID}/credentials/generate-ice-servers +Authorization: Bearer {TURN_KEY_API_TOKEN} +Content-Type: application/json + +{"ttl":86400} +``` + +Cloudflare returns an `iceServers` array. Its documented maximum lifetime is +48 hours, and the returned ICE server structure has no TTL. Derive the local +expiry from the requested TTL and the time before the request started, +allowing for request duration and a connection-establishment margin. Reject a +response which has already become too old. See +[credential generation](https://developers.cloudflare.com/realtime/turn/generate-credentials/) +and [the TURN FAQ](https://developers.cloudflare.com/realtime/turn/faq/). + +Only the Key ID, API token, and requested lifetime go to the provider. The +source has no need for a Vault passphrase, Group ID, peer name, or file data. +Use a TURN Key API Token, not an account-wide API key. Cloudflare documents a +server-side secret model; this design explicitly permits users to place and +share their own issuance token on their participating devices. Whoever +receives that token can issue credentials under its authority. + +All maintained hosts inject `API.webCompatFetch`, using standard fetch +cancellation and redirect controls. The source refuses redirects, omits cookies, +requests `no-store`, and applies a 15-second deadline. It bounds the response to +32 KiB, 16 ICE entries, and 32 URLs. Commonlib independently validates the +result and requires at least 30 seconds of remaining lifetime before use. + +A read-only CORS preflight on 15 September 2026 returned HTTP 204 and allowed +POST, `Authorization`, and `Content-Type` from the requested origin. This +establishes preflight support, not successful authenticated issuance. Obsidian's +`nativeFetch` adapter is not used here because its `requestUrl` path does not +forward all required fetch controls. Provider HTTP behaviour is covered by +fixtures; operator-owned credentials are still required for real issuance and +TURN allocation validation. + +## Room reuse and credential expiry + +### Runtime state and decision + +Keep one private cached result for the effective source configuration in the +P2P room owner. It contains the validated ICE servers, `expiresAt`, and the +source identity which produced them. Reuse it while that source still matches +and its remaining lifetime is sufficient. Clear it on source change, explicit +disconnect, suspension, or owner disposal. Neither the credentials nor the +expiry becomes a persisted setting. + +`expiresAt` is derived from issuance time and the requested TTL. A fixed TTL +value alone cannot identify whether an earlier issuance has expired. Keep the +expiry check separate from the stable settings signature rather than making +wall-clock time an ordinary configuration field. + +The existing `reconcileTransport()` reuse decision becomes conceptually: + +```typescript +const reusable = + current?.host.isServing && + bindingsMatch(activeBinding, desiredBinding) && + credentialsRemainUsable(activeCredentials, now); +``` + +Manual configuration has no managed expiry and preserves the existing +behaviour. For a managed source, a missing or expired result makes the room +ineligible for reuse even if the saved settings have not changed. + +When reuse is unavailable, use the existing lifecycle queue: + +1. Retire the current session, if present. Its cancellation and settlement + path also handles any in-progress transfers. +2. Resolve valid cached credentials for the desired source, or await a new + `acquire()` result. Serialised reconciliation shares this work rather than + issuing a request for each physical peer. +3. Construct the replacement session with a temporary, resolved ICE + configuration. Keep that configuration separate from persisted manual + fields and the settings projection used for policy changes. +4. Before publishing the session, recheck the source identity, expiry, + enabled state, and room demand. Discard obsolete results and candidates. + +Acquisition and room opening have bounded deadlines. A result which expires +before publication is unusable. Each reconciliation makes one acquisition +attempt; a later explicit retry or existing reconciliation can try again. A credential test uses its own result and does not replace +the active room's cache. + +### When the check runs + +Use existing reconciliation opportunities, including explicit connection, +changes to room demand, and applicable settings/lifecycle events. Time passing +alone does not run reconciliation or close a room. If reconciliation runs +after expiry, ordinary replacement may interrupt a transfer; no additional +idle wait or transfer-preservation mechanism is required. + +Not every operation passes this decision. A transfer admitted directly by an +existing session, a signalling WebSocket reconnect, and Trystero's internal +physical-peer reconnection can proceed without owner reconciliation. This +scope checks credential validity during room reconciliation and acquires a +new set when needed. Individual physical connection attempts use the room's +existing configuration. +A room which remains open beyond expiry may require an explicit reconnect +before new TURN-dependent peers can connect. + +### Existing transport boundary + +The inspected baseline is Commonlib `0.1.24` and Trystero `0.25.3`, as pinned +in the LiveSync lockfile: + +| Package boundary | Relevant behaviour | +| --- | --- | +| Commonlib `P2PRoomSessionOwner.reconcileTransport()` | Reuses an equivalent serving room; otherwise retires it and constructs another session. | +| Commonlib `P2PRoomSession.retire()` | Rejects new work, cancels current finite operations, waits for settlement, and disposes the room. | +| Commonlib `TrysteroReplicatorP2PServer.start()` | Supplies resolved options to Trystero when joining the room. | +| Trystero `dist/strategy.mjs` and `dist/offer-pool.mjs` | The final room leave destroys the outgoing offer pool; a later join can use new options. | +| Trystero `dist/shared-peer.mjs` | Live physical peers may survive logical room leave/rejoin under Trystero ownership. | + +Use the normal retire-before-open path. LiveSync does not close raw peers or +create another transport generation. The design requires no Trystero peer +factory extension, eager-pool change, or existing-peer configuration update. +Verify fresh TURN allocation after normal room replacement in the maintained +host topology; a still-connected shared peer can remain usable and is not +proof that a fresh allocation used the new credentials. This assumes one +active P2P room per host; pool replacement while another room remains open +needs separate validation. + +### Replication after interruption + +Commonlib `0.1.24` uses `replicateShim()` for P2P transfer. Its checkpoint is +stored in database-local documents, using the source and destination database +names and a source-side marker. The Trystero peer ID is not the checkpoint +identity. Rejoining the same databases with a new peer ID therefore retains +replication progress. + +For each batch, the shim reads changes, compares destination revisions with +`revsDiff`, fetches missing revisions, writes them with `new_edits: false`, +and invokes the processing callback before advancing the checkpoint. Room +retirement does not delete the database documents or replication checkpoints. + +Consequently, the next replication attempt starts at the last committed +checkpoint. If interruption or a lost response leaves writes beyond that +checkpoint, it may scan that batch again; revision comparison avoids fetching +already stored revisions again. Missing or incomplete document revisions are +retried. This preserves received Metadata and Chunks, but does not resume a +partially received network message at its last byte. Normal P2P calls use +`rewind: false`; database replacement, removed checkpoint state, or an explicit +rewind can require an earlier scan. + +Starting that next attempt follows existing synchronisation policy. An +unfinished AutoSync baseline remains eligible when an accepted matching peer +is advertised again: `P2PAutomationCoordinator` only records completed +baselines. A cancelled manual transfer does not automatically restart merely +because the room reconnects; the next requested synchronisation uses the +same stored progress. This feature adds no universal transfer retry loop and +does not report a cancelled attempt as successful. + +A focused check executed the pinned `ReplicatorShim.js` with in-memory +database boundaries and confirmed both cancellation after a committed batch +and loss of completion after writes but before the checkpoint. Both subsequent +attempts fetched only missing revisions. The pinned automation coordinator +also allowed another attempt after a cancelled baseline. These checks verify +the algorithms; they do not establish real WebRTC reconnection or file +reflection behaviour, which remains part of implementation validation. + +### Failure and cancellation + +Acquisition failure leaves the attempted room opening unavailable and reports +a safe, actionable state. Do not fall back to saved manual credentials, +choose another provider, or relax relay-only mode. Authentication and +configuration errors wait for correction or an explicit retry. Transient +failures are marked retryable for the existing lifecycle or an explicit retry; +this source adds no automatic acquisition or reconnect loop. + +Explicit disconnect, source changes, and application suspension invalidate +pending acquisition. A late HTTP result cannot publish a room or restore an +obsolete source. Cancellation must take effect while room opening awaits +acquisition rather than waiting behind it in the lifecycle queue. The owner +rechecks current demand and configuration before exposing a replacement. + +## Compatibility and verification + +### Stored settings and sharing formats + +Update Commonlib's P2P setting type, `pickP2PSyncSettings`, connection-string +parser, Setup URI processing, and settings encryption together. Update the +LiveSync Setup dialogue, import handler, profile export, Markdown settings, +and report paths. Existing fixed-field serialisers would otherwise discard +the source. Generated credentials never populate the manual fields. + +Managed profile strings need a distinguishable format, +`sls+p2p-v2://`. Commonlib `0.1.24` rejects that scheme, whereas it silently +drops unknown fields in ordinary `sls+p2p://` strings. Manual profiles retain +their current format. Validate the source before activation; unknown sources +must not become manual connections. + +Full encrypted Setup URIs also need a distinguishable outer format, +`obsidian://setuplivesync-v2?settings=`, and a versioned encrypted +envelope when managed profiles are included. The old import path decrypts and +merges arbitrary JSON, so a nested profile version alone is insufficient. +Validate the new envelope before applying settings in every maintained host. +Apply stored settings schema checks on load and import, including downgrades; +older clients must not activate a managed profile after dropping its source. +Document any minimum-client and downgrade requirements with the implementation. + +For a selected managed source, save the complete P2P connection in its +versioned profile and disable the persisted legacy P2P projection: clear its +Group ID and passphrase, and save `P2P_Enabled` and `P2P_AutoStart` as false. +A compatible client restores those runtime values from the selected profile. +This prevents an older client which rejects the profile URI from connecting +through leftover manual fields. Source-only settings without a configured +room can remain disabled until setup is complete. The live settings and +setting-saved notifications retain their usable runtime values. A selected +manual profile retains its established persisted representation, even when +another saved profile has a managed source. + +The P2P data protocol and Group ID remain unchanged. A peer using manually +configured TURN can communicate with one using issued credentials; validate +that interoperability without requiring both peers to use the same issuer. + +### Real-provider verification + +On 15 September 2026, the local LiveSync build with Commonlib +`0.1.25-dev.turn-credentials.3` passed a real Cloudflare TURN check in two +isolated Obsidian 1.12.7 instances on one Linux host. Both instances used the +Cloudflare source and `P2P_connectionPath: "relay"`, with a local Nostr relay +used only for signalling. + +- The source received HTTP 201 responses and acquired credentials with a + requested 24-hour lifetime. The Obsidian instances also received successful + issuance responses through their own HTTP integration. +- Both endpoints reported selected local and remote candidates of type + `relay`, using UDP, before transferring a note. The receiving Vault contained + the expected note content after replication completed. +- Explicitly disconnecting one instance removed its peer advertisement from + the other. Reconnecting issued credentials again and established a new + relay-only connection. A second note then travelled in the reverse direction + and appeared with the expected content in the receiving Vault. + +This check covers initial provider issuance, real relayed replication, and +credential reacquisition after an explicit disconnect. It does not establish +natural TTL expiry, interruption within a replication batch, mobile operating +system behaviour, mixed manual/managed peers, or connectivity between different +networks. Those cases retain their separate validation requirements. The +results contain no provider token, TURN username, or TURN credential. + +### Acceptance criteria for implementation + +- Manual configuration, default STUN, and existing Setup URIs retain their + behaviour. Unsupported managed sources fail explicitly. +- Provider tokens survive restart, profile selection, optional configuration + encryption, and encrypted Setup URI sharing. Reports and logs reveal no + tokens or issued credentials, including inactive and encoded copies. +- Issued credentials never enter persisted settings, exports, or reports. +- Equivalent settings and valid credentials reuse the room. Expired + credentials cause the next owner reconciliation to acquire and replace + through the existing lifecycle; manual settings retain their behaviour. +- Concurrent reconciliation does not duplicate acquisition. Late responses + after disconnect, source change, or suspension cannot publish a room. + Expiry tests cover delayed responses and clock changes. +- Time passing alone triggers no acquisition or replacement. There is no + per-peer acquisition hook, `setConfiguration()`, or credential-driven ICE + restart. +- Replacement during a batch settles the old attempt and preserves stored + documents and checkpoints. The next attempt transfers missing revisions; + test interrupted AutoSync and explicit manual retry separately. +- Safe failures cover authentication, rate limits, network errors, timeouts, + and malformed responses without an automatic source or route-policy change. +- Real relay-only connections verify initial establishment and room + replacement after expiry, including mixed manual/managed peers and both + initiator roles. A selected relayed candidate pair is required evidence; + direct traffic alone does not validate TURN use. +- Real Obsidian checks cover HTTP behaviour, desktop/mobile lifecycle, + persistence/sharing, and a file round trip after reconnection. Validate + supported CLI/browser hosts before enabling their direct integration. + +The existing Setup connection check remains a signalling check. Credential +issuance, a disposable TURN allocation check, actual peer data transfer, and +LiveSync file synchronisation establish different facts. Tests and status +must identify which boundary they verify. + +Implement the Commonlib contract, settings, runtime expiry, and existing room +replacement integration in its own repository. Validate the packed Commonlib +artefact before updating LiveSync's exact dependency and composing the +Cloudflare source. Use deterministic provider fixtures and an open-source +Coturn test service for repeatable +coverage; verify the real provider path with operator-owned test credentials. + +Run Commonlib checks, LiveSync `npm run check`, unit tests, builds, and focused +consumer tests for the implementation. Deterministic source, lifecycle, persistence, sharing, and redaction tests +cover the implemented boundaries. Real provider allocation and host +reconnection evidence must be recorded separately before release. diff --git a/docs/p2p.md b/docs/p2p.md index 23253771..1184409c 100644 --- a/docs/p2p.md +++ b/docs/p2p.md @@ -18,7 +18,7 @@ flowchart LR The signalling relay and TURN server have different roles: - The **signalling relay** is required for peer discovery and connection negotiation. LiveSync uses Nostr-compatible WebSocket relays for this role. The relay does not store or transfer Vault contents. -- A **TURN server** is an optional fallback. WebRTC uses it to relay the encrypted peer connection only when the devices cannot establish a direct path through their networks. +- A **TURN server** is an optional fallback. WebRTC uses it to relay the encrypted peer connection when the devices cannot establish a direct path through their networks, or whenever **TURN relay only** is selected. ## The project's public signalling relay @@ -40,16 +40,50 @@ Both settings contain server addresses, but they are not interchangeable. | Setting | Required | Carries Vault contents | Purpose | | --- | --- | --- | --- | | **Signalling relay URLs** | Yes | No | Finds peers and exchanges the information needed to establish WebRTC connections. | -| **TURN server URLs** | Only when direct WebRTC connectivity fails | Encrypted WebRTC traffic | Relays traffic between peers when NAT or firewall rules prevent a direct path. | +| **TURN server URLs** | When direct WebRTC connectivity fails or **TURN relay only** is selected | Encrypted WebRTC traffic | Relays traffic between peers when NAT or firewall rules prevent a direct path. | -A TURN provider cannot read LiveSync's encrypted Vault contents, but it can observe connection metadata and traffic volume. Use a provider you trust. The project does not operate an official TURN service. +WebRTC encrypts data between the devices, including when it passes through TURN. The TURN provider cannot read the transferred data, but it can observe network addresses and traffic volume. This transport encryption also applies when LiveSync's optional database encryption is disabled. The project does not operate an official TURN service. + +## TURN credentials + +In **TURN configuration**, select **Manual** to enter your own TURN server URLs, +username, and credential, or select **Cloudflare** to enter a **TURN Key ID** and +**TURN Key API Token**. Cloudflare is optional; the project does not require a +particular TURN provider or operate a credential broker. See Cloudflare's +[credential instructions](https://developers.cloudflare.com/realtime/turn/generate-credentials/) +for creating a TURN key and its API token. + +The API token is saved with the P2P profile. Use an encrypted Setup URI to share +it with your other devices. Managed profiles use the versioned Setup URI format +and require a client which supports that format; update receiving devices +before importing it. Older clients leave the saved managed P2P connection +disabled. Select and save a manual TURN configuration in a compatible client +before downgrading if P2P must remain usable. Plain QR export redirects to +encrypted Setup URI sharing. +Markdown settings omit the connection profile group when it contains a managed +TURN source, including inactive profiles, and importing those omitted settings +preserves this device's existing profiles. Diagnostic reports redact the source +configuration. Optional configuration encryption also covers the saved token. + +Each device requests temporary TURN credentials before opening a room when no +valid credentials are cached. Cloudflare credentials have a requested lifetime +of 24 hours and remain in memory only. Expiry is checked when LiveSync next +reconciles the room connection. If necessary, it replaces the room and obtains +new credentials. There is no periodic renewal: if a long-lived room cannot +reconnect after credentials expire, disconnect and open the connection again. + +Room replacement may interrupt replication. The next synchronisation keeps +received Metadata and Chunks, resumes from its saved checkpoint, and compares +revisions to fetch missing data. An unfinished network message can be sent +again. Automatic synchronisation follows the existing peer rules; after an +interrupted manual operation, use **Replicate now** again. ## Connection compatibility profiles `P2P Configuration` includes a separate `Connection compatibility` section. Its defaults preserve the existing transport behaviour: - **P2P message size** defaults to **Standard**. **Reduced**, **Conservative**, and **Maximum compatibility** progressively limit outgoing P2P messages when a network path appears to drop larger WebRTC messages. This is not a Vault Chunk size or an IP MTU. Smaller values add framing and processing overhead. -- **Connection path** defaults to **Automatic**, which lets WebRTC select a viable direct or TURN-relayed path. **TURN relay only** forces the encrypted connection through TURN and is available only when the profile contains at least one valid `turn:` or `turns:` URL. +- **Connection path** defaults to **Automatic**, which lets WebRTC select a viable direct or TURN-relayed path. **TURN relay only** forces the encrypted connection through TURN and is available when the profile contains a valid manual TURN URL or a configured TURN credential source. The sending device controls its outgoing message size. Select the same conservative preset on every device which may send across the constrained path. Existing devices do not receive the choice retrospectively merely because another device changed it. diff --git a/docs/settings.md b/docs/settings.md index feddbb73..96fab9f8 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -485,6 +485,26 @@ Setting key: P2P_AutoBroadcast When enabled, this device notifies connected peers after a local change. The notification contains no Vault data. A receiving peer fetches the change only when it follows this device. +#### TURN configuration + +Setting key: P2P_iceServerSource + +Select **Manual** for the existing TURN server fields, or **Cloudflare** for a +TURN Key ID and TURN Key API Token. The API token is persisted with the profile +and included in encrypted Setup URI sharing. Issued temporary credentials are +kept in memory only. Reports redact the source configuration. See +[TURN credentials](p2p.md#turn-credentials) for sharing, expiry, and reconnect +behaviour. + +#### TURN Key ID and TURN Key API Token + +Setting keys: P2P_iceServerSource.configuration.turnKeyId, +P2P_iceServerSource.configuration.apiToken + +These fields appear when **Cloudflare** is selected. Enter the TURN key's ID and +its dedicated API token. The token field is masked. No account ID, custom +endpoint, or renewal interval is required. + #### TURN Server URLs (comma-separated) Setting key: P2P_turnServers @@ -515,7 +535,7 @@ The sender controls the size of its outgoing messages. Select the same conservat Setting key: P2P_connectionPath -**Automatic** lets WebRTC select a viable direct or TURN-relayed path and is the default. **TURN relay only** forces `iceTransportPolicy: 'relay'` and is available only when the profile contains at least one valid `turn:` or `turns:` URL. Removing the last valid TURN URL while relay-only mode is selected restores **Automatic** and displays a Notice. +**Automatic** lets WebRTC select a viable direct or TURN-relayed path and is the default. **TURN relay only** forces `iceTransportPolicy: 'relay'` and is available when the profile contains a valid manual TURN URL or a configured TURN credential source. Removing the manual TURN configuration while relay-only mode is selected restores **Automatic** and displays a Notice. A selected credential source which cannot supply credentials prevents the connection from opening; it does not change the connection path. This choice belongs to the P2P profile and is retained in P2P connection strings and encrypted Setup URIs. Separate profiles may use the same Group ID and credentials with different compatibility choices; only the selected P2P profile is active. diff --git a/eslint.community.config.mjs b/eslint.community.config.mjs index 4944a5f8..059b82f7 100644 --- a/eslint.community.config.mjs +++ b/eslint.community.config.mjs @@ -63,6 +63,13 @@ export default defineConfig( "@typescript-eslint/no-unnecessary-type-assertion": "warn", }, }, + { + files: ["src/integrations/**/*.ts"], + rules: { + // External-service integrations also run in Node and do not own window UI. + "obsidianmd/no-global-this": "off", + }, + }, { files: ["src/apps/**/*.{ts,js,mjs}"], rules: { diff --git a/eslint.config.mjs b/eslint.config.mjs index b436c9e3..84cbbfbd 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -99,6 +99,13 @@ export default defineConfig([ ...ImportAliasRules("."), }, }, + { + files: ["src/integrations/**/*.ts"], + rules: { + // External-service integrations also run in Node and do not own window UI. + "obsidianmd/no-global-this": "off", + }, + }, { files: ["src/apps/**/*.ts"], rules: { diff --git a/package-lock.json b/package-lock.json index 6e70af04..6261b61d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "@smithy/types": "^4.14.3", "@smithy/util-retry": "^4.4.5", "@vrtmrz/browser-ui-kit": "0.1.0", - "@vrtmrz/livesync-commonlib": "0.1.24", + "@vrtmrz/livesync-commonlib": "file:.devmemo.local/packages/vrtmrz-livesync-commonlib-0.1.25-dev.turn-credentials.3.tgz", "@vrtmrz/obsidian-plugin-kit": "0.1.4", "@vrtmrz/ui-interactions": "0.1.2", "diff-match-patch": "^1.0.5", @@ -4567,9 +4567,9 @@ } }, "node_modules/@vrtmrz/livesync-commonlib": { - "version": "0.1.24", - "resolved": "https://registry.npmjs.org/@vrtmrz/livesync-commonlib/-/livesync-commonlib-0.1.24.tgz", - "integrity": "sha512-gOXKo3ptEUYDkjLd5PGq2vAhOSvxOqW6xE7YWo9Y8ienglfYBz8R3eZ0I/JruvwZltH2B7Bmi41pHMjmRJQe3Q==", + "version": "0.1.25-dev.turn-credentials.3", + "resolved": "file:.devmemo.local/packages/vrtmrz-livesync-commonlib-0.1.25-dev.turn-credentials.3.tgz", + "integrity": "sha512-hngE1zlNocD8IeMgRvssX4WN7SpINOyWOp9Mzs0PGM10yzVvYtR5WcVdxWtEIbaA71DLk9SPmgmQkgu+o7Oy8g==", "license": "MIT", "dependencies": { "@aws-sdk/client-s3": "^3.808.0", diff --git a/package.json b/package.json index abacfc2b..706922eb 100644 --- a/package.json +++ b/package.json @@ -181,7 +181,7 @@ "@smithy/types": "^4.14.3", "@smithy/util-retry": "^4.4.5", "@vrtmrz/browser-ui-kit": "0.1.0", - "@vrtmrz/livesync-commonlib": "0.1.24", + "@vrtmrz/livesync-commonlib": "file:.devmemo.local/packages/vrtmrz-livesync-commonlib-0.1.25-dev.turn-credentials.3.tgz", "@vrtmrz/obsidian-plugin-kit": "0.1.4", "@vrtmrz/ui-interactions": "0.1.2", "diff-match-patch": "^1.0.5", diff --git a/src/apps/browser/BrowserP2PTransportSettings.svelte b/src/apps/browser/BrowserP2PTransportSettings.svelte index eed3bc52..2d13a081 100644 --- a/src/apps/browser/BrowserP2PTransportSettings.svelte +++ b/src/apps/browser/BrowserP2PTransportSettings.svelte @@ -1,105 +1,61 @@
Optional TURN server settings -

- Configure TURN only when a direct peer-to-peer connection cannot be established. -

- - - +

Configure TURN only when a direct peer-to-peer connection cannot be established.

+
- -
@@ -107,27 +63,7 @@
diff --git a/src/apps/cli/commands/runCommand.ts b/src/apps/cli/commands/runCommand.ts index 8f27b870..8e3bf50c 100644 --- a/src/apps/cli/commands/runCommand.ts +++ b/src/apps/cli/commands/runCommand.ts @@ -1,5 +1,5 @@ import { decodeSettingsFromSetupURI } from "@vrtmrz/livesync-commonlib/compat/API/processSetting"; -import { configURIBase } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const"; +import { configURIBase, configURIBaseV2 } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const"; import { DEFAULT_SETTINGS, type FilePathWithPrefix, @@ -298,7 +298,7 @@ export async function runCommand(options: CLIOptions, context: CLICommandContext throw new Error("setup requires one argument: "); } const setupURI = options.commandArgs[0].trim(); - if (!setupURI.startsWith(configURIBase)) { + if (!setupURI.startsWith(configURIBase) && !setupURI.startsWith(configURIBaseV2)) { throw new Error(`setup URI must start with ${configURIBase}`); } const passphrase = await standardIo.prompt("Enter setup URI passphrase: "); diff --git a/src/apps/cli/commands/runCommand.unit.spec.ts b/src/apps/cli/commands/runCommand.unit.spec.ts index 651e47d1..f34e7cfd 100644 --- a/src/apps/cli/commands/runCommand.unit.spec.ts +++ b/src/apps/cli/commands/runCommand.unit.spec.ts @@ -1,7 +1,7 @@ import { fsPromises as fs, os, path } from "@vrtmrz/livesync-commonlib/node"; import * as processSetting from "@vrtmrz/livesync-commonlib/compat/API/processSetting"; import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString"; -import { configURIBase } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const"; +import { configURIBase, configURIBaseV2 } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const"; import { DEFAULT_SETTINGS, REMOTE_COUCHDB, @@ -419,6 +419,31 @@ describe("runCommand abnormal cases", () => { expect(appliedSettings.useIndexedDBAdapter).toBe(false); }); + it("setup imports managed TURN through the versioned encrypted URI", async () => { + const core = createCoreMock(); + const source = { + version: 1, + id: "cloudflare", + configuration: { turnKeyId: "turn-key", apiToken: "private-token" }, + }; + const passphrase = "setup-passphrase"; + const setupURI = await processSetting.encodeSettingsToSetupURI( + { + ...DEFAULT_SETTINGS, + P2P_iceServerSource: source, + }, + passphrase + ); + expect(setupURI.startsWith(configURIBaseV2)).toBe(true); + expect(setupURI).not.toContain("private-token"); + core.services.context.standardIo.prompt.mockResolvedValue(passphrase); + await runCommand(makeOptions("setup", [setupURI]), { ...context, core }); + expect(core.services.setting.applyExternalSettings).toHaveBeenCalledWith( + expect.objectContaining({ P2P_iceServerSource: source }), + true + ); + }); + it("setup rejects encoded URI when passphrase is wrong", async () => { const core = createCoreMock(); const setupURI = await createSetupURI("correct-passphrase"); diff --git a/src/apps/cli/main.ts b/src/apps/cli/main.ts index 1b2bff53..7f17e0a9 100644 --- a/src/apps/cli/main.ts +++ b/src/apps/cli/main.ts @@ -1,3 +1,4 @@ +import { useIceServerSources } from "@/serviceFeatures/useIceServerSources"; import { NodeServiceContext, NodeServiceHub } from "./services/NodeServiceHub"; import { configureNodeLocalStorage, ensureGlobalNodeLocalStorage } from "./services/NodeLocalStorage"; import { LiveSyncBaseCore, type StartupDatabaseOptions } from "@/LiveSyncBaseCore"; @@ -524,7 +525,9 @@ export async function main( useOfflineScanner(core); } // Register P2P replicator feature. - p2pReplicator = useP2PReplicatorFeature(core); + p2pReplicator = useP2PReplicatorFeature(core, undefined, undefined, { + iceServerSources: useIceServerSources(core.services.API.webCompatFetch.bind(core.services.API)), + }); // Add target filter to prevent internal files are handled core.services.vault.isTargetFile.addHandler(async (target) => { const targetPath = stripAllPrefixes(getPathFromUXFileInfo(target)); diff --git a/src/apps/webapp/WebAppRuntime.ts b/src/apps/webapp/WebAppRuntime.ts index 63940081..9fa841cb 100644 --- a/src/apps/webapp/WebAppRuntime.ts +++ b/src/apps/webapp/WebAppRuntime.ts @@ -1,3 +1,4 @@ +import { useIceServerSources } from "@/serviceFeatures/useIceServerSources"; /** Browser runtime for Self-hosted LiveSync over the File System Access API. */ import { LiveSyncBaseCore } from "@/LiveSyncBaseCore"; @@ -217,7 +218,9 @@ export class WebAppRuntime { useRedFlagFeatures(core); useCheckRemoteSize(core); useRemoteConfiguration(core); - this.p2p = useP2PReplicatorFeature(core); + this.p2p = useP2PReplicatorFeature(core, undefined, undefined, { + iceServerSources: useIceServerSources(core.services.API.webCompatFetch.bind(core.services.API)), + }); this.paneHost = { services: core.services, p2p: this.p2p, diff --git a/src/apps/webpeer/src/WebPeerRuntime.ts b/src/apps/webpeer/src/WebPeerRuntime.ts index c6e7d433..da0d87e8 100644 --- a/src/apps/webpeer/src/WebPeerRuntime.ts +++ b/src/apps/webpeer/src/WebPeerRuntime.ts @@ -1,3 +1,4 @@ +import { useIceServerSources } from "@/serviceFeatures/useIceServerSources"; import { type P2PSyncSetting, SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types"; import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions"; import { EVENT_LAYOUT_READY } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents"; @@ -70,9 +71,8 @@ export class WebPeerRuntime { isScheduled: () => this.restartScheduled, }, }); - this.p2p = useP2PReplicatorFeature({ - services: this.services, - serviceModules: {}, + this.p2p = useP2PReplicatorFeature({ services: this.services, serviceModules: {} }, undefined, undefined, { + iceServerSources: useIceServerSources(this.services.API.webCompatFetch.bind(this.services.API)), }); this.p2pLogCollector = new P2PLogCollector(this.events); this.paneHost = { diff --git a/src/common/messages/LiveSyncProvisionalMessages.ts b/src/common/messages/LiveSyncProvisionalMessages.ts index 37f0a7b3..5acfcb64 100644 --- a/src/common/messages/LiveSyncProvisionalMessages.ts +++ b/src/common/messages/LiveSyncProvisionalMessages.ts @@ -7,6 +7,33 @@ * remove it from this map in the same change. */ export const liveSyncProvisionalEnglishMessages = { + "Configure TURN when a direct connection cannot be established or when you select TURN relay only.": + "Configure TURN when a direct connection cannot be established or when you select TURN relay only.", + "TURN configuration could not be decrypted.": "TURN configuration could not be decrypted.", + "TURN configuration": "TURN configuration", + Manual: "Manual", + Cloudflare: "Cloudflare", + "TURN Key ID": "TURN Key ID", + "TURN Key API Token": "TURN Key API Token", + "Unsupported TURN configuration": "Unsupported TURN configuration", + "The API token is saved with this profile and included in encrypted Setup URI sharing. Temporary TURN credentials are kept in memory only.": + "The API token is saved with this profile and included in encrypted Setup URI sharing. Temporary TURN credentials are kept in memory only.", + "TURN relay only requires a TURN server or a configured credential source under Advanced Settings.": + "TURN relay only requires a TURN server or a configured credential source under Advanced Settings.", + "TURN relay only requires TURN configuration. Connection path has been restored to Automatic.": + "TURN relay only requires TURN configuration. Connection path has been restored to Automatic.", + "Cloudflare TURN configuration is invalid.": "Cloudflare TURN configuration is invalid.", + "Cloudflare TURN configuration contains an unsupported field.": + "Cloudflare TURN configuration contains an unsupported field.", + "Enter a TURN Key ID.": "Enter a TURN Key ID.", + "TURN Key ID contains unsupported characters.": "TURN Key ID contains unsupported characters.", + "Enter a TURN Key API Token.": "Enter a TURN Key API Token.", + "TURN Key API Token must use Bearer token syntax.": "TURN Key API Token must use Bearer token syntax.", + "TURN configuration source version is not supported.": "TURN configuration source version is not supported.", + "TURN configuration source is invalid.": "TURN configuration source is invalid.", + "The selected TURN configuration source is not supported.": + "The selected TURN configuration source is not supported.", + "Setup Complete: Preparing to Fetch from Another Device": "Setup Complete: Preparing to Fetch from Another Device", "The P2P connection has been configured successfully. The initial synchronisation data must now be fetched from an online source device.": "The P2P connection has been configured successfully. The initial synchronisation data must now be fetched from an online source device.", @@ -28,8 +55,8 @@ export const liveSyncProvisionalEnglishMessages = { "The project's public signalling relay is a best-effort convenience operated by the project author. It does not store Vault contents, but signalling metadata may be visible to the relay. Availability and log retention are not guaranteed. You can replace it with your own Nostr-compatible relay.", "Learn more about P2P connections": "Learn more about P2P connections", "Learn more about signalling and TURN": "Learn more about signalling and TURN", - "TURN relays the encrypted WebRTC connection only when a direct path cannot be established. A TURN provider cannot read encrypted Vault contents, but it can observe connection metadata and traffic volume. Use a provider you trust.": - "TURN relays the encrypted WebRTC connection only when a direct path cannot be established. A TURN provider cannot read encrypted Vault contents, but it can observe connection metadata and traffic volume. Use a provider you trust.", + "WebRTC encrypts data between your devices, including when it passes through TURN. The TURN provider cannot read the transferred data. It can see network addresses and traffic volume.": + "WebRTC encrypts data between your devices, including when it passes through TURN. The TURN provider cannot read the transferred data. It can see network addresses and traffic volume.", "Connection compatibility": "Connection compatibility", "P2P message size": "P2P message size", Standard: "Standard", diff --git a/src/common/reportTool.ts b/src/common/reportTool.ts index 5942dfa8..e8bded42 100644 --- a/src/common/reportTool.ts +++ b/src/common/reportTool.ts @@ -1,3 +1,4 @@ +import { redactTurnSourceForReport } from "./turnSettingsPrivacy"; import { REMOTE_COUCHDB, REMOTE_MINIO } from "@vrtmrz/livesync-commonlib/compat/common/models/setting.const"; import { DEFAULT_SETTINGS, type ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/settings"; import { generateCredentialObject } from "@vrtmrz/livesync-commonlib/compat/replication/httplib"; @@ -67,6 +68,7 @@ export async function generateReport(settings: ObsidianLiveSyncSettings, core: L delete pluginConfig[key as keyof ObsidianLiveSyncSettings]; } + redactTurnSourceForReport(pluginConfig); pluginConfig.couchDB_DBNAME = REDACTED; pluginConfig.couchDB_PASSWORD = REDACTED; const scheme = pluginConfig.couchDB_URI.startsWith("http:") diff --git a/src/common/reportTool.unit.spec.ts b/src/common/reportTool.unit.spec.ts new file mode 100644 index 00000000..18afd7fe --- /dev/null +++ b/src/common/reportTool.unit.spec.ts @@ -0,0 +1,41 @@ +import { describe, expect, it, vi } from "vitest"; +import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/settings"; +import { REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/types"; +import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore"; +import { generateReport } from "./reportTool"; + +vi.mock("./utils", () => ({ requestToCouchDBWithCredentials: vi.fn() })); +vi.mock("@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions", () => ({ + compatGlobal: { origin: "test", navigator: { userAgent: "test" } }, +})); + +describe("TURN credentials in diagnostic reports", () => { + it("redacts top-level, encrypted, and inactive encoded source copies", async () => { + const token = "private+token/with=symbols"; + const source = { version: 1, id: "cloudflare", configuration: { turnKeyId: "private-key", apiToken: token } }; + const settings = { + ...DEFAULT_SETTINGS, + remoteType: REMOTE_P2P, + P2P_iceServerSource: source, + encryptedP2PIceServerSource: "encrypted-private-copy", + remoteConfigurations: { + inactive: { + id: "inactive", + name: "Inactive TURN", + isEncrypted: false, + uri: `sls+p2p-v2://room?source=${encodeURIComponent(JSON.stringify(source))}`, + }, + }, + }; + const core = { services: { vault: { isStorageInsensitive: () => false } } } as unknown as LiveSyncBaseCore; + const report = await generateReport(settings, core); + const text = JSON.stringify(report); + expect(text).not.toContain(token); + expect(text).not.toContain(encodeURIComponent(token)); + expect(text).not.toContain("private-key"); + expect(text).not.toContain("encrypted-private-copy"); + expect(report.pluginConfig.remoteConfigurations.inactive.uri).toBe("sls+p2p-v2://"); + expect(settings.P2P_iceServerSource).toEqual(source); + expect(settings.encryptedP2PIceServerSource).toBe("encrypted-private-copy"); + }); +}); diff --git a/src/common/turnSettingsPrivacy.ts b/src/common/turnSettingsPrivacy.ts new file mode 100644 index 00000000..adc3ae32 --- /dev/null +++ b/src/common/turnSettingsPrivacy.ts @@ -0,0 +1,52 @@ +import { + hasManagedP2PIceServerSource as hasManagedTurnSettings, + type ObsidianLiveSyncSettings, +} from "@vrtmrz/livesync-commonlib/compat/common/types"; + +import { iceServerSourceDefinitions } from "@/integrations/iceServerSources"; + +export { hasManagedTurnSettings }; + +/** Reports retain the selected source label, but no opaque source configuration. */ +export function redactTurnSourceForReport(settings: Partial): void { + if (settings.encryptedP2PIceServerSource) settings.encryptedP2PIceServerSource = "REDACTED"; + if (settings.P2P_iceServerSource !== undefined) { + settings.P2P_iceServerSource = { + version: 1, + id: + iceServerSourceDefinitions.find((source) => source.id === settings.P2P_iceServerSource?.id)?.id ?? + "redacted", + configuration: { redacted: true }, + }; + } +} + +/** Managed connection profiles are shared through encrypted Setup URIs. */ +export function omitManagedTurnProfilesFromMarkdown(settings: Partial): void { + if (!hasManagedTurnSettings(settings)) return; + delete settings.P2P_iceServerSource; + delete settings.encryptedP2PIceServerSource; + delete settings.remoteConfigurations; + delete settings.activeConfigurationId; + delete settings.P2P_ActiveRemoteConfigurationId; +} + +/** An omitted profile group leaves this device's existing connection selection intact. */ +export function preserveManagedTurnProfilesOnMarkdownImport( + incoming: Partial, + current: ObsidianLiveSyncSettings, + merged: ObsidianLiveSyncSettings +): void { + if ( + !hasManagedTurnSettings(current) || + incoming.remoteConfigurations !== undefined || + incoming.P2P_iceServerSource !== undefined + ) { + return; + } + merged.remoteConfigurations = structuredClone(current.remoteConfigurations); + merged.activeConfigurationId = current.activeConfigurationId; + merged.P2P_ActiveRemoteConfigurationId = current.P2P_ActiveRemoteConfigurationId; + merged.P2P_iceServerSource = structuredClone(current.P2P_iceServerSource); + merged.encryptedP2PIceServerSource = current.encryptedP2PIceServerSource; +} diff --git a/src/common/turnSettingsPrivacy.unit.spec.ts b/src/common/turnSettingsPrivacy.unit.spec.ts new file mode 100644 index 00000000..5a56b967 --- /dev/null +++ b/src/common/turnSettingsPrivacy.unit.spec.ts @@ -0,0 +1,71 @@ +import { describe, expect, it } from "vitest"; +import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types"; +import { + hasManagedTurnSettings, + omitManagedTurnProfilesFromMarkdown, + preserveManagedTurnProfilesOnMarkdownImport, + redactTurnSourceForReport, +} from "./turnSettingsPrivacy"; + +function configuredSettings() { + return { + ...DEFAULT_SETTINGS, + P2P_iceServerSource: { + version: 1, + id: "cloudflare", + configuration: { turnKeyId: "private-key-id", apiToken: "private-token" }, + }, + remoteConfigurations: { + managed: { + id: "managed", + name: "Managed TURN", + isEncrypted: false, + uri: "sls+p2p-v2://room?source=private-token", + }, + }, + activeConfigurationId: "central", + P2P_ActiveRemoteConfigurationId: "managed", + }; +} + +describe("managed TURN settings privacy", () => { + it("redacts all opaque source fields, including unknown integrations", () => { + const settings = configuredSettings(); + settings.P2P_iceServerSource.id = "private-token"; + redactTurnSourceForReport(settings); + expect(JSON.stringify(settings.P2P_iceServerSource)).not.toMatch(/private-token|private-key-id/); + expect(settings.P2P_iceServerSource.configuration).toEqual({ redacted: true }); + }); + + it("omits the whole managed profile group from Markdown, including inactive sources", () => { + const settings = configuredSettings(); + settings.P2P_iceServerSource.id = "manual"; + expect(hasManagedTurnSettings(settings)).toBe(true); + omitManagedTurnProfilesFromMarkdown(settings); + expect(JSON.stringify(settings)).not.toMatch(/private-token|private-key-id|sls\+p2p-v2/); + expect(settings).not.toHaveProperty("remoteConfigurations"); + expect(settings).not.toHaveProperty("activeConfigurationId"); + expect(settings).not.toHaveProperty("P2P_ActiveRemoteConfigurationId"); + }); + + it("preserves existing profiles and both selections when Markdown omits the group", () => { + const current = configuredSettings(); + const incoming = { ...DEFAULT_SETTINGS }; + delete (incoming as Partial).remoteConfigurations; + delete (incoming as Partial).P2P_iceServerSource; + const merged = { ...DEFAULT_SETTINGS, ...incoming }; + preserveManagedTurnProfilesOnMarkdownImport(incoming, current, merged); + expect(merged.remoteConfigurations).toEqual(current.remoteConfigurations); + expect(merged.remoteConfigurations).not.toBe(current.remoteConfigurations); + expect(merged.P2P_iceServerSource).toEqual(current.P2P_iceServerSource); + expect(merged.activeConfigurationId).toBe("central"); + expect(merged.P2P_ActiveRemoteConfigurationId).toBe("managed"); + }); + + it("retains the manual-only Markdown contract", () => { + const settings = { ...DEFAULT_SETTINGS }; + const before = structuredClone(settings); + omitManagedTurnProfilesFromMarkdown(settings); + expect(settings).toEqual(before); + }); +}); diff --git a/src/common/types.ts b/src/common/types.ts index 2be50128..374e73d6 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -51,7 +51,7 @@ export type queueItem = { export const FileWatchEventQueueMax = 10; -export { configURIBase, configURIBaseQR } from "@vrtmrz/livesync-commonlib/compat/common/types"; +export { configURIBase, configURIBaseV2, configURIBaseQR } from "@vrtmrz/livesync-commonlib/compat/common/types"; export { CHeader, diff --git a/src/features/P2PSync/TurnConfiguration.svelte b/src/features/P2PSync/TurnConfiguration.svelte new file mode 100644 index 00000000..780b4d68 --- /dev/null +++ b/src/features/P2PSync/TurnConfiguration.svelte @@ -0,0 +1,83 @@ + + +
+ + {#if sourceId === "manual"} + + + + {:else if definition} + {#each definition.fields as field (field.key)} + + {/each} +

{translate("The API token is saved with this profile and included in encrypted Setup URI sharing. Temporary TURN credentials are kept in memory only.")}

+ {/if} + {#if error} +

{translateIfAvailable(error)}

+ {/if} +
+ + diff --git a/src/integrations/cloudflare/iceServerSource.ts b/src/integrations/cloudflare/iceServerSource.ts new file mode 100644 index 00000000..07e81150 --- /dev/null +++ b/src/integrations/cloudflare/iceServerSource.ts @@ -0,0 +1,384 @@ +import { IceServerSourceError } from "@vrtmrz/livesync-commonlib/p2p"; +import type { IceServerConfiguration, IceServerSource } from "@vrtmrz/livesync-commonlib/p2p"; +import { + CLOUDFLARE_TURN_CREDENTIAL_ENDPOINT, + CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS, + parseCloudflareIceServerSourceConfiguration, + type CloudflareIceServerSourceConfiguration, + validateCloudflareIceServerSourceConfiguration, +} from "./settings"; + +/** Fetch-compatible function supplied by the host composition. */ +export type CloudflareIceServerSourceFetch = (input: string | Request, init?: RequestInit) => Promise; + +export interface CloudflareIceServerSourceDependencies { + readonly fetch: CloudflareIceServerSourceFetch; + readonly now?: () => number; + readonly requestDeadlineMs?: number; +} + +export const CLOUDFLARE_TURN_REQUEST_DEADLINE_MS = 15_000 as const; +export const CLOUDFLARE_TURN_MAX_RESPONSE_BYTES = 32 * 1024; +export const CLOUDFLARE_TURN_MAX_ICE_SERVER_ENTRIES = 16 as const; +export const CLOUDFLARE_TURN_MAX_ICE_SERVER_URLS = 32 as const; +export const CLOUDFLARE_TURN_MIN_REMAINING_LIFETIME_MS = 1_000 as const; + +type IceServerSourceFailureCode = "configuration" | "authentication" | "unavailable" | "invalid-response"; + +const SOURCE_FAILURE_MESSAGES: Record = { + configuration: "The Cloudflare TURN source configuration is invalid.", + authentication: "The Cloudflare TURN credential request was not authorised.", + unavailable: "The Cloudflare TURN service is unavailable.", + "invalid-response": "The Cloudflare TURN service returned an invalid response.", +}; + +function sourceFailure(code: IceServerSourceFailureCode, retryable: boolean): IceServerSourceError { + return new IceServerSourceError(code, SOURCE_FAILURE_MESSAGES[code], retryable); +} + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function abortError(): Error { + try { + return new DOMException("The operation was aborted.", "AbortError"); + } catch { + const error = new Error("The operation was aborted."); + error.name = "AbortError"; + return error; + } +} + +function throwIfAborted(signal: AbortSignal): void { + if (signal.aborted) { + throw abortError(); + } +} + +function isControlCharacter(value: string): boolean { + return Array.from(value).some((character) => { + const code = character.charCodeAt(0); + return code <= 0x1f || code === 0x7f; + }); +} + +function isPort(value: string): boolean { + if (!/^\d{1,5}$/.test(value)) return false; + const port = Number(value); + return port >= 1 && port <= 65_535; +} + +function isHost(value: string): boolean { + return value.length > 0 && /^[A-Za-z0-9._-]+$/.test(value); +} + +/** + * Validates the URL forms accepted by WebRTC's ICE server configuration. + * TURN URLs may carry only the standard transport query parameter; userinfo, + * paths, fragments, and arbitrary query values are not accepted. + */ +export function isSupportedIceServerUrl(value: string): boolean { + if (value.length === 0 || value.length > 2_048 || isControlCharacter(value)) return false; + const schemeMatch = /^(stun|stuns|turn|turns):(.+)$/i.exec(value); + if (!schemeMatch) return false; + + const remainder = schemeMatch[2]; + const queryIndex = remainder.indexOf("?"); + const authority = queryIndex >= 0 ? remainder.slice(0, queryIndex) : remainder; + const query = queryIndex >= 0 ? remainder.slice(queryIndex + 1) : ""; + if (authority.length === 0 || authority.includes("/") || authority.includes("#") || authority.includes("@")) { + return false; + } + if (authority.includes("%")) return false; + + if (authority.startsWith("[")) { + const closingBracket = authority.indexOf("]"); + if (closingBracket < 0) return false; + const host = authority.slice(1, closingBracket); + if (!/^[0-9A-Fa-f:.]+$/.test(host) || !host.includes(":")) return false; + const suffix = authority.slice(closingBracket + 1); + if (suffix !== "" && (!suffix.startsWith(":") || !isPort(suffix.slice(1)))) return false; + } else { + const colonIndex = authority.lastIndexOf(":"); + const host = colonIndex >= 0 ? authority.slice(0, colonIndex) : authority; + if (!isHost(host) || (colonIndex >= 0 && !isPort(authority.slice(colonIndex + 1)))) return false; + // IPv6 literals must use brackets so a colon cannot be interpreted as + // an ambiguous port separator. + if (colonIndex >= 0 && host.includes(":")) return false; + } + + if (query.length === 0) return true; + const queryParts = query.split("&"); + return queryParts.length === 1 && /^transport=(udp|tcp)$/i.test(queryParts[0]); +} + +function isTurnUrl(value: string): boolean { + return /^(turn|turns):/i.test(value); +} + +function isCredential(value: unknown): value is string { + return typeof value === "string" && value.length > 0 && value.length <= 4_096 && !isControlCharacter(value); +} + +function normaliseIceServers(value: unknown): readonly RTCIceServer[] { + if (!isRecord(value) || !Array.isArray(value.iceServers)) { + throw sourceFailure("invalid-response", false); + } + if (value.iceServers.length === 0 || value.iceServers.length > CLOUDFLARE_TURN_MAX_ICE_SERVER_ENTRIES) { + throw sourceFailure("invalid-response", false); + } + + const servers: RTCIceServer[] = []; + let urlCount = 0; + let hasTurnServer = false; + + for (const candidate of value.iceServers) { + if (!isRecord(candidate)) throw sourceFailure("invalid-response", false); + const rawUrls = candidate.urls; + const urls = + typeof rawUrls === "string" + ? [rawUrls] + : Array.isArray(rawUrls) && rawUrls.every((url): url is string => typeof url === "string") + ? [...rawUrls] + : undefined; + if (!urls || urls.length === 0) throw sourceFailure("invalid-response", false); + + urlCount += urls.length; + if (urlCount > CLOUDFLARE_TURN_MAX_ICE_SERVER_URLS || urls.some((url) => !isSupportedIceServerUrl(url))) { + throw sourceFailure("invalid-response", false); + } + + const turnEntry = urls.some(isTurnUrl); + hasTurnServer ||= turnEntry; + const normalised: RTCIceServer = { urls }; + if (turnEntry) { + if (!isCredential(candidate.username) || !isCredential(candidate.credential)) { + throw sourceFailure("invalid-response", false); + } + normalised.username = candidate.username; + normalised.credential = candidate.credential; + } + servers.push(normalised); + } + + if (!hasTurnServer) throw sourceFailure("invalid-response", false); + return Object.freeze(servers); +} + +class BoundedResponseError extends Error { + constructor(readonly kind: "too-large" | "invalid-length" | "read-failed") { + super(kind); + } +} + +async function readResponseBody(response: Response): Promise { + const contentLength = response.headers.get("content-length"); + if (contentLength !== null) { + const declaredLength = Number(contentLength); + if (!Number.isFinite(declaredLength) || declaredLength < 0) { + throw new BoundedResponseError("invalid-length"); + } + if (declaredLength > CLOUDFLARE_TURN_MAX_RESPONSE_BYTES) { + throw new BoundedResponseError("too-large"); + } + } + + if (!response.body) { + try { + const text = await response.text(); + if (new TextEncoder().encode(text).byteLength > CLOUDFLARE_TURN_MAX_RESPONSE_BYTES) { + throw new BoundedResponseError("too-large"); + } + return text; + } catch (error) { + if (error instanceof BoundedResponseError) throw error; + throw new BoundedResponseError("read-failed"); + } + } + + const reader = response.body.getReader(); + const chunks: Uint8Array[] = []; + let totalBytes = 0; + try { + while (true) { + const result = await reader.read(); + if (result.done) break; + totalBytes += result.value.byteLength; + if (totalBytes > CLOUDFLARE_TURN_MAX_RESPONSE_BYTES) { + try { + await reader.cancel(); + } catch { + // The response is already invalid because it exceeded the + // bound; cancellation failure must not change the safe + // classification or expose a host-specific error. + } + throw new BoundedResponseError("too-large"); + } + chunks.push(result.value); + } + } catch (error) { + if (error instanceof BoundedResponseError) throw error; + throw new BoundedResponseError("read-failed"); + } finally { + reader.releaseLock(); + } + + const bytes = new Uint8Array(totalBytes); + let offset = 0; + for (const chunk of chunks) { + bytes.set(chunk, offset); + offset += chunk.byteLength; + } + return new TextDecoder().decode(bytes); +} + +function classifyHttpFailure(status: number): IceServerSourceError { + if (status === 401 || status === 403) { + return sourceFailure("authentication", false); + } + if (status === 408 || status === 429 || status >= 500) { + return sourceFailure("unavailable", true); + } + return sourceFailure("unavailable", false); +} + +function parseResponseBody(body: string): readonly RTCIceServer[] { + let value: unknown; + try { + value = JSON.parse(body) as unknown; + } catch { + throw sourceFailure("invalid-response", false); + } + return normaliseIceServers(value); +} + +function createSource( + configuration: CloudflareIceServerSourceConfiguration, + dependencies: CloudflareIceServerSourceDependencies +): IceServerSource { + const now = dependencies.now ?? Date.now; + const requestDeadlineMs = dependencies.requestDeadlineMs ?? CLOUDFLARE_TURN_REQUEST_DEADLINE_MS; + + return { + async acquire(signal: AbortSignal): Promise { + throwIfAborted(signal); + const requestStartedAt = now(); + if (!Number.isFinite(requestStartedAt)) { + throw sourceFailure("unavailable", true); + } + + const requestController = new AbortController(); + let cancelledByCaller = false; + let rejectCaller: ((reason?: unknown) => void) | undefined; + const callerAbort = new Promise((_resolve, reject) => { + rejectCaller = reject; + }); + let timedOut = false; + const onAbort = () => { + cancelledByCaller = true; + requestController.abort(); + rejectCaller?.(abortError()); + }; + signal.addEventListener("abort", onAbort, { once: true }); + if (signal.aborted) { + signal.removeEventListener("abort", onAbort); + requestController.abort(); + throw abortError(); + } + let timeoutId: ReturnType | undefined; + const deadline = new Promise((_resolve, reject) => { + timeoutId = globalThis.setTimeout(() => { + timedOut = true; + requestController.abort(); + reject(sourceFailure("unavailable", true)); + }, requestDeadlineMs); + }); + + const cleanup = () => { + if (timeoutId !== undefined) globalThis.clearTimeout(timeoutId); + signal.removeEventListener("abort", onAbort); + }; + + const endpoint = `${CLOUDFLARE_TURN_CREDENTIAL_ENDPOINT}/${configuration.turnKeyId}/credentials/generate-ice-servers`; + let response: Response; + try { + response = await Promise.race([ + dependencies.fetch(endpoint, { + method: "POST", + headers: { + Authorization: `Bearer ${configuration.apiToken}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ ttl: CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS }), + signal: requestController.signal, + redirect: "error", + credentials: "omit", + cache: "no-store", + }), + callerAbort, + deadline, + ]); + } catch { + cleanup(); + if (cancelledByCaller || signal.aborted) throw abortError(); + if (timedOut) throw sourceFailure("unavailable", true); + throw sourceFailure("unavailable", true); + } + + if (cancelledByCaller || signal.aborted) { + cleanup(); + throw abortError(); + } + if (timedOut || requestController.signal.aborted) { + cleanup(); + throw sourceFailure("unavailable", true); + } + if (response.status !== 201) { + cleanup(); + throw classifyHttpFailure(response.status); + } + + let body: string; + try { + body = await Promise.race([readResponseBody(response), callerAbort, deadline]); + } catch (error) { + cleanup(); + if (cancelledByCaller || signal.aborted) throw abortError(); + if (timedOut) throw sourceFailure("unavailable", true); + if (error instanceof BoundedResponseError && error.kind === "read-failed") { + throw sourceFailure("unavailable", true); + } + throw sourceFailure("invalid-response", false); + } + + try { + throwIfAborted(signal); + const iceServers = parseResponseBody(body); + const expiresAt = requestStartedAt + CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS * 1_000; + if (!Number.isFinite(expiresAt) || expiresAt <= now() + CLOUDFLARE_TURN_MIN_REMAINING_LIFETIME_MS) { + throw sourceFailure("invalid-response", false); + } + return { iceServers, expiresAt }; + } finally { + cleanup(); + } + }, + }; +} + +/** + * Creates a Cloudflare source after validating its persisted configuration. + * Validation is synchronous and performs no network request. + */ +export function createCloudflareIceServerSource( + configuration: Readonly>, + dependencies: CloudflareIceServerSourceDependencies +): IceServerSource { + const parsed = parseCloudflareIceServerSourceConfiguration(configuration); + if (!parsed) throw sourceFailure("configuration", false); + return createSource(parsed, dependencies); +} + +/** Exposes the provider validation for the integration catalogue and UI. */ +export { validateCloudflareIceServerSourceConfiguration }; diff --git a/src/integrations/cloudflare/iceServerSource.unit.spec.ts b/src/integrations/cloudflare/iceServerSource.unit.spec.ts new file mode 100644 index 00000000..8b61d356 --- /dev/null +++ b/src/integrations/cloudflare/iceServerSource.unit.spec.ts @@ -0,0 +1,164 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { + CLOUDFLARE_TURN_MAX_RESPONSE_BYTES, + CLOUDFLARE_TURN_REQUEST_DEADLINE_MS, + createCloudflareIceServerSource, +} from "./iceServerSource"; +import { + CLOUDFLARE_TURN_CREDENTIAL_ENDPOINT, + CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS, + validateCloudflareIceServerSourceConfiguration, +} from "./settings"; + +const configuration = { + turnKeyId: "key-123", + apiToken: "token_abc-123", +} as const; + +function response(body: unknown, status = 201): Response { + return new Response(JSON.stringify(body), { + status, + headers: { "content-type": "application/json" }, + }); +} + +function validBody() { + return { + iceServers: [ + { + urls: ["turn:relay.example.test:3478?transport=udp", "turns:relay.example.test:5349"], + username: "turn-user", + credential: "turn-password", + }, + { urls: "stun:stun.example.test:3478" }, + ], + }; +} + +afterEach(() => { + vi.useRealTimers(); +}); + +describe("Cloudflare ICE server source", () => { + it("requests the fixed endpoint with the bearer token and TTL", async () => { + const now = 1_000_000; + let requestUrl: string | Request | undefined; + let requestInit: RequestInit | undefined; + const fetch = vi.fn(async (input: string | Request, init?: RequestInit) => { + requestUrl = input; + requestInit = init; + return response(validBody()); + }); + const source = createCloudflareIceServerSource(configuration, { fetch, now: () => now }); + + const result = await source.acquire(new AbortController().signal); + + expect(requestUrl).toBe(`${CLOUDFLARE_TURN_CREDENTIAL_ENDPOINT}/key-123/credentials/generate-ice-servers`); + expect(requestInit).toMatchObject({ + method: "POST", + redirect: "error", + credentials: "omit", + cache: "no-store", + body: JSON.stringify({ ttl: CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS }), + }); + expect(new Headers(requestInit?.headers).get("authorization")).toBe("Bearer token_abc-123"); + expect(new Headers(requestInit?.headers).get("content-type")).toBe("application/json"); + expect(requestInit?.signal).toBeInstanceOf(AbortSignal); + expect(result.iceServers).toHaveLength(2); + expect(result.expiresAt).toBe(now + CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS * 1_000); + }); + + it("rejects malformed, oversized, and STUN-only responses without exposing secrets", async () => { + const cases: Array<{ body: unknown; expectedCode: string }> = [ + { body: { iceServers: [] }, expectedCode: "invalid-response" }, + { body: { iceServers: [{ urls: "turn:relay.example.test:3478" }] }, expectedCode: "invalid-response" }, + { body: { iceServers: [{ urls: "stun:stun.example.test:3478" }] }, expectedCode: "invalid-response" }, + ]; + for (const testCase of cases) { + const source = createCloudflareIceServerSource(configuration, { + fetch: vi.fn(async () => response(testCase.body)), + now: () => 1_000_000, + }); + const error = await source.acquire(new AbortController().signal).catch((reason: unknown) => reason); + expect(error).toMatchObject({ code: testCase.expectedCode }); + expect(String(error)).not.toContain(configuration.apiToken); + expect(String(error)).not.toContain(configuration.turnKeyId); + } + + const oversized = "x".repeat(CLOUDFLARE_TURN_MAX_RESPONSE_BYTES + 1); + const source = createCloudflareIceServerSource(configuration, { + fetch: vi.fn(async () => new Response(oversized, { status: 201 })), + now: () => 1_000_000, + }); + const error = await source.acquire(new AbortController().signal).catch((reason: unknown) => reason); + expect(error).toMatchObject({ code: "invalid-response" }); + }); + + it("classifies authentication and transient provider failures", async () => { + const authSource = createCloudflareIceServerSource(configuration, { + fetch: vi.fn(async () => response({}, 401)), + }); + await expect(authSource.acquire(new AbortController().signal)).rejects.toMatchObject({ + code: "authentication", + retryable: false, + }); + + const transientSource = createCloudflareIceServerSource(configuration, { + fetch: vi.fn(async () => response({}, 503)), + }); + await expect(transientSource.acquire(new AbortController().signal)).rejects.toMatchObject({ + code: "unavailable", + retryable: true, + }); + }); + + it("propagates caller cancellation and turns a deadline into an unavailable failure", async () => { + const controller = new AbortController(); + const fetch = vi.fn((_input: string | Request, init?: RequestInit) => { + return new Promise((_resolve, reject) => { + init?.signal?.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { + once: true, + }); + }); + }); + const source = createCloudflareIceServerSource(configuration, { fetch }); + const cancelled = source.acquire(controller.signal); + controller.abort(); + await expect(cancelled).rejects.toMatchObject({ name: "AbortError" }); + + vi.useFakeTimers(); + const timedSource = createCloudflareIceServerSource(configuration, { fetch }); + const timed = timedSource.acquire(new AbortController().signal); + const assertion = expect(timed).rejects.toMatchObject({ code: "unavailable", retryable: true }); + await vi.advanceTimersByTimeAsync(CLOUDFLARE_TURN_REQUEST_DEADLINE_MS); + await assertion; + }); + + it("rejects an issuance which has no usable remaining lifetime", async () => { + let now = 1_000_000; + const source = createCloudflareIceServerSource(configuration, { + fetch: vi.fn(async () => { + now += CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS * 1_000; + return response(validBody()); + }), + now: () => now, + }); + await expect(source.acquire(new AbortController().signal)).rejects.toMatchObject({ + code: "invalid-response", + }); + }); +}); + +describe("Cloudflare ICE source validation", () => { + it("rejects unknown fields and malformed bearer credentials", () => { + expect(validateCloudflareIceServerSourceConfiguration({ ...configuration, unexpected: "value" })).toContain( + "unsupported field" + ); + expect( + validateCloudflareIceServerSourceConfiguration({ turnKeyId: "key/id", apiToken: configuration.apiToken }) + ).toContain("unsupported characters"); + expect( + validateCloudflareIceServerSourceConfiguration({ ...configuration, apiToken: "token with spaces" }) + ).toContain("Bearer token syntax"); + }); +}); diff --git a/src/integrations/cloudflare/settings.ts b/src/integrations/cloudflare/settings.ts new file mode 100644 index 00000000..3cd88f96 --- /dev/null +++ b/src/integrations/cloudflare/settings.ts @@ -0,0 +1,87 @@ +/** The source identifier persisted in a P2P profile for Cloudflare TURN. */ +export const CLOUDFLARE_ICE_SERVER_SOURCE_ID = "cloudflare" as const; + +/** The lifetime requested from Cloudflare for each issued credential set. */ +export const CLOUDFLARE_TURN_CREDENTIAL_TTL_SECONDS = 86_400 as const; + +/** The Cloudflare TURN credential-generation endpoint. */ +export const CLOUDFLARE_TURN_CREDENTIAL_ENDPOINT = "https://rtc.live.cloudflare.com/v1/turn/keys" as const; + +/** A validated Cloudflare TURN source configuration. */ +export interface CloudflareIceServerSourceConfiguration { + readonly turnKeyId: string; + readonly apiToken: string; +} + +const CLOUDFLARE_CONFIGURATION_KEYS = ["turnKeyId", "apiToken"] as const; + +// TURN Key IDs are inserted into one fixed URL path. Keep the accepted set +// deliberately narrower than URI escaping so a configuration cannot alter +// the request path or add a query string. +const TURN_KEY_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,255}$/; + +// RFC 6750's b64token grammar, including optional trailing padding. This +// also excludes whitespace and control characters from the Authorization +// header without exposing the token in a validation message. +const BEARER_TOKEN_PATTERN = /^[A-Za-z0-9._~+/-]+={0,2}$/; +const MAX_BEARER_TOKEN_LENGTH = 4_096; + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function hasOnlyCloudflareConfigurationKeys(value: Record): boolean { + const keys = Object.keys(value); + return ( + keys.length === CLOUDFLARE_CONFIGURATION_KEYS.length && + CLOUDFLARE_CONFIGURATION_KEYS.every((key) => Object.prototype.hasOwnProperty.call(value, key)) + ); +} + +/** + * Returns a safe validation message for a Cloudflare source configuration. + * The result never includes the supplied Key ID or API token. + */ +export function validateCloudflareIceServerSourceConfiguration(value: unknown): string | undefined { + if (!isRecord(value)) { + return "Cloudflare TURN configuration is invalid."; + } + if (!hasOnlyCloudflareConfigurationKeys(value)) { + return "Cloudflare TURN configuration contains an unsupported field."; + } + + const turnKeyId = value.turnKeyId; + if (typeof turnKeyId !== "string" || turnKeyId.length === 0) { + return "Enter a TURN Key ID."; + } + if (!TURN_KEY_ID_PATTERN.test(turnKeyId)) { + return "TURN Key ID contains unsupported characters."; + } + + const apiToken = value.apiToken; + if (typeof apiToken !== "string" || apiToken.length === 0) { + return "Enter a TURN Key API Token."; + } + if (apiToken.length > MAX_BEARER_TOKEN_LENGTH || !BEARER_TOKEN_PATTERN.test(apiToken)) { + return "TURN Key API Token must use Bearer token syntax."; + } + + return undefined; +} + +/** + * Converts an untrusted profile value into a validated source configuration. + * The returned object is a fresh copy so later settings mutations cannot + * change a source which is already being used by the P2P owner. + */ +export function parseCloudflareIceServerSourceConfiguration( + value: unknown +): CloudflareIceServerSourceConfiguration | undefined { + if (validateCloudflareIceServerSourceConfiguration(value) !== undefined || !isRecord(value)) { + return undefined; + } + return { + turnKeyId: value.turnKeyId as string, + apiToken: value.apiToken as string, + }; +} diff --git a/src/integrations/iceServerSources.ts b/src/integrations/iceServerSources.ts new file mode 100644 index 00000000..aaba78f6 --- /dev/null +++ b/src/integrations/iceServerSources.ts @@ -0,0 +1,85 @@ +import { CLOUDFLARE_ICE_SERVER_SOURCE_ID, validateCloudflareIceServerSourceConfiguration } from "./cloudflare/settings"; + +export const MANUAL_ICE_SERVER_SOURCE_ID = "manual" as const; + +export type IceServerSourceSelectionId = typeof MANUAL_ICE_SERVER_SOURCE_ID | typeof CLOUDFLARE_ICE_SERVER_SOURCE_ID; + +export interface IceServerSourceFieldDefinition { + readonly key: string; + readonly label: string; + readonly secret: boolean; +} + +export interface IceServerSourceDefinition { + readonly id: string; + readonly label: string; + readonly fields: readonly IceServerSourceFieldDefinition[]; +} + +export interface IceServerSourceDescriptorLike { + readonly version?: unknown; + readonly id?: unknown; + readonly configuration?: unknown; +} + +/** + * The service-owned field metadata used by the P2P settings dialogue. Manual + * TURN values remain the existing settings fields and therefore do not occur + * in this provider catalogue. + */ +export const iceServerSourceDefinitions = [ + { + id: CLOUDFLARE_ICE_SERVER_SOURCE_ID, + label: "Cloudflare", + fields: [ + { key: "turnKeyId", label: "TURN Key ID", secret: false }, + { key: "apiToken", label: "TURN Key API Token", secret: true }, + ], + }, +] as const satisfies readonly IceServerSourceDefinition[]; + +/** The user-facing source choice, including the existing manual mode. */ +export const turnConfigurationChoices = [ + { id: MANUAL_ICE_SERVER_SOURCE_ID, label: "Manual" }, + { id: CLOUDFLARE_ICE_SERVER_SOURCE_ID, label: "Cloudflare" }, +] as const; + +export const iceServerSourceChoices = turnConfigurationChoices; + +function isRecord(value: unknown): value is IceServerSourceDescriptorLike { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +/** + * Validates a selected source descriptor without performing network access. + * An absent descriptor represents the existing manual TURN configuration. + */ +export function validateIceServerSourceConfiguration( + descriptor: IceServerSourceDescriptorLike | null | undefined +): string | undefined { + if (descriptor === undefined || descriptor === null) return undefined; + if (!isRecord(descriptor)) return "TURN configuration source is invalid."; + if (descriptor.version !== 1) return "TURN configuration source version is not supported."; + if (descriptor.id === MANUAL_ICE_SERVER_SOURCE_ID) { + return undefined; + } + if (descriptor.id !== CLOUDFLARE_ICE_SERVER_SOURCE_ID) { + return "The selected TURN configuration source is not supported."; + } + return validateCloudflareIceServerSourceConfiguration(descriptor.configuration); +} + +export function getIceServerSourceDefinition(id: string): IceServerSourceDefinition | undefined { + return iceServerSourceDefinitions.find((definition) => definition.id === id); +} + +/** Validate the selected settings projection, including an unavailable encrypted source. */ +export function validateTurnSettings(settings: { + readonly P2P_iceServerSource?: IceServerSourceDescriptorLike | null; + readonly encryptedP2PIceServerSource?: string; +}): string | undefined { + if (!settings.P2P_iceServerSource && settings.encryptedP2PIceServerSource) { + return "TURN configuration could not be decrypted."; + } + return validateIceServerSourceConfiguration(settings.P2P_iceServerSource); +} diff --git a/src/integrations/iceServerSources.unit.spec.ts b/src/integrations/iceServerSources.unit.spec.ts new file mode 100644 index 00000000..b3790e91 --- /dev/null +++ b/src/integrations/iceServerSources.unit.spec.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from "vitest"; +import { + iceServerSourceDefinitions, + validateIceServerSourceConfiguration, + validateTurnSettings, +} from "./iceServerSources"; + +describe("ICE server source catalogue", () => { + it("blocks an unavailable encrypted source instead of presenting manual settings as valid", () => { + expect(validateTurnSettings({ encryptedP2PIceServerSource: "private-ciphertext" })).toBe( + "TURN configuration could not be decrypted." + ); + expect(validateTurnSettings({})).toBeUndefined(); + }); + + it("describes the Cloudflare fields without owning manual TURN fields", () => { + expect(iceServerSourceDefinitions).toEqual([ + { + id: "cloudflare", + label: "Cloudflare", + fields: [ + { key: "turnKeyId", label: "TURN Key ID", secret: false }, + { key: "apiToken", label: "TURN Key API Token", secret: true }, + ], + }, + ]); + }); + + it("accepts absent or explicit manual selection and rejects unsupported versions", () => { + expect(validateIceServerSourceConfiguration(undefined)).toBeUndefined(); + expect(validateIceServerSourceConfiguration({ version: 1, id: "manual" })).toBeUndefined(); + expect(validateIceServerSourceConfiguration({ version: 2, id: "cloudflare", configuration: {} })).toContain( + "version" + ); + expect(validateIceServerSourceConfiguration({ version: 1, id: "unknown", configuration: {} })).toContain( + "not supported" + ); + }); +}); diff --git a/src/main.ts b/src/main.ts index 549c7941..d83451da 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import { useIceServerSources } from "@/serviceFeatures/useIceServerSources"; import { getLanguage, Notice, Plugin, type App, type PluginManifest } from "./deps"; import { setGetLanguage } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions"; setGetLanguage(getLanguage); @@ -182,7 +183,8 @@ export default class ObsidianLiveSyncPlugin extends Plugin { const replicator = useP2PReplicatorFeature( core, (_compatibilityReplicator, p2p) => createInteractiveP2PReplication(p2p), - createOpenRebuildUI(this.app) + createOpenRebuildUI(this.app), + { iceServerSources: useIceServerSources(core.services.API.webCompatFetch.bind(core.services.API)) } ); setupManager.registerP2PSetupConnectionProbe(replicator.connectionProbe); useP2PReplicatorCommands(core, replicator); diff --git a/src/modules/features/ModuleObsidianSettingAsMarkdown.ts b/src/modules/features/ModuleObsidianSettingAsMarkdown.ts index cbbdab3e..b44e140c 100644 --- a/src/modules/features/ModuleObsidianSettingAsMarkdown.ts +++ b/src/modules/features/ModuleObsidianSettingAsMarkdown.ts @@ -1,3 +1,8 @@ +import { + hasManagedTurnSettings, + omitManagedTurnProfilesFromMarkdown, + preserveManagedTurnProfilesOnMarkdownImport, +} from "@/common/turnSettingsPrivacy"; // import { PouchDB } from "../../lib/src/pouchdb/pouchdb-browser"; import { isObjectDifferent } from "octagonal-wheels/object"; import { EVENT_SETTING_SAVED, eventHub } from "@/common/events"; @@ -129,6 +134,7 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractModule { let settingToApply = { ...DEFAULT_SETTINGS } as ObsidianLiveSyncSettings; settingToApply = { ...settingToApply, ...newSetting }; + preserveManagedTurnProfilesOnMarkdownImport(newSetting, this.settings, settingToApply); if (!settingToApply?.writeCredentialsForSettingSync) { //New setting does not contains credentials. settingToApply.couchDB_USER = this.settings.couchDB_USER; @@ -208,11 +214,18 @@ export class ModuleObsidianSettingsAsMarkdown extends AbstractModule { delete saveData.couchDB_CustomHeaders; delete saveData.bucketCustomHeaders; } + omitManagedTurnProfilesFromMarkdown(saveData); return saveData; } async saveSettingToMarkdown(filename: string) { const saveData = this.generateSettingForMarkdown(); + if (hasManagedTurnSettings(this.settings)) { + this._log( + "Share TURN provider credentials through an encrypted Setup URI. Connection profiles are omitted from Markdown settings.", + LOG_LEVEL_INFO + ); + } const file = await this.core.storageAccess.isExists(filename); if (!file) { diff --git a/src/modules/features/SettingDialogue/PaneRemoteConfig.ts b/src/modules/features/SettingDialogue/PaneRemoteConfig.ts index 260ec3c0..209f3eaa 100644 --- a/src/modules/features/SettingDialogue/PaneRemoteConfig.ts +++ b/src/modules/features/SettingDialogue/PaneRemoteConfig.ts @@ -1,3 +1,5 @@ +import { copySetupURI } from "@/serviceFeatures/setupObsidian/setupUri"; +import { createInstanceLogFunction } from "@vrtmrz/livesync-commonlib/compat/services/lib/logUtils"; import { REMOTE_COUCHDB, REMOTE_MINIO, @@ -416,6 +418,15 @@ export function paneRemoteConfig( }) .addItem((item) => { item.setTitle("📤 Export").onClick(async () => { + if (config.uri.startsWith("sls+p2p-v2://")) { + await copySetupURI( + this.core, + createInstanceLogFunction("TURN setup sharing", this.services.API), + true, + getSettingsFromEditingSettings(this.editingSettings) + ); + return; + } await this.services.UI.promptCopyToClipboard( `Remote configuration: ${config.name}`, config.uri diff --git a/src/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte b/src/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte index 8a79a891..9a064586 100644 --- a/src/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte +++ b/src/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte @@ -1,4 +1,6 @@ @@ -339,24 +345,24 @@ {translateMessage( - "TURN relay only is available when at least one valid TURN server URL is configured under Advanced Settings." + "TURN relay only requires a TURN server or a configured credential source under Advanced Settings." )} {translateMessage( - "TURN relay only requires at least one valid TURN server URL. Connection path has been restored to Automatic." + "TURN relay only requires TURN configuration. Connection path has been restored to Automatic." )} {translateMessage( - "TURN server settings are only necessary if you are behind a strict NAT or firewall that prevents direct P2P connections. In most cases, you can leave these fields blank." + "Configure TURN when a direct connection cannot be established or when you select TURN relay only." )} - + {translateMessage( - "TURN relays the encrypted WebRTC connection only when a direct path cannot be established. A TURN provider cannot read encrypted Vault contents, but it can observe connection metadata and traffic volume. Use a provider you trust." + "WebRTC encrypts data between your devices, including when it passes through TURN. The TURN provider cannot read the transferred data. It can see network addresses and traffic volume." )} {translateMessage("Learn more about signalling and TURN")}. - - - - - - - - - + {error} diff --git a/src/modules/features/SetupWizard/dialogs/UseSetupURI.svelte b/src/modules/features/SetupWizard/dialogs/UseSetupURI.svelte index 5300558b..08208e1d 100644 --- a/src/modules/features/SetupWizard/dialogs/UseSetupURI.svelte +++ b/src/modules/features/SetupWizard/dialogs/UseSetupURI.svelte @@ -1,6 +1,5 @@