Bug of the week · 2026-10-19
The redirect check that trusts a leading slash
The post-login redirect in an Express API · Security · difficulty 1/3
After a successful login, the API redirects the user back to the page they originally asked for. The target comes from a ?next= query parameter, so it is user-controlled. A teammate asked the AI assistant: "Write resolveRedirect(target, fallback) for our login endpoint. Only allow redirects to pages on our own site; anything else should go to the fallback."
The code came back with tests that pass. The contract promised by the docstring:
resolveRedirect(target, fallback)returnstargetwhen it is a local path on this site, andfallbackotherwise.- A missing, empty or whitespace-only
targetfalls back. - Absolute URLs to other hosts (
https://evil.example) must never be returned. isLocalPath(url)is the predicate behind it:trueonly for paths that the browser will resolve on the current origin.
Keep in mind how a browser interprets a Location header: it does not only look for http://. Review the code before it ships.
Click the line (or lines) where the defect is, pick its category, then submit. The code is read-only in this mode.