| 1 |
// Copyright 2023-2026 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 |
"strings" |
| 10 |
|
| 11 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 12 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 13 |
"github.com/pb33f/libopenapi/orderedmap" |
| 14 |
|
| 15 |
"github.com/pb33f/libopenapi-validator/config" |
| 16 |
"github.com/pb33f/libopenapi-validator/errors" |
| 17 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 18 |
"github.com/pb33f/libopenapi-validator/paths" |
| 19 |
) |
| 20 |
|
| 21 |
func (v *paramValidator) ValidateSecurity(request *http.Request) (bool, []*errors.ValidationError) { |
| 22 |
pathItem, errs, foundPath := paths.FindPath(request, v.document, v.options) |
| 23 |
if len(errs) > 0 { |
| 24 |
return false, errs |
| 25 |
} |
| 26 |
return v.ValidateSecurityWithPathItem(request, pathItem, foundPath) |
| 27 |
} |
| 28 |
|
| 29 |
func (v *paramValidator) ValidateSecurityWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) { |
| 30 |
if pathItem == nil { |
| 31 |
return false, []*errors.ValidationError{{ |
| 32 |
ValidationType: helpers.PathValidation, |
| 33 |
ValidationSubType: helpers.ValidationMissing, |
| 34 |
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path), |
| 35 |
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+ |
| 36 |
"however that path, or the %s method for that path does not exist in the specification", |
| 37 |
request.Method, request.URL.Path, request.Method), |
| 38 |
SpecLine: -1, |
| 39 |
SpecCol: -1, |
| 40 |
HowToFix: errors.HowToFixPath, |
| 41 |
}} |
| 42 |
} |
| 43 |
if !v.options.SecurityValidation { |
| 44 |
return true, nil |
| 45 |
} |
| 46 |
// extract security for the operation, falling back to document-level global security |
| 47 |
security := helpers.EffectiveSecurityForOperation(request, pathItem, v.document.Security) |
| 48 |
|
| 49 |
if len(security) == 0 { |
| 50 |
return true, nil |
| 51 |
} |
| 52 |
|
| 53 |
var allErrors []*errors.ValidationError |
| 54 |
|
| 55 |
// each security requirement in the array is OR'd - any one passing is sufficient |
| 56 |
for _, sec := range security { |
| 57 |
if sec.ContainsEmptyRequirement { |
| 58 |
return true, nil |
| 59 |
} |
| 60 |
|
| 61 |
// within a requirement, all schemes are AND'd - all must pass |
| 62 |
requirementSatisfied := true |
| 63 |
var requirementErrors []*errors.ValidationError |
| 64 |
|
| 65 |
for pair := orderedmap.First(sec.Requirements); pair != nil; pair = pair.Next() { |
| 66 |
secName := pair.Key() |
| 67 |
|
| 68 |
// look up security from components |
| 69 |
if v.document.Components == nil || v.document.Components.SecuritySchemes.GetOrZero(secName) == nil { |
| 70 |
validationErrors := []*errors.ValidationError{ |
| 71 |
{ |
| 72 |
Message: fmt.Sprintf("Security scheme '%s' is missing", secName), |
| 73 |
Reason: fmt.Sprintf("The security scheme '%s' is defined as being required, "+ |
| 74 |
"however it's missing from the components", secName), |
| 75 |
ValidationType: helpers.SecurityValidation, |
| 76 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 77 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 78 |
HowToFix: "Add the missing security scheme to the components", |
| 79 |
}, |
| 80 |
} |
| 81 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 82 |
requirementSatisfied = false |
| 83 |
requirementErrors = append(requirementErrors, validationErrors...) |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
secScheme := v.document.Components.SecuritySchemes.GetOrZero(secName) |
| 88 |
schemeValid, schemeErrors := v.validateSecurityScheme(secName, secScheme, pair.Value(), sec, request, pathValue) |
| 89 |
if !schemeValid { |
| 90 |
requirementSatisfied = false |
| 91 |
requirementErrors = append(requirementErrors, schemeErrors...) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// if all schemes in this requirement passed (AND), the overall security passes (OR) |
| 96 |
if requirementSatisfied { |
| 97 |
return true, nil |
| 98 |
} |
| 99 |
allErrors = append(allErrors, requirementErrors...) |
| 100 |
} |
| 101 |
|
| 102 |
return false, allErrors |
| 103 |
} |
| 104 |
|
| 105 |
// validateSecurityScheme checks if a single security scheme is satisfied by the request. |
| 106 |
func (v *paramValidator) validateSecurityScheme( |
| 107 |
secName string, |
| 108 |
secScheme *v3.SecurityScheme, |
| 109 |
scopes []string, |
| 110 |
sec *base.SecurityRequirement, |
| 111 |
request *http.Request, |
| 112 |
pathValue string, |
| 113 |
) (bool, []*errors.ValidationError) { |
| 114 |
if v.options.AuthenticationFunc != nil { |
| 115 |
return v.validateAuthenticationFunc(secName, secScheme, scopes, sec, request, pathValue) |
| 116 |
} |
| 117 |
|
| 118 |
switch strings.ToLower(secScheme.Type) { |
| 119 |
case "http": |
| 120 |
return v.validateHTTPSecurityScheme(secScheme, sec, request, pathValue) |
| 121 |
case "apikey": |
| 122 |
return v.validateAPIKeySecurityScheme(secScheme, sec, request, pathValue) |
| 123 |
} |
| 124 |
// unknown scheme type - consider it valid to avoid false negatives |
| 125 |
return true, nil |
| 126 |
} |
| 127 |
|
| 128 |
func (v *paramValidator) validateAuthenticationFunc( |
| 129 |
secName string, |
| 130 |
secScheme *v3.SecurityScheme, |
| 131 |
scopes []string, |
| 132 |
sec *base.SecurityRequirement, |
| 133 |
request *http.Request, |
| 134 |
pathValue string, |
| 135 |
) (bool, []*errors.ValidationError) { |
| 136 |
authErr := v.options.AuthenticationFunc(request.Context(), &config.AuthenticationInput{ |
| 137 |
Request: request, |
| 138 |
SecuritySchemeName: secName, |
| 139 |
SecurityScheme: secScheme, |
| 140 |
Scopes: scopes, |
| 141 |
}) |
| 142 |
if authErr == nil { |
| 143 |
return true, nil |
| 144 |
} |
| 145 |
|
| 146 |
validationErrors := []*errors.ValidationError{ |
| 147 |
{ |
| 148 |
Message: fmt.Sprintf("Authentication failed for security scheme '%s'", secName), |
| 149 |
Reason: authErr.Error(), |
| 150 |
ValidationType: helpers.SecurityValidation, |
| 151 |
ValidationSubType: secScheme.Type, |
| 152 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 153 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 154 |
HowToFix: fmt.Sprintf("Provide valid credentials for security scheme '%s'", secName), |
| 155 |
}, |
| 156 |
} |
| 157 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 158 |
return false, validationErrors |
| 159 |
} |
| 160 |
|
| 161 |
func (v *paramValidator) validateHTTPSecurityScheme( |
| 162 |
secScheme *v3.SecurityScheme, |
| 163 |
sec *base.SecurityRequirement, |
| 164 |
request *http.Request, |
| 165 |
pathValue string, |
| 166 |
) (bool, []*errors.ValidationError) { |
| 167 |
authorizationHeader := request.Header.Get("Authorization") |
| 168 |
if authorizationHeader == "" { |
| 169 |
validationErrors := []*errors.ValidationError{ |
| 170 |
{ |
| 171 |
Message: fmt.Sprintf("Authorization header for '%s' scheme", secScheme.Scheme), |
| 172 |
Reason: "Authorization header was not found", |
| 173 |
ValidationType: helpers.SecurityValidation, |
| 174 |
ValidationSubType: secScheme.Scheme, |
| 175 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 176 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 177 |
HowToFix: "Add an 'Authorization' header to this request", |
| 178 |
}, |
| 179 |
} |
| 180 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 181 |
return false, validationErrors |
| 182 |
} |
| 183 |
if len(authorizationHeader) < len(secScheme.Scheme) || !strings.EqualFold(authorizationHeader[:len(secScheme.Scheme)], secScheme.Scheme) { |
| 184 |
validationErrors := []*errors.ValidationError{ |
| 185 |
{ |
| 186 |
Message: fmt.Sprintf("Authorization header scheme '%s' mismatch", secScheme.Scheme), |
| 187 |
Reason: "Authorization header had incorrect scheme", |
| 188 |
ValidationType: helpers.SecurityValidation, |
| 189 |
ValidationSubType: secScheme.Scheme, |
| 190 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 191 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 192 |
HowToFix: fmt.Sprintf("Use the scheme '%s' in the Authorization header "+ |
| 193 |
"for this request", secScheme.Scheme), |
| 194 |
}, |
| 195 |
} |
| 196 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 197 |
return false, validationErrors |
| 198 |
} |
| 199 |
return true, nil |
| 200 |
} |
| 201 |
|
| 202 |
func (v *paramValidator) validateAPIKeySecurityScheme( |
| 203 |
secScheme *v3.SecurityScheme, |
| 204 |
sec *base.SecurityRequirement, |
| 205 |
request *http.Request, |
| 206 |
pathValue string, |
| 207 |
) (bool, []*errors.ValidationError) { |
| 208 |
switch secScheme.In { |
| 209 |
case "header": |
| 210 |
if request.Header.Get(secScheme.Name) == "" { |
| 211 |
validationErrors := []*errors.ValidationError{ |
| 212 |
{ |
| 213 |
Message: fmt.Sprintf("API Key %s not found in header", secScheme.Name), |
| 214 |
Reason: "API Key not found in http header for security scheme 'apiKey' with type 'header'", |
| 215 |
ValidationType: helpers.SecurityValidation, |
| 216 |
ValidationSubType: "apiKey", |
| 217 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 218 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 219 |
HowToFix: fmt.Sprintf("Add the API Key via '%s' as a header of the request", secScheme.Name), |
| 220 |
}, |
| 221 |
} |
| 222 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 223 |
return false, validationErrors |
| 224 |
} |
| 225 |
return true, nil |
| 226 |
|
| 227 |
case "query": |
| 228 |
if request.URL.Query().Get(secScheme.Name) == "" { |
| 229 |
copyUrl := *request.URL |
| 230 |
fixed := ©Url |
| 231 |
q := fixed.Query() |
| 232 |
q.Add(secScheme.Name, "your-api-key") |
| 233 |
fixed.RawQuery = q.Encode() |
| 234 |
|
| 235 |
validationErrors := []*errors.ValidationError{ |
| 236 |
{ |
| 237 |
Message: fmt.Sprintf("API Key %s not found in query", secScheme.Name), |
| 238 |
Reason: "API Key not found in URL query for security scheme 'apiKey' with type 'query'", |
| 239 |
ValidationType: helpers.SecurityValidation, |
| 240 |
ValidationSubType: "apiKey", |
| 241 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 242 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 243 |
HowToFix: fmt.Sprintf("Add an API Key via '%s' to the query string "+ |
| 244 |
"of the URL, for example '%s'", secScheme.Name, fixed.String()), |
| 245 |
}, |
| 246 |
} |
| 247 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 248 |
return false, validationErrors |
| 249 |
} |
| 250 |
return true, nil |
| 251 |
|
| 252 |
case "cookie": |
| 253 |
cookies := request.Cookies() |
| 254 |
for _, cookie := range cookies { |
| 255 |
if cookie.Name == secScheme.Name { |
| 256 |
return true, nil |
| 257 |
} |
| 258 |
} |
| 259 |
validationErrors := []*errors.ValidationError{ |
| 260 |
{ |
| 261 |
Message: fmt.Sprintf("API Key %s not found in cookies", secScheme.Name), |
| 262 |
Reason: "API Key not found in http request cookies for security scheme 'apiKey' with type 'cookie'", |
| 263 |
ValidationType: helpers.SecurityValidation, |
| 264 |
ValidationSubType: "apiKey", |
| 265 |
SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 266 |
SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 267 |
HowToFix: fmt.Sprintf("Submit an API Key '%s' as a cookie with the request", secScheme.Name), |
| 268 |
}, |
| 269 |
} |
| 270 |
errors.PopulateValidationErrors(validationErrors, request, pathValue) |
| 271 |
return false, validationErrors |
| 272 |
} |
| 273 |
|
| 274 |
return true, nil |
| 275 |
} |