| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package strict |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 12 |
|
| 13 |
"github.com/pb33f/libopenapi-validator/config" |
| 14 |
) |
| 15 |
|
| 16 |
// Validate performs strict validation on the input data against the schema. |
| 17 |
// This is the main entry point for body validation. |
| 18 |
// |
| 19 |
// It detects undeclared properties even when additionalProperties: true |
| 20 |
// would normally allow them. This is useful for API governance scenarios |
| 21 |
// where you want to ensure clients only send explicitly documented properties. |
| 22 |
func (v *Validator) Validate(input Input) *Result { |
| 23 |
result := &Result{Valid: true} |
| 24 |
|
| 25 |
if input.Schema == nil || input.Data == nil { |
| 26 |
return result |
| 27 |
} |
| 28 |
|
| 29 |
ctx := newTraversalContext(input.Direction, v.compiledIgnorePaths, input.BasePath) |
| 30 |
|
| 31 |
undeclared := v.validateValue(ctx, input.Schema, input.Data) |
| 32 |
|
| 33 |
if len(undeclared) > 0 { |
| 34 |
result.Valid = false |
| 35 |
result.UndeclaredValues = undeclared |
| 36 |
} |
| 37 |
|
| 38 |
return result |
| 39 |
} |
| 40 |
|
| 41 |
// ValidateBody is a convenience method for validating request/response bodies. |
| 42 |
func ValidateBody(schema *base.Schema, data any, direction Direction, options *config.ValidationOptions, version float32) *Result { |
| 43 |
v := NewValidator(options, version) |
| 44 |
return v.Validate(Input{ |
| 45 |
Schema: schema, |
| 46 |
Data: data, |
| 47 |
Direction: direction, |
| 48 |
Options: options, |
| 49 |
BasePath: "$.body", |
| 50 |
Version: version, |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
// ValidateQueryParams checks for undeclared query parameters in an HTTP request. |
| 55 |
// It compares the query parameters present in the request against those |
| 56 |
// declared in the OpenAPI operation. |
| 57 |
func ValidateQueryParams( |
| 58 |
request *http.Request, |
| 59 |
declaredParams []*v3.Parameter, |
| 60 |
options *config.ValidationOptions, |
| 61 |
) []UndeclaredValue { |
| 62 |
if request == nil || options == nil || !options.StrictMode { |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
v := NewValidator(options, 3.2) |
| 67 |
|
| 68 |
// build set of declared query params (case-sensitive) |
| 69 |
declared := make(map[string]bool) |
| 70 |
for _, param := range declaredParams { |
| 71 |
if param.In == "query" { |
| 72 |
declared[param.Name] = true |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
var undeclared []UndeclaredValue |
| 77 |
|
| 78 |
// check each query parameter in the request |
| 79 |
for paramName := range request.URL.Query() { |
| 80 |
if !declared[paramName] { |
| 81 |
// build path using proper notation for special characters |
| 82 |
path := buildPath("$.query", paramName) |
| 83 |
if v.matchesIgnorePath(path) { |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
undeclared = append(undeclared, |
| 88 |
newUndeclaredParam(path, paramName, request.URL.Query().Get(paramName), "query", getParamNames(declaredParams, "query"), DirectionRequest)) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return undeclared |
| 93 |
} |
| 94 |
|
| 95 |
// ValidateRequestHeaders checks for undeclared headers in an HTTP request. |
| 96 |
// Header names are normalized to lowercase for path generation and pattern matching. |
| 97 |
// |
| 98 |
// The securityHeaders parameter contains header names that are valid due to security |
| 99 |
// scheme definitions (e.g., "X-API-Key" for apiKey schemes, "Authorization" for |
| 100 |
// http/oauth2/openIdConnect schemes). These headers are considered "declared" even |
| 101 |
// though they don't appear in the operation's parameters array. |
| 102 |
func ValidateRequestHeaders( |
| 103 |
headers http.Header, |
| 104 |
declaredParams []*v3.Parameter, |
| 105 |
securityHeaders []string, |
| 106 |
options *config.ValidationOptions, |
| 107 |
) []UndeclaredValue { |
| 108 |
if headers == nil || options == nil || !options.StrictMode { |
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
v := NewValidator(options, 3.2) |
| 113 |
|
| 114 |
// build set of declared headers (case-insensitive) |
| 115 |
declared := make(map[string]bool) |
| 116 |
for _, param := range declaredParams { |
| 117 |
if param.In == "header" { |
| 118 |
declared[strings.ToLower(param.Name)] = true |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// add security scheme headers (case-insensitive) |
| 123 |
for _, h := range securityHeaders { |
| 124 |
declared[strings.ToLower(h)] = true |
| 125 |
} |
| 126 |
|
| 127 |
var undeclared []UndeclaredValue |
| 128 |
|
| 129 |
// check each header |
| 130 |
for headerName := range headers { |
| 131 |
lowerName := strings.ToLower(headerName) |
| 132 |
|
| 133 |
// skip if declared (via parameters or security schemes) |
| 134 |
if declared[lowerName] { |
| 135 |
continue |
| 136 |
} |
| 137 |
|
| 138 |
// skip if in ignored headers list |
| 139 |
if v.isHeaderIgnored(headerName, DirectionRequest) { |
| 140 |
continue |
| 141 |
} |
| 142 |
|
| 143 |
// build path using lowercase name for case-insensitive pattern matching |
| 144 |
path := buildPath("$.headers", lowerName) |
| 145 |
if v.matchesIgnorePath(path) { |
| 146 |
continue |
| 147 |
} |
| 148 |
|
| 149 |
undeclared = append(undeclared, |
| 150 |
newUndeclaredParam(path, headerName, headers.Get(headerName), "header", getParamNames(declaredParams, "header"), DirectionRequest)) |
| 151 |
} |
| 152 |
|
| 153 |
return undeclared |
| 154 |
} |
| 155 |
|
| 156 |
// ValidateCookies checks for undeclared cookies in an HTTP request. |
| 157 |
func ValidateCookies( |
| 158 |
request *http.Request, |
| 159 |
declaredParams []*v3.Parameter, |
| 160 |
options *config.ValidationOptions, |
| 161 |
) []UndeclaredValue { |
| 162 |
if request == nil || options == nil || !options.StrictMode { |
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
v := NewValidator(options, 3.2) |
| 167 |
|
| 168 |
// build set of declared cookies |
| 169 |
declared := make(map[string]bool) |
| 170 |
for _, param := range declaredParams { |
| 171 |
if param.In == "cookie" { |
| 172 |
declared[param.Name] = true |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var undeclared []UndeclaredValue |
| 177 |
|
| 178 |
// check each cookie in the request |
| 179 |
for _, cookie := range request.Cookies() { |
| 180 |
if !declared[cookie.Name] { |
| 181 |
// build path using proper notation for special characters |
| 182 |
path := buildPath("$.cookies", cookie.Name) |
| 183 |
if v.matchesIgnorePath(path) { |
| 184 |
continue |
| 185 |
} |
| 186 |
|
| 187 |
undeclared = append(undeclared, |
| 188 |
newUndeclaredParam(path, cookie.Name, cookie.Value, "cookie", getParamNames(declaredParams, "cookie"), DirectionRequest)) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return undeclared |
| 193 |
} |
| 194 |
|
| 195 |
// getParamNames extracts parameter names of a specific type. |
| 196 |
func getParamNames(params []*v3.Parameter, paramType string) []string { |
| 197 |
var names []string |
| 198 |
for _, param := range params { |
| 199 |
if param.In == paramType { |
| 200 |
names = append(names, param.Name) |
| 201 |
} |
| 202 |
} |
| 203 |
return names |
| 204 |
} |
| 205 |
|
| 206 |
// ValidateResponseHeaders checks for undeclared headers in an HTTP response. |
| 207 |
// Uses the declared headers from the OpenAPI response object. |
| 208 |
// Header names are normalized to lowercase for path generation and pattern matching. |
| 209 |
func ValidateResponseHeaders( |
| 210 |
headers http.Header, |
| 211 |
declaredHeaders *map[string]*v3.Header, |
| 212 |
options *config.ValidationOptions, |
| 213 |
) []UndeclaredValue { |
| 214 |
if headers == nil || options == nil || !options.StrictMode { |
| 215 |
return nil |
| 216 |
} |
| 217 |
|
| 218 |
v := NewValidator(options, 3.2) |
| 219 |
|
| 220 |
// build set of declared headers (case-insensitive) |
| 221 |
declared := make(map[string]bool) |
| 222 |
if declaredHeaders != nil { |
| 223 |
for name := range *declaredHeaders { |
| 224 |
declared[strings.ToLower(name)] = true |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
var undeclared []UndeclaredValue |
| 229 |
var declaredNames []string |
| 230 |
if declaredHeaders != nil { |
| 231 |
for name := range *declaredHeaders { |
| 232 |
declaredNames = append(declaredNames, name) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
for headerName := range headers { |
| 237 |
lowerName := strings.ToLower(headerName) |
| 238 |
|
| 239 |
if declared[lowerName] { |
| 240 |
continue |
| 241 |
} |
| 242 |
|
| 243 |
if v.isHeaderIgnored(headerName, DirectionResponse) { |
| 244 |
continue |
| 245 |
} |
| 246 |
|
| 247 |
// build path using lowercase name for case-insensitive pattern matching |
| 248 |
path := buildPath("$.headers", lowerName) |
| 249 |
if v.matchesIgnorePath(path) { |
| 250 |
continue |
| 251 |
} |
| 252 |
|
| 253 |
undeclared = append(undeclared, |
| 254 |
newUndeclaredParam(path, headerName, headers.Get(headerName), "header", declaredNames, DirectionResponse)) |
| 255 |
} |
| 256 |
|
| 257 |
return undeclared |
| 258 |
} |