#984 closed defect (invalid)

http auth: Incorrect actions order

Reported by: e-kinst.mail.ru@… Owned by:
Priority: minor Milestone:
Component: nginx-module Version: 1.6.x
Keywords: Cc:
Sensitive: no
uname -a: Linux infoprintme.ru.myihor.ru 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u3 (2016-01-17) x86_64 GNU/Linux
nginx -V: configure arguments: --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-z,relro --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/nginx.pid --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-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/build/nginx-T5fW9e/nginx-1.6.2/debian/modules/nginx-auth-pam --add-module=/build/nginx-T5fW9e/nginx-1.6.2/debian/modules/nginx-dav-ext-module --add-module=/build/nginx-T5fW9e/nginx-1.6.2/debian/modules/nginx-echo --add-module=/build/nginx-T5fW9e/nginx-1.6.2/debian/modules/nginx-upstream-fair --add-module=/build/nginx-T5fW9e/nginx-1.6.2/debian/modules/ngx_http_substitutions_filter_module



I have NGINX + Apache web server and try to restrict access to CGI scripts by HTTP Basic autentication at NGINX side. At the same time I want to check that script file exists before proxies request to Apache.

My NGINX site config is:

server {
	listen 80;
	listen [::]:80;

	root /ххх/ххххх/www/htdocs;
	access_log /ххх/ххххх/log/nginx_access.log;
	error_log /xxx/ххххх/log/nginx_error.log error;
	index index.html index.htm;
	server_name ххххххххххххххххх;

	location / {
			try_files $uri $uri/ =404;
	# Deny access to any files or folders which name started with '.'
	location ~ /\. {
		deny all;
	# Static content
	location ~* ^.+\.(html?|jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|wav|bmp|rtf|js)$ {
		try_files $uri =404;
		expires 24h;
	# CGI scripts
	# general CGI processing
	location ~* /.+\.(cgi|pl)$ {
		root /xxx/ххххх/www;
		expires -48h;
		include proxy_params;

		# authorized staff area
		location ~ ^/cgi-bin/ {
			auth_basic "Staff Area";
			auth_basic_user_file /xxx/ххххх/www/.htpasswdCal;
			include snippets/cgi-ххххх;
			# authorized manager area
			location ~ ^/cgi-bin/man/ {
				auth_basic "Managers Area";
  				auth_basic_user_file /xxx/ххххх/www/.htpasswdCalMan;
				include snippets/cgi-ххххх;
		return 415;
	# deny access to all file types non-specified explicitly above
	location ~ \.[^/]*$ {
		deny all;

/etc/nginx/snippets/cgi-ххххх is

#### CGI processing (action part)
try_files $uri =416;
if ( !-x $request_filename ) {
	return 417;


Everything is fine when I access URLs existed - I'm asked login/password corresponding to area I try to access. But when I specify script with non-existant URI in restricted area I get custom error 417 rather than login window. Clearly server actions order is incorrect - I expected autorization first and check files second.

So unauthorized person gets info about the files in restricted area - luckily about the absence of a file but not about existance. I'm not sure about the severity of this bug but I want you to be sure that there are no really serious problems around it.

Best Regards,

Change History (1)

comment:1 Changed 17 months ago by mdounin

  • Resolution set to invalid
  • Status changed from new to closed

The rewrite module directives, including if, are executed while selecting a configuration to work with, and this is expected to happen before authorization. There is no bug here. If you want to do checks after authorization, use try_files instead.

Note: See TracTickets for help on using tickets.