Skip to content

fix(traefik): make prefix path rules segment-aware - #4042

Open
Tyagiquamar wants to merge 1 commit into
fosrl:mainfrom
Tyagiquamar:fix/4039-redirect-prefix-segment
Open

Tyagiquamar wants to merge 1 commit into
fosrl:mainfrom
Tyagiquamar:fix/4039-redirect-prefix-segment

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

A redirect with the Prefix match type made sibling paths return 418 for logged-in users. With a Prefix redirect on /office/zz, a request to /office/zzabc answered 418 I'm a teapot instead of being proxied, while the same path was correctly treated as "not matched" for anonymous users.

Fixes #4039.

Root cause

The router rule and the auth gate disagreed about what "prefix" means.

  • appendPathMatch emitted PathPrefix(/office/zz). Traefik's PathPrefix is a plain string prefix, so the redirect router matched /office/zzabc.
  • Badger's findRedirect asks matchesPath, which is segment-aware: /office/zzabc is not under /office/zz, so it found no redirect and let the request continue.
  • The redirect router's service is noop@internal, and its comment says a request reaching it is a bug in the middleware chain. noop answers 418, which is what the user saw.

Anonymous requests got 401 instead because badger requires auth before this point, so they never reached the noop service.

Fix

For a Prefix path without a trailing slash, emit Path(<path>) || PathPrefix(<path>/) instead of a bare PathPrefix. A trailing slash keeps the plain PathPrefix, which already matches exactly the same requests as matchesPath. The comment in rule.ts now states why the two matchers are paired.

This applies to resource routers as well as redirect routers. They use the same helper, and a resource with a Prefix path had the same mismatch: its router matched /productsforsale while the auth gate treated it as a different path.

Testing

  • npx tsx server/lib/traefik/rule.test.ts - all matchesPath, priority and rule quoting tests pass, including three new cases: prefix without a trailing slash emits the paired matcher, a prefix without a leading slash is normalised, and a prefix ending in a slash stays a single PathPrefix.
  • npx tsx server/lib/traefik/pathEncoding.test.ts - 15 passed.
  • Prettier check on both changed files is clean.

middleware.test.ts and loadBalancer.test.ts could not run in this checkout: node_modules is not installed here and they import @server/* aliases, so tsx fails to resolve them. They do not touch appendPathMatch. I did not run the Traefik config snapshot suite (traefikConfig.test.ts is listed as known-failing in scripts/run-tests.mjs); CI will run it.

No end-to-end Traefik test: that needs a running stack with an SSO session, which this change does not have access to.

Traefik's PathPrefix is a plain string prefix, so a Prefix redirect on
`/office/zz` also matched `/office/zzabc`. Badger decides whether a request
belongs to a redirect with matchesPath, which is segment-aware, so it passed
the request through; the router had already sent it to the noop service, which
answered 418. Emit Path() plus PathPrefix(`<path>/`) for prefix matches so the
router and the auth gate agree.
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.

Redirects: authenticated requests to paths that only string-prefix-match a "Prefix" redirect return 418 I'm a teapot

1 participant