Spaces:
Paused
Paused
Download src/lib/utils/externalLink.ts from Mike0021/chat-ui-agent-demo: direct link, hf CLI and curl.
- Browser
- Download file 804 Bytes
-
https://huggingface.co/spaces/Mike0021/chat-ui-agent-demo/resolve/main/src/lib/utils/externalLink.ts
- Command line
-
hf download hf://spaces/Mike0021/chat-ui-agent-demo/src/lib/utils/externalLink.ts
-
curl -L -o externalLink.ts https://huggingface.co/spaces/Mike0021/chat-ui-agent-demo/resolve/main/src/lib/utils/externalLink.ts
804 Bytes
| /** | |
| * Parse an untrusted href (e.g. forwarded via postMessage from a sandboxed | |
| * artifact preview) into a URL that is safe to open in a new tab. Only | |
| * absolute http(s) URLs survive; anything else (javascript:, data:, relative, | |
| * malformed) returns undefined. URLs with embedded credentials are rejected | |
| * too: `https://trusted.com@evil.com` is a phishing classic, and userinfo | |
| * breaks the guarantee that the confirm dialog renders exactly what opens. | |
| */ | |
| export function parseExternalUrl(href: unknown): URL | undefined { | |
| if (typeof href !== "string") return undefined; | |
| let url: URL; | |
| try { | |
| url = new URL(href); | |
| } catch { | |
| return undefined; | |
| } | |
| if (url.protocol !== "http:" && url.protocol !== "https:") return undefined; | |
| if (url.username || url.password) return undefined; | |
| return url; | |
| } | |