Skip to content

Commit 6639677

Browse files
committed
Validate file content-length
At least one run of v0.0.2 encountered a corrupt tarball downloaded from GitHub. As there aren't any hashes reported by GitHub's api, it seems somewhat worthwhile to at least validate the reported file size from the HTTP server against the file size on disk. If you encounter a problem where this length is mismatched, please file a bug with details.
1 parent 16cd1fa commit 6639677

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

gh-program-downloader

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,42 @@ set_up_auth() {
8080
fi
8181
}
8282

83+
get_content_length() {
84+
perl -e '
85+
my $headers_file=shift;
86+
my $length;
87+
{
88+
open(my $headers, q(<), $headers_file);
89+
local $/="\r\n";
90+
while (<$headers>) {
91+
chomp;
92+
next unless m/^content-length:\s+(\d+)$/i;
93+
$length=$1;
94+
}
95+
close $headers;
96+
}
97+
print $length;
98+
' "$1"
99+
}
100+
101+
validate_file_length() {
102+
perl -e '
103+
my $artifact=$ENV{artifact};
104+
my $temp_file="$ENV{temp_file}";
105+
my $length=$ENV{content_length};
106+
my $size=-s $temp_file;
107+
die "downloaded artifact ($artifact) length ($size) is not expected length ($length)" unless ($size) == $length;
108+
print "\n";
109+
'
110+
}
111+
83112
download_artifact() {
84113
temp_file=$(mktemp)
85-
curl -H "$AUTHORIZATION_HEADER" -o "$temp_file" -q -s -L "$artifact"
114+
headers_file=$(mktemp)
115+
curl -D "$headers_file" -H "$AUTHORIZATION_HEADER" -o "$temp_file" -q -s -L "$artifact"
116+
content_length=$(get_content_length "$headers_file")
117+
content_length="$content_length" artifact="$artifact" temp_file="$temp_file" validate_file_length
118+
86119
echo "url=$artifact" >> "$GITHUB_OUTPUT"
87120
}
88121

0 commit comments

Comments
 (0)