Skip to content

Make API URL in API promotion links safer - #1062

Merged
pascalchevrel merged 1 commit into
mozfr:mainfrom
pascalchevrel:snyk_safer_api_url
Nov 11, 2026
Merged

pascalchevrel merged 1 commit into
mozfr:mainfrom
pascalchevrel:snyk_safer_api_url

Conversation

@pascalchevrel

Copy link
Copy Markdown
Member

Utils::redirectToAPI() printed the raw REQUEST_URI into the href of the "API request" link, which had several problems:

  • XSS: quotes or <> in the request URI could break out of the href attribute.
  • Off-site links: a request target such as //evil.example.com/... or https://evil.example.com/... produced a link to another host.
  • Wrong URL without a query string: QUERY_STRING is usually an empty string, not null, so the link became /path&json instead of /path?json.
  • PHP warning when QUERY_STRING was not set.

Changes

  • Rename Utils::redirectToAPI() to Utils::getAPIUrl(), since it builds a URL and doesn't redirect.
  • Add a private Utils::getRequestPathAndQuery() helper that rebuilds the path and query from REQUEST_URI only (scheme, host, user info and fragment are dropped, the path always starts with a single /). It falls back to / if REQUEST_URI is missing, not a string, or can't be parsed.
  • getAPIUrl() escapes its output with secureText(), so it can be printed directly in templates. It must not be used for a Location: header, since & is returned as an HTML entity.
  • APIPromotion() now parses the query string from the helper instead of the output of redirectToAPI(). Its output format is unchanged, except that without a query string it no longer includes a stray /?json= parameter.

Tests

Added cases for an empty query string, protocol-relative and absolute URLs, a malformed URI, a missing or non-string REQUEST_URI, an attribute breakout attempt, and APIPromotion() with no query string or HTML in the path. The full suite passes (320 tests).

🤖 Generated with Claude Code

Rename Utils::redirectToAPI() to Utils::getAPIUrl() since it builds a
URL and doesn't redirect.

- Rebuild the URL from the path and query of REQUEST_URI only, so the
  link is always same-origin (no //host or absolute URLs).
- Escape the returned URL with secureText() so it can't break out of
  the href attribute (XSS).
- Stop relying on QUERY_STRING, which is usually an empty string and
  not null, producing '/path&json' instead of '/path?json'.
- APIPromotion() now parses the query string directly instead of the
  output of redirectToAPI().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pascalchevrel
pascalchevrel merged commit 142b6de into mozfr:main Nov 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant