better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 2.17 kB
Source Map (JSON)
{"version":3,"file":"trusted-origins.mjs","names":[],"sources":["../../src/auth/trusted-origins.ts"],"sourcesContent":["import { getHost, getOrigin, getProtocol } from \"../utils/url\";\nimport { wildcardMatch } from \"../utils/wildcard\";\n\n/**\n * Matches the given url against an origin or origin pattern\n * See \"options.trustedOrigins\" for details of supported patterns\n *\n * @param url The url to test\n * @param pattern The origin pattern\n * @param [settings] Specify supported pattern matching settings\n * @returns {boolean} true if the URL matches the origin pattern, false otherwise.\n */\nexport const matchesOriginPattern = (\n\turl: string,\n\tpattern: string,\n\tsettings?: { allowRelativePaths: boolean },\n): boolean => {\n\tif (url.startsWith(\"/\")) {\n\t\tif (settings?.allowRelativePaths) {\n\t\t\treturn (\n\t\t\t\turl.startsWith(\"/\") &&\n\t\t\t\t/^\\/(?!\\/|\\\\|%2f|%5c)[\\w\\-.\\+/@]*(?:\\?[\\w\\-.\\+/=&%@]*)?$/.test(url)\n\t\t\t);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t// Check if pattern contains wildcard characters (*, **, or ?)\n\tconst hasWildcard = pattern.includes(\"*\") || pattern.includes(\"?\");\n\tif (hasWildcard) {\n\t\t// For protocol-specific wildcards, match the full origin\n\t\tif (pattern.includes(\"://\")) {\n\t\t\treturn wildcardMatch(pattern)(getOrigin(url) || url);\n\t\t}\n\t\tconst host = getHost(url);\n\t\tif (!host) {\n\t\t\treturn false;\n\t\t}\n\t\t// For host-only wildcards, match just the host\n\t\treturn wildcardMatch(pattern)(host);\n\t}\n\tconst protocol = getProtocol(url);\n\treturn protocol === \"http:\" || protocol === \"https:\" || !protocol\n\t\t? pattern === getOrigin(url)\n\t\t: url.startsWith(pattern);\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,wBACZ,KACA,SACA,aACa;AACb,KAAI,IAAI,WAAW,IAAI,EAAE;AACxB,MAAI,UAAU,mBACb,QACC,IAAI,WAAW,IAAI,IACnB,0DAA0D,KAAK,IAAI;AAIrE,SAAO;;AAKR,KADoB,QAAQ,SAAS,IAAI,IAAI,QAAQ,SAAS,IAAI,EACjD;AAEhB,MAAI,QAAQ,SAAS,MAAM,CAC1B,QAAO,cAAc,QAAQ,CAAC,UAAU,IAAI,IAAI,IAAI;EAErD,MAAM,OAAO,QAAQ,IAAI;AACzB,MAAI,CAAC,KACJ,QAAO;AAGR,SAAO,cAAc,QAAQ,CAAC,KAAK;;CAEpC,MAAM,WAAW,YAAY,IAAI;AACjC,QAAO,aAAa,WAAW,aAAa,YAAY,CAAC,WACtD,YAAY,UAAU,IAAI,GAC1B,IAAI,WAAW,QAAQ"}