Support empty hash in fetchers

fetchTarball, fetchTree, and fetchGit all have *optional* hash attrs.
This means that we need to be careful with what we allow to avoid
accidentally making these defaults. When ‘hash = ""’ we assume the
empty hash is wanted.
This commit is contained in:
Matthew Bauer 2020-06-09 11:10:54 -05:00
parent 762273f1fd
commit 19aa892f20
4 changed files with 32 additions and 12 deletions

View file

@ -1131,9 +1131,14 @@ static void prim_path(EvalState & state, const Pos & pos, Value * * args, Value
filterFun = attr.value;
} else if (n == "recursive")
method = FileIngestionMethod { state.forceBool(*attr.value, *attr.pos) };
else if (n == "sha256")
expectedHash = Hash(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
else
else if (n == "sha256") {
auto hashStr = state.forceStringNoCtx(*attr.value, *attr.pos);
if (hashStr == "") {
expectedHash = Hash(htSHA256);
printError("warning: found empty hash, assuming you wanted '%s'", expectedHash.to_string());
} else
expectedHash = Hash(hashStr, htSHA256);
} else
throw EvalError(format("unsupported argument '%1%' to 'addPath', at %2%") % attr.name % *attr.pos);
}
if (path.empty())

View file

@ -102,9 +102,14 @@ static void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
string n(attr.name);
if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (n == "sha256")
expectedHash = Hash(state.forceStringNoCtx(*attr.value, *attr.pos), htSHA256);
else if (n == "name")
else if (n == "sha256") {
auto hashStr = state.forceStringNoCtx(*attr.value, *attr.pos);
if (hashStr == "") {
expectedHash = Hash(htSHA256);
printError("warning: found empty hash, assuming you wanted '%s'", expectedHash->to_string());
} else
expectedHash = Hash(hashStr, htSHA256);
} else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else
throw EvalError("unsupported argument '%s' to '%s', at %s",

View file

@ -34,9 +34,14 @@ std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs)
for (auto & inputScheme : *inputSchemes) {
auto res = inputScheme->inputFromAttrs(attrs2);
if (res) {
if (auto narHash = maybeGetStrAttr(attrs, "narHash"))
if (auto narHash = maybeGetStrAttr(attrs, "narHash")) {
if (narHash->empty()) {
res->narHash = Hash(htUnknown);
printError("warning: found empty hash, assuming you wanted '%s'", res->narHash->to_string());
} else
// FIXME: require SRI hash.
res->narHash = Hash(*narHash);
}
return res;
}
}

View file

@ -263,9 +263,14 @@ struct TarballInputScheme : InputScheme
throw Error("unsupported tarball input attribute '%s'", name);
auto input = std::make_unique<TarballInput>(parseURL(getStrAttr(attrs, "url")));
if (auto hash = maybeGetStrAttr(attrs, "hash"))
if (auto hash = maybeGetStrAttr(attrs, "hash")) {
if (hash->empty()) {
input->hash = Hash(htUnknown);
printError("warning: found empty hash, assuming you wanted '%s'", input->hash->to_string());
} else
// FIXME: require SRI hash.
input->hash = Hash(*hash);
}
return input;
}