Debian Codegraph @

parameters/cookie_parameters.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package parameters
5
6 import (
7 "fmt"
8 "net/http"
9 "strconv"
10 "strings"
11
12 "github.com/pb33f/libopenapi/datamodel/high/base"
13
14 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
15
16 "github.com/pb33f/libopenapi-validator/errors"
17 "github.com/pb33f/libopenapi-validator/helpers"
18 "github.com/pb33f/libopenapi-validator/paths"
19 "github.com/pb33f/libopenapi-validator/strict"
20 )
21
22 func (v *paramValidator) ValidateCookieParams(request *http.Request) (bool, []*errors.ValidationError) {
23 pathItem, errs, foundPath := paths.FindPath(request, v.document, v.options)
24 if len(errs) > 0 {
25 return false, errs
26 }
27 return v.ValidateCookieParamsWithPathItem(request, pathItem, foundPath)
28 }
29
30 func (v *paramValidator) ValidateCookieParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) {
31 if pathItem == nil {
32 return false, []*errors.ValidationError{{
33 ValidationType: helpers.PathValidation,
34 ValidationSubType: helpers.ValidationMissing,
35 Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
36 Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
37 "however that path, or the %s method for that path does not exist in the specification",
38 request.Method, request.URL.Path, request.Method),
39 SpecLine: -1,
40 SpecCol: -1,
41 HowToFix: errors.HowToFixPath,
42 }}
43 }
44 // extract params for the operation
45 params := helpers.ExtractParamsForOperation(request, pathItem)
46 var validationErrors []*errors.ValidationError
47 operation := strings.ToLower(request.Method)
48
49 // build a map of cookies from the request for efficient lookup
50 cookieMap := make(map[string]*http.Cookie)
51 for _, cookie := range request.Cookies() {
52 cookieMap[cookie.Name] = cookie
53 }
54
55 for _, p := range params {
56 if p.In == helpers.Cookie {
57 // look up the cookie by name (cookies are case-sensitive)
58 cookie, found := cookieMap[p.Name]
59 if !found {
60 // cookie not present in request - check if required
61 if p.Required != nil && *p.Required {
62 validationErrors = append(validationErrors, errors.CookieParameterMissing(p, pathValue, operation, ""))
63 }
64 continue
65 }
66
67 var sch *base.Schema
68 if p.Schema != nil {
69 sch = p.Schema.Schema()
70 }
71
72 // Get rendered schema for ReferenceSchema field in errors (uses cache if available)
73 renderedSchema := GetRenderedSchema(sch, v.options)
74
75 pType := sch.Type
76
77 for _, ty := range pType {
78 switch ty {
79 case helpers.Integer:
80 if _, err := strconv.ParseInt(cookie.Value, 10, 64); err != nil {
81 validationErrors = append(validationErrors,
82 errors.InvalidCookieParamInteger(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
83 break
84 }
85 // validate value matches allowed enum values
86 if sch.Enum != nil {
87 matchFound := false
88 for _, enumVal := range sch.Enum {
89 if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal.Value) {
90 matchFound = true
91 break
92 }
93 }
94 if !matchFound {
95 validationErrors = append(validationErrors,
96 errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
97 }
98 }
99 case helpers.Number:
100 if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
101 validationErrors = append(validationErrors,
102 errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
103 break
104 }
105 // validate value matches allowed enum values
106 if sch.Enum != nil {
107 matchFound := false
108 for _, enumVal := range sch.Enum {
109 if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal.Value) {
110 matchFound = true
111 break
112 }
113 }
114 if !matchFound {
115 validationErrors = append(validationErrors,
116 errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
117 }
118 }
119 case helpers.Boolean:
120 if _, err := strconv.ParseBool(cookie.Value); err != nil {
121 validationErrors = append(validationErrors,
122 errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
123 }
124 case helpers.Object:
125 if !p.IsExploded() {
126 encodedObj := helpers.ConstructMapFromCSVWithSchema(cookie.Value, sch)
127
128 // if a schema was extracted
129 if sch != nil {
130 validationErrors = append(validationErrors,
131 ValidateParameterSchema(sch, encodedObj, "",
132 "Cookie parameter",
133 "The cookie parameter",
134 p.Name,
135 helpers.ParameterValidation,
136 helpers.ParameterValidationQuery,
137 v.options)...)
138 }
139 }
140 case helpers.Array:
141
142 if !p.IsExploded() {
143 // well we're already in an array, so we need to check the items schema
144 // to ensure this array items matches the type
145 // only check if items is a schema, not a boolean
146 if sch.Items.IsA() {
147 validationErrors = append(validationErrors,
148 ValidateCookieArray(sch, p, cookie.Value, pathValue, operation, renderedSchema)...)
149 }
150 }
151
152 case helpers.String:
153
154 // check if the schema has an enum, and if so, match the value against one of
155 // the defined enum values.
156 if sch.Enum != nil {
157 matchFound := false
158 for _, enumVal := range sch.Enum {
159 if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal.Value) {
160 matchFound = true
161 break
162 }
163 }
164 if !matchFound {
165 validationErrors = append(validationErrors,
166 errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch, pathValue, operation, renderedSchema))
167 break
168 }
169 }
170 validationErrors = append(validationErrors,
171 ValidateSingleParameterSchema(
172 sch,
173 cookie.Value,
174 "Cookie parameter",
175 "The cookie parameter",
176 p.Name,
177 helpers.ParameterValidation,
178 helpers.ParameterValidationCookie,
179 v.options,
180 pathValue,
181 operation,
182 )...)
183 }
184 }
185 }
186 }
187
188 errors.PopulateValidationErrors(validationErrors, request, pathValue)
189
190 if len(validationErrors) > 0 {
191 return false, validationErrors
192 }
193
194 // strict mode: check for undeclared cookies
195 if v.options.StrictMode {
196 undeclaredCookies := strict.ValidateCookies(request, params, v.options)
197 for _, undeclared := range undeclaredCookies {
198 validationErrors = append(validationErrors,
199 errors.UndeclaredCookieError(
200 undeclared.Path,
201 undeclared.Name,
202 undeclared.Value,
203 undeclared.DeclaredProperties,
204 request.URL.Path,
205 request.Method,
206 ))
207 }
208 }
209
210 if len(validationErrors) > 0 {
211 return false, validationErrors
212 }
213 return true, nil
214 }