Opened 4 weeks ago

Last modified 3 weeks ago

#2016 accepted defect

nginx does not percent-encode Location header properly

Reported by: yurivkhan@… Owned by:
Priority: minor Milestone:
Component: nginx-module Version: 1.18.x
Keywords: Cc: yurivkhan@…
uname -a: Linux partaker 5.4.0-40-generic #44-Ubuntu SMP Tue Jun 23 00:01:04 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-5J5hor/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/ --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module


When nginx is serving static files and the client requests a path that maps to a name of a directory, nginx will respond with a 301 status code and a Location header, redirecting to the same path with a trailing slash.

This Location header is not properly percent-encoded. Example 1:

$ mkdir "/var/www/html/test dir"
$ curl -v "localhost/test dir" 2>&1 | grep Location
< Location: http://localhost/test dir/

Example 2:

$ mkdir "/var/www/html/баг"
$ curl -v "localhost/%D0%B1%D0%B0%D0%B3" 2>&1 | grep Location
< Location: http://localhost/баг/

The Location header is defined in RFC 7231 section 7.1.2 as having a value of URI-reference, which, in turn, is defined in RFC 3986 section 4.1. The BNF grammar there allows a subset of ASCII characters to appear raw; all other characters must be percent-encoding.

By sheer luck, most such percent-unencoded redirects will appear to work. However, example 3:

$ mkdir "/var/www/html/foo%25"
$ echo 123 >"/var/www/html/foo%25/index.html"
$ mkdir "/var/www/html/foo%2525"
$ echo 456 >"/var/www/html/foo%2525/index.html"

$ curl -v "localhost/foo%25" 2>&1 | grep "< HTTP"
< HTTP/1.1 404 Not Found
# expected; foo%25 decodes to "foo%" and that does not exist

$ curl "localhost/foo%2525/"
# expected; foo%2525 decodes to "foo%25"

$ curl "localhost/foo%252525/"
# expected; foo%252525 decodes to "foo%2525"

$ curl -Lv "localhost/foo%2525" 2>&1 | grep "> GET\|< HTTP\|Location"
> GET /foo%2525 HTTP/1.1
< HTTP/1.1 301 Moved Permanently
< Location: http://localhost/foo%25/
> GET /foo%25/ HTTP/1.1
< HTTP/1.1 404 Not Found
# Unexpected! Note that curl is diligently repeating the path given.

$ curl "localhost/foo%252525"
# Unexpected! By now you see what’s happening behind the scenes.

Change History (1)

comment:1 by Maxim Dounin, 3 weeks ago

Status: newaccepted

Yes, known issue, thanks for the report.

Note: See TracTickets for help on using tickets.