|
| 1 | +# Test whether builtin:fetchurl properly performs TLS certificate |
| 2 | +# checks on HTTPS servers. |
| 3 | + |
| 4 | +{ lib, config, pkgs, ... }: |
| 5 | + |
| 6 | +let |
| 7 | + |
| 8 | + makeTlsCert = name: pkgs.runCommand name { |
| 9 | + nativeBuildInputs = with pkgs; [ openssl ]; |
| 10 | + } '' |
| 11 | + mkdir -p $out |
| 12 | + openssl req -x509 \ |
| 13 | + -subj '/CN=${name}/' -days 49710 \ |
| 14 | + -addext 'subjectAltName = DNS:${name}' \ |
| 15 | + -keyout "$out/key.pem" -newkey ed25519 \ |
| 16 | + -out "$out/cert.pem" -noenc |
| 17 | + ''; |
| 18 | + |
| 19 | + goodCert = makeTlsCert "good"; |
| 20 | + badCert = makeTlsCert "bad"; |
| 21 | + |
| 22 | +in |
| 23 | + |
| 24 | +{ |
| 25 | + name = "nss-preload"; |
| 26 | + |
| 27 | + nodes = { |
| 28 | + machine = { lib, pkgs, ... }: { |
| 29 | + services.nginx = { |
| 30 | + enable = true; |
| 31 | + |
| 32 | + virtualHosts."good" = { |
| 33 | + addSSL = true; |
| 34 | + sslCertificate = "${goodCert}/cert.pem"; |
| 35 | + sslCertificateKey = "${goodCert}/key.pem"; |
| 36 | + root = pkgs.runCommand "nginx-root" {} '' |
| 37 | + mkdir "$out" |
| 38 | + echo 'hello world' > "$out/index.html" |
| 39 | + ''; |
| 40 | + }; |
| 41 | + |
| 42 | + virtualHosts."bad" = { |
| 43 | + addSSL = true; |
| 44 | + sslCertificate = "${badCert}/cert.pem"; |
| 45 | + sslCertificateKey = "${badCert}/key.pem"; |
| 46 | + root = pkgs.runCommand "nginx-root" {} '' |
| 47 | + mkdir "$out" |
| 48 | + echo 'foobar' > "$out/index.html" |
| 49 | + ''; |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + security.pki.certificateFiles = [ "${goodCert}/cert.pem" ]; |
| 54 | + |
| 55 | + networking.hosts."127.0.0.1" = [ "good" "bad" ]; |
| 56 | + |
| 57 | + virtualisation.writableStore = true; |
| 58 | + |
| 59 | + nix.settings.experimental-features = "nix-command"; |
| 60 | + }; |
| 61 | + }; |
| 62 | + |
| 63 | + testScript = { nodes, ... }: '' |
| 64 | + machine.wait_for_unit("nginx") |
| 65 | + machine.wait_for_open_port(443) |
| 66 | +
|
| 67 | + out = machine.succeed("curl https://good/index.html") |
| 68 | + assert out == "hello world\n" |
| 69 | +
|
| 70 | + # Fetching from a server with a trusted cert should work. |
| 71 | + machine.succeed("nix build --no-substitute --expr 'import <nix/fetchurl.nix> { url = \"https://good/index.html\"; hash = \"sha256-qUiQTy8PR5uPgZdpSzAYSw0u0cHNKh7A+4XSmaGSpEc=\"; }'") |
| 72 | +
|
| 73 | + # Fetching from a server with an untrusted cert should fail. |
| 74 | + err = machine.fail("nix build --no-substitute --expr 'import <nix/fetchurl.nix> { url = \"https://bad/index.html\"; hash = \"sha256-rsBwZF/lPuOzdjBZN2E08FjMM3JHyXit0Xi2zN+wAZ8=\"; }' 2>&1") |
| 75 | + print(err) |
| 76 | + assert "SSL certificate problem: self-signed certificate" in err |
| 77 | + ''; |
| 78 | +} |
0 commit comments