mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-24 18:22:38 +00:00
Protect relay sessions against replayed commands with authenticated Noise NK negotiation, ordered records, and identity-bound security pins. Preserve legacy admission during migration and add daemon enforcement policy. All 21 CI checks passed. General engineering review is complete; specialist cryptographic review of the handwritten Noise implementation and static-key reuse remains required before release or deployment. Closes #359
63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
import { z } from "zod";
|
|
|
|
/**
|
|
* Relay-only pairing offer.
|
|
*
|
|
* `serverId` is a stable daemon identifier scoped to `PASEO_HOME`, and is also
|
|
* used as the relay session identifier.
|
|
*/
|
|
export const ConnectionOfferV2Schema = z.object({
|
|
v: z.literal(2),
|
|
serverId: z.string().min(1),
|
|
daemonPublicKeyB64: z.string().min(1),
|
|
relay: z.object({
|
|
endpoint: z.string().min(1),
|
|
useTls: z.boolean().optional(),
|
|
}),
|
|
// COMPAT(relayProtectedSecurity): added in v0.13.0. Present when the daemon requires
|
|
// protected endpoint security; clients pin it before their first handshake.
|
|
minimumSecurityMode: z.literal("protected").optional(),
|
|
});
|
|
|
|
export type ConnectionOfferV2 = z.infer<typeof ConnectionOfferV2Schema>;
|
|
|
|
export const ConnectionOfferSchema = ConnectionOfferV2Schema;
|
|
export type ConnectionOffer = ConnectionOfferV2;
|
|
|
|
function decodeBase64UrlToUtf8(input: string): string {
|
|
const base64 = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
const padded = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), "=");
|
|
const binary = globalThis.atob(padded);
|
|
const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
}
|
|
|
|
export function decodeOfferFragmentPayload(encoded: string): unknown {
|
|
const json = decodeBase64UrlToUtf8(encoded);
|
|
return JSON.parse(json) as unknown;
|
|
}
|
|
|
|
const OFFER_FRAGMENT_PREFIX = "#offer=";
|
|
|
|
function extractOfferFragmentEncoded(input: string): string | null {
|
|
const trimmed = input.trim();
|
|
if (!trimmed) return null;
|
|
const fragmentIndex = trimmed.indexOf(OFFER_FRAGMENT_PREFIX);
|
|
if (fragmentIndex === -1) return null;
|
|
const encoded = trimmed.slice(fragmentIndex + OFFER_FRAGMENT_PREFIX.length).trim();
|
|
return encoded.length > 0 ? encoded : null;
|
|
}
|
|
|
|
/**
|
|
* Parse a pairing-offer URL of the form `https://app.paseo.sh/#offer=<base64url>`.
|
|
*
|
|
* Returns `null` if the input has no `#offer=` fragment. Throws if the fragment
|
|
* exists but the payload is malformed or fails schema validation.
|
|
*/
|
|
export function parseConnectionOfferFromUrl(input: string): ConnectionOffer | null {
|
|
const encoded = extractOfferFragmentEncoded(input);
|
|
if (!encoded) return null;
|
|
const payload = decodeOfferFragmentPayload(encoded);
|
|
return ConnectionOfferSchema.parse(payload);
|
|
}
|