| 1 | const DEFAULT_MARKETING_SITE_URL = "https://t3.codes"; |
| 2 | |
| 3 | function resolveMarketingSiteUrl(override: string | undefined): URL { |
| 4 | try { |
| 5 | const url = new URL(override?.trim() || DEFAULT_MARKETING_SITE_URL); |
| 6 | if (url.protocol !== "https:" && url.protocol !== "http:") { |
| 7 | return new URL(DEFAULT_MARKETING_SITE_URL); |
| 8 | } |
| 9 | |
| 10 | url.search = ""; |
| 11 | url.hash = ""; |
| 12 | url.pathname = `${url.pathname.replace(/\/+$/, "")}/`; |
| 13 | return url; |
| 14 | } catch { |
| 15 | return new URL(DEFAULT_MARKETING_SITE_URL); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | const MARKETING_SITE_URL = resolveMarketingSiteUrl(process.env.EXPO_PUBLIC_MARKETING_SITE_URL); |
| 20 | |
| 21 | function marketingSiteDocumentUrl(path: string): string { |
| 22 | return new URL(path, MARKETING_SITE_URL).toString(); |
| 23 | } |
| 24 | |
| 25 | export const PRIVACY_POLICY_URL = marketingSiteDocumentUrl("privacy-policy"); |
| 26 | export const SECURITY_POLICY_URL = marketingSiteDocumentUrl("security-policy"); |
| 27 | export const TERMS_OF_SERVICE_URL = marketingSiteDocumentUrl("terms-of-service"); |
| 28 | export const LEGAL_URL = marketingSiteDocumentUrl("legal"); |
| 29 | |
| 30 | export const ALLOWED_LEGAL_DOCUMENT_URLS = [ |
| 31 | LEGAL_URL, |
| 32 | PRIVACY_POLICY_URL, |
| 33 | TERMS_OF_SERVICE_URL, |
| 34 | SECURITY_POLICY_URL, |
| 35 | ] as const; |
| 36 | |
| 37 | function webDocumentIdentity(value: string): string | null { |
| 38 | try { |
| 39 | const url = new URL(value); |
| 40 | if (url.protocol !== "https:" && url.protocol !== "http:") return null; |
| 41 | |
| 42 | const pathname = url.pathname.replace(/\/+$/, "") || "/"; |
| 43 | return `${url.origin}${pathname}`; |
| 44 | } catch { |
| 45 | return null; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const ALLOWED_LEGAL_DOCUMENT_IDENTITIES = new Set( |
| 50 | ALLOWED_LEGAL_DOCUMENT_URLS.map(webDocumentIdentity).filter( |
| 51 | (value): value is string => value !== null, |
| 52 | ), |
| 53 | ); |
| 54 | |
| 55 | export function isLegalDocumentUrl(value: string): boolean { |
| 56 | const identity = webDocumentIdentity(value); |
| 57 | return identity !== null && ALLOWED_LEGAL_DOCUMENT_IDENTITIES.has(identity); |
| 58 | } |