about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/store-api.cc
diff options
context:
space:
mode:
authorAndreas Rammhold <andreas@rammhold.de>2020-06-16T10·34+0200
committertazjin <mail@tazj.in>2020-07-10T19·41+0000
commit1937bc86244c0d420f7b40eeb38f14827e92d162 (patch)
tree4554181e3b27d25e1ef6a83ed2692ffb720b5aeb /third_party/nix/src/libstore/store-api.cc
parentd06237707b604da79957282b240baff40bae3ad9 (diff)
fix(3p/nix/libstore): use exception instead of assertion when storepath is malformed r/1249
Previously the nix-daemon would crash if a user fed it invalid store
paths for drv files. The crash was due to the changed assertion
triggering. Whenever that assertion would hit the nix-daemon process
along with all it's current childs (running builds from all users) would
be interrupted.

Before this patch:

 $ nix-store --realise /nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.drv
 don't know how to build these paths:
   /nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.drv
 error: unexpected end-of-file

 < nix-daemon terminates >

With this patch:

 $ nix-store --realise /nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.drv
 don't know how to build these paths:
   /nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.drv
 error: path '/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.drv' is not a valid store path

 < nix-daemon does *NOT* terminate >

Change-Id: I01c5048c8a43a8b9154bdeb781d05b7744869ec0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/981
Tested-by: BuildkiteCI
Reviewed-by: glittershark <grfn@gws.fyi>
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'third_party/nix/src/libstore/store-api.cc')
-rw-r--r--third_party/nix/src/libstore/store-api.cc13
1 files changed, 11 insertions, 2 deletions
diff --git a/third_party/nix/src/libstore/store-api.cc b/third_party/nix/src/libstore/store-api.cc
index 081cdea9873d..f28b13c83d68 100644
--- a/third_party/nix/src/libstore/store-api.cc
+++ b/third_party/nix/src/libstore/store-api.cc
@@ -66,8 +66,17 @@ Path Store::followLinksToStorePath(const Path& path) const {
 
 std::string storePathToName(const Path& path) {
   auto base = baseNameOf(path);
-  assert(base.size() == storePathHashLen ||
-         (base.size() > storePathHashLen && base[storePathHashLen] == '-'));
+
+  // The base name of the store path must be `storePathHashLen` characters long,
+  // if it is not `storePathHashLen` long then the next character, following
+  // the hash part, MUST be a dash (`-`).
+  const bool hasLengthMismatch = base.size() != storePathHashLen;
+  const bool hasInvalidSuffix =
+      base.size() > storePathHashLen && base[storePathHashLen] != '-';
+  if (hasLengthMismatch && hasInvalidSuffix) {
+    throw Error(format("path '%1%' is not a valid store path") % path);
+  }
+
   return base.size() == storePathHashLen
              ? ""
              : std::string(base, storePathHashLen + 1);