Skip to content

Conversation

@YapWC
Copy link
Contributor

@YapWC YapWC commented Dec 25, 2025

…unsafe is true

When use_real_request_uri_unsafe is set to true, url that was requested with query string, arguments is not parsed. Expected to parse the arguments just like same default behavior when use_real_request_uri_unsafe is set to false.

The real issue lies in the fact that when use_real_request_uri_unsafe is set to true and conf.uri is provided, the core.utils.resolve_var output back an upstream uri that does not include query string. Therefore we need to concatenate the query string for the case if conf.uri is true.

Description

Which issue(s) this PR fixes:

Fixes #12623

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

…unsafe is true

When use_real_request_uri_unsafe is set to true, url that was requested with query string, arguments is not parsed. Expected to parse the arguments just like same default behavior when use_real_request_uri_unsafe is set to false.

The reason this problem exist is because `if ctx.var.is_args == "?" then` statement is inside `if not conf.use_real_request_uri_unsafe then` which then bypasses args parsing.
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Dec 25, 2025
Copy link
Contributor

@Baoyuantop Baoyuantop left a comment

Choose a reason for hiding this comment

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

If the plugin only configures "use_real_request_uri_unsafe": true, it will cause the query to be concatenated repeatedly.

@Baoyuantop Baoyuantop added wait for update wait for the author's response in this issue/PR and removed awaiting review labels Dec 26, 2025
@YapWC
Copy link
Contributor Author

YapWC commented Dec 26, 2025

Hi @Baoyuantop alright I think I understand what you mean. I think it is better for me to debug it line by line in this case. For your side normally how do you run debugging/printing code output?

YapWC and others added 2 commits December 26, 2025 11:46
The real issue lies in the fact that when use_real_request_uri_unsafe is set to true and conf.uri is provided, the core.utils.resolve_var output back an upstream uri that does not include query string. Therefore we need to concatenate the query string for the case if conf.uri is true.

The previous commit does not solve the issue as the issue is not related in the `if not conf.use_real_request_uri_unsafe then` statement.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Dec 27, 2025
@Baoyuantop Baoyuantop added awaiting review and removed wait for update wait for the author's response in this issue/PR labels Jan 4, 2026
local separator_escaped = false

local query_string = ""
-- When use_real_request_uri_unsafe is true, we use real_request_uri directly
Copy link
Contributor

Choose a reason for hiding this comment

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

Please simplify the comments.

@Baoyuantop Baoyuantop requested a review from Copilot January 26, 2026 08:46
@Baoyuantop Baoyuantop added wait for update wait for the author's response in this issue/PR and removed awaiting review labels Jan 26, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes proxy-rewrite behavior where the original request query string is dropped when use_real_request_uri_unsafe=true and conf.uri is set, aligning unsafe mode with the default (safe) behavior.

Changes:

  • Update proxy-rewrite rewrite logic to preserve the query string when use_real_request_uri_unsafe=true and uri is configured.
  • Add regression tests covering query preservation in unsafe mode.
  • Extend/adjust tests around URI normalization behavior when unsafe mode is disabled.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
apisix/plugins/proxy-rewrite.lua Appends the original query string when building upstream_uri from conf.uri in unsafe mode.
t/plugin/proxy-rewrite.t Adds tests to verify query args are preserved when use_real_request_uri_unsafe=true and uri is rewritten.
t/plugin/proxy-rewrite3.t Updates test descriptions and adds coverage for normalization behavior when unsafe mode is not enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

-- uri has higher priority than regex_uri.
if conf.uri ~= nil then
-- Set the upstream_uri by resolving variables in conf.uri
upstream_uri = core.utils.resolve_var(conf.uri, ctx.var, escape_separator) .. query_string
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

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

When use_real_request_uri_unsafe is true and conf.uri already contains a query string (e.g. /path?x=1), concatenating .. query_string (which starts with ?) produces an invalid URI like /path?x=1?y=2. This also diverges from the non-unsafe branch which merges query params using ?/& logic. Please merge the extracted query into conf.uri the same way: if the resolved conf.uri already has ?, append & plus the extracted query without its leading ?; otherwise append it as-is. Add/adjust a test to cover conf.uri containing its own query when use_real_request_uri_unsafe=true.

Suggested change
upstream_uri = core.utils.resolve_var(conf.uri, ctx.var, escape_separator) .. query_string
local resolved_uri = core.utils.resolve_var(conf.uri, ctx.var, escape_separator)
if query_string ~= "" then
-- Merge extracted query string into resolved_uri:
-- if resolved_uri already has a query part, append with '&',
-- otherwise append the extracted query as-is.
if str_find(resolved_uri, "?", 1, true) then
upstream_uri = resolved_uri .. "&" .. sub_str(query_string, 2)
else
upstream_uri = resolved_uri .. query_string
end
else
upstream_uri = resolved_uri
end

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files. wait for update wait for the author's response in this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: proxy-rewrite discard the query string when use_real_request_uri_unsafe is true

2 participants