ngx.req.set_uri_args behavior with escape sequences

187 views
Skip to first unread message

Arunabha Saha

unread,
Sep 20, 2021, 3:29:07 PM9/20/21
to openre...@googlegroups.com
I'm using openresty to rewrite the query params in a URI. The way I
do it is of course to extra the query params, rewrite them and then
use ngx.req.set_uri_args() to set the query params. What I notice is
that it unescapes the URI also.

For the website I was having issues with, the URL is of the form
https://host/uri?query=x

Here uri is actually json that is run through a url encoder to escape
it. When I try to rewrite the query=x part using
ngx.req.set_uri_args(), I'm running into an issue where uri is now
transformed back into its original json and breaks the request.

I have worked around it by copying $request_uri to $orig_uri and
encoding the proxy_pass as something like proxy_pass
https://host$orig_uri$rewrite_args (i set $rewrite_args instead of
using ngx.req.set_uri_args). However, was wondering if there is
something I am missing here that would avoid the need for this
workaround.

--
regards,
Arun

DeJiang Zhu

unread,
Sep 26, 2021, 8:41:38 PM9/26/21
to openre...@googlegroups.com
Hi Arun,

I can reproduce it on my side. Maybe it's by design in Nginx. Anyway, I will take a deep look at it soon.

result:
```
# original server
$ curl 'http://127.0.0.1:8098/%7B%22f%20o%20%23%5C%2F%22%3A%22%20bar%20%40%3F%20%22%7D?query=x'
uri: /{"f o #\/":" bar @? "}
request_uri: /%7B%22f%20o%20%23%5C%2F%22%3A%22%20bar%20%40%3F%20%22%7D?query=x

# server with rewrite
$ curl 'http://127.0.0.1:8099/%7B%22f%20o%20%23%5C%2F%22%3A%22%20bar%20%40%3F%20%22%7D?query=x'
uri: /{"f o #\/":" bar @? "}
request_uri: /{"f%20o%20%23\/":"%20bar%20@%3F%20"}?query=rewrite
```

with the following configuration:
```
    server {
        listen       8098;

        location / {
            content_by_lua_block {
                ngx.say("uri: ", ngx.var.uri)
                ngx.say("request_uri: ", ngx.var.request_uri)
            }
        }
    }

    server {
        listen       8099;

        location / {
            rewrite_by_lua_block {
                ngx.req.set_uri_args("query=rewrite")
            }
            proxy_pass http://local;
        }

        location /hello {
            content_by_lua_block {
                local body = "hello"
                ngx.header['Content-Length'] = #body + 1
                ngx.say(body)
            }
        }
    }
````

--
regards,
doujiang24

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/CABCok%3DLpg4JPNHshGNfcFsPKW6mbFTnxurChVopbFk4U_Zyi6g%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages