summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorReepca Russelstein <reepca@russelstein.xyz>2026-06-15 20:37:53 -0500
committerLudovic Courtès <ludo@gnu.org>2026-07-02 19:42:47 +0200
commit26d7eb8a4adafc648ef035e91b6bbc4945d4c962 (patch)
tree18461d366e7f1d9a6882817bac4a191e240fa318 /tests
parent3e5c3217f334805531e5db1680d97490999253f7 (diff)
scripts: substitute: restored nars only appear after verification.
The hash of a nar is only known once 'download-nar' completes. By that point the nar may already have been partially-restored to its destination for some time. Additionally, after a hash mismatch is detected, 'guix substitute' leaves the invalid contents at the specified destination. This means that untrusted, attacker-controlled contents are present at what at some point may have been or at some point may become a valid store path. It is entirely possible that a user or program won't check that a given store item is valid before trying to use it (it may be that 'guix gc' was run while an obscured reference was held). Let's protect them in that case by ensuring that unverified contents are kept elsewhere, in a temporary directory. While implementing this one such program was discovered: tests/substitute.scm. It actually incorrectly computes the hashes of the nars it supplies to (guix scripts substitute), computing the hash of the plain contents instead of the hash of the nar. These tests nevertheless passed because they didn't check what (guix scripts substitute) gave as output, instead only checking that the expected contents were at the specified destination. * guix/scripts/substitute.scm (call-with-temporary-directory-in): new procedure. (guix-substitute): use it to create a temporary directory next to the final destination to restore the nar inside of. Once it is fully restored, if the hash matches, move it to the final destination, deleting the file currently there if it already exists. * tests/substitute.scm (plain-file-nar-sha256): new procedure. (%narinfo, "substitute, narinfo with several URLS" test): use it. Change-Id: Ifeb9b4c912f9d9b2e7477d237da04e7d5175ff83 Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/substitute.scm20
1 files changed, 18 insertions, 2 deletions
diff --git a/tests/substitute.scm b/tests/substitute.scm
index 10b74db084e..086bd588358 100644
--- a/tests/substitute.scm
+++ b/tests/substitute.scm
@@ -39,6 +39,7 @@
39 #:use-module (rnrs io ports) 39 #:use-module (rnrs io ports)
40 #:use-module (web uri) 40 #:use-module (web uri)
41 #:use-module (ice-9 regex) 41 #:use-module (ice-9 regex)
42 #:use-module (ice-9 binary-ports)
42 #:use-module (srfi srfi-11) 43 #:use-module (srfi srfi-11)
43 #:use-module (srfi srfi-26) 44 #:use-module (srfi srfi-26)
44 #:use-module (srfi srfi-34) 45 #:use-module (srfi srfi-34)
@@ -142,6 +143,21 @@ version identifier.."
142 ;; <https://www.rfc-editor.org/rfc/rfc5737>. 143 ;; <https://www.rfc-editor.org/rfc/rfc5737>.
143 "http://203.0.113.1") 144 "http://203.0.113.1")
144 145
146(define (plain-file-nar-sha256 string)
147 (sha256 (call-with-output-bytevector
148 (lambda (port)
149 (let ((bv (string->utf8 string)))
150 (call-with-input-bytevector
151 bv
152 (lambda (contents)
153 (write-file-tree #t port
154 #:file-type+size
155 (lambda (_)
156 (values 'regular
157 (bytevector-length
158 bv)))
159 #:file-port
160 (const contents)))))))))
145 161
146(define %narinfo 162(define %narinfo
147 ;; Skeleton of the narinfo used below. 163 ;; Skeleton of the narinfo used below.
@@ -150,7 +166,7 @@ version identifier.."
150URL: example.nar 166URL: example.nar
151Compression: none 167Compression: none
152NarHash: sha256:" (bytevector->nix-base32-string 168NarHash: sha256:" (bytevector->nix-base32-string
153 (sha256 (string->utf8 "Substitutable data."))) " 169 (plain-file-nar-sha256 "Substitutable data.")) "
154NarSize: 42 170NarSize: 42
155References: bar baz 171References: bar baz
156Deriver: " (%store-prefix) "/foo.drv 172Deriver: " (%store-prefix) "/foo.drv
@@ -819,7 +835,7 @@ Compression: lzip
819URL: example.nar 835URL: example.nar
820Compression: none 836Compression: none
821NarHash: sha256:" (bytevector->nix-base32-string 837NarHash: sha256:" (bytevector->nix-base32-string
822 (sha256 (string->utf8 "Substitutable data."))) " 838 (plain-file-nar-sha256 "Substitutable data.")) "
823NarSize: 42 839NarSize: 42
824References: bar baz 840References: bar baz
825Deriver: " (%store-prefix) "/foo.drv 841Deriver: " (%store-prefix) "/foo.drv