After adding a two-factor login gate to a self-hosted file manager, I checked which request paths it protected.
The original file-manager login page was visible while a blog path still served the same file. The audit later found that public requests had bypassed the newly configured gate. These are separate data-path and gateway-ingress findings.
The original 5.5 MB file and status observations are retained below while checking routes, proxy identity, one-time codes and caches. A one-year cache lifetime differs from an observed year of retention.
One set of bytes, two paths
The file manager’s root directory pointed at the blog’s uploads directory. That was a convenience decision at setup time: most of what goes into the drive ends up published on the blog anyway, so sharing one copy of the storage avoided shuffling files back and forth.
The blog’s nginx publicly serves the uploads directory for images, attachments, and download bundles. A single file on disk therefore had two URLs that returned it:
# Via the drive hostname: stopped by the gate, 302 to the login page
curl -sI https://prairie.example.com/wp-content/uploads/private.pdf | head -1
# Via the blog hostname: same bytes, 200
curl -sI https://www.example.com/wp-content/uploads/private.pdf | head -1
The record reports a 5.5 MB PDF, one 302, one 200 and a full download. The HEAD commands below show headers only; compare downloaded sizes and hashes to establish byte identity. A gate does not automatically protect a second hostname’s data path.
Authentication applies to a request path. If several URLs reach the same data, each needs checking. Here, the second path lived in another vhost and could not be found by inspecting the gate’s configuration alone.
The gate was not in the request path
To verify that the gate blocked the request, I opened its nginx access log and looked for the 302 response.
The log contained nothing but a handful of loopback curls I had run on the server myself.
This machine’s public entry point is a managed tunnel whose ingress rules live in the provider’s dashboard rather than on the server. That rule pointed at 127.0.0.1:8091 — the file manager itself. The nginx vhost where I had carefully configured auth_request listened on a different port and had never seen real traffic.
Before I found that the blog hostname exposed the files, the drive hostname had also been bypassing the gate.
The login page shown by the drive hostname belonged to the file manager itself. It differed from the gate’s page, but I had not compared them and missed the bypass.
Correlate an external request marker across tunnel and vhost logs. A tunnel can legitimately appear as loopback to Nginx, so a non-loopback IP is not necessary proof; forwarded addresses also require trust checks.
awk '{print $1}' /var/log/nginx/prairie.access.log \
| sort -u | grep -v '^127\.' | head
No output calls for checks of logging, format, caches and ingress. Change the tunnel target or listener ports to place the gate on the path, then record and verify the resulting configuration.
Proxy auth outsources trust to a request header
Once the ports were swapped, one thing remained: the user should not have to log in twice.
In proxy authentication mode, the file manager trusts the X-Auth-User header that nginx supplies. Identity verification therefore moves to the proxy, with two constraints that must hold together:
location / {
auth_request /__auth;
auth_request_set $authuser $upstream_http_x_auth_user;
proxy_set_header X-Auth-User $authuser; # only ever from the gate's response
proxy_pass http://127.0.0.1:8090;
}
location ^~ /share/ {
proxy_set_header X-Auth-User ""; # unauthenticated paths must blank it
proxy_pass http://127.0.0.1:8090;
}
Overwrite trusted identity headers, clear them on exempt paths and restrict direct backend access. Also inspect cookies, query parameters and other identity inputs. Whether a forged header grants administrator access depends on application trust and routes. The example omits the internal auth endpoint, 401/403 handling and backend isolation.
A related detail belongs to the same family: the gate must reject usernames containing newlines or non-printable bytes when it mints its cookie, because that value eventually flows into a response header.
Validated is not the same as logged in
The gate also had three state problems involving code consumption, failed-login diagnosis, and logout.
The first was when a one-time code gets consumed. Replay protection requires each code to be usable once, implemented by recording the counter already spent and demanding that subsequent codes strictly advance it. My first version performed that advance inside the function that validates the code — the moment a code matched, it was marked spent.
That produced the following behaviour: the user types a correct code and a wrong password, the login fails, and the code is burned anyway. They fix the password and retry, and the code from that same 30-second window is now rejected as a replay. All they can do is wait for the next window.
After password and TOTP validation, atomically check and commit the user’s consumed counter, then issue a session only on success. The retained pseudocode requires commit_totp to handle concurrent replay, persistence and failure. Separate reads and writes can admit two requests using one code.
counter = check_totp(code) # decide only, no side effect
if counter is None or not verify_password(user, pw):
return unauthorized()
commit_totp(counter) # consume only once everything passed
return issue_cookie(user)
Coordinate code consumption with session issuance. Failed-attempt counters, rate limits and audit logs still need updates on failure paths; not every side effect belongs only after success.
The bug also provided a diagnostic clue. The public error message deliberately concealed whether the password or code was wrong. Internal debugging therefore needed another record.
Counter changes can help diagnose the original implementation, but a 401 can arise from other checks or session failures. Correlate explicit internal stages with a request rather than inferring each credential result from the state file alone.
The third is logging out. The application’s own logout button clears the session credential it issued — but under proxy authentication its next request re-identifies the same user from X-Auth-User, so nothing was logged out. Logout has to reach the gate’s logout endpoint and clear the cookie the gate issued. This became true the moment proxy authentication was switched on, and the “sign out” button in the interface looks perfectly normal throughout. You do not find it without testing it.
Two traps while patching
I added an allowlist to the blog’s nginx, publicly serving only named uploads subdirectories and returning 404 for the rest. This location had to precede the static-extension regex so that the latter did not match the request first.
Writing that configuration cost me two mistakes, neither of them in the logic.
Quote Nginx regex patterns containing braces. An isolated nginx -t check of this example passed with quotes and failed with a nonzero regex-compilation error without them. The original claim of silent truncation without a syntax error does not hold for this example.
location ~ "^/wp-content/uploads/(20\d{2}|published)/" { }
The second issue was in the reload script:
if nginx -t | tail -1 | grep -q successful; then systemctl reload nginx; fi
A shell normally uses the last pipeline status, but nginx -t usually writes diagnostics to stderr, outside this unredirected pipe. The original guard can therefore suppress reload even on success; the shown command does not establish the claimed failure-output match and reload. Check Nginx’s own exit status directly.
The correct form tests nginx -t‘s own exit status:
if nginx -t; then systemctl reload nginx; fi
Blocked at the origin, still served at the edge
The last layer is one I only verified afterwards: blocking a file at the origin does not remove copies already sitting in the CDN’s edge cache.
The record includes max-age=31536000, immutable and an edge HIT. Those seconds specify about a year of freshness; HIT establishes that request’s cache use, not a year of continuous retention. Eviction can occur earlier. Revoke origin access and purge every public cache path.
Check origin and edge access separately. Query parameters alter cache keys only according to configuration. Use a verified origin path, defined purge and content comparisons.
There was a related complication. The API token I had on hand was scoped for DNS validation only and could not purge cache — the call returns an authentication error outright. Purging needs either the dashboard or a separately issued token carrying cache-purge rights. That is worth confirming before you need it rather than during an incident.
What generalises
The audit also produced three reusable checks.
First, check every URL that can reach the same data. Try the path of a file that should be private against each public hostname, in addition to reviewing individual configuration files.
Second, correlate request markers with target logs to establish routing, and verify client identity through trusted proxy information.
Third, check each authentication exemption for unchanged headers, cookies, or query parameters on which the authenticated path relies.
Afterwards, I separated private storage from disk paths directly served by the public web server. Allowlist entries can grow with the system; separate storage reduces reliance on that list.