Skip to content

Commit 58d167a

Browse files
committed
(chocolatey-archiveGH-427) Use remote checksums if available
Verify remote checksum for X-Checksum-Sha1 if it is available.
1 parent f5c541a commit 58d167a

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎src/helpers/functions/Get-ChocolateyWebFile.ps1‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ param(
7474

7575
#$downloader = new-object System.Net.WebClient
7676
#$downloader.DownloadFile($url, $fileFullPath)
77+
$headers = @{}
7778
if ($url.StartsWith('http')) {
79+
$headers = Get-WebHeaders $url
80+
7881
Write-Host "Downloading $packageName $bitPackage bit
7982
from `'$url`'"
8083
Get-WebFile $url $fileFullPath
@@ -91,6 +94,15 @@ param(
9194

9295
Start-Sleep 2 #give it a sec or two to finish up copying
9396

97+
if ($headers.Count -ne 0) {
98+
if ($headers.ContainsKey("X-Checksum-Sha1")) {
99+
$remoteChecksum = $headers["X-Checksum-Sha1"]
100+
Write-Debug "Verifying remote checksum of `'$remoteChecksum`' for `'$fileFullPath`'."
101+
Get-CheckSumValid -file $fileFullPath -checkSum $remoteChecksum -checksumType 'sha1'
102+
}
103+
}
104+
105+
Write-Debug "Verifying package provided checksum of `'$checksum`' for `'$fileFullPath`'."
94106
Get-CheckSumValid -file $fileFullPath -checkSum $checksum -checksumType $checksumType
95107

96108
# $url is already set properly to the used location.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function Get-WebHeaders {
2+
param(
3+
$url = '',
4+
$userAgent = 'chocolatey command line'
5+
)
6+
Write-Debug "Running 'Get-WebHeaders' with url:`'$url`', userAgent: `'$userAgent`'";
7+
if ($url -eq '') { return }
8+
9+
$request = [System.Net.HttpWebRequest]::Create($url);
10+
#to check if a proxy is required
11+
$client = New-Object System.Net.WebClient
12+
if (!$client.Proxy.IsBypassed($url))
13+
{
14+
$creds = [Net.CredentialCache]::DefaultCredentials
15+
if ($creds -eq $null) {
16+
Write-Debug "Default credentials were null. Attempting backup method"
17+
$cred = Get-Credential
18+
$creds = $cred.GetNetworkCredential();
19+
}
20+
$proxyAddress = $client.Proxy.GetProxy($url).Authority
21+
Write-host "Using this proxyserver: $proxyAddress"
22+
$proxy = New-Object System.Net.WebProxy($proxyAddress)
23+
$proxy.credentials = $creds
24+
$request.proxy = $proxy
25+
}
26+
27+
#http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-httpw
28+
$request.CookieContainer = New-Object System.Net.CookieContainer
29+
if ($userAgent -ne $null) {
30+
Write-Debug "Setting the UserAgent to `'$userAgent`'"
31+
$request.UserAgent = $userAgent
32+
}
33+
34+
$response = $request.GetResponse();
35+
36+
$headers = @{}
37+
Write-Debug "Web Headers Received:"
38+
foreach ($key in $response.Headers) {
39+
$value = $response.Headers[$key];
40+
if ($value) {
41+
$headers.Add("$key","$value")
42+
Write-Debug " `'$key`':`'$value`'"
43+
}
44+
}
45+
$response.Close();
46+
47+
$headers
48+
}

0 commit comments

Comments
 (0)