diff --git a/backend/src/middleware/trustedDomains.ts b/backend/src/middleware/trustedDomains.ts index f904dbaf..9a55857a 100644 --- a/backend/src/middleware/trustedDomains.ts +++ b/backend/src/middleware/trustedDomains.ts @@ -5,8 +5,8 @@ import { Request, Response, NextFunction } from 'express'; * These are our own frontends that should have unrestricted access. */ const TRUSTED_DOMAINS = [ - 'cannaiq.co', - 'www.cannaiq.co', + '*.cannaiq.co', + '*.cannabrands.app', 'findagram.co', 'www.findagram.co', 'findadispo.com', @@ -32,6 +32,24 @@ function extractDomain(header: string): string | null { } } +/** + * Checks if a domain matches any trusted domain (supports *.domain.com wildcards) + */ +function isTrustedDomain(domain: string): boolean { + for (const trusted of TRUSTED_DOMAINS) { + if (trusted.startsWith('*.')) { + // Wildcard: *.example.com matches example.com and any subdomain + const baseDomain = trusted.slice(2); + if (domain === baseDomain || domain.endsWith('.' + baseDomain)) { + return true; + } + } else if (domain === trusted) { + return true; + } + } + return false; +} + /** * Checks if the request comes from a trusted domain */ @@ -42,7 +60,7 @@ function isRequestFromTrustedDomain(req: Request): boolean { // Check Origin header first (preferred for CORS requests) if (origin) { const domain = extractDomain(origin); - if (domain && TRUSTED_DOMAINS.includes(domain)) { + if (domain && isTrustedDomain(domain)) { return true; } } @@ -50,7 +68,7 @@ function isRequestFromTrustedDomain(req: Request): boolean { // Fallback to Referer header if (referer) { const domain = extractDomain(referer); - if (domain && TRUSTED_DOMAINS.includes(domain)) { + if (domain && isTrustedDomain(domain)) { return true; } }