Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·6 min read

My video token check trusted a header the client sent

I had written a token check for the video server of Movix and I was rather pleased with it. A token is issued for a file, stored with the requester's IP, and every request is validated against that stored record. Then I read my own code the way someone who wanted a free download would read it, and the check turned out to be weaker than it looked in two separate ways. Both were small, and I fixed them in the early hours of 14 April 2025.

Project
Movix
Date
Apr 2025
Stack
Node.js, Express, MongoDB
Commit
7b417f4, 69cb168, 1b02bee

What broke

The design, in one paragraph: the player never gets a plain file URL. It asks a separate video server for a URL, the server issues an opaque token and stores it with the requester's IP, the content type and an expiry, and each range request is validated against that stored record. This is hotlink control, not DRM. It stops a casual link from being shared or reused, and it can only do that if the IP in the record means something.

It did not, quite. When the player asked for a URL, the server took the requester's address from a custom header. The client sent that header. A token bound to an IP was therefore bound to whatever the caller wrote in a header, and a client that wanted a token for another address only had to claim it.

The second problem was in validation. Besides comparing the stored address with the request's address, the check required the request to equal one fixed address that I had written into the code. I cannot reconstruct from the history why it was there. It looks like something left over from testing against my own setup. Either way, the stored address was not the only rule, and a check that quietly depends on one hard-coded value is a check I cannot reason about.

What the code showed

The two conditions look like this, with the names changed and the address left out:

js
// before: the caller's claim, plus a hard-coded second rule
const ip = req.headers["x-client-supplied-ip"];
if (stored.ip !== ip || ip !== FIXED_ADDRESS) deny();

Reading it again, the lesson was about where an address comes from. A server learns a client's address from the connection, or, behind a reverse proxy, from a header that the proxy sets and overwrites. It must not learn it from a header the client may send freely, because the whole point of binding a token to an address is to take that choice away from the client.

The fixed address was the more embarrassing of the two, since it was visible in the code and had slipped through anyway. A condition like that is the kind of thing that makes a check look like it is doing more than it does.

The fix

It took three commits.

7b417f4 (Apr 2025), with the commit message "real IPs", switched the server to the forwarded client address that the reverse proxy sets. In the same commit I added a dedicated CORS step for the watch route and kept the origin and referer allow list in front of everything, so a request from an unknown site is refused before it reaches any token logic.

69cb168 (Apr 2025), a few hours later the same morning, removed the second condition. The stored address is now the only rule: the token must exist, its type must match, the address must match the one stored with it, and it must not have expired. While I was there I fixed two things I had noticed. The usage counter was called tries although it only ever counted up, so it became times. And downloads, which have no playback time, no longer get a watch time written. The download route also had a call to the validator with its arguments out of step with the function, which this commit corrected.

1b02bee (Apr 2025), the next day, split the Express app from the process that runs it and started one worker per CPU. The primary process respawns any worker that exits, and each worker connects to the database on its own.

request with tokenforwarded addresscompares stored IPBrowsersends no trusted addressReverse proxysets the forwarded addressVideo serverone worker per CPUToken storeIP, type, expiry
Figure 1. The browser claims nothing, the proxy sets the address, and the video server compares it to the stored one.

The clustering is not a security fix, but it belongs to the same story. A server that holds up behind a proxy under load is a different thing from one that works on my machine. It was cheap to do here for one reason I had not planned for: the tokens live in the database and not in process memory, so a request can land on any worker and see the same record.

Warning. The forwarded header is trustworthy only when a proxy you control sets it. The video server is meant to sit behind one and I would not expose it directly, because a direct client could write that header itself and the original problem would return.

What I would do differently

I would let the framework own the address. Express can be told how many proxies to trust and then exposes the client address in one place. Reading a raw header at each call site, which is what I did, works only as long as each site agrees on which header to read and what to do when it holds a chain of addresses. I also have not checked how the production proxy formats that header, and a chain would need parsing to take the right hop.

I would write a test for the negative case. The hard-coded address survived because every manual test I ran passed from my own address. A test that sends a request with a valid token from a different address and expects a refusal would have failed on day one.

And I would review a security check by asking what a request can write, not what it reads. Every value a client can set is input, including headers that look like infrastructure. I now go through the request in that order when I look at access control.

Where this fits

This was the first real hardening of the video server, and it set the rule I still use there: the server decides who a token belongs to, never the caller. The token design, range streaming and the later fixes around it are in the full Movix case study.

The full project story covers how Movix streams video behind tokens, how its catalog fills itself and what else I changed after launch.

Read the full case study

#express#security#video-streaming#nodejs

Keep reading

Have a project in mind?