How discord displays images and website previews

a discord youtube video embed displaying little dark age

Have you ever wondered how discord displays images and links when you post them in the channel? Discord has a media proxy which will make the request and return the item to the user. This helps reduce the amount of requests to the web server and keeps users safe.

When you post a link of an image or video in discord, it renders a preview. You can still see the original by clicking on the image and clicking view original to see it in your browser. But how does it get to your device? It’s passed through a proxy and cached on your device for faster viewing next time. But why do discord do this?

Caching

I’ve mentioned caching on your own device, but discord also caches it on their own servers. Discord tend to keep a copy of website embeds and images so they can be sent faster over discord. It’s not often web resources change so this is an effective method. This means resources load almost instantly in discord’s if they’ve recently been posted.

Without these methods, the pages would have to be fetched each time by the users client. This isn’t a huge issue for small discords, since there won’t be that many people looking at the channel. But if a @everyone ping is made in a big discord with 100,000s of members, that could be hundreds of requests a second, effectively a DDoS attack from so many people seeing what the message was.

However this doesn’t come without its flaws. If you’re developing a website and trying to make your meta tags to make a discord embed for your site, this can be infuriating. Thankfully there is an easy way around this, you can supply GET parameters to the end of your page and discord will treat it as a separate page. For example add ?a=1 on the end of the URL. If you need to refresh after that do ?a=2 instead and so on.

Security

Making a request over the internet requires an IP address. IP addresses (while it’s rarely ever the case) being leaked can cause security concerns. Although I wouldn’t worry about this unless you are running some kind of web server that’s vulnerable to hacking. (If you are knowingly doing that, I don’t even know what to say to you).

Regardless of what you are running, the proxy will prevent your IP being leaked by using Discord’s IP address to make requests.

Looking at how the discord media proxy behaves

For these experiments I’m going to be using webhook.site, a tool I use often for testing Discord embeds. While I recommend it, it is an IP grabber. So if you use it, make sure you do this in a private Discord channel, mods won’t take kindly to IP grabbers being posted.

When you post a link, discord makes a GET request to that link as a standard browser would. It identifies itself in each request with a user-agent as most bots do to tell the web admin who it is.

a summary of a web request from the discord media proxy, it just states its user agent is discordbot 2.0
A request from the discord media proxy on webhook.site with some details censored

It will also make any follow up requests if it’s previewing a website which has an image or other media in the preview.

a discord youtube video embed displaying little dark age
An embed from my ‘joke-embeds’ application, this embed requests multiple resources since it requests a thumbnail for the video and then video file itself.

An interesting aspect of the proxy it that it will follow redirections. If the proxy makes a request to the site and receives a 301 or similar status code, it will go the site it’s been redirected to. When it forwards a request to the new site it will contain a referrer header, specifying the site discord was originally trying to preview.

a request from the discord media proxy, it has a referrer header saying its come from a bitly link
A request from the discord media proxy after a bit.ly shortlink redirected it using a 301

Discord made a very technical system which is very intriguing to explore. Given how much data this much process a day, the possibilities are endless. Discord themselves made a blog which goes into the technical details of image resizing if you want to know about that. What’s your take on media proxies like this one?

How discord displays images and website previews Click To Tweet

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.