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

request validation: readOnly properties

Open squiril opened this issue 4 years ago • 2 comments

Hi,

Currently kinopenapi-filter does not block requests with readOnly properties (using openapi3filter.ValidateRequestBody() or openapi3.VisitAsRequest() option).

per OpenAPI specification: readOnly | Relevant only for Schema "properties" definitions. Declares the property as "read only". This means that it MAY be sent as part of a response but **SHOULD NOT** be sent as part of the request. ...

Would you be open to extending validation to reject requests with readOnly properties in the request body (as long as VisitAsRequest() option is used)? Since OpenAPI uses SHOULD NOT, this validation could be optional, e.g. only if openapi3.RejectRequestsWithReadOnly=true)

example schema+code: id is defined as readOnly and set in request body. retuns "Valid" (no error from ValidateRequestBody)

package main

import (
	"bytes"
	"context"
	"fmt"
	"net/http"

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

const testSchema = `openapi: 3.0.2
info:
  title: AP
  version: 0.1.0
paths:
  /application:
    put:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Application"
      responses:
        "202":
          $ref: "#/components/responses/JobAccepted"
components:
  responses:
    JobAccepted:
      description: Accepted
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
                type: string
  schemas:
    Application:
      type: object
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string
      additionalProperties: false
      required:
        - name
`

func main() {
	var (
		err error
	)

	jsn := []byte(`{
		"name": "test",
    "id": 123
	  }`)

	b := bytes.NewReader(jsn)
	swagger, err := openapi3.NewLoader().LoadFromData([]byte(testSchema))
	if err != nil {
		panic(fmt.Sprintf("error loading swagger : %s", err))
	}

	router, _ := legacyrouter.NewRouter(swagger)

	req, _ := http.NewRequest(http.MethodPut, "/application", b)
	route, pathParams, _ := router.FindRoute(req)
	req.Header.Set("Content-Type", "application/json")

	reqBody := route.Operation.RequestBody.Value

	requestValidationInput := &openapi3filter.RequestValidationInput{
		Request:    req,
		PathParams: pathParams,
		Route:      route,
	}

	err = openapi3filter.ValidateRequestBody(context.TODO(), requestValidationInput, reqBody)
	if err == nil {
		fmt.Println("Valid")
	} else {
		fmt.Printf("NOT valid. %s\n", err)
	}
}

squiril avatar Mar 02 '22 08:03 squiril

Hi! Thanks for this clear issue and perfectly acceptable proposal. Feel free to implement this! I'll review it.

Note: once this is in, https://github.com/getkin/kin-openapi/pull/748/files#diff-eedc527294a2440ebfc33853dd748e456137c0685da2ac66d6578bca0d78aee6R158-R176 should get uncommented.

fenollp avatar Jan 23 '23 13:01 fenollp

Now rejected, with https://pkg.go.dev/github.com/getkin/[email protected]/openapi3filter#Options.ExcludeReadOnlyValidations to opt out.

fenollp avatar Oct 09 '26 05:10 fenollp