Skip to content

fix: preserve query string from X-Forwarded-Uri in decision handler#1265

Open
LennartKoot wants to merge 1 commit into
ory:masterfrom
ShipitSmarter:fix/preserve-query-string-in-decision-mode
Open

fix: preserve query string from X-Forwarded-Uri in decision handler#1265
LennartKoot wants to merge 1 commit into
ory:masterfrom
ShipitSmarter:fix/preserve-query-string-in-decision-mode

Conversation

@LennartKoot

@LennartKoot LennartKoot commented Mar 19, 2026

Copy link
Copy Markdown

This PR sets the r.URL.RawQuery to a potential query string provided within the X-Forwarded-Uri header. This was dropped in an earlier version because including it in the path broke the URL matcher, but fully stripping the query string breaks authenticator that rely on query parameters.

Related issue(s)

#1264

Checklist

  • I have read the contributing guidelines.
  • I have referenced an issue containing the design document if my change
    introduces a new feature.
  • I am following the
    contributing code guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security vulnerability, I
    confirm that I got the approval (please contact
    security@ory.com) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further Comments

Summary by CodeRabbit

  • Bug Fixes
    • Fixed improper handling of query parameters in forwarded URIs, ensuring they are now correctly preserved during request processing.

Review Change Stack

@LennartKoot
LennartKoot requested review from a team and aeneasr as code owners March 19, 2026 13:47
@CLAassistant

CLAassistant commented Mar 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@LennartKoot
LennartKoot force-pushed the fix/preserve-query-string-in-decision-mode branch 2 times, most recently from e127fb1 to 569188b Compare March 19, 2026 13:58
@LennartKoot
LennartKoot force-pushed the fix/preserve-query-string-in-decision-mode branch from 569188b to 5246bef Compare May 27, 2026 11:21
@coderabbitai

coderabbitai Bot commented May 27, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This change refines how the decision handler processes the X-Forwarded-Uri header by explicitly parsing query strings. The implementation now conditionally extracts path and query components when the header is present, falling back to deriving the path from the current request URL when absent. Tests validate that query strings are preserved and requests are correctly matched.

Changes

X-Forwarded-Uri Query String Handling

Layer / File(s) Summary
X-Forwarded-Uri parsing with query string support
api/decision.go, api/decision_test.go
DecisionHandler.Decisions replaces single-expression path assignment with explicit conditional logic: if X-Forwarded-Uri is set, it splits on ? to populate both r.URL.Path and r.URL.RawQuery; otherwise it trims the /decisions prefix from the current path. Tests add a scenario with query strings in the forwarded URI and verify RawQuery is correctly populated.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: preserving query strings from the X-Forwarded-Uri header in the decision handler, which is the primary focus of the code modifications.
Description check ✅ Passed The description explains the issue being fixed (query strings dropped in earlier version breaking authenticators), references the related issue (#1264), marks most checklist items, and provides tests. However, the documentation update checklist is unchecked.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@api/decision.go`:
- Around line 74-82: When X-Forwarded-Uri is present (check using xForwardedUri)
the code sets r.URL.Path but currently only assigns r.URL.RawQuery when a "?"
and second part exist, leaving any previous RawQuery intact; update the block in
the handler that inspects fwdUri (the if fwdUri := r.Header.Get(xForwardedUri) {
... } branch) so that when parts := strings.SplitN(fwdUri, "?", 2) yields
len(parts) == 2 you set r.URL.RawQuery = parts[1], and otherwise explicitly
clear r.URL.RawQuery = "" (so the forwarded URI is the sole source of path and
query, and previous queries from the /decisions prefix don’t leak). Ensure the
else branch that trims DecisionPath remains unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dfe6e8d7-68df-459b-8603-ece9e1af1621

📥 Commits

Reviewing files that changed from the base of the PR and between 1a9cb5f and 5246bef.

📒 Files selected for processing (2)
  • api/decision.go
  • api/decision_test.go

Comment thread api/decision.go
Comment on lines +74 to +82
if fwdUri := r.Header.Get(xForwardedUri); fwdUri != "" {
parts := strings.SplitN(fwdUri, "?", 2)
r.URL.Path = parts[0]
if len(parts) == 2 {
r.URL.RawQuery = parts[1]
}
} else {
r.URL.Path = r.URL.Path[len(DecisionPath):]
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Clear RawQuery when X-Forwarded-Uri has no query string.

When X-Forwarded-Uri is present but contains no query string (no ?), the current code doesn't clear r.URL.RawQuery. This means if the original request to /decisions included a query string (e.g., /decisions?foo=bar), it would persist and be incorrectly combined with the forwarded path.

For example: a request to /decisions?original=query with X-Forwarded-Uri: /target/path would result in /target/path?original=query, which is likely unintended.

When X-Forwarded-Uri is set, it should be the sole source of truth for both path and query.

🔧 Proposed fix
 if fwdUri := r.Header.Get(xForwardedUri); fwdUri != "" {
 	parts := strings.SplitN(fwdUri, "?", 2)
 	r.URL.Path = parts[0]
 	if len(parts) == 2 {
 		r.URL.RawQuery = parts[1]
+	} else {
+		r.URL.RawQuery = ""
 	}
 } else {
 	r.URL.Path = r.URL.Path[len(DecisionPath):]
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if fwdUri := r.Header.Get(xForwardedUri); fwdUri != "" {
parts := strings.SplitN(fwdUri, "?", 2)
r.URL.Path = parts[0]
if len(parts) == 2 {
r.URL.RawQuery = parts[1]
}
} else {
r.URL.Path = r.URL.Path[len(DecisionPath):]
}
if fwdUri := r.Header.Get(xForwardedUri); fwdUri != "" {
parts := strings.SplitN(fwdUri, "?", 2)
r.URL.Path = parts[0]
if len(parts) == 2 {
r.URL.RawQuery = parts[1]
} else {
r.URL.RawQuery = ""
}
} else {
r.URL.Path = r.URL.Path[len(DecisionPath):]
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@api/decision.go` around lines 74 - 82, When X-Forwarded-Uri is present (check
using xForwardedUri) the code sets r.URL.Path but currently only assigns
r.URL.RawQuery when a "?" and second part exist, leaving any previous RawQuery
intact; update the block in the handler that inspects fwdUri (the if fwdUri :=
r.Header.Get(xForwardedUri) { ... } branch) so that when parts :=
strings.SplitN(fwdUri, "?", 2) yields len(parts) == 2 you set r.URL.RawQuery =
parts[1], and otherwise explicitly clear r.URL.RawQuery = "" (so the forwarded
URI is the sole source of path and query, and previous queries from the
/decisions prefix don’t leak). Ensure the else branch that trims DecisionPath
remains unchanged.

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.

2 participants