Skip to content

Commit b5db2aa

Browse files
authored
Merge pull request from GHSA-7p8m-22h4-9pj7
Add Authorization header when applicable
2 parents 54ce11f + eebd7ca commit b5db2aa

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

client/pull.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,28 @@ func (c *Client) legacyDownloadImage(ctx context.Context, arch, name, tag string
236236
return err
237237
}
238238

239+
redirectURL, err := url.Parse(res.Header.Get("Location"))
240+
if err != nil {
241+
return err
242+
}
243+
239244
var creds credentials
240-
if c.AuthToken != "" {
245+
if c.AuthToken != "" && samehost(c.BaseURL, redirectURL) {
246+
// Only include credentials if redirected to same host as base URL
241247
creds = bearerTokenCredentials{authToken: c.AuthToken}
242248
}
243249

244-
// Use uri from Location header to download artifact
245-
return c.multipartDownload(ctx, res.Header.Get("Location"), creds, dst, img.Size, spec, pb)
250+
// Use redirect URL to download artifact
251+
return c.multipartDownload(ctx, redirectURL.String(), creds, dst, img.Size, spec, pb)
252+
}
253+
254+
// samehost returns true if host1 and host2 are, in fact, the same host by
255+
// comparing scheme (https == https) and host, including port.
256+
//
257+
// Hosts will be treated as dissimilar if one host includes domain suffix
258+
// and the other does not, even if the host names match.
259+
func samehost(host1, host2 *url.URL) bool {
260+
return strings.EqualFold(host1.Scheme, host2.Scheme) && strings.EqualFold(host1.Host, host2.Host)
246261
}
247262

248263
func parseContentLengthHeader(val string) (int64, error) {

0 commit comments

Comments
 (0)