Archived
scripts: hfget refuses names curl cannot fetch faithfully
A listing entry named "weights.bin#x" downloaded cleanly, checked clean, and was the wrong object. curl ends a URL path at '#' and '?': it requested "weights.bin" while hfget recorded the entry as "weights.bin#x". An attacker who lists the truncated object's real size and oid under a decorated name passes the size check, the oid check and the manifest -- the bytes arrive under a name nobody asked for, and the directory reads complete. hfget's name screen checked traversal, control characters and its own manifest, but not curl's path terminators. The jq screen now refuses any entry whose name carries # or ?. The child that writes each file re-screens its name too, and its old case missed a name ending in "a/.." (the parent's regex caught those, the child's did not) as well as the terminators -- both arms added, the `?` quoted so it stays literal and not the single-character wildcard. Two hfget tests: a listing carrying "model.bin#x" is refused whole, and a child handed model.bin#x, model.bin?y or sub/.. refuses before any request or write.
This commit is contained in:
1 parent
965a7caa51
commit
444b85158b
2 files changed
+54
-5
No files matched your search
+11
-5
@@ -44,9 +44,11 @@ set -e
|
||||
if [ -n "$HFGET_CHILD" ]; then
|
||||
size=${1%% *}; rest=${1#* }; oid=${rest%% *}; path=${rest#* }
|
||||
# The parent has vetted every name; this holds anyway, because $out is written straight
|
||||
# from it and an escape writes outside the directory the operator named.
|
||||
# from it and an escape writes outside the directory the operator named. The `*/..` arm
|
||||
# closes the trailing-segment gap (../.. is the prefix arm, a/../b the interior one), and
|
||||
# the quoted `?` is literal, where a bare one would be the single-character wildcard.
|
||||
case "$path" in
|
||||
""|/*|*/|.|..|../*|*/../*)
|
||||
""|/*|*/|.|..|../*|*/../*|*/..|*'#'*|*'?'*)
|
||||
echo "hfget: refusing a file named '$path', which does not land inside the destination" >&2
|
||||
exit 1;;
|
||||
esac
|
||||
@@ -137,12 +139,16 @@ done
|
||||
echo "hfget: $page page(s) of listing"
|
||||
# A file name that does not land inside the destination is not a listing oddity: an absolute
|
||||
# path, a `..` segment, a control character (a tab splices one entry into two fields of the
|
||||
# transfer list), or a name shadowing hfget's own manifest, which the next run would read back
|
||||
# as its record of verified content. Name the entry and stop; skipping silently would leave a
|
||||
# directory that reads as complete and is not.
|
||||
# transfer list), a `#` or `?` (curl ends the path there and requests the fragment- or query-
|
||||
# stripped object, which for a listing chosen to match is a real object with the listed size
|
||||
# and oid -- the bytes arrive, the name never asked for lands, and no check can tell), or a
|
||||
# name shadowing hfget's own manifest, which the next run would read back as its record of
|
||||
# verified content. Name the entry and stop; skipping silently would leave a directory that
|
||||
# reads as complete and is not.
|
||||
bad=$(printf '%s' "$listing" | jq -r '[.[] | select(.type == "file")
|
||||
| select((.path | test("(^/|(^|/)\\.\\.(/|$))"))
|
||||
or (.path | test("[[:cntrl:]]"))
|
||||
or (.path | test("[#?]"))
|
||||
or (.path | test("(^|/)[.]hfget-manifest")))] | map(.path) | join(", ")')
|
||||
[ -n "$bad" ] && { echo "hfget: the listing names '$bad', which does not land inside the destination" >&2; exit 1; }
|
||||
have_st=$(printf '%s' "$listing" | jq '[.[] | select(.path | endswith(".safetensors"))] | length')
|
||||
|
||||
@@ -312,6 +312,49 @@ TEST(a_listing_naming_a_path_outside_the_destination_is_refused) {
|
||||
CHECK(has(err, "does not land inside the destination"));
|
||||
}
|
||||
|
||||
/* `#` and `?` end the URL's path: curl requests the stripped object instead. A listing that
|
||||
* spells out a truncated name with the truncated object's size and oid passes every check
|
||||
* afterwards -- the bytes arrive under a name nobody asked for, and the manifest certifies the
|
||||
* directory complete. Refused as an escaping name is. */
|
||||
const char* kFragmentListing =
|
||||
"#!/bin/sh\n"
|
||||
"for a in \"$@\"; do case \"$a\" in *api/models*)\n"
|
||||
" printf '[{\"type\":\"file\",\"path\":\"model.bin#x\",\"size\":1},"
|
||||
"{\"type\":\"file\",\"path\":\"kept.bin\",\"size\":1}]'; exit 0;; esac; done\n"
|
||||
"exit 22\n";
|
||||
|
||||
TEST(a_listing_name_that_curl_would_truncate_is_refused_whole) {
|
||||
NEED_ARTIFACT(hfget_beside_this_binary());
|
||||
if (!on_path("jq")) SKIP("no jq on PATH, which hfget.sh needs");
|
||||
TempDir root;
|
||||
REQUIRE(!root.path.empty());
|
||||
FakeCurl curl(kFragmentListing);
|
||||
const std::string cmd = "sh '" + hfget_beside_this_binary() +
|
||||
"' radiance-test-owner/radiance-test-model '" + root.path +
|
||||
"' >/dev/null 2>&1";
|
||||
CHECK(std::system(cmd.c_str()) != 0);
|
||||
CHECK(!fs::exists(root.path + "/model.bin#x"));
|
||||
CHECK(!fs::exists(root.path + "/model.bin")); /* curl would have fetched this one */
|
||||
CHECK(!fs::exists(root.path + "/kept.bin")); /* the whole listing is refused */
|
||||
CHECK(!fs::exists(root.path + "/.hfget-manifest"));
|
||||
}
|
||||
|
||||
/* The per-file child re-screens its name without waiting for the parent: a trailing `..` (the
|
||||
* parent's regex caught it, the child's old case did not) and curl's own path terminators must
|
||||
* fail there too, before any request or any write. */
|
||||
TEST(the_child_screen_refuses_trailing_dotdot_and_curl_terminator_names) {
|
||||
NEED_ARTIFACT(hfget_beside_this_binary());
|
||||
TempDir root;
|
||||
REQUIRE(!root.path.empty());
|
||||
for (const char* name : {"model.bin#x", "model.bin?y", "sub/.."}) {
|
||||
const std::string cmd =
|
||||
"HFGET_CHILD=1 HFGET_DEST='" + root.path + "' sh '" + hfget_beside_this_binary() +
|
||||
"' \"$(printf '5\\t\\t" + std::string(name) + "')\" >/dev/null 2>&1";
|
||||
CHECK(std::system(cmd.c_str()) != 0);
|
||||
}
|
||||
CHECK(fs::is_empty(root.path));
|
||||
}
|
||||
|
||||
/* An LFS file's oid is the hub's claim about its bytes; the download is kept only once those
|
||||
* bytes hash to it -- the size check alone would take a swapped file of the same length, and a
|
||||
* spliced resume at any length the hub likes. A mismatch deletes the file and fails the run;
|
||||
|
||||
Reference in new issue
Block a user