golang/go

net/url: setPath does not set rawPath for foo%252Fbar

Closed this issue · 3 comments

Go version

1.25.3

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/ldesauw/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/ldesauw/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3444246438=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/ldesauw/go/pkg/mod'
GONOPROXY='stash.ovh.net,github.com/ovhcloud-kms/*'
GONOSUMDB='stash.ovh.net,github.com/ovhcloud-kms/*'
GOOS='linux'
GOPATH='/home/ldesauw/go'
GOPRIVATE='stash.ovh.net,github.com/ovhcloud-kms/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/ldesauw/bin/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/ldesauw/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/ldesauw/bin/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

  1. Created an URL u
  2. Called `u.SetPath("foo%252Fbar")

What did you see happen?

The RawPath was equal to ""

What did you expect to see?

The RawPath equal to "foo%252Fbar"

setPath isn't a public method
please demonstrate an issue using the public apis

The case I encoutered was in the midle of a big pile, so I tried to reproduce an atomic version of the problem.
We create a http.Handler that print the url received in its path.

package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
	"time"
)

type Engine struct {
}

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	fmt.Printf("RawPath: %v\n", req.URL.RawPath)
	fmt.Printf("Path: %v\n", req.URL.Path)
}

func main() {

	hdl := &Engine{}

	bindAddr := "localhost:8081"

	httpSrv := &http.Server{
		Addr:              bindAddr,
		Handler:           hdl,
		ReadHeaderTimeout: 30 * time.Second,
		IdleTimeout:       30 * time.Second,
	}

	listener, err := net.Listen("tcp", bindAddr)
	if err != nil {
		log.Fatal(err)
	}
	httpSrv.Serve(listener)
}

I run this code with go run main.go &

And query with curl localhost:8081/foo%252Fbar

I receive

RawPath:
Path: /foo%2Fbar

Where I was expecting

RawPath: /foo%252Fbar
Path: /foo%2Fbar

As the docs for the field notes, it's only a hint used when the encoding is different than what EscapedPath would have produced.
The docs on tip are slightly clearer on this https://pkg.go.dev/net/url@master#URL

https://pkg.go.dev/net/url@master#URL