Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/pb33f/libopenapi-validator

go 1.19
go 1.20

require (
github.com/pb33f/libopenapi v0.8.0
github.com/pb33f/libopenapi v0.8.1
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/stretchr/testify v1.8.0
github.com/vmware-labs/yaml-jsonpath v0.3.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/pb33f/libopenapi v0.7.0 h1:2mQgDa0UQs8/5Hd2z+KbgWvoGx0++N3ihMKihcAS5M
github.com/pb33f/libopenapi v0.7.0/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
github.com/pb33f/libopenapi v0.8.0 h1:zfXNu7/Hk9sgMV3UC2hnSB4CGlgQkMaWWwPn0Id+eLI=
github.com/pb33f/libopenapi v0.8.0/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
github.com/pb33f/libopenapi v0.8.1 h1:kudGljKp7cErupRr8ND5oIaTsfjOfQYAxSd3Z2dxt6k=
github.com/pb33f/libopenapi v0.8.1/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 h1:WCcC4vZDS1tYNxjWlwRJZQy28r8CMoggKnxNzxsVDMQ=
Expand Down
219 changes: 110 additions & 109 deletions parameters/cookie_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,124 +4,125 @@
package parameters

import (
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strconv"
"strings"
"fmt"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strconv"
"strings"
)

func (v *paramValidator) ValidateCookieParams(request *http.Request) (bool, []*errors.ValidationError) {

// find path
var pathItem *v3.PathItem
var errs []*errors.ValidationError
if v.pathItem == nil {
pathItem, errs, _ = paths.FindPath(request, v.document)
if pathItem == nil || errs != nil {
v.errors = errs
return false, errs
}
} else {
pathItem = v.pathItem
}
// find path
var pathItem *v3.PathItem
var errs []*errors.ValidationError
if v.pathItem == nil {
pathItem, errs, _ = paths.FindPath(request, v.document)
if pathItem == nil || errs != nil {
v.errors = errs
return false, errs
}
} else {
pathItem = v.pathItem
}

// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
var validationErrors []*errors.ValidationError
for _, p := range params {
if p.In == helpers.Cookie {
for _, cookie := range request.Cookies() {
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required
// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
var validationErrors []*errors.ValidationError
for _, p := range params {
if p.In == helpers.Cookie {
for _, cookie := range request.Cookies() {
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required

var sch *base.Schema
if p.Schema != nil {
sch = p.Schema.Schema()
}
pType := sch.Type
var sch *base.Schema
if p.Schema != nil {
sch = p.Schema.Schema()
}
pType := sch.Type

for _, ty := range pType {
switch ty {
case helpers.Integer, helpers.Number:
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
validationErrors = append(validationErrors,
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
break
}
// check if enum is in range
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
case helpers.Boolean:
if _, err := strconv.ParseBool(cookie.Value); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
}
case helpers.Object:
if !p.IsExploded() {
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)
for _, ty := range pType {
switch ty {
case helpers.Integer, helpers.Number:
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
validationErrors = append(validationErrors,
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
break
}
// check if enum is in range
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal) {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
case helpers.Boolean:
if _, err := strconv.ParseBool(cookie.Value); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
}
case helpers.Object:
if !p.IsExploded() {
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)

// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch, encodedObj, "",
"Cookie parameter",
"The cookie parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationQuery)...)
}
}
case helpers.Array:
// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch, encodedObj, "",
"Cookie parameter",
"The cookie parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationQuery)...)
}
}
case helpers.Array:

if !p.IsExploded() {
// well we're already in an array, so we need to check the items schema
// to ensure this array items matches the type
// only check if items is a schema, not a boolean
if sch.Items.IsA() {
validationErrors = append(validationErrors,
ValidateCookieArray(sch, p, cookie.Value)...)
}
}
if !p.IsExploded() {
// well we're already in an array, so we need to check the items schema
// to ensure this array items matches the type
// only check if items is a schema, not a boolean
if sch.Items.IsA() {
validationErrors = append(validationErrors,
ValidateCookieArray(sch, p, cookie.Value)...)
}
}

case helpers.String:
case helpers.String:

// check if the schema has an enum, and if so, match the value against one of
// the defined enum values.
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
}
}
}
}
}
}
if len(validationErrors) > 0 {
return false, validationErrors
}
return true, nil
// check if the schema has an enum, and if so, match the value against one of
// the defined enum values.
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal) {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
}
}
}
}
}
}
if len(validationErrors) > 0 {
return false, validationErrors
}
return true, nil
}
Loading