@@ -236,13 +236,28 @@ func (c *Client) legacyDownloadImage(ctx context.Context, arch, name, tag string
236
236
return err
237
237
}
238
238
239
+ redirectURL , err := url .Parse (res .Header .Get ("Location" ))
240
+ if err != nil {
241
+ return err
242
+ }
243
+
239
244
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
241
247
creds = bearerTokenCredentials {authToken : c .AuthToken }
242
248
}
243
249
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 )
246
261
}
247
262
248
263
func parseContentLengthHeader (val string ) (int64 , error ) {
0 commit comments