Skip to content

Commit

Permalink
fix: fixed isAllowedDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
Uninen committed Jan 22, 2025
1 parent f7a8308 commit 4c9164b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,19 @@ export function copyToClipboard(content: string) {
*/
export function isAllowedDomain(url: string, allowedDomains: string[]): boolean {
try {
const hostname = new URL(url).hostname
return allowedDomains.some(domain => hostname === domain || hostname.endsWith(`.${domain}`))
const parsedUrl = new URL(url)
const hostWithPort = parsedUrl.port ? `${parsedUrl.hostname}:${parsedUrl.port}` : parsedUrl.hostname

return allowedDomains.some(allowedDomain => {
const [allowedHost, allowedPort] = allowedDomain.split(':')
const [urlHost, _] = hostWithPort.split(':')

if (allowedPort) {
return hostWithPort === allowedDomain
}

return urlHost === allowedHost || urlHost.endsWith(`.${allowedHost}`)
})
} catch {
return false
}
Expand Down

0 comments on commit 4c9164b

Please sign in to comment.