Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: Double slash in request leads Method to always be GET #69063

Closed
heathcliff26 opened this issue Aug 25, 2024 · 2 comments
Closed

net/http: Double slash in request leads Method to always be GET #69063

heathcliff26 opened this issue Aug 25, 2024 · 2 comments

Comments

@heathcliff26
Copy link

Go version

go version go1.23.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/user/repos/testrepo/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3255200026=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I accidentally created a double slash in an http request URL. I have written the following to reproduce:

Server server.go:

package main

import "net/http"

func main() {
	http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
		if req.Method != http.MethodPost {
			rw.WriteHeader(http.StatusBadRequest)
			rw.Write([]byte("Wrong request type: " + req.Method))
		} else {
			rw.Write([]byte("Success"))
		}
	})

	err := http.ListenAndServe("localhost:8080", nil)
	if err != nil {
		panic(err)
	}
}

Client client.go:

package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

func main() {
	req, err := http.NewRequest(http.MethodPost, "http://localhost:8080/"+"/foo/bar", strings.NewReader("test"))
	if err != nil {
		panic(fmt.Errorf("Failed to create request: %v", err))
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(fmt.Errorf("Failed to send request: %v", err))
	}
	buf, err := io.ReadAll(res.Body)
	if err != nil {
		panic(fmt.Errorf("Failed to read response body: %v", err))
	}
	fmt.Println(string(buf))
}

What did you see happen?

Server receives a GET request, with no errors on the client/server side

What did you expect to see?

Server receives a POST request, or possible an error when creating the request.

heathcliff26 added a commit to heathcliff26/kube-upgrade that referenced this issue Aug 25, 2024
A double slash in the URL causes the request method to become GET.
Remove it to ensure the daemon can aquire the lock.

See: golang/go#69063
Signed-off-by: Heathcliff <heathcliff@heathcliff.eu>
heathcliff26 added a commit to heathcliff26/kube-upgrade that referenced this issue Aug 25, 2024
A double slash in the URL causes the request method to become GET.
Remove it to ensure the daemon can aquire the lock.

See: golang/go#69063
Signed-off-by: Heathcliff <heathcliff@heathcliff.eu>
@seankhliao
Copy link
Member

The server returns a redirect to a clean path. Changing to GET is expected for the given http response code.

Closing as working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants