← Writing
boxlang link previews oembed

Link previews for BoxLang with bx-unfurl

bx-unfurl is a BoxLang module for link previews. unfurl() fetches a URL and returns its Open Graph, Twitter card and plain-HTML metadata as one struct; unfurlHtml() reads HTML you already have. It refuses internal addresses on every redirect hop to block SSRF, caps size and time, and reads GitHub, YouTube, Vimeo, npm, ForgeBox, Mastodon, Bluesky, Reddit and Wikipedia from their APIs for richer cards. It also supports oEmbed. Requires BoxLang 1.19+; MIT licensed.

Someone pastes a link into a post, and a card appears under it: a title, a line of description, a picture. It looks like the simplest feature in the app. Fetch the page, find og:title, done.

Then you look at what you just built. Your server now fetches any address a stranger types, from inside your network. http://localhost:8080/admin is an address. So is http://169.254.169.254/, where a cloud server keeps its credentials. So is a public page that redirects to either of them. And the pages that are harmless are a mess of their own: three kinds of tags that disagree, relative image addresses, a 40 MB page, a charset that's declared only in the HTTP header, and GitHub, whose pages say much less than its API does.

I worked through these building the link preview card in DismalThreads, a community app written in BoxLang. bx-unfurl is that work pulled out into a module: everything between "here's a link someone pasted" and "here's what to draw". HTML is read by jsoup, which ships inside the module.

This post walks through it:

  • Reading a card from a page
  • What's in the card, and where each field comes from
  • Sites that are read from their API instead
  • Embeds
  • Errors, and what to do about each
  • What can go wrong when your server fetches what strangers ask it to

The output shown comes from scripts I ran against a small local server while writing this, with the server's address swapped for example.com.

Step 1: Install the module

You need BoxLang 1.19 or later. Install from ForgeBox with any of these:

install-bx-module bx-unfurl --local
install-bx-module bx-unfurl
box install bx-unfurl

The first installs into your project's boxlang_modules folder. The second installs into your BoxLang home, so every app on the machine gets it. The third uses CommandBox.

The module registers two BIFs. unfurl( url, options ) fetches an address and returns its card. unfurlHtml( html, url ) reads a card from HTML you already have and fetches nothing.

card = unfurl( "https://example.com/article" )

println( "#card.title# (#card.type#) from #card.siteName#" )
println( card.description )
println( "#card.image# #card.images[ 1 ].width#x#card.images[ 1 ].height#" )
println( "by #card.author.name#, #card.publishedAt#" )
The Article (article) from Example News
What it says
https://example.com/cover.jpg 1200x630
by Ada Lovelace, 2026-11-02T10:00:00Z

That page wrote its image as /cover.jpg. The card has it absolute, resolved against the address the page was actually served from, which matters when the link redirected on the way there:

card = unfurl( "https://example.com/old-address" )
println( "asked for #card.requestedUrl#, read #card.fetchedUrl#" )
asked for https://example.com/old-address, read https://example.com/article

A link straight to a picture is a card too. There's no page to read, so the picture is the card, and source says so:

source image, image https://example.com/photo.png

Step 3: Know what's in the card

Pages describe themselves three ways: Open Graph tags (og:title), Twitter card tags (twitter:title) and plain HTML (<title>, <meta name="description">). Most pages have some of each and they don't always agree. Each field of the card takes the best one the page has:

FieldRead from, in order
titleog:title, twitter:title, <title>
descriptionog:description, twitter:description, <meta name="description">
urlog:url, the canonical link, the address read
imagesog:image and its properties, twitter:image, <link rel="image_src">
faviconrel="icon", an apple-touch-icon
author<meta name="author"> and article:author
publishedAtarticle:published_time

There's also canonical, type, siteName, locale, videos, audios, twitter, embed and details. The README has the whole table.

Every key is always there. Text that's missing is "", a list that's missing is [], a width nobody declared is 0. A page with nothing but a title still gives you a card you can draw without checking a single key:

bare = unfurlHtml( "<title>Just a title</title><p>Nothing else.</p>", "https://example.com/page" )
println( "'#bare.title#', image '#bare.image#', #bare.images.len()# images, url #bare.url#" )
'Just a title', image '', 0 images, url https://example.com/page

og:image can appear more than once, each followed by properties that describe it. They come back as one array, in page order, and card.image is the first one's address:

[
	{ "url": "https://example.com/cover.jpg", "secureUrl": "", "type": "", "width": 1200, "height": 0, "alt": "" },
	{ "url": "https://example.com/second.png", "secureUrl": "", "type": "", "width": 0, "height": 0, "alt": "" }
]

Addresses you can put in an attribute

Every address in the card is absolute and http or https, or it's "". A page that sets its og:video to javascript:alert(1) gets an empty videos array, so nothing from a card can turn into a script when you write it into href or src.

The one exception is card.tags, which is every og:, twitter: and article: tag exactly as the page wrote it, for when you need one the card doesn't have a field for:

{ "og:image": [ "/cover.jpg", "/second.png" ], "article:tag": [ "one", "two" ], "og:video": [ "javascript:alert(1)" ] }

Treat those values as text from a stranger. Titles and descriptions are plain text too: escape them on output like anything else a user typed.

Step 4: Sites that have a better answer

A GitHub issue's page will tell you its title. GitHub's API will tell you it's closed, who opened it, how many comments it has and what it's labelled. For nine sites the module asks the API and never fetches the page: GitHub, YouTube, Vimeo, npm, ForgeBox, Mastodon, Bluesky, Reddit and Wikipedia.

card = unfurl( "https://github.com/acme/widget/issues/3" )
println( "#card.title# | #card.description# | #card.author.name# | #card.source#" )
println( jsonSerialize( card.details ) )
It broke | Line one line two | ada | github
{"kind":"issue","repo":"acme/widget","number":3,"state":"notPlanned","author":"ada","comments":4,"createdAt":"2026-10-01T00:00:00Z","labels":[{"name":"bug","color":"d73a4a"}]}

The card has the same keys as any other, so the code that draws a plain link draws this one. What's extra is in details, and details.kind tells you what you have: repo, issue, pull, video, package, post or article. That's enough to draw a state badge on an issue, a star count on a repository or weekly downloads on an npm package when you want to, and to ignore it when you don't.

card.source is the provider's name, or page when the card was read from the page's tags.

A few things worth knowing:

  • A provider that can't answer isn't an error. Rate limited, a private repository, a server that turned out not to be Mastodon: the page is read instead, and you get whatever its tags say.
  • GitHub allows 60 calls an hour without a token and 5000 with one. Set githubToken in the module's settings. A token with no scopes is enough for public repositories.
  • Posts are careful with what they show. A Mastodon post behind a content warning gives the warning as its description and the author's avatar as its image. A Bluesky post labelled adult or graphic, and a Reddit post marked NSFW or spoiler, get no media image.
  • You choose which are asked. providers: [ "github", "youtube" ] asks only those two, and providers: [] reads every address as a page.

Step 5: Embeds

Many pages point at an oEmbed endpoint, a small JSON document with the author, a thumbnail and the HTML for a player. When a page names one, the module reads it and fills in what the tags left out:

card = unfurl( "https://example.com/talk" )
println( "#card.title# by #card.author.name#, image #card.image#" )
println( "#card.embed.type# #card.embed.width#x#card.embed.height# from #card.embed.providerName#" )
A talk by Ada, image https://example.com/1.jpg
video 640x360 from Talks

That page had a <title> and nothing else. The author, the image and the embed all came from oEmbed. YouTube and Vimeo cards get their embed the same way.

card.embed.html is the provider's own iframe. Putting it on a page sends every reader's browser to that provider as soon as the page loads. Draw the thumbnail with a play button and swap in the iframe on click, and that only happens for readers who asked for it. Turn the whole thing off with oembed: false.

Step 6: Handle what goes wrong

Three errors, and each wants something different from you:

try {
	card = unfurl( link )
} catch ( "Unfurl.Refused" e ) {
	// not http(s), or an internal address: never retry, show the bare link
} catch ( "Unfurl.RateLimited" e ) {
	// HTTP 429: try again later; e.extendedInfo is Retry-After's seconds when it gave a number
} catch ( "Unfurl.FetchError" e ) {
	// timeouts, 4xx/5xx, too many redirects, a host that doesn't resolve
}
Rate limited: HTTP 429 (retry after 120s)
Failed: HTTP 404
Refused: not http(s)

A link with no card is still a link. Whatever happens, show it bare and carry on; a failed preview should never be the reason a post can't be saved. Unfurl after the post is saved, not while the user waits.

What can go wrong

The feature is "my server fetches what a stranger asks it to". That has a name, server-side request forgery, and it's how a link preview becomes a way into your network. The module's fetching exists to close it.

Internal addresses. unfurl() refuses any address that isn't on the public internet:

Refused: 127.0.0.1 is an internal address
Refused: 169.254.169.254 is an internal address
Refused: [::1] is an internal address

Loopback, private ranges, link-local (which is where cloud metadata lives), carrier-grade NAT, reserved ranges, IPv6 unique-local, and internal addresses wrapped in NAT64 are all refused.

Redirects. Checking the address you were given isn't enough, because a public page can answer with Location: http://localhost/. Redirects are followed by hand and every hop gets the same check. Provider APIs and oEmbed endpoints go through it too: an oEmbed link is an address a stranger's page chose.

DNS rebinding. A hostname can resolve to a public address when you check it and an internal one a moment later when you connect. The check and the connection both use the JVM's DNS cache, so they see the same answer. Leave sun.net.inetaddr.ttl alone; setting it to 0 takes that away.

Your token. The GitHub token is sent to the first address asked and never to wherever that redirects.

Big and slow pages. A fetch reads at most maxBytes (1 MB by default; the tags are in the <head>), waits at most timeoutSeconds (8) and follows at most maxRedirects (5).

Set allowPrivateAddresses: true in development and in tests, and nowhere else.

Settings

In boxlang.json, under the module's name:

"modules": {
	"bxUnfurl": {
		"settings": {
			"userAgent": "MyApp/1.0 (+https://myapp.example)",
			"githubToken": "a token, for 5000 calls an hour"
		}
	}
}

Any setting can be given for one call as well: unfurl( link, { timeoutSeconds: 3, providers: [] } ).

Set the User-Agent to something that says who you are. Some sites only serve their tags to agents they recognize, and a few serve nothing to anyone who isn't a browser. Those links get a card with whatever the page did send, or a FetchError.

Testing without the network

The module's own specs never ask a real site for anything. Pages are served from a tiny HTTP server started on a random free port, and so are the providers, because every API's address is a setting. You can do the same in your app's tests:

httpServer = createObject( "java", "com.sun.net.httpserver.HttpServer" )
	.create( createObject( "java", "java.net.InetSocketAddress" ).init( "127.0.0.1", 0 ), 0 )
httpServer.createContext( "/", ( exchange ) => {
	var body = fileReadBinary( "tests/resources/repo.json" )
	exchange.sendResponseHeaders( 200, javacast( "long", arrayLen( body ) ) )
	exchange.getResponseBody().write( body )
	exchange.close()
} )
httpServer.start()

card = unfurl( "https://github.com/acme/widget", {
	allowPrivateAddresses : true,
	endpoints             : { github: "http://127.0.0.1:#httpServer.getAddress().getPort()#" }
} )
httpServer.stop( 0 )

The same setting points the GitHub provider at a GitHub Enterprise server. examples/smoke.bxs in the module runs both BIFs this way in one script.

What bx-unfurl leaves out

  • A cache. Every call fetches. Store the card with the post, or cache it by address for a day; a popular link shouldn't be fetched once per reader.
  • Copying images. Images come back as addresses. Showing one straight from the page's server tells that server who's reading your site and when. Copy it to your own storage or serve it through a proxy if that matters to you.
  • Sanitizing embeds. embed.html is the provider's markup, as sent.
  • Running JavaScript. A page that builds its tags in the browser has no tags as far as the module can see.

Wrapping up

A link preview is a small feature sitting on top of a fetch you have to get right. bx-unfurl is that fetch, plus the reading: it refuses the addresses it should, asks the API when the API knows more, and hands you the same struct whether the link was a news article, a pull request or a picture. Drawing the card is yours.

It's MIT licensed. Issues and pull requests are welcome, and so are links that break it.

No comments yet — be the first.