admin-branding.js (4933B)
1 import { HttpError } from './http/http-error.js'; 2 3 const LOGO_MAX_LENGTH = 2048; 4 const PRIMARY_COLOR_PATTERN = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i; 5 6 function invalidRequest(message) { 7 return new HttpError(400, 'invalid_request', message); 8 } 9 10 function isPlainObject(value) { 11 return value !== null && typeof value === 'object' && !Array.isArray(value); 12 } 13 14 function parseBranding(rawBranding) { 15 if (isPlainObject(rawBranding)) return rawBranding; 16 17 try { 18 const parsed = JSON.parse(rawBranding || '{}'); 19 return isPlainObject(parsed) ? parsed : {}; 20 } catch { 21 return {}; 22 } 23 } 24 25 function isLoopbackHostname(hostname) { 26 const host = hostname.startsWith('[') && hostname.endsWith(']') 27 ? hostname.slice(1, -1) 28 : hostname; 29 return ( 30 host === 'localhost' || 31 host.endsWith('.localhost') || 32 host === '127.0.0.1' || 33 host === '::1' 34 ); 35 } 36 37 function normalizeStoredPrimaryColor(value) { 38 if (typeof value !== 'string') return null; 39 40 const color = value.trim().toLowerCase(); 41 if (!PRIMARY_COLOR_PATTERN.test(color)) return null; 42 if (color.length === 4) { 43 return `#${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${color[3]}`; 44 } 45 return color; 46 } 47 48 function normalizePrimaryColor(value) { 49 if (value === null || (typeof value === 'string' && !value.trim())) return null; 50 if (typeof value !== 'string') { 51 throw invalidRequest('primary_color must be a string or null'); 52 } 53 54 const color = normalizeStoredPrimaryColor(value); 55 if (!color) { 56 throw invalidRequest('primary_color must be a #rgb or #rrggbb hex color'); 57 } 58 return color; 59 } 60 61 function normalizeLogoUrl(value) { 62 if (value === null || (typeof value === 'string' && !value.trim())) return null; 63 if (typeof value !== 'string') { 64 throw invalidRequest('logo_url must be a string or null'); 65 } 66 67 const url = value.trim(); 68 if (!url) return null; 69 if (url.length > LOGO_MAX_LENGTH) { 70 throw invalidRequest(`logo_url must be at most ${LOGO_MAX_LENGTH} characters`); 71 } 72 if (url.includes('*')) { 73 throw invalidRequest('logo_url cannot contain wildcards'); 74 } 75 76 let parsed; 77 try { 78 parsed = new URL(url); 79 } catch { 80 throw invalidRequest('logo_url must be an absolute URL'); 81 } 82 83 const allowedProtocol = 84 parsed.protocol === 'https:' || 85 (parsed.protocol === 'http:' && isLoopbackHostname(parsed.hostname)); 86 if (!allowedProtocol) { 87 throw invalidRequest('logo_url must use https, or http for a loopback host'); 88 } 89 if (parsed.username || parsed.password) { 90 throw invalidRequest('logo_url cannot contain credentials'); 91 } 92 if (parsed.hash) { 93 throw invalidRequest('logo_url cannot contain a fragment'); 94 } 95 96 return url; 97 } 98 99 export function hasBrandingPatch(body) { 100 return ( 101 isPlainObject(body) && 102 (Object.hasOwn(body, 'logo_url') || Object.hasOwn(body, 'primary_color')) 103 ); 104 } 105 106 export function normalizeBrandingPatch(body) { 107 const input = {}; 108 if (Object.hasOwn(body, 'logo_url')) { 109 input.logo_url = normalizeLogoUrl(body.logo_url); 110 } 111 if (Object.hasOwn(body, 'primary_color')) { 112 input.primary_color = normalizePrimaryColor(body.primary_color); 113 } 114 return input; 115 } 116 117 export function applyBrandingPatch(rawBranding, patch) { 118 if (!hasBrandingPatch(patch)) { 119 return typeof rawBranding === 'string' ? rawBranding : JSON.stringify(parseBranding(rawBranding)); 120 } 121 122 const current = parseBranding(rawBranding); 123 const next = { ...current }; 124 125 if (Object.hasOwn(patch, 'logo_url')) { 126 if (patch.logo_url === null) { 127 delete next.logo_url; 128 } else { 129 next.logo_url = patch.logo_url; 130 } 131 } 132 133 if (Object.hasOwn(patch, 'primary_color')) { 134 if (patch.primary_color === null) { 135 delete next.primary_color; 136 } else { 137 next.primary_color = patch.primary_color; 138 } 139 } 140 141 return JSON.stringify(next); 142 } 143 144 export function serializeBranding(rawBranding) { 145 const branding = parseBranding(rawBranding); 146 const logoUrl = 147 typeof branding.logo_url === 'string' && branding.logo_url.trim() 148 ? branding.logo_url.trim() 149 : null; 150 151 return { 152 logo_url: logoUrl, 153 primary_color: normalizeStoredPrimaryColor(branding.primary_color), 154 }; 155 } 156 157 export function normalizeBrandingObject(rawBranding) { 158 const branding = parseBranding(rawBranding); 159 const normalized = { ...branding }; 160 161 if (Object.hasOwn(normalized, 'logo_url')) { 162 const logoUrl = 163 typeof normalized.logo_url === 'string' ? normalized.logo_url.trim() : ''; 164 if (logoUrl) { 165 normalized.logo_url = logoUrl; 166 } else { 167 delete normalized.logo_url; 168 } 169 } 170 171 if (Object.hasOwn(normalized, 'primary_color')) { 172 const color = normalizeStoredPrimaryColor(normalized.primary_color); 173 if (color) { 174 normalized.primary_color = color; 175 } else { 176 delete normalized.primary_color; 177 } 178 } 179 180 return normalized; 181 } 182 183 export function parseBrandingValue(rawBranding) { 184 return parseBranding(rawBranding); 185 }