Advisory

GHSA-h4r5-mxv5-8mjc

Details, part 2 of 2. The advisory

Details

Save as openapi3filter/advisory_poc_bench_test.go (same package, alongside the PoC):

package openapi3filter_test

import (
	"context"
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/getkin/kin-openapi/openapi3"
	"github.com/getkin/kin-openapi/openapi3filter"
	"github.com/getkin/kin-openapi/routers/gorillamux"
)

// BenchmarkDeepObjectJunkKeys measures the per-request validation cost of a
// deepObject query parameter as the number of attacker-controlled junk query
// keys grows. Before the fix, cost scales linearly with the key count because a
// loop-invariant regexp is recompiled once per key; after the fix it is
// dominated by url-parsing and grows far more gently.
func BenchmarkDeepObjectJunkKeys(b *testing.B) {
	loader := openapi3.NewLoader()
	doc, err := loader.LoadFromData([]byte(specDeepObjectParam))
	if err != nil {
		b.Fatalf("load: %v", err)
	}
	if err := doc.Validate(loader.Context); err != nil {
		b.Fatalf("validate: %v", err)
	}
	router, err := gorillamux.NewRouter(doc)
	if err != nil {
		b.Fatalf("router: %v", err)
	}

	for _, n := range []int{1000, 5000, 20000, 40000, 80000, 100000} {
		var sb strings.Builder
		sb.WriteString("/f?filter[a]=x")
		for i := 0; i < n; i++ {
			fmt.Fprintf(&sb, "&j%d=1", i)
		}
		target := sb.String()

		b.Run(fmt.Sprintf("keys=%d", n), func(b *testing.B) {
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				req, _ := http.NewRequest(http.MethodGet, target, nil)
				route, pathParams, err := router.FindRoute(req)
				if err != nil {
					b.Fatalf("route: %v", err)
				}
				_ = openapi3filter.ValidateRequest(context.Background(), &openapi3filter.RequestValidationInput{
					Request: req, PathParams: pathParams, Route: route,
					Options: &openapi3filter.Options{AuthenticationFunc: openapi3filter.NoopAuthenticationFunc},
				})
			}
		})
	}
}

Run:

go test ./openapi3filter/ -run '^$' -bench BenchmarkDeepObjectJunkKeys -benchmem -benchtime=2s

Results on the vulnerable code (Go 1.25, darwin/arm64, Apple M4 Pro):

| junk keys | ns/op | B/op | allocs/op |
|----------:|------------:|-------------:|------------:|
| 1,000 | 1,355,966 | 3,853,606 | 48,114 |
| 5,000 | 7,833,901 | 18,975,316 | 240,221 |
| 20,000 | 33,701,124 | 75,748,660 | 960,562 |
| 40,000 | 63,365,171 | 151,394,478 | 1,920,786 |
| 80,000 | 117,388,579 | 302,663,170 | 3,841,109 |
| 100,000 | 147,468,311 | 375,159,565 | 4,801,189 |

The benchmark quantifies the report along two axes the single-shot PoC only hinted at:

  • CPU scales cleanly linearly, with no self-limiting plateau. Every metric grows in near-exact proportion to the junk-key count across two orders of magnitude: CPU, bytes and allocations all rise ~100× from 1k to 100k keys. At 100k keys a single request burns ~147 ms of CPU before any handler runs, and the cost does not taper off — an attacker gets full linear return on each additional junk byte, with the only ceiling being the request-size limit the server chooses to impose (if any).
  • The recompilation dominates allocations, not just CPU. Each junk key both compiles a throwaway *regexp.Regexp and allocates its compiled program as garbage: the vulnerable path costs a steady ~48 allocations and ~3.75 KB per junk key (375 MB and 4.8M allocations at 100k keys). So the same root cause drives both CPU exhaustion and heavy GC/allocator pressure — which is why this mechanism, while distinct from the adjacent memory-exhaustion advisory, also inflates memory traffic.

Impact

Uncontrolled CPU consumption (denial of service). Any service that uses openapi3filter.ValidateRequest (directly or via a router) to validate requests against a spec containing at least one in: query, style: deepObject parameter is affected. An unauthenticated attacker who can reach such an endpoint can send requests with large numbers of junk query keys; each request performs a number of regexp.MustCompile calls proportional to the request's query-key count times the number of deepObject parameters on the route. Because the work is CPU-bound and executed during validation (before any application handler runs), modest request rates can starve the process of CPU and degrade or deny service to legitimate clients. There is no impact to confidentiality or integrity.

Remediation

Hoist the invariant regexp out of the per-key loop: compile the ^<param>\[ pattern once per deepObject parameter (before iterating the query keys) and reuse the compiled *regexp.Regexp for every key — mirroring how the package-level deepObjectBracketRE (req_resp_decoder.go:41) is already handled. This makes decode cost independent of the junk-key count. Matching semantics are unchanged (the pattern still derives from the spec parameter name via regexp.QuoteMeta, so it stays panic-safe for any name), and *regexp.Regexp is safe for the reuse.

As a mitigation before upgrading, front affected endpoints with a limit on request/URL size or query-key count (e.g. at a reverse proxy or middleware), which bounds the number of loop iterations an attacker can force.

Notes

  • Advisory relationship: Novel. Adjacent to previously reported memory-exhaustion issue in deepObject decoding (via sliceMapToSlice), but this is a distinct CPU-exhaustion mechanism — repeated compilation of a loop-invariant regexp, once per attacker-supplied query key — not covered by that earlier memory-focused advisory.

Sources & licences