Skip to content

Commit 211c806

Browse files
authored
Merge pull request #11587 from NixOS/mergify/bp/2.19-maintenance/pr-11585
builtin:fetchurl: Enable TLS verification (backport #11585)
2 parents 0446f7b + aca7a73 commit 211c806

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

doc/manual/rl-next/verify-tls.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
synopsis: "`<nix/fetchurl.nix>` uses TLS verification"
3+
prs: [11585]
4+
---
5+
6+
Previously `<nix/fetchurl.nix>` did not do TLS verification. This was because the Nix sandbox in the past did not have access to TLS certificates, and Nix checks the hash of the fetched file anyway. However, this can expose authentication data from `netrc` and URLs to man-in-the-middle attackers. In addition, Nix now in some cases (such as when using impure derivations) does *not* check the hash. Therefore we have now enabled TLS verification. This means that downloads by `<nix/fetchurl.nix>` will now fail if you're fetching from a HTTPS server that does not have a valid certificate.
7+
8+
`<nix/fetchurl.nix>` is also known as the builtin derivation builder `builtin:fetchurl`. It's not to be confused with the evaluation-time function `builtins.fetchurl`, which was not affected by this issue.

src/libstore/builtins/fetchurl.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
3434

3535
auto source = sinkToSource([&](Sink & sink) {
3636

37-
/* No need to do TLS verification, because we check the hash of
38-
the result anyway. */
3937
FileTransferRequest request(url);
40-
request.verifyTLS = false;
4138
request.decompress = false;
4239

4340
auto decompressor = makeDecompressionSink(

tests/nixos/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ in
4444
ca-fd-leak = runNixOSTestFor "x86_64-linux" ./ca-fd-leak;
4545

4646
user-sandboxing = runNixOSTestFor "x86_64-linux" ./user-sandboxing;
47+
48+
fetchurl = runNixOSTestFor "x86_64-linux" ./fetchurl.nix;
4749
}

tests/nixos/fetchurl.nix

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)