nixpkgs/pkgs/servers/http
aszlig e1d63ada02
nginx: Fix ETag patch to ignore realpath(3) error
While our ETag patch works pretty fine if it comes to serving data off
store paths, it unfortunately broke something that might be a bit more
common, namely when using regexes to extract path components of
location directives for example.

Recently, @devhell has reported a bug with a nginx location directive
like this:

  location ~^/\~([a-z0-9_]+)(/.*)?$" {
    alias /home/$1/public_html$2;
  }

While this might look harmless at first glance, it does however cause
issues with our ETag patch. The alias directive gets broken up by nginx
like this:

  *2 http script copy: "/home/"
  *2 http script capture: "foo"
  *2 http script copy: "/public_html/"
  *2 http script capture: "bar.txt"

In our patch however, we use realpath(3) to get the canonicalised path
from ngx_http_core_loc_conf_s.root, which returns the *configured* value
from the root or alias directive. So in the example above, realpath(3)
boils down to the following syscalls:

  lstat("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
  lstat("/home/$1", 0x7ffd08da6f60) = -1 ENOENT (No such file or directory)

During my review[1] of the initial patch, I didn't actually notice that
what we're doing here is returning NGX_ERROR if the realpath(3) call
fails, which in turn causes an HTTP 500 error.

Since our patch actually made the canonicalisation (and thus additional
syscalls) necessary, we really shouldn't introduce an additional error
so let's - at least for now - silently skip return value if realpath(3)
has failed.

However since we're using the unaltered root from the config we have
another issue, consider this root:

  /nix/store/...-abcde/$1

Calling realpath(3) on this path will fail (except if there's a file
called "$1" of course), so even this fix is not enough because it
results in the ETag not being set to the store path hash.

While this is very ugly and we should fix this very soon, it's not as
serious as getting HTTP 500 errors for serving static files.

I added a small NixOS VM test, which uses the example above as a
regression test.

It seems that my memory is failing these days, since apparently I *knew*
about this issue since digging for existing issues in nixpkgs, I found
this similar pull request which I even reviewed:

https://github.com/NixOS/nixpkgs/pull/66532

However, since the comments weren't addressed and the author hasn't
responded to the pull request, I decided to keep this very commit and do
a follow-up pull request.

[1]: https://github.com/NixOS/nixpkgs/pull/48337

Signed-off-by: aszlig <aszlig@nix.build>
Reported-by: @devhell
Acked-by: @7c6f434c
Acked-by: @yorickvP
Merges: https://github.com/NixOS/nixpkgs/pull/80671
Fixes: https://github.com/NixOS/nixpkgs/pull/66532
2020-03-28 02:57:21 +01:00
..
apache-httpd nixos/httpd: symlink apache configuration to /etc/httpd/httpd.conf for use in the apachectl command 2020-01-08 10:37:46 -05:00
apache-modules tomcat_connectors: 1.2.46 -> 1.2.48 2020-03-14 14:30:51 +01:00
apt-cacher-ng treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
couchdb Merge branch 'master' into staging-next 2019-08-24 08:55:37 +02:00
darkhttpd treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
gatling treewide: name -> pname 2019-08-17 10:54:38 +00:00
h2o h2o: build with openssl 2019-12-21 22:55:58 +00:00
hiawatha maintainer-list.nix: remove ndowens 2019-12-26 16:47:41 -05:00
hyp
jboss
jetty jetty: 9.4.24.v20191120 -> 9.4.25.v20191220 2020-01-21 16:06:08 -08:00
lighttpd lighttpd: 1.4.54 -> 1.4.55 2020-02-08 18:13:10 +01:00
lwan lwan: 0.2 -> 0.3, fix build w/glibc-2.30 2020-02-01 17:30:18 +01:00
micro-httpd treewide: remove redundant rec 2019-08-28 11:07:32 +00:00
mini-httpd
myserver treewide: name -> pname 2019-08-17 10:54:38 +00:00
nginx nginx: Fix ETag patch to ignore realpath(3) error 2020-03-28 02:57:21 +01:00
nix-binary-cache treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
openresty openresty: 1.15.8.2 -> 1.15.8.3 2020-03-24 11:37:44 -05:00
pshs treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
redstore
showoff bundlerUpdateScript: init and use (#64822) 2019-07-22 12:02:47 +00:00
spawn-fcgi treewide: fix redirected urls 2020-01-22 11:26:22 +01:00
tengine treewide: configureFlags is a flat list 2019-12-31 01:37:49 +01:00
thttpd treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
tomcat tomcat9: 9.0.21 -> 9.0.31 2020-03-11 07:51:04 -04:00
unit unit: 1.15.0 -> 1.16.0 2020-03-12 19:53:13 +00:00
webfs treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
webhook treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00
yaws treewide: name -> pname (easy cases) (#66585) 2019-08-15 13:41:18 +01:00