Dev.to Security 🔐 Cybersecurity 👁 0 📖 5 min read

nginx secure_link returns 403 for a hash that looks right? It wants Base64URL MD5, not hex and not '+/'

You have a download link that nginx is supposed to protect with secure_link. Your backend builds the hash, the link looks fine, and nginx still answers 403. Most of the time the MD5 itself is correct. It's just written t

You have a download link that nginx is supposed to protect with secure_link. Your backend builds the hash, the link looks fine, and nginx still answers 403. Most of the time the MD5 itself is correct. It's just written the wrong way.

nginx's ngx_http_secure_link_module has two modes, and each one wants the same kind of MD5 digest written differently:

Mode Directive How the hash is written
Old mode secure_link_secret word; hex, 32 characters, inside the path: /p/5e814704a28d9bc1914ff19fa0c4a00a/link
Expiring links secure_link + secure_link_md5 Base64URL, 22 characters, usually in a query argument: ?md5=_e4Nc3iduzkWRm01TBBNYw&expires=...

Both examples come from the official module documentation (read 5 Dec 2026), which says outright: "The MD5 hash value passed in a request is encoded in base64url."

What nginx actually checks

We read the module source (src/http/modules/ngx_http_secure_link_module.c and ngx_decode_base64url() in src/core/ngx_string.c, nginx master on GitHub, 5 Dec 2026). The checks run in this order:

  1. Whatever comes before the comma in the secure_link expression is the hash. If it's longer than 24 characters, the check fails. A 32-character hex digest never gets past this step.
  2. The value is decoded with a URL-safe-only alphabet. In that decoder - and _ are valid, while + and / are not. A standard Base64 digest containing either one fails here.
  3. Decoding stops at the first =, so == padding is tolerated (22 + 2 = 24 characters still passes step 1).
  4. The decoded result must be exactly 16 bytes, which is the size of an MD5 digest.
  5. nginx computes MD5 over the secure_link_md5 expression and compares all 16 bytes. If they differ, $secure_link is empty and you get 403 in the usual config.
  6. Only after the hash matches does it compare expires with the current time. An expired link sets $secure_link to "0" (410 in the docs' example config).

Worked example (illustrative values, tested on a real nginx)

Config, following the documented example:

location /files/ {
    secure_link     $arg_md5,$arg_expires;
    secure_link_md5 "$secure_link_expires$uri$remote_addr s3cr3t-demo";
    if ($secure_link = "")  { return 403; }
    if ($secure_link = "0") { return 410; }
}

Illustrative inputs:

  • expires = 1798786800 (1 Jan 2027, 07:00 UTC = 12:30 IST)
  • $uri = /files/report-q4.pdf
  • $remote_addr = 127.0.0.1
  • secret = s3cr3t-demo (an example only, so use your own)

The string that gets hashed is 1798786800/files/report-q4.pdf127.0.0.1 s3cr3t-demo. Its MD5 written three ways:

Encoding Value
hex fc0851058832ed2426e22e8c2979d8e9
standard Base64 /AhRBYgy7SQm4i6MKXnY6Q==
Base64URL, no padding _AhRBYgy7SQm4i6MKXnY6Q

We ran this against nginx 1.30.8 (the official nginx.org Debian package, built --with-http_secure_link_module) on 127.0.0.1 and requested each variant:

md5= value sent Result
_AhRBYgy7SQm4i6MKXnY6Q 200
_AhRBYgy7SQm4i6MKXnY6Q== 200 (padding tolerated)
/AhRBYgy7SQm4i6MKXnY6Q== (standard Base64) 403
same, percent-encoded (%2FAhR...%3D%3D) 403
fc0851...d8e9 (hex) 403
correct hash, but expires=1798786801 403 (expires is part of the hashed string)
correct hash for expires=1700000000 (in the past) 410

The percent-encoded row matters. $arg_md5 reaches the module as it appears in the query string, without URL-decoding. Escaping / as %2F doesn't help. The fix is to switch to the URL-safe alphabet.

Generating the value correctly

The docs' shell recipe, with our inputs:

echo -n '1798786800/files/report-q4.pdf127.0.0.1 s3cr3t-demo' \
  | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =
# _AhRBYgy7SQm4i6MKXnY6Q

Node.js (the base64url digest encoding already leaves out the padding):

const crypto = require("crypto");
const md5 = crypto.createHash("md5")
  .update("1798786800/files/report-q4.pdf127.0.0.1 s3cr3t-demo")
  .digest("base64url"); // "_AhRBYgy7SQm4i6MKXnY6Q"

Python:

import base64, hashlib
s = "1798786800/files/report-q4.pdf127.0.0.1 s3cr3t-demo"
md5 = base64.urlsafe_b64encode(hashlib.md5(s.encode()).digest()).rstrip(b"=").decode()

The two mistakes we see most often are md5(s).hexdigest() (hex, 32 characters) and base64.b64encode(md5(s).hexdigest()). The second one Base64-encodes the hex text, so you get 44 characters encoding 32 ASCII bytes instead of 22 encoding 16 raw bytes.

Debugging a 403: compare bytes, not strings

When a link fails, check whether your backend and nginx agree on the bytes:

  1. Copy the md5= value from the failing link.
  2. Open the Hex ↔ Base64 converter and click Base64 to Hex.
  3. Paste the value. The tool turns -/_ back into +// and restores missing padding before decoding, so a Base64URL value works as-is. _AhRBYgy7SQm4i6MKXnY6Q shows FC 08 51 05 88 32 ED 24 26 E2 2E 8C 29 79 D8 E9.
  4. Compare that with echo -n '<your string>' | openssl md5. If the 16 bytes match, the encoding is fine and the problem is the string. If you only have a hex digest, use Hex to Base64 instead. Its output is standard Base64 (/AhRBYgy7SQm4i6MKXnY6Q==), so you still need to change +/ to -_ and drop the =.

If the output has more or fewer than 16 bytes, you're looking at an encoding problem. Hex text that got Base64-encoded, for example, decodes to 32 bytes.

When the bytes are right but the string is wrong

  • Proxies and $remote_addr. Behind a load balancer or CDN, $remote_addr is the proxy's address, not the visitor's, unless you've set up the realip module. Your backend hashes the user's IP and nginx hashes the proxy's, so you get 403. In our test, a hash built for 203.0.113.7 while nginx saw 127.0.0.1 returned 403.
  • $uri is the decoded path. For /files/my%20report.pdf, nginx hashes /files/my report.pdf with a real space. Hashing the decoded path gave 200 in our test, and hashing the percent-encoded path gave 403.
  • Spaces and order. The docs' expression has exactly one space, before the secret, and none between expires, URI and IP. Your backend has to build the string in exactly the same order.
  • Seconds, not milliseconds. expires is Unix time in seconds. Date.now() returns milliseconds.

Limits worth knowing

MD5 is what this module uses. It's fine for short-lived links with a strong secret, but anyone who has the secret can mint links, and the link only lives as long as expires says. Base64URL here is only a compact way to write 16 bytes. It doesn't encrypt anything, and the decoded bytes aren't secret.

— The Mind vs Drive team

Disclosure: this tool is from Mind vs Drive, the team behind this account. The Hex ↔ Base64 converter runs in the browser; check your organisation's policy before pasting production secrets into any web page.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.