Skip to content

Commit

Permalink
builtins.unpackChannel: wrap filesystem errors and sanitize channelName
Browse files Browse the repository at this point in the history
Otherwise these errors are not caught correctly
  • Loading branch information
Mic92 committed Sep 5, 2024
1 parent 05a1ffe commit 70c52d7
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/libstore/builtins/unpack-channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,37 @@ void builtinUnpackChannel(
return i->second;
};

auto out = outputs.at("out");
auto channelName = getAttr("channelName");
std::filesystem::path out(outputs.at("out"));
std::filesystem::path channelName(getAttr("channelName"));
auto src = getAttr("src");

if (channelName.filename() != channelName) {
throw Error("channelName is not allowed to contain filesystem seperators, got %1%", channelName);
}

createDirs(out);

unpackTarfile(src, out);

auto entries = std::filesystem::directory_iterator{out};
auto fileName = entries->path().string();
auto fileCount = std::distance(std::filesystem::begin(entries), std::filesystem::end(entries));
size_t fileCount;
std::string fileName;
try {
auto entries = std::filesystem::directory_iterator{out};
fileName = entries->path().string();
fileCount = std::distance(std::filesystem::begin(entries), std::filesystem::end(entries));
} catch (std::filesystem::filesystem_error &e) {
throw SysError("failed to read directory %1%", out);
}


if (fileCount != 1)
throw Error("channel tarball '%s' contains more than one file", src);
std::filesystem::rename(fileName, (out + "/" + channelName));
std::filesystem::path target(out / channelName);
try {
std::filesystem::rename(fileName, target);
} catch (std::filesystem::filesystem_error &e) {
throw SysError("failed to rename %1% to %2%", fileName, target);
}
}

}

0 comments on commit 70c52d7

Please sign in to comment.