Security
What unfurl() refuses, and why a pasted link can't reach your internal network.
A link preview fetches whatever address a user pastes, from your server. Without checks, that's a way to make your server request http://localhost:8080/admin or a cloud metadata address and show the answer back as a card. This is server-side request forgery, and unfurl() is built to refuse it.
What's refused
Every hop of every redirect is checked, not just the first address. A host that resolves to any of these throws Unfurl.Refused:
- loopback (
127.0.0.0/8,::1) and the wildcard address (0.0.0.0/8) - private ranges (
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16) - link-local, which includes cloud metadata at
169.254.169.254 - carrier-grade NAT and Tailscale (
100.64.0.0/10) - multicast,
192.0.0.0/24, benchmarking (198.18.0.0/15) and reserved (240.0.0.0/4) - IPv6 unique-local addresses (
fc00::/7) - internal IPv4 addresses wrapped in NAT64 IPv6 addresses (
64:ff9b::/96)
If a host resolves to several addresses, every one of them has to be public.
Addresses that aren't http(s) are refused too.
DNS rebinding
The check and the connection both use the JVM's DNS cache, which holds an answer for 30 seconds by default. That's what stops a host from resolving to a public address for the check and an internal one for the connection. Don't set sun.net.inetaddr.ttl to 0.
Because the check resolves the host itself, fetching needs DNS. A server that reaches the internet only through an HTTP proxy, with no DNS of its own, can't fetch anything.
Headers and limits
Request headers, which is where the GitHub token goes, are sent to the first hop only, never to wherever it redirects. Providers' APIs and oEmbed endpoints are fetched through the same checks as pages.
Each fetch stops after timeoutSeconds (8), maxBytes (1 MB) and maxRedirects (5), so a slow or enormous page can't tie up a request thread.
allowPrivateAddresses
allowPrivateAddresses: true turns the address check off. It's there so tests and local development can unfurl a server on localhost, the way examples/smoke.bxs does. Never set it where users can paste links.
What it doesn't do
- It doesn't fetch images. Cards hold image addresses. Showing an image straight from the page's server tells that server who's reading; copy images to your own storage if that matters.
- It doesn't sanitize values. Titles, descriptions and
embed.htmlcome from someone else's page. Encode every value when you render it, and renderembed.htmlonly for providers you trust.