Skip to content
Go: Reject malformed queries before using partial values

Go: Reject malformed queries before using partial values

November 18, 2026

A malformed query can still produce a map containing valid entries. url.ParseQuery returns valid pairs alongside an error, so a caller that ignores the error may process an incomplete request. Reject the entire query when your endpoint requires every pair to parse.

Set up

Tested on Linux amd64 with Go 1.26.10. Use Bash with Go on your PATH, a text editor, and the mktemp, rm, and rmdir utilities. The example uses only the standard library. Keep the same terminal through cleanup, and stop if setup fails before saving the source.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save as go-url-reject-partial-query.go:

package main

import (
	"fmt"
	"net/url"
)

func checkedQuery(raw string) (url.Values, error) {
	values, err := url.ParseQuery(raw)
	if err != nil {
		return nil, err
	}
	return values, nil
}

func main() {
	for _, raw := range []string{
		"item=tea&limit=10",
		"item=tea&limit=%ZZ",
		"item=tea&limit=10;mode=fast",
	} {
		partial, parseErr := url.ParseQuery(raw)
		accepted, err := checkedQuery(raw)
		fmt.Printf("query=%q\n", raw)
		fmt.Printf("parser values=%q error=%t\n", partial.Encode(), parseErr != nil)
		fmt.Printf("accepted=%t values=%q\n", err == nil, accepted.Encode())
		if err != nil && accepted != nil {
			panic("partial values escaped validation")
		}
	}
	u := &url.URL{RawQuery: "item=tea&limit=%ZZ"}
	fmt.Printf("URL.Query values=%q\n", u.Query().Encode())
}

Run:

go run go-url-reject-partial-query.go

Observed output:

query="item=tea&limit=10"
parser values="item=tea&limit=10" error=false
accepted=true values="item=tea&limit=10"
query="item=tea&limit=%ZZ"
parser values="item=tea" error=true
accepted=false values=""
query="item=tea&limit=10;mode=fast"
parser values="item=tea" error=true
accepted=false values=""
URL.Query values="item=tea"

Read the result

The valid query retains both fields. In the second query, the percent escape is invalid; in the third, an unescaped semicolon makes that setting invalid. Both still yield the valid item field. The wrapper returns a nil map on failure so later code cannot accidentally consume those partial values.

The final line demonstrates URL.Query, which returns values without an error result and silently discards malformed pairs. Use url.ParseQuery on u.RawQuery when rejecting malformed query syntax is part of the request contract. This example constructs an address in memory and sends no request.

Apply it carefully

Successful decoding does not validate a schema. Check required keys, allowed values and duplicate parameters separately. In particular, Values.Get returns only the first value for a key, so it cannot establish that the client supplied the key exactly once.

The wrapper enforces an all-or-nothing parsing policy; a diagnostic tool might intentionally display partial results instead. Bound request size separately and avoid logging raw queries that may contain credentials. An empty query can parse successfully, so a missing required field still needs an explicit check.

See ParseQuery and URL.Query.

Clean up

Remove the saved source and the empty scratch directory. Go manages its build cache separately. If the directory is not empty, inspect its contents before removing anything else.

rm -- go-url-reject-partial-query.go
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on