Skip to main content

The Server Fetched Any URL It Was Handed

Updated 5 min readMarkdown

A reader can paste a link and import the book behind it. The server downloads the file, parses it, and puts it on the shelf. The feature is convenient because the reader never touches the file. The trouble was that the server never asked where the link pointed. It checked the scheme, fetched the URL, and stored whatever came back.

Anyone holding a login cookie could trigger it. No admin role was needed, and no special tier: the import ran before the quota check, so the quota couldn't stop it, and the error responses carried the exception's message, which said plenty about what the server could reach. A link to the cloud metadata address would have come back as a book.

The check existed. It just wasn't on this path.

The codebase had a check for this. is_safe_url takes a URL, allows only http and https, resolves the host, and rejects anything that lands on a private, loopback, link-local, reserved, or multicast address. It was a real check, and it worked. It just wasn't called on the import path. The import file had zero references to it.

The same configuration got different treatment depending on which button was pressed. The "test connection" button ran the check and rejected a private address with a 400. Saving the same configuration skipped the check entirely. And the stored configuration was never re-checked: every sync decrypted it and connected straight away. An address saved once would be dialed on every sync.

The S3 endpoint was never checked, because it isn't called url

The test-connection check looked at one field: config['url']. A WebDAV source keeps its address in url, so it got checked. An S3 source keeps its address in endpoint. There is no url key in an S3 config, so the condition skipped, and the endpoint was never checked — not on test, not on save, not on sync. An S3 endpoint pointing at 127.0.0.1:9000 sailed through all three.

The check was written against a field name. It never asked which addresses the server was about to connect to. Any provider that stored its address under a different name got a free pass. The fix declares the fields that need checking — url and endpoint — and validates all of them, so a new provider has to register its address fields instead of inheriting an exemption by naming.

A checked URL can still bounce

Even where the check ran, it ran once, at the moment the address was submitted. The request library follows redirects by default, and nothing in the project ever turned that off. A public URL that passed the check could answer with a 302 to an internal address, and the request would follow it. The gate never saw the second address.

The language-model calls had the same hole in a different shape. Their base URL was checked on every call. But the two request sites followed redirects, and the check covered the base URL. The redirect target was never checked. Anyone could configure their own provider base URL, point it at a domain they control, and have it bounce the server's request inward.

The check moved to where the request is made

The fix moved the check to the layer that actually sends the request. A new safe_request helper validates the URL first, sends with redirects disabled, and validates each hop before following it. The book import goes through it now, with up to three hops allowed — enough for the usual http-to-https and CDN jumps — so the check holds no matter which entry point called it. An unsafe address now fails with a 400 before any request leaves the server.

The same pass added the check to the three source paths that lacked it: creating a source, updating one, and syncing. Sync re-validates the stored configuration before connecting, so an address saved before the fix gets caught too. The language-model calls got the narrower fix they needed: redirects disabled, nothing else changed, because the existing tests mock the request calls and the goal was only to stop following redirects.

Seventeen new tests cover the rejected addresses, the allowlist, the blocked redirect, and the stored-config re-check. The full suite ran 1041 passed, 7 skipped, none failed.

The allowlist is there because one rule can't cover every deployment

Blocking every private address would have broken real setups. A WebDAV source pointing at a home NAS, an S3 source pointing at a self-hosted MinIO, a language model running on the same machine — all of these are private addresses, and all of them are legitimate uses. So the gate takes an allowlist, JBOOK_SSRF_ALLOW_HOSTS, read from the environment: hostnames, IPs, or CIDR ranges, comma-separated. Empty by default, which means public addresses only — the same behavior as before the change. A deployment that needs its NAS reachable configures it explicitly.

About the author

  • Writing about ebook technology.

No comments yet. Be the first to comment.