Skip to content

Commit

Permalink
fix: check if ip address when sanitizing urls (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiendan authored Sep 4, 2023
1 parent ba65bcf commit 22f2b3d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ export function shortenAddress(
}

export function sanitizeURLProtocol(protocol: 'ws' | 'http', endpoint: string) {
return location.protocol.startsWith('https')
return location.protocol.startsWith('https') &&
endpoint.indexOf('localhost') === -1 &&
!isStringIpAddress(endpoint)
? `${protocol}s://${endpoint}`
: `${protocol}://${endpoint}`
}

function isStringIpAddress(value: string) {
if (
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
value
)
) {
return true
}
return false
}

0 comments on commit 22f2b3d

Please sign in to comment.