hashicorp / go-getter

Package for downloading things from a string URL using a variety of protocols.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The `filename` magic param isn't always removed from the request

pkosiec opened this issue · comments

Description

The filename magic parameter sometimes isn't removed from the underlying request, which make some requests fail, e.g. when using signed URLs to GCS bucket.

Details

Based on the code, I believe all the "magic" parameters should be removed before making a request.
This is not the case in case of an URL to tar.gz directory, like this:

https://storage.googleapis.com/bucket/v1.0.0/binary_darwin_amd64.tar.gz

and configuration:

	getterCli := &getter.Client{
		Ctx:  ctx,
		Src:  url, // url with `filename` magic param
		Dst:  tmpDestPath,
		Pwd:  pwd,
		Mode: getter.ClientModeAny,
	}

I tracked the code execution, and it falls in the if which changes the mode:

mode = ClientModeFile

	if decompressor != nil {
		// ...
		mode = ClientModeFile
	}

Which is why the filename param is not removed, as the next if is skipped, which actually removes the filename param.

go-getter/client.go

Lines 230 to 249 in a2ebce9

if mode == ClientModeAny {
// Ask the getter which client mode to use
mode, err = g.ClientMode(u)
if err != nil {
return err
}
// Destination is the base name of the URL path in "any" mode when
// a file source is detected.
if mode == ClientModeFile {
filename := filepath.Base(u.Path)
// Determine if we have a custom file name
if v := q.Get("filename"); v != "" {
// Delete the query parameter if we have it.
q.Del("filename")
u.RawQuery = q.Encode()
filename = v
}

Fix incoming

I fixed it locally and will prepare a pull request soon. Hope you'll like it!