Skip to content

net/http: http.FileServer returns 500 when a null byte is passed in the URL #72091

Closed
@lodig-einride

Description

@lodig-einride

Go version

go version go1.24.0 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/greg/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/greg/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/02/s6w7nrhn6hl8_b1t1nslsfkh0000gp/T/go-build942617912=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/greg/Documents/yopass-500/go.mod'
GOMODCACHE='/Users/greg/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/greg/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/greg/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Go's http.FileServer will return a 500 for requests that have a null byte character in the path, like http://localhost:8080/test%00.

The client is requesting to be served a file named test%00 but null bytes are not allowed in file names in any modern filesystems AFAIK.

Therefore I think the HTTP error code should be 400 (Bad Request) instead of 500 (Internal Server Error).

Repro code:

package main

import "net/http"

func main() {
	fileServer := http.FileServer(http.Dir("public"))

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fileServer.ServeHTTP(w, r)
	})

	http.ListenAndServe(":8080", nil)
}

What did you see happen?

❯ curl http://localhost:8080/test
404 page not found

❯ curl http://localhost:8080/test%00
500 Internal Server Error

What did you expect to see?

❯ curl http://localhost:8080/test
404 page not found

❯ curl http://localhost:8080/test%00
400 bad request

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions