nixpkgs/nixos/tests/nginx-etag.nix
aszlig ccf55bead1
nginx: Clear Last-Modified if ETag is from store
This is what I've suspected a while ago[1]:

> Heads-up everyone: After testing this in a few production instances,
> it seems that some browsers still get cache hits for new store paths
> (and changed contents) for some reason. I highly suspect that it might
> be due to the last-modified header (as mentioned in [2]).
>
> Going to test this with last-modified disabled for a little while and
> if this is the case I think we should improve that patch by disabling
> last-modified if serving from a store path.

Much earlier[2] when I reviewed the patch, I wrote this:

> Other than that, it looks good to me.
>
> However, I'm not sure what we should do with Last-Modified header.
> From RFC 2616, section 13.3.4:
>
> - If both an entity tag and a Last-Modified value have been
>   provided by the origin server, SHOULD use both validators in
>   cache-conditional requests. This allows both HTTP/1.0 and
>   HTTP/1.1 caches to respond appropriately.
>
> I'm a bit nervous about the SHOULD here, as user agents in the wild
> could possibly just use Last-Modified and use the cached content
> instead.

Unfortunately, I didn't pursue this any further back then because
@pbogdan noted[3] the following:

> Hmm, could they (assuming they are conforming):
>
>  * If an entity tag has been provided by the origin server, MUST
>    use that entity tag in any cache-conditional request (using If-
>    Match or If-None-Match).

Since running with this patch in some deployments, I found that both
Firefox and Chrome/Chromium do NOT re-validate against the ETag if the
Last-Modified header is still the same.

So I wrote a small NixOS VM test with Geckodriver to have a test case
which is closer to the real world and I indeed was able to reproduce
this.

Whether this is actually a bug in Chrome or Firefox is an entirely
different issue and even IF it is the fault of the browsers and it is
fixed at some point, we'd still need to handle this for older browser
versions.

Apart from clearing the header, I also recreated the patch by using a
plain "git diff" with a small description on top. This should make it
easier for future authors to work on that patch.

[1]: https://github.com/NixOS/nixpkgs/pull/48337#issuecomment-495072764
[2]: https://github.com/NixOS/nixpkgs/pull/48337#issuecomment-451644084
[3]: https://github.com/NixOS/nixpkgs/pull/48337#issuecomment-451646135

Signed-off-by: aszlig <aszlig@nix.build>
2019-12-30 14:30:36 +01:00

89 lines
2.6 KiB
Nix

import ./make-test-python.nix {
name = "nginx-etag";
nodes = {
server = { pkgs, lib, ... }: {
networking.firewall.enable = false;
services.nginx.enable = true;
services.nginx.virtualHosts.server = {
root = pkgs.runCommandLocal "testdir" {} ''
mkdir "$out"
cat > "$out/test.js" <<EOF
document.getElementById('foobar').setAttribute('foo', 'bar');
EOF
cat > "$out/index.html" <<EOF
<!DOCTYPE html>
<div id="foobar">test</div>
<script src="test.js"></script>
EOF
'';
};
nesting.clone = lib.singleton {
services.nginx.virtualHosts.server = {
root = lib.mkForce (pkgs.runCommandLocal "testdir2" {} ''
mkdir "$out"
cat > "$out/test.js" <<EOF
document.getElementById('foobar').setAttribute('foo', 'yay');
EOF
cat > "$out/index.html" <<EOF
<!DOCTYPE html>
<div id="foobar">test</div>
<script src="test.js"></script>
EOF
'');
};
};
};
client = { pkgs, lib, ... }: {
virtualisation.memorySize = 512;
environment.systemPackages = let
testRunner = pkgs.writers.writePython3Bin "test-runner" {
libraries = [ pkgs.python3Packages.selenium ];
} ''
import os
import time
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('--headless')
driver = Firefox(options=options)
driver.implicitly_wait(20)
driver.get('http://server/')
driver.find_element_by_xpath('//div[@foo="bar"]')
open('/tmp/passed_stage1', 'w')
while not os.path.exists('/tmp/proceed'):
time.sleep(0.5)
driver.get('http://server/')
driver.find_element_by_xpath('//div[@foo="yay"]')
open('/tmp/passed', 'w')
'';
in [ pkgs.firefox-unwrapped pkgs.geckodriver testRunner ];
};
};
testScript = { nodes, ... }: let
inherit (nodes.server.config.system.build) toplevel;
newSystem = "${toplevel}/fine-tune/child-1";
in ''
start_all()
server.wait_for_unit("nginx.service")
client.wait_for_unit("multi-user.target")
client.execute("test-runner &")
client.wait_for_file("/tmp/passed_stage1")
server.succeed(
"${newSystem}/bin/switch-to-configuration test >&2"
)
client.succeed("touch /tmp/proceed")
client.wait_for_file("/tmp/passed")
'';
}