app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

support.ts (3470B)


      1 import { navigationHref } from '../navigation-url.ts';
      2 import { canonicalRelayOrigin } from './relays.ts';
      3 
      4 export interface SupportManifest {
      5   readonly schemaVersion: 1;
      6   readonly owner: string;
      7   readonly destination: string;
      8 }
      9 declare const supportBrand: unique symbol;
     10 export type SupportPolicy = Readonly<{ [supportBrand]: true }>;
     11 const policies = new WeakMap<SupportPolicy, string>();
     12 
     13 function destination(value: string): string | undefined {
     14   if (!value) return '';
     15   if (!navigationHref(value)) return undefined;
     16   try {
     17     const url = new URL(value);
     18     if (url.search || url.hash || url.username || url.password)
     19       return undefined;
     20     if (url.protocol === 'https:') {
     21       if (url.href !== value || !canonicalRelayOrigin('wss://' + url.host))
     22         return undefined;
     23       return value;
     24     }
     25     if (url.protocol !== 'mailto:' || !value.startsWith('mailto:'))
     26       return undefined;
     27     // One conservative, literal address. No recipient list, encoded header,
     28     // subject/body carrier or automatically attached report content.
     29     const address = value.slice(7);
     30     const parts = address.split('@');
     31     if (parts.length !== 2) return undefined;
     32     const [local, host] = parts;
     33     if (
     34       !local ||
     35       local.length > 64 ||
     36       !/^[A-Za-z0-9._+-]+$/.test(local) ||
     37       local.startsWith('.') ||
     38       local.endsWith('.') ||
     39       local.includes('..') ||
     40       !canonicalRelayOrigin('wss://' + host) ||
     41       new URL('https://' + host).hostname !== host
     42     )
     43       return undefined;
     44     return value;
     45   } catch {
     46     return undefined;
     47   }
     48 }
     49 
     50 // Public configuration only, bounded before JSON parsing. Caller objects and
     51 // getters never enter this owner. Admission is not operator qualification.
     52 export function validateSupportPolicy(raw: unknown): SupportPolicy | undefined {
     53   if (
     54     typeof raw !== 'string' ||
     55     raw.length > 16384 ||
     56     !raw.isWellFormed() ||
     57     new TextEncoder().encode(raw).length > 16384
     58   )
     59     return undefined;
     60   try {
     61     const value: unknown = JSON.parse(raw);
     62     if (!value || typeof value !== 'object' || Array.isArray(value))
     63       return undefined;
     64     const row = value as Record<string, unknown>;
     65     let count = 0;
     66     for (const key in row) {
     67       if (!['schemaVersion', 'owner', 'destination'].includes(key))
     68         return undefined;
     69       count++;
     70     }
     71     if (
     72       count !== 3 ||
     73       row.schemaVersion !== 1 ||
     74       typeof row.owner !== 'string' ||
     75       typeof row.destination !== 'string' ||
     76       !row.owner.isWellFormed() ||
     77       row.owner !== row.owner.trim() ||
     78       new TextEncoder().encode(row.owner).length > 256 ||
     79       /[\p{Cc}\p{Cf}]/u.test(row.owner)
     80     )
     81       return undefined;
     82     const href = destination(row.destination);
     83     if (href === undefined) return undefined;
     84     const token = Object.freeze({}) as SupportPolicy;
     85     policies.set(
     86       token,
     87       JSON.stringify({ schemaVersion: 1, owner: row.owner, destination: href })
     88     );
     89     return token;
     90   } catch {
     91     return undefined;
     92   }
     93 }
     94 export function readSupportPolicy(policy: SupportPolicy): SupportManifest {
     95   const raw = policies.get(policy);
     96   if (!raw) throw Error('support_policy_invalid');
     97   return JSON.parse(raw) as SupportManifest;
     98 }
     99 export function requireLaunchSupportPolicy(
    100   policy: SupportPolicy
    101 ): SupportPolicy {
    102   const actual = readSupportPolicy(policy);
    103   if (!actual.owner || !actual.destination)
    104     throw Error('support_launch_inputs_missing');
    105   return policy;
    106 }