Skip to content

Commit c341e96

Browse files
committed
HttpBinaryCacheStore: Don't ignore 401/407 errors
The only reason it treats 403 errors as 404s is that S3 returns 403 for files that don't exist if the bucket is unlistable. But we don't want to ignore (and definitely shouldn't cache) 401/407 errors as "file not found". This fixes "token expired" errors from cache.flakehub.com being silently ignored and cached. Now you get: # nix build --dry-run /nix/store/qnfhg5anpfpr4il3jlp9bnkf6vhyzbnj-determinate-nix-3.20.0 error: unable to download 'https://cache.flakehub.com/qnfhg5anpfpr4il3jlp9bnkf6vhyzbnj.narinfo': HTTP error 401 response body: {"code":401,"error":"Unauthorized","message":"Unauthorized.","request_id":"019e3a82-2474-7f80-8564-6e1bc2234654"} don't know how to build these paths: /nix/store/qnfhg5anpfpr4il3jlp9bnkf6vhyzbnj-determinate-nix-3.20.0 i.e. it's a fatal error now unless you use `--fallback`.
1 parent c9453f7 commit c341e96

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/libstore/filetransfer.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,12 @@ struct curlFileTransfer : public FileTransfer
685685
if (httpStatus == 404 || httpStatus == 410 || code == CURLE_FILE_COULDNT_READ_FILE) {
686686
// The file is definitely not there
687687
err = NotFound;
688-
} else if (httpStatus == 401 || httpStatus == 403 || httpStatus == 407) {
689-
// Don't retry on authentication/authorization failures
688+
} else if (httpStatus == 401 || httpStatus == 407) {
689+
err = Unauthorized;
690+
} else if (httpStatus == 403) {
691+
// Don't retry on authentication/authorization failures.
692+
// Note: the only reason we treat this differently from 401/407 is S3 returns 403 if a file doesn't
693+
// exist and the bucket is unlistable.
690694
err = Forbidden;
691695
} else if (httpStatus == 429) {
692696
// 429 means too many requests, so we retry (with a substantially longer delay)

src/libstore/include/nix/store/filetransfer.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public:
385385
void
386386
download(FileTransferRequest && request, Sink & sink, std::function<void(FileTransferResult)> resultCallback = {});
387387

388-
enum Error { NotFound, Forbidden, Misc, Transient, Interrupted };
388+
enum Error { NotFound, Unauthorized, Forbidden, Misc, Transient, Interrupted };
389389
};
390390

391391
/**

0 commit comments

Comments
 (0)