kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Case-insensitive usmarshalling for json/v1 and json-schema / openapi validation

Open aywan opened this issue 11 months ago • 2 comments

Hi!

I found a tricky case that should probably be known about, but I couldn't find the information.

TLDR> validation is breaking due to problems in json/v1 with case insensitivity, which allows you to bypass validation.

It's easier to explain with an example:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"

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

var schema = `openapi: 3.0.0
info:
  title: Test API
  version: 1.0.0
paths:
  /test:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                value:
                  type: string
                  enum:
                    - "aniki"
      responses:
        '204':
          description: OK
`

var examples = []struct {
	name string
	body string
}{
	{"good", `{"value": "aniki"}`},
	{"bad", `{"value": "beniki"}`},
	{"evil", `{"value": "aniki", "Value": "beniki"}`},
	{"chaotic evil", `{"Value": "beniki", "vaLue": "chaos"}`},
}

type ValueObj struct {
	Value string `json:"value"`
}

func main() {
	ctx := context.Background()
	loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}

	doc, err := loader.LoadFromData([]byte(schema))
	if err != nil {
		log.Fatal("load schema: ", err)
	}

	err = doc.Validate(ctx)
	if err != nil {
		log.Fatal("validate schema: ", err)
	}

	router, err := gorillamux.NewRouter(doc)
	if err != nil {
		log.Fatal("create router: ", err)
	}

	for _, ex := range examples {
		httpReq, _ := http.NewRequest(http.MethodPost, "/test", strings.NewReader(ex.body))
		httpReq.Header.Set("Content-Type", "application/json")

		route, pathParams, _ := router.FindRoute(httpReq)
		requestValidationInput := &openapi3filter.RequestValidationInput{
			Request:    httpReq,
			PathParams: pathParams,
			Route:      route,
		}

		err := openapi3filter.ValidateRequest(ctx, requestValidationInput)
		if err != nil {
			fmt.Println(ex.name, "failed:", err)
		} else {
			fmt.Println(ex.name, "passed")
		}

		var val ValueObj
		err = json.Unmarshal([]byte(ex.body), &val)
		if err != nil {
			log.Fatal("unmarshal: ", err)
		}
		fmt.Printf("%s value: %+v\n", ex.name, val)
		fmt.Println("-----")
	}
}

output:

good passed
good value: {Value:aniki}
-----
bad failed: request body has an error: doesn't match schema: Error at "/value": value is not one of the allowed values ["aniki"]
Schema:
  {
    "enum": [
      "aniki"
    ],
    "type": "string"
  }

Value:
  "beniki"

bad value: {Value:beniki}
-----
evil passed
evil value: {Value:beniki}
-----
chaotic evil passed
chaotic evil value: {Value:chaos}
-----

As you can see, you can slip in any value and bypass the validation. This means that in many cases it is not worth relying on validation.

https://github.com/getkin/kin-openapi/blob/master/openapi3/schema.go#L1999 It seems we need to fix the code here a bit to solve this problem. You need to add an option to reduce all names to the same case.

go.mod

go 1.25.5

require github.com/getkin/kin-openapi v0.133.0

aywan avatar Dec 22 '25 16:12 aywan

This is a subtle but critical boundary issue when combining Go's encoding/json with OpenAPI schema validation.

In Go's standard library encoding/json.Unmarshal, struct field lookup is case-insensitive if an exact tag match is absent. In contrast, JSON Schema and OpenAPI 3.x specify strict case-sensitive property matching. If a payload sends {"Email": "..."} while the specification defines required: ["email"]:

  • Validating the raw unmarshalled map[string]any correctly flags that "email" is missing.
  • However, if the payload is decoded directly into a Go struct prior to validation, or if additionalProperties: false is not strictly asserted on the schema, casing variations can bypass schema constraints while still populating internal model fields.

Two recommended safeguards when using openapi3filter:

  1. Use json.NewDecoder(r.Body).DisallowUnknownFields() to prevent unmapped or variant-cased attributes from silently binding.
  2. Ensure openapi3filter.ValidateRequest runs against the unmarshalled raw JSON tree before unmarshalling into application domain structs.

In our Release Guardian contract audits, we monitor case-sensitivity drift between Go API runtimes and OpenAPI specifications. If helpful, I can share a minimal test reproducing the bypass in openapi3filter.

genesiscode2026 avatar Sep 27 '26 16:09 genesiscode2026

FYI I'm exploring a solution that's similar to https://github.com/getkin/kin-openapi/pull/1311 but for the whole project. Weighing whether to just upgrade the repo to 1.27.

My own fuzzing discovered this as well. Will push the tests and fix soon(tm).

Thanks for reporting!

fenollp avatar Oct 11 '26 11:10 fenollp