| 1 |
// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package parameters |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
stderrors "errors" |
| 9 |
"net/http" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/pb33f/libopenapi" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
|
| 16 |
"github.com/pb33f/libopenapi-validator/config" |
| 17 |
"github.com/pb33f/libopenapi-validator/paths" |
| 18 |
) |
| 19 |
|
| 20 |
func TestParamValidator_ValidateSecurity_APIKeyHeader_NotFound(t *testing.T) { |
| 21 |
spec := `openapi: 3.1.0 |
| 22 |
paths: |
| 23 |
/products: |
| 24 |
post: |
| 25 |
security: |
| 26 |
- ApiKeyAuth: |
| 27 |
- write:products |
| 28 |
components: |
| 29 |
securitySchemes: |
| 30 |
ApiKeyAuth: |
| 31 |
type: apiKey |
| 32 |
in: header |
| 33 |
name: X-API-Key |
| 34 |
` |
| 35 |
|
| 36 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 37 |
|
| 38 |
m, _ := doc.BuildV3Model() |
| 39 |
|
| 40 |
v := NewParameterValidator(&m.Model) |
| 41 |
|
| 42 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 43 |
|
| 44 |
valid, errors := v.ValidateSecurity(request) |
| 45 |
assert.False(t, valid) |
| 46 |
assert.Equal(t, 1, len(errors)) |
| 47 |
assert.Equal(t, "API Key X-API-Key not found in header", errors[0].Message) |
| 48 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 49 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 50 |
assert.Equal(t, "/products", errors[0].SpecPath) |
| 51 |
} |
| 52 |
|
| 53 |
func TestParamValidator_ValidateSecurity_APIKeyHeader(t *testing.T) { |
| 54 |
spec := `openapi: 3.1.0 |
| 55 |
paths: |
| 56 |
/products: |
| 57 |
post: |
| 58 |
security: |
| 59 |
- ApiKeyAuth: |
| 60 |
- write:products |
| 61 |
components: |
| 62 |
securitySchemes: |
| 63 |
ApiKeyAuth: |
| 64 |
type: apiKey |
| 65 |
in: header |
| 66 |
name: X-API-Key |
| 67 |
` |
| 68 |
|
| 69 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 70 |
|
| 71 |
m, _ := doc.BuildV3Model() |
| 72 |
|
| 73 |
v := NewParameterValidator(&m.Model) |
| 74 |
|
| 75 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 76 |
request.Header.Add("X-API-Key", "1234") |
| 77 |
|
| 78 |
valid, errors := v.ValidateSecurity(request) |
| 79 |
assert.True(t, valid) |
| 80 |
assert.Equal(t, 0, len(errors)) |
| 81 |
} |
| 82 |
|
| 83 |
func TestParamValidator_ValidateSecurity_APIKeyQuery_NotFound(t *testing.T) { |
| 84 |
spec := `openapi: 3.1.0 |
| 85 |
paths: |
| 86 |
/products: |
| 87 |
post: |
| 88 |
security: |
| 89 |
- ApiKeyAuth: |
| 90 |
- write:products |
| 91 |
components: |
| 92 |
securitySchemes: |
| 93 |
ApiKeyAuth: |
| 94 |
type: apiKey |
| 95 |
in: query |
| 96 |
name: X-API-Key |
| 97 |
` |
| 98 |
|
| 99 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 100 |
|
| 101 |
m, _ := doc.BuildV3Model() |
| 102 |
|
| 103 |
v := NewParameterValidator(&m.Model) |
| 104 |
|
| 105 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 106 |
|
| 107 |
valid, errors := v.ValidateSecurity(request) |
| 108 |
assert.False(t, valid) |
| 109 |
assert.Equal(t, 1, len(errors)) |
| 110 |
assert.Equal(t, "API Key X-API-Key not found in query", errors[0].Message) |
| 111 |
assert.Equal(t, "Add an API Key via 'X-API-Key' to the query string of the URL, "+ |
| 112 |
"for example 'https://things.com/products?X-API-Key=your-api-key'", errors[0].HowToFix) |
| 113 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 114 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 115 |
assert.Equal(t, "/products", errors[0].SpecPath) |
| 116 |
} |
| 117 |
|
| 118 |
func TestParamValidator_ValidateSecurity_APIKeyQuery(t *testing.T) { |
| 119 |
spec := `openapi: 3.1.0 |
| 120 |
paths: |
| 121 |
/products: |
| 122 |
post: |
| 123 |
security: |
| 124 |
- ApiKeyAuth: |
| 125 |
- write:products |
| 126 |
components: |
| 127 |
securitySchemes: |
| 128 |
ApiKeyAuth: |
| 129 |
type: apiKey |
| 130 |
in: query |
| 131 |
name: X-API-Key |
| 132 |
` |
| 133 |
|
| 134 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 135 |
|
| 136 |
m, _ := doc.BuildV3Model() |
| 137 |
|
| 138 |
v := NewParameterValidator(&m.Model) |
| 139 |
|
| 140 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products?X-API-Key=12345", nil) |
| 141 |
|
| 142 |
valid, errors := v.ValidateSecurity(request) |
| 143 |
assert.True(t, valid) |
| 144 |
assert.Equal(t, 0, len(errors)) |
| 145 |
} |
| 146 |
|
| 147 |
func TestParamValidator_ValidateSecurity_APIKeyCookie_NotFound(t *testing.T) { |
| 148 |
spec := `openapi: 3.1.0 |
| 149 |
paths: |
| 150 |
/products: |
| 151 |
post: |
| 152 |
security: |
| 153 |
- ApiKeyAuth: |
| 154 |
- write:products |
| 155 |
components: |
| 156 |
securitySchemes: |
| 157 |
ApiKeyAuth: |
| 158 |
type: apiKey |
| 159 |
in: cookie |
| 160 |
name: X-API-Key |
| 161 |
` |
| 162 |
|
| 163 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 164 |
|
| 165 |
m, _ := doc.BuildV3Model() |
| 166 |
|
| 167 |
v := NewParameterValidator(&m.Model) |
| 168 |
|
| 169 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 170 |
|
| 171 |
valid, errors := v.ValidateSecurity(request) |
| 172 |
assert.False(t, valid) |
| 173 |
assert.Equal(t, 1, len(errors)) |
| 174 |
assert.Equal(t, "API Key X-API-Key not found in cookies", errors[0].Message) |
| 175 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 176 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 177 |
assert.Equal(t, "/products", errors[0].SpecPath) |
| 178 |
} |
| 179 |
|
| 180 |
func TestParamValidator_ValidateSecurity_APIKeyCookie(t *testing.T) { |
| 181 |
spec := `openapi: 3.1.0 |
| 182 |
paths: |
| 183 |
/products: |
| 184 |
post: |
| 185 |
security: |
| 186 |
- ApiKeyAuth: |
| 187 |
- write:products |
| 188 |
components: |
| 189 |
securitySchemes: |
| 190 |
ApiKeyAuth: |
| 191 |
type: apiKey |
| 192 |
in: cookie |
| 193 |
name: X-API-Key |
| 194 |
` |
| 195 |
|
| 196 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 197 |
|
| 198 |
m, _ := doc.BuildV3Model() |
| 199 |
|
| 200 |
v := NewParameterValidator(&m.Model) |
| 201 |
|
| 202 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 203 |
|
| 204 |
request.AddCookie(&http.Cookie{ |
| 205 |
Name: "X-API-Key", |
| 206 |
Value: "1234", |
| 207 |
}) |
| 208 |
|
| 209 |
valid, errors := v.ValidateSecurity(request) |
| 210 |
assert.True(t, valid) |
| 211 |
assert.Equal(t, 0, len(errors)) |
| 212 |
} |
| 213 |
|
| 214 |
func TestParamValidator_ValidateSecurity_Basic_NotFound(t *testing.T) { |
| 215 |
spec := `openapi: 3.1.0 |
| 216 |
paths: |
| 217 |
/products: |
| 218 |
post: |
| 219 |
security: |
| 220 |
- ApiKeyAuth: |
| 221 |
- write:products |
| 222 |
components: |
| 223 |
securitySchemes: |
| 224 |
ApiKeyAuth: |
| 225 |
type: http |
| 226 |
scheme: basic |
| 227 |
` |
| 228 |
|
| 229 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 230 |
|
| 231 |
m, _ := doc.BuildV3Model() |
| 232 |
|
| 233 |
v := NewParameterValidator(&m.Model) |
| 234 |
|
| 235 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 236 |
|
| 237 |
valid, errors := v.ValidateSecurity(request) |
| 238 |
assert.False(t, valid) |
| 239 |
assert.Equal(t, 1, len(errors)) |
| 240 |
assert.Equal(t, "Authorization header for 'basic' scheme", errors[0].Message) |
| 241 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 242 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 243 |
assert.Equal(t, "/products", errors[0].SpecPath) |
| 244 |
} |
| 245 |
|
| 246 |
func TestParamValidator_ValidateSecurity_Basic(t *testing.T) { |
| 247 |
spec := `openapi: 3.1.0 |
| 248 |
paths: |
| 249 |
/products: |
| 250 |
post: |
| 251 |
security: |
| 252 |
- ApiKeyAuth: |
| 253 |
- write:products |
| 254 |
components: |
| 255 |
securitySchemes: |
| 256 |
ApiKeyAuth: |
| 257 |
type: http |
| 258 |
scheme: basic |
| 259 |
` |
| 260 |
|
| 261 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 262 |
|
| 263 |
m, _ := doc.BuildV3Model() |
| 264 |
|
| 265 |
v := NewParameterValidator(&m.Model) |
| 266 |
|
| 267 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 268 |
request.Header.Add("Authorization", "Basic 1234") |
| 269 |
|
| 270 |
valid, errors := v.ValidateSecurity(request) |
| 271 |
assert.True(t, valid) |
| 272 |
assert.Equal(t, 0, len(errors)) |
| 273 |
} |
| 274 |
|
| 275 |
func TestParamValidator_ValidateSecurity_BadPath(t *testing.T) { |
| 276 |
spec := `openapi: 3.1.0 |
| 277 |
paths: |
| 278 |
/products: |
| 279 |
post: |
| 280 |
security: |
| 281 |
- ApiKeyAuth: |
| 282 |
- write:products |
| 283 |
components: |
| 284 |
securitySchemes: |
| 285 |
ApiKeyAuth: |
| 286 |
type: http |
| 287 |
scheme: basic |
| 288 |
` |
| 289 |
|
| 290 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 291 |
|
| 292 |
m, _ := doc.BuildV3Model() |
| 293 |
|
| 294 |
v := NewParameterValidator(&m.Model) |
| 295 |
|
| 296 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/blimpo", nil) |
| 297 |
valid, errors := v.ValidateSecurity(request) |
| 298 |
assert.False(t, valid) |
| 299 |
assert.Equal(t, 1, len(errors)) |
| 300 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 301 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 302 |
assert.Equal(t, "", errors[0].SpecPath) |
| 303 |
} |
| 304 |
|
| 305 |
func TestParamValidator_ValidateSecurity_MissingSecuritySchemes(t *testing.T) { |
| 306 |
spec := `openapi: 3.1.0 |
| 307 |
paths: |
| 308 |
/products: |
| 309 |
post: |
| 310 |
security: |
| 311 |
- ApiKeyAuth: |
| 312 |
- write:products |
| 313 |
components: {} |
| 314 |
` |
| 315 |
|
| 316 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 317 |
|
| 318 |
m, _ := doc.BuildV3Model() |
| 319 |
|
| 320 |
v := NewParameterValidator(&m.Model) |
| 321 |
|
| 322 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 323 |
valid, errors := v.ValidateSecurity(request) |
| 324 |
assert.False(t, valid) |
| 325 |
assert.Equal(t, 1, len(errors)) |
| 326 |
} |
| 327 |
|
| 328 |
func TestParamValidator_ValidateSecurity_NoComponents(t *testing.T) { |
| 329 |
spec := `openapi: 3.1.0 |
| 330 |
paths: |
| 331 |
/products: |
| 332 |
post: |
| 333 |
security: |
| 334 |
- ApiKeyAuth: |
| 335 |
- write:products |
| 336 |
` |
| 337 |
|
| 338 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 339 |
|
| 340 |
m, _ := doc.BuildV3Model() |
| 341 |
|
| 342 |
v := NewParameterValidator(&m.Model) |
| 343 |
|
| 344 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 345 |
valid, errors := v.ValidateSecurity(request) |
| 346 |
assert.False(t, valid) |
| 347 |
assert.Equal(t, 1, len(errors)) |
| 348 |
} |
| 349 |
|
| 350 |
func TestParamValidator_ValidateSecurity_PresetPath(t *testing.T) { |
| 351 |
spec := `openapi: 3.1.0 |
| 352 |
paths: |
| 353 |
/products: |
| 354 |
post: |
| 355 |
` |
| 356 |
|
| 357 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 358 |
|
| 359 |
m, _ := doc.BuildV3Model() |
| 360 |
|
| 361 |
v := NewParameterValidator(&m.Model) |
| 362 |
|
| 363 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 364 |
pathItem, errs, pv := paths.FindPath(request, &m.Model, nil) |
| 365 |
assert.Nil(t, errs) |
| 366 |
|
| 367 |
valid, errors := v.ValidateSecurityWithPathItem(request, pathItem, pv) |
| 368 |
assert.True(t, valid) |
| 369 |
assert.Equal(t, 0, len(errors)) |
| 370 |
} |
| 371 |
|
| 372 |
func TestParamValidator_ValidateSecurity_PresetPath_notfound(t *testing.T) { |
| 373 |
spec := `openapi: 3.1.0 |
| 374 |
paths: |
| 375 |
/products: |
| 376 |
post: |
| 377 |
` |
| 378 |
|
| 379 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 380 |
|
| 381 |
m, _ := doc.BuildV3Model() |
| 382 |
|
| 383 |
v := NewParameterValidator(&m.Model) |
| 384 |
|
| 385 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/beef", nil) |
| 386 |
pathItem, _, pv := paths.FindPath(request, &m.Model, &config.ValidationOptions{RegexCache: &sync.Map{}}) |
| 387 |
|
| 388 |
valid, errors := v.ValidateSecurityWithPathItem(request, pathItem, pv) |
| 389 |
assert.False(t, valid) |
| 390 |
assert.Len(t, errors, 1) |
| 391 |
assert.Equal(t, "POST Path '/beef' not found", errors[0].Message) |
| 392 |
} |
| 393 |
|
| 394 |
func TestParamValidator_ValidateSecurity_MultipleSecurity(t *testing.T) { |
| 395 |
spec := `openapi: 3.1.0 |
| 396 |
paths: |
| 397 |
/products: |
| 398 |
post: |
| 399 |
security: |
| 400 |
- ApiKeyAuthQuery: |
| 401 |
- write:products |
| 402 |
- ApiKeyAuthHeader: |
| 403 |
- write:products |
| 404 |
components: |
| 405 |
securitySchemes: |
| 406 |
ApiKeyAuthQuery: |
| 407 |
type: apiKey |
| 408 |
in: query |
| 409 |
name: X-API-Key |
| 410 |
ApiKeyAuthHeader: |
| 411 |
type: apiKey |
| 412 |
in: header |
| 413 |
name: X-API-Key |
| 414 |
` |
| 415 |
|
| 416 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 417 |
|
| 418 |
m, _ := doc.BuildV3Model() |
| 419 |
|
| 420 |
v := NewParameterValidator(&m.Model) |
| 421 |
|
| 422 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 423 |
request.Header.Add("X-API-Key", "1234") |
| 424 |
|
| 425 |
valid, errors := v.ValidateSecurity(request) |
| 426 |
assert.True(t, valid) |
| 427 |
assert.Equal(t, 0, len(errors)) |
| 428 |
} |
| 429 |
|
| 430 |
func TestParamValidator_ValidateSecurity_MultipleSecurity_EmptyOption(t *testing.T) { |
| 431 |
spec := `openapi: 3.1.0 |
| 432 |
paths: |
| 433 |
/products: |
| 434 |
post: |
| 435 |
security: |
| 436 |
- ApiKeyAuth: |
| 437 |
- write:products |
| 438 |
- {} |
| 439 |
components: |
| 440 |
securitySchemes: |
| 441 |
ApiKeyAuth: |
| 442 |
type: apiKey |
| 443 |
in: header |
| 444 |
name: X-API-Key |
| 445 |
` |
| 446 |
|
| 447 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 448 |
|
| 449 |
m, _ := doc.BuildV3Model() |
| 450 |
|
| 451 |
v := NewParameterValidator(&m.Model) |
| 452 |
|
| 453 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 454 |
|
| 455 |
valid, errors := v.ValidateSecurity(request) |
| 456 |
assert.True(t, valid) |
| 457 |
assert.Equal(t, 0, len(errors)) |
| 458 |
} |
| 459 |
|
| 460 |
func TestParamValidator_ValidateSecurity_MultipleSecurity_NotFound(t *testing.T) { |
| 461 |
spec := `openapi: 3.1.0 |
| 462 |
paths: |
| 463 |
/products: |
| 464 |
post: |
| 465 |
security: |
| 466 |
- ApiKeyAuthQuery: |
| 467 |
- write:products |
| 468 |
- ApiKeyAuthHeader: |
| 469 |
- write:products |
| 470 |
components: |
| 471 |
securitySchemes: |
| 472 |
ApiKeyAuthQuery: |
| 473 |
type: apiKey |
| 474 |
in: query |
| 475 |
name: X-API-Key |
| 476 |
ApiKeyAuthHeader: |
| 477 |
type: apiKey |
| 478 |
in: header |
| 479 |
name: X-API-Key |
| 480 |
` |
| 481 |
|
| 482 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 483 |
|
| 484 |
m, _ := doc.BuildV3Model() |
| 485 |
|
| 486 |
v := NewParameterValidator(&m.Model) |
| 487 |
|
| 488 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 489 |
|
| 490 |
valid, errors := v.ValidateSecurity(request) |
| 491 |
assert.False(t, valid) |
| 492 |
assert.Equal(t, 2, len(errors)) |
| 493 |
|
| 494 |
assert.Equal(t, "API Key X-API-Key not found in query", errors[0].Message) |
| 495 |
assert.Equal(t, "Add an API Key via 'X-API-Key' to the query string of the URL, "+ |
| 496 |
"for example 'https://things.com/products?X-API-Key=your-api-key'", errors[0].HowToFix) |
| 497 |
assert.Equal(t, request.Method, errors[0].RequestMethod) |
| 498 |
assert.Equal(t, request.URL.Path, errors[0].RequestPath) |
| 499 |
assert.Equal(t, "/products", errors[0].SpecPath) |
| 500 |
|
| 501 |
assert.Equal(t, "API Key X-API-Key not found in header", errors[1].Message) |
| 502 |
assert.Equal(t, request.Method, errors[1].RequestMethod) |
| 503 |
assert.Equal(t, request.URL.Path, errors[1].RequestPath) |
| 504 |
assert.Equal(t, "/products", errors[1].SpecPath) |
| 505 |
} |
| 506 |
|
| 507 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_APIKeyHeader(t *testing.T) { |
| 508 |
spec := `openapi: 3.1.0 |
| 509 |
paths: |
| 510 |
/products: |
| 511 |
post: |
| 512 |
security: |
| 513 |
- ApiKeyAuth: |
| 514 |
- write:products |
| 515 |
components: |
| 516 |
securitySchemes: |
| 517 |
ApiKeyAuth: |
| 518 |
type: apiKey |
| 519 |
in: header |
| 520 |
name: X-API-Key |
| 521 |
` |
| 522 |
|
| 523 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 524 |
|
| 525 |
m, _ := doc.BuildV3Model() |
| 526 |
|
| 527 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 528 |
|
| 529 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 530 |
// No API key header provided |
| 531 |
|
| 532 |
valid, errors := v.ValidateSecurity(request) |
| 533 |
assert.True(t, valid) |
| 534 |
assert.Equal(t, 0, len(errors)) |
| 535 |
} |
| 536 |
|
| 537 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_APIKeyQuery(t *testing.T) { |
| 538 |
spec := `openapi: 3.1.0 |
| 539 |
paths: |
| 540 |
/products: |
| 541 |
post: |
| 542 |
security: |
| 543 |
- ApiKeyAuth: |
| 544 |
- write:products |
| 545 |
components: |
| 546 |
securitySchemes: |
| 547 |
ApiKeyAuth: |
| 548 |
type: apiKey |
| 549 |
in: query |
| 550 |
name: X-API-Key |
| 551 |
` |
| 552 |
|
| 553 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 554 |
|
| 555 |
m, _ := doc.BuildV3Model() |
| 556 |
|
| 557 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 558 |
|
| 559 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 560 |
// No API key query param provided |
| 561 |
|
| 562 |
valid, errors := v.ValidateSecurity(request) |
| 563 |
assert.True(t, valid) |
| 564 |
assert.Equal(t, 0, len(errors)) |
| 565 |
} |
| 566 |
|
| 567 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_APIKeyCookie(t *testing.T) { |
| 568 |
spec := `openapi: 3.1.0 |
| 569 |
paths: |
| 570 |
/products: |
| 571 |
post: |
| 572 |
security: |
| 573 |
- ApiKeyAuth: |
| 574 |
- write:products |
| 575 |
components: |
| 576 |
securitySchemes: |
| 577 |
ApiKeyAuth: |
| 578 |
type: apiKey |
| 579 |
in: cookie |
| 580 |
name: X-API-Key |
| 581 |
` |
| 582 |
|
| 583 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 584 |
|
| 585 |
m, _ := doc.BuildV3Model() |
| 586 |
|
| 587 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 588 |
|
| 589 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 590 |
// No API key cookie provided |
| 591 |
|
| 592 |
valid, errors := v.ValidateSecurity(request) |
| 593 |
assert.True(t, valid) |
| 594 |
assert.Equal(t, 0, len(errors)) |
| 595 |
} |
| 596 |
|
| 597 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_BasicAuth(t *testing.T) { |
| 598 |
spec := `openapi: 3.1.0 |
| 599 |
paths: |
| 600 |
/products: |
| 601 |
post: |
| 602 |
security: |
| 603 |
- ApiKeyAuth: |
| 604 |
- write:products |
| 605 |
components: |
| 606 |
securitySchemes: |
| 607 |
ApiKeyAuth: |
| 608 |
type: http |
| 609 |
scheme: basic |
| 610 |
` |
| 611 |
|
| 612 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 613 |
|
| 614 |
m, _ := doc.BuildV3Model() |
| 615 |
|
| 616 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 617 |
|
| 618 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 619 |
// No Authorization header provided |
| 620 |
|
| 621 |
valid, errors := v.ValidateSecurity(request) |
| 622 |
assert.True(t, valid) |
| 623 |
assert.Equal(t, 0, len(errors)) |
| 624 |
} |
| 625 |
|
| 626 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_WithPathItem(t *testing.T) { |
| 627 |
spec := `openapi: 3.1.0 |
| 628 |
paths: |
| 629 |
/products: |
| 630 |
post: |
| 631 |
security: |
| 632 |
- ApiKeyAuth: |
| 633 |
- write:products |
| 634 |
components: |
| 635 |
securitySchemes: |
| 636 |
ApiKeyAuth: |
| 637 |
type: apiKey |
| 638 |
in: header |
| 639 |
name: X-API-Key |
| 640 |
` |
| 641 |
|
| 642 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 643 |
|
| 644 |
m, _ := doc.BuildV3Model() |
| 645 |
|
| 646 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 647 |
|
| 648 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 649 |
pathItem, errs, pv := paths.FindPath(request, &m.Model, &config.ValidationOptions{RegexCache: &sync.Map{}}) |
| 650 |
assert.Nil(t, errs) |
| 651 |
|
| 652 |
valid, errors := v.ValidateSecurityWithPathItem(request, pathItem, pv) |
| 653 |
assert.True(t, valid) |
| 654 |
assert.Equal(t, 0, len(errors)) |
| 655 |
} |
| 656 |
|
| 657 |
func TestParamValidator_ValidateSecurity_SecurityValidationDisabled_MissingPath(t *testing.T) { |
| 658 |
spec := `openapi: 3.1.0 |
| 659 |
paths: |
| 660 |
/products: |
| 661 |
post: |
| 662 |
security: |
| 663 |
- ApiKeyAuth: |
| 664 |
- write:products |
| 665 |
components: |
| 666 |
securitySchemes: |
| 667 |
ApiKeyAuth: |
| 668 |
type: apiKey |
| 669 |
in: header |
| 670 |
name: X-API-Key |
| 671 |
` |
| 672 |
|
| 673 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 674 |
|
| 675 |
m, _ := doc.BuildV3Model() |
| 676 |
|
| 677 |
v := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 678 |
|
| 679 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/nonexistent", nil) |
| 680 |
|
| 681 |
valid, errors := v.ValidateSecurity(request) |
| 682 |
assert.False(t, valid) // Should still fail for invalid paths |
| 683 |
assert.Equal(t, 1, len(errors)) |
| 684 |
assert.Contains(t, errors[0].Message, "Path '/nonexistent' not found") |
| 685 |
} |
| 686 |
|
| 687 |
func TestParamValidator_ValidateSecurity_SecurityValidationEnabled_vs_Disabled(t *testing.T) { |
| 688 |
spec := `openapi: 3.1.0 |
| 689 |
paths: |
| 690 |
/products: |
| 691 |
post: |
| 692 |
security: |
| 693 |
- ApiKeyAuth: |
| 694 |
- write:products |
| 695 |
components: |
| 696 |
securitySchemes: |
| 697 |
ApiKeyAuth: |
| 698 |
type: apiKey |
| 699 |
in: header |
| 700 |
name: X-API-Key |
| 701 |
` |
| 702 |
|
| 703 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 704 |
m, _ := doc.BuildV3Model() |
| 705 |
|
| 706 |
// Test with security validation enabled (default) |
| 707 |
vEnabled := NewParameterValidator(&m.Model) |
| 708 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 709 |
|
| 710 |
valid, errors := vEnabled.ValidateSecurity(request) |
| 711 |
assert.False(t, valid) |
| 712 |
assert.Equal(t, 1, len(errors)) |
| 713 |
assert.Equal(t, "API Key X-API-Key not found in header", errors[0].Message) |
| 714 |
|
| 715 |
// Test with security validation disabled |
| 716 |
vDisabled := NewParameterValidator(&m.Model, config.WithoutSecurityValidation()) |
| 717 |
|
| 718 |
valid, errors = vDisabled.ValidateSecurity(request) |
| 719 |
assert.True(t, valid) |
| 720 |
assert.Equal(t, 0, len(errors)) |
| 721 |
} |
| 722 |
|
| 723 |
func TestParamValidator_ValidateSecurity_ANDRequirement_BothPresent(t *testing.T) { |
| 724 |
// Test AND security requirement: both schemes in same requirement must pass |
| 725 |
spec := `openapi: 3.1.0 |
| 726 |
paths: |
| 727 |
/products: |
| 728 |
post: |
| 729 |
security: |
| 730 |
- ApiKeyAuthHeader: [] |
| 731 |
BasicAuth: [] |
| 732 |
components: |
| 733 |
securitySchemes: |
| 734 |
ApiKeyAuthHeader: |
| 735 |
type: apiKey |
| 736 |
in: header |
| 737 |
name: X-API-Key |
| 738 |
BasicAuth: |
| 739 |
type: http |
| 740 |
scheme: basic |
| 741 |
` |
| 742 |
|
| 743 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 744 |
m, _ := doc.BuildV3Model() |
| 745 |
v := NewParameterValidator(&m.Model) |
| 746 |
|
| 747 |
// Request with BOTH api key AND authorization header - should pass |
| 748 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 749 |
request.Header.Add("X-API-Key", "1234") |
| 750 |
request.Header.Add("Authorization", "Basic dXNlcjpwYXNz") |
| 751 |
|
| 752 |
valid, errors := v.ValidateSecurity(request) |
| 753 |
assert.True(t, valid) |
| 754 |
assert.Empty(t, errors) |
| 755 |
} |
| 756 |
|
| 757 |
func TestParamValidator_ValidateSecurity_ANDRequirement_OnlyApiKey(t *testing.T) { |
| 758 |
// Test AND security requirement: missing one scheme should fail |
| 759 |
spec := `openapi: 3.1.0 |
| 760 |
paths: |
| 761 |
/products: |
| 762 |
post: |
| 763 |
security: |
| 764 |
- ApiKeyAuthHeader: [] |
| 765 |
BasicAuth: [] |
| 766 |
components: |
| 767 |
securitySchemes: |
| 768 |
ApiKeyAuthHeader: |
| 769 |
type: apiKey |
| 770 |
in: header |
| 771 |
name: X-API-Key |
| 772 |
BasicAuth: |
| 773 |
type: http |
| 774 |
scheme: basic |
| 775 |
` |
| 776 |
|
| 777 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 778 |
m, _ := doc.BuildV3Model() |
| 779 |
v := NewParameterValidator(&m.Model) |
| 780 |
|
| 781 |
// Request with ONLY api key - should fail because BasicAuth is also required |
| 782 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 783 |
request.Header.Add("X-API-Key", "1234") |
| 784 |
|
| 785 |
valid, errors := v.ValidateSecurity(request) |
| 786 |
assert.False(t, valid) |
| 787 |
assert.Len(t, errors, 1) |
| 788 |
assert.Contains(t, errors[0].Message, "Authorization header") |
| 789 |
} |
| 790 |
|
| 791 |
func TestParamValidator_ValidateSecurity_ANDRequirement_OnlyBasicAuth(t *testing.T) { |
| 792 |
// Test AND security requirement: missing one scheme should fail |
| 793 |
spec := `openapi: 3.1.0 |
| 794 |
paths: |
| 795 |
/products: |
| 796 |
post: |
| 797 |
security: |
| 798 |
- ApiKeyAuthHeader: [] |
| 799 |
BasicAuth: [] |
| 800 |
components: |
| 801 |
securitySchemes: |
| 802 |
ApiKeyAuthHeader: |
| 803 |
type: apiKey |
| 804 |
in: header |
| 805 |
name: X-API-Key |
| 806 |
BasicAuth: |
| 807 |
type: http |
| 808 |
scheme: basic |
| 809 |
` |
| 810 |
|
| 811 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 812 |
m, _ := doc.BuildV3Model() |
| 813 |
v := NewParameterValidator(&m.Model) |
| 814 |
|
| 815 |
// Request with ONLY authorization header - should fail because ApiKeyAuthHeader is also required |
| 816 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 817 |
request.Header.Add("Authorization", "Basic dXNlcjpwYXNz") |
| 818 |
|
| 819 |
valid, errors := v.ValidateSecurity(request) |
| 820 |
assert.False(t, valid) |
| 821 |
assert.Len(t, errors, 1) |
| 822 |
assert.Contains(t, errors[0].Message, "API Key") |
| 823 |
} |
| 824 |
|
| 825 |
func TestParamValidator_ValidateSecurity_ANDRequirement_NeitherPresent(t *testing.T) { |
| 826 |
// Test AND security requirement: missing both schemes should return errors for both |
| 827 |
spec := `openapi: 3.1.0 |
| 828 |
paths: |
| 829 |
/products: |
| 830 |
post: |
| 831 |
security: |
| 832 |
- ApiKeyAuthHeader: [] |
| 833 |
BasicAuth: [] |
| 834 |
components: |
| 835 |
securitySchemes: |
| 836 |
ApiKeyAuthHeader: |
| 837 |
type: apiKey |
| 838 |
in: header |
| 839 |
name: X-API-Key |
| 840 |
BasicAuth: |
| 841 |
type: http |
| 842 |
scheme: basic |
| 843 |
` |
| 844 |
|
| 845 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 846 |
m, _ := doc.BuildV3Model() |
| 847 |
v := NewParameterValidator(&m.Model) |
| 848 |
|
| 849 |
// Request with neither - should fail with errors for both |
| 850 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 851 |
|
| 852 |
valid, errors := v.ValidateSecurity(request) |
| 853 |
assert.False(t, valid) |
| 854 |
assert.Len(t, errors, 2) |
| 855 |
} |
| 856 |
|
| 857 |
func TestParamValidator_ValidateSecurity_ORWithAND_FirstOROptionPasses(t *testing.T) { |
| 858 |
// Test mixed OR and AND: first option is single scheme, second is AND requirement |
| 859 |
spec := `openapi: 3.1.0 |
| 860 |
paths: |
| 861 |
/products: |
| 862 |
post: |
| 863 |
security: |
| 864 |
- ApiKeyAuthHeader: [] |
| 865 |
- BasicAuth: [] |
| 866 |
BearerAuth: [] |
| 867 |
components: |
| 868 |
securitySchemes: |
| 869 |
ApiKeyAuthHeader: |
| 870 |
type: apiKey |
| 871 |
in: header |
| 872 |
name: X-API-Key |
| 873 |
BasicAuth: |
| 874 |
type: http |
| 875 |
scheme: basic |
| 876 |
BearerAuth: |
| 877 |
type: http |
| 878 |
scheme: bearer |
| 879 |
` |
| 880 |
|
| 881 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 882 |
m, _ := doc.BuildV3Model() |
| 883 |
v := NewParameterValidator(&m.Model) |
| 884 |
|
| 885 |
// Request with only API key - should pass (first OR option) |
| 886 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 887 |
request.Header.Add("X-API-Key", "1234") |
| 888 |
|
| 889 |
valid, errors := v.ValidateSecurity(request) |
| 890 |
assert.True(t, valid) |
| 891 |
assert.Empty(t, errors) |
| 892 |
} |
| 893 |
|
| 894 |
func TestParamValidator_ValidateSecurity_ORWithAND_SecondOROptionPasses(t *testing.T) { |
| 895 |
// Test mixed OR and AND: second option (AND requirement) passes |
| 896 |
spec := `openapi: 3.1.0 |
| 897 |
paths: |
| 898 |
/products: |
| 899 |
post: |
| 900 |
security: |
| 901 |
- ApiKeyAuthHeader: [] |
| 902 |
- BasicAuth: [] |
| 903 |
ApiKeyAuthQuery: [] |
| 904 |
components: |
| 905 |
securitySchemes: |
| 906 |
ApiKeyAuthHeader: |
| 907 |
type: apiKey |
| 908 |
in: header |
| 909 |
name: X-API-Key |
| 910 |
ApiKeyAuthQuery: |
| 911 |
type: apiKey |
| 912 |
in: query |
| 913 |
name: api_key |
| 914 |
BasicAuth: |
| 915 |
type: http |
| 916 |
scheme: basic |
| 917 |
` |
| 918 |
|
| 919 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 920 |
m, _ := doc.BuildV3Model() |
| 921 |
v := NewParameterValidator(&m.Model) |
| 922 |
|
| 923 |
// Request with basic auth AND query API key - should pass (second OR option, which is AND) |
| 924 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products?api_key=secret", nil) |
| 925 |
request.Header.Add("Authorization", "Basic dXNlcjpwYXNz") |
| 926 |
|
| 927 |
valid, errors := v.ValidateSecurity(request) |
| 928 |
assert.True(t, valid) |
| 929 |
assert.Empty(t, errors) |
| 930 |
} |
| 931 |
|
| 932 |
func TestParamValidator_ValidateSecurity_ORWithAND_PartialSecondOption(t *testing.T) { |
| 933 |
// Test mixed OR and AND: partial match on second option should try both and fail |
| 934 |
spec := `openapi: 3.1.0 |
| 935 |
paths: |
| 936 |
/products: |
| 937 |
post: |
| 938 |
security: |
| 939 |
- ApiKeyAuthHeader: [] |
| 940 |
- BasicAuth: [] |
| 941 |
ApiKeyAuthQuery: [] |
| 942 |
components: |
| 943 |
securitySchemes: |
| 944 |
ApiKeyAuthHeader: |
| 945 |
type: apiKey |
| 946 |
in: header |
| 947 |
name: X-API-Key |
| 948 |
ApiKeyAuthQuery: |
| 949 |
type: apiKey |
| 950 |
in: query |
| 951 |
name: api_key |
| 952 |
BasicAuth: |
| 953 |
type: http |
| 954 |
scheme: basic |
| 955 |
` |
| 956 |
|
| 957 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 958 |
m, _ := doc.BuildV3Model() |
| 959 |
v := NewParameterValidator(&m.Model) |
| 960 |
|
| 961 |
// Request with only basic auth - should fail (first option needs X-API-Key header, |
| 962 |
// second option needs BOTH basic auth AND api_key query param) |
| 963 |
request, _ := http.NewRequest(http.MethodPost, "https://things.com/products", nil) |
| 964 |
request.Header.Add("Authorization", "Basic dXNlcjpwYXNz") |
| 965 |
|
| 966 |
valid, errors := v.ValidateSecurity(request) |
| 967 |
assert.False(t, valid) |
| 968 |
// Should have errors from both OR options |
| 969 |
assert.GreaterOrEqual(t, len(errors), 1) |
| 970 |
} |
| 971 |
|
| 972 |
func TestParamValidator_ValidateSecurity_UnknownSchemeType(t *testing.T) { |
| 973 |
// Test oauth2 type - unknown to our validator, should pass through (not fail) |
| 974 |
spec := `openapi: 3.1.0 |
| 975 |
paths: |
| 976 |
/products: |
| 977 |
get: |
| 978 |
security: |
| 979 |
- OAuth2: [] |
| 980 |
components: |
| 981 |
securitySchemes: |
| 982 |
OAuth2: |
| 983 |
type: oauth2 |
| 984 |
flows: |
| 985 |
implicit: |
| 986 |
authorizationUrl: https://example.com/oauth |
| 987 |
scopes: |
| 988 |
read: Read access |
| 989 |
` |
| 990 |
|
| 991 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 992 |
m, _ := doc.BuildV3Model() |
| 993 |
v := NewParameterValidator(&m.Model) |
| 994 |
|
| 995 |
// Request with no auth - should pass because oauth2 type is not validated |
| 996 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 997 |
|
| 998 |
valid, errors := v.ValidateSecurity(request) |
| 999 |
assert.True(t, valid) |
| 1000 |
assert.Empty(t, errors) |
| 1001 |
} |
| 1002 |
|
| 1003 |
func TestParamValidator_ValidateSecurity_CustomHTTPScheme(t *testing.T) { |
| 1004 |
// Test custom HTTP scheme - should pass with correct scheme in header |
| 1005 |
spec := `openapi: 3.1.0 |
| 1006 |
paths: |
| 1007 |
/products: |
| 1008 |
get: |
| 1009 |
security: |
| 1010 |
- CustomAuth: [] |
| 1011 |
components: |
| 1012 |
securitySchemes: |
| 1013 |
CustomAuth: |
| 1014 |
type: http |
| 1015 |
scheme: custom |
| 1016 |
` |
| 1017 |
|
| 1018 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1019 |
m, _ := doc.BuildV3Model() |
| 1020 |
v := NewParameterValidator(&m.Model) |
| 1021 |
|
| 1022 |
// Request with custom auth header - should pass |
| 1023 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1024 |
request.Header.Add("Authorization", "Custom dXNlcjpwYXNz") |
| 1025 |
|
| 1026 |
valid, errors := v.ValidateSecurity(request) |
| 1027 |
assert.True(t, valid) |
| 1028 |
assert.Empty(t, errors) |
| 1029 |
} |
| 1030 |
|
| 1031 |
func TestParamValidator_ValidateSecurity_APIKey_UnknownInLocation(t *testing.T) { |
| 1032 |
// Test apiKey with unknown "in" location - should pass through (fallback at line 221) |
| 1033 |
spec := `openapi: 3.1.0 |
| 1034 |
paths: |
| 1035 |
/products: |
| 1036 |
get: |
| 1037 |
security: |
| 1038 |
- ApiKeyAuth: [] |
| 1039 |
components: |
| 1040 |
securitySchemes: |
| 1041 |
ApiKeyAuth: |
| 1042 |
type: apiKey |
| 1043 |
in: body |
| 1044 |
name: X-API-Key |
| 1045 |
` |
| 1046 |
|
| 1047 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1048 |
m, _ := doc.BuildV3Model() |
| 1049 |
v := NewParameterValidator(&m.Model) |
| 1050 |
|
| 1051 |
// Request with no auth - should pass because "body" is an unknown "in" location |
| 1052 |
// and the validator falls through to return true (line 221) |
| 1053 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1054 |
|
| 1055 |
valid, errors := v.ValidateSecurity(request) |
| 1056 |
assert.True(t, valid) |
| 1057 |
assert.Empty(t, errors) |
| 1058 |
} |
| 1059 |
|
| 1060 |
func TestParamValidator_ValidateSecurity_HTTPScheme_Mismatch(t *testing.T) { |
| 1061 |
// Test http scheme with mismatch in header: should return errors |
| 1062 |
spec := `openapi: 3.1.0 |
| 1063 |
paths: |
| 1064 |
/products: |
| 1065 |
get: |
| 1066 |
security: |
| 1067 |
- CustomAuth: [] |
| 1068 |
components: |
| 1069 |
securitySchemes: |
| 1070 |
CustomAuth: |
| 1071 |
type: http |
| 1072 |
scheme: custom |
| 1073 |
` |
| 1074 |
|
| 1075 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1076 |
m, _ := doc.BuildV3Model() |
| 1077 |
v := NewParameterValidator(&m.Model) |
| 1078 |
|
| 1079 |
// Request with auth header - should fail as header scheme is incorrect |
| 1080 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1081 |
request.Header.Add("Authorization", "Basic dXNlcjpwYXNz") |
| 1082 |
|
| 1083 |
valid, errors := v.ValidateSecurity(request) |
| 1084 |
assert.False(t, valid) |
| 1085 |
assert.Len(t, errors, 1) |
| 1086 |
assert.Contains(t, errors[0].Message, "Authorization header scheme 'custom' mismatch") |
| 1087 |
} |
| 1088 |
|
| 1089 |
func TestValidateSecurity_GlobalSecurity_NoOperationSecurity_Fails(t *testing.T) { |
| 1090 |
// Global security defined, no operation-level override — request without credentials should fail |
| 1091 |
spec := `openapi: 3.1.0 |
| 1092 |
paths: |
| 1093 |
/products: |
| 1094 |
get: |
| 1095 |
responses: |
| 1096 |
"200": |
| 1097 |
description: OK |
| 1098 |
security: |
| 1099 |
- ApiKeyAuth: [] |
| 1100 |
components: |
| 1101 |
securitySchemes: |
| 1102 |
ApiKeyAuth: |
| 1103 |
type: apiKey |
| 1104 |
in: header |
| 1105 |
name: X-API-Key |
| 1106 |
` |
| 1107 |
|
| 1108 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1109 |
m, _ := doc.BuildV3Model() |
| 1110 |
v := NewParameterValidator(&m.Model) |
| 1111 |
|
| 1112 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1113 |
|
| 1114 |
valid, errors := v.ValidateSecurity(request) |
| 1115 |
assert.False(t, valid) |
| 1116 |
assert.Len(t, errors, 1) |
| 1117 |
assert.Equal(t, "API Key X-API-Key not found in header", errors[0].Message) |
| 1118 |
} |
| 1119 |
|
| 1120 |
func TestValidateSecurity_GlobalSecurity_RequestSatisfies(t *testing.T) { |
| 1121 |
// Global security defined, request provides correct credentials — should pass |
| 1122 |
spec := `openapi: 3.1.0 |
| 1123 |
paths: |
| 1124 |
/products: |
| 1125 |
get: |
| 1126 |
responses: |
| 1127 |
"200": |
| 1128 |
description: OK |
| 1129 |
security: |
| 1130 |
- ApiKeyAuth: [] |
| 1131 |
components: |
| 1132 |
securitySchemes: |
| 1133 |
ApiKeyAuth: |
| 1134 |
type: apiKey |
| 1135 |
in: header |
| 1136 |
name: X-API-Key |
| 1137 |
` |
| 1138 |
|
| 1139 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1140 |
m, _ := doc.BuildV3Model() |
| 1141 |
v := NewParameterValidator(&m.Model) |
| 1142 |
|
| 1143 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1144 |
request.Header.Set("X-API-Key", "my-secret") |
| 1145 |
|
| 1146 |
valid, errors := v.ValidateSecurity(request) |
| 1147 |
assert.True(t, valid) |
| 1148 |
assert.Empty(t, errors) |
| 1149 |
} |
| 1150 |
|
| 1151 |
func TestValidateSecurity_GlobalSecurity_OperationOverridesWithEmpty(t *testing.T) { |
| 1152 |
// Global security defined, but operation overrides with empty array (opt-out) — should pass without creds |
| 1153 |
spec := `openapi: 3.1.0 |
| 1154 |
paths: |
| 1155 |
/products: |
| 1156 |
get: |
| 1157 |
security: [] |
| 1158 |
responses: |
| 1159 |
"200": |
| 1160 |
description: OK |
| 1161 |
security: |
| 1162 |
- ApiKeyAuth: [] |
| 1163 |
components: |
| 1164 |
securitySchemes: |
| 1165 |
ApiKeyAuth: |
| 1166 |
type: apiKey |
| 1167 |
in: header |
| 1168 |
name: X-API-Key |
| 1169 |
` |
| 1170 |
|
| 1171 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1172 |
m, _ := doc.BuildV3Model() |
| 1173 |
v := NewParameterValidator(&m.Model) |
| 1174 |
|
| 1175 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1176 |
// No credentials |
| 1177 |
|
| 1178 |
valid, errors := v.ValidateSecurity(request) |
| 1179 |
assert.True(t, valid) |
| 1180 |
assert.Empty(t, errors) |
| 1181 |
} |
| 1182 |
|
| 1183 |
func TestValidateSecurity_GlobalSecurity_OperationOverridesWithDifferent(t *testing.T) { |
| 1184 |
// Global security defines ApiKey, operation overrides with BasicAuth — operation takes precedence |
| 1185 |
spec := `openapi: 3.1.0 |
| 1186 |
paths: |
| 1187 |
/products: |
| 1188 |
get: |
| 1189 |
security: |
| 1190 |
- BasicAuth: [] |
| 1191 |
responses: |
| 1192 |
"200": |
| 1193 |
description: OK |
| 1194 |
security: |
| 1195 |
- ApiKeyAuth: [] |
| 1196 |
components: |
| 1197 |
securitySchemes: |
| 1198 |
ApiKeyAuth: |
| 1199 |
type: apiKey |
| 1200 |
in: header |
| 1201 |
name: X-API-Key |
| 1202 |
BasicAuth: |
| 1203 |
type: http |
| 1204 |
scheme: basic |
| 1205 |
` |
| 1206 |
|
| 1207 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1208 |
m, _ := doc.BuildV3Model() |
| 1209 |
v := NewParameterValidator(&m.Model) |
| 1210 |
|
| 1211 |
// Request with Basic auth (satisfying operation-level security, not global) |
| 1212 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1213 |
request.Header.Set("Authorization", "Basic dXNlcjpwYXNz") |
| 1214 |
|
| 1215 |
valid, errors := v.ValidateSecurity(request) |
| 1216 |
assert.True(t, valid) |
| 1217 |
assert.Empty(t, errors) |
| 1218 |
|
| 1219 |
// Request with API key (satisfying global, not operation-level) should fail |
| 1220 |
request2, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1221 |
request2.Header.Set("X-API-Key", "my-secret") |
| 1222 |
|
| 1223 |
valid2, errors2 := v.ValidateSecurity(request2) |
| 1224 |
assert.False(t, valid2) |
| 1225 |
assert.Len(t, errors2, 1) |
| 1226 |
assert.Contains(t, errors2[0].Message, "Authorization header") |
| 1227 |
} |
| 1228 |
|
| 1229 |
func TestValidateSecurity_GlobalSecurity_HTTPBasic(t *testing.T) { |
| 1230 |
// Global security with HTTP basic auth |
| 1231 |
spec := `openapi: 3.1.0 |
| 1232 |
paths: |
| 1233 |
/products: |
| 1234 |
get: |
| 1235 |
responses: |
| 1236 |
"200": |
| 1237 |
description: OK |
| 1238 |
security: |
| 1239 |
- BasicAuth: [] |
| 1240 |
components: |
| 1241 |
securitySchemes: |
| 1242 |
BasicAuth: |
| 1243 |
type: http |
| 1244 |
scheme: basic |
| 1245 |
` |
| 1246 |
|
| 1247 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1248 |
m, _ := doc.BuildV3Model() |
| 1249 |
v := NewParameterValidator(&m.Model) |
| 1250 |
|
| 1251 |
// No auth header — should fail |
| 1252 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1253 |
valid, errors := v.ValidateSecurity(request) |
| 1254 |
assert.False(t, valid) |
| 1255 |
assert.Len(t, errors, 1) |
| 1256 |
|
| 1257 |
// With correct auth header — should pass |
| 1258 |
request2, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1259 |
request2.Header.Set("Authorization", "Basic dXNlcjpwYXNz") |
| 1260 |
valid2, errors2 := v.ValidateSecurity(request2) |
| 1261 |
assert.True(t, valid2) |
| 1262 |
assert.Empty(t, errors2) |
| 1263 |
} |
| 1264 |
|
| 1265 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_OAuth2Scopes(t *testing.T) { |
| 1266 |
spec := `openapi: 3.1.0 |
| 1267 |
paths: |
| 1268 |
/products: |
| 1269 |
get: |
| 1270 |
security: |
| 1271 |
- OAuth2: |
| 1272 |
- read:products |
| 1273 |
- write:products |
| 1274 |
components: |
| 1275 |
securitySchemes: |
| 1276 |
OAuth2: |
| 1277 |
type: oauth2 |
| 1278 |
flows: |
| 1279 |
clientCredentials: |
| 1280 |
tokenUrl: https://example.com/oauth/token |
| 1281 |
scopes: |
| 1282 |
read:products: Read products |
| 1283 |
write:products: Write products |
| 1284 |
` |
| 1285 |
|
| 1286 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1287 |
m, _ := doc.BuildV3Model() |
| 1288 |
|
| 1289 |
type authContextKey struct{} |
| 1290 |
var request *http.Request |
| 1291 |
var gotScopes []string |
| 1292 |
called := 0 |
| 1293 |
authFn := func(ctx context.Context, input *config.AuthenticationInput) error { |
| 1294 |
called++ |
| 1295 |
assert.Equal(t, "ctx-value", ctx.Value(authContextKey{})) |
| 1296 |
assert.Equal(t, request, input.Request) |
| 1297 |
assert.Equal(t, "OAuth2", input.SecuritySchemeName) |
| 1298 |
assert.Equal(t, "oauth2", input.SecurityScheme.Type) |
| 1299 |
gotScopes = append([]string(nil), input.Scopes...) |
| 1300 |
return nil |
| 1301 |
} |
| 1302 |
|
| 1303 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn)) |
| 1304 |
request, _ = http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1305 |
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, "ctx-value")) |
| 1306 |
|
| 1307 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1308 |
assert.True(t, valid) |
| 1309 |
assert.Empty(t, validationErrors) |
| 1310 |
assert.Equal(t, 1, called) |
| 1311 |
assert.ElementsMatch(t, []string{"read:products", "write:products"}, gotScopes) |
| 1312 |
} |
| 1313 |
|
| 1314 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_OpenIDConnect(t *testing.T) { |
| 1315 |
spec := `openapi: 3.1.0 |
| 1316 |
paths: |
| 1317 |
/products: |
| 1318 |
get: |
| 1319 |
security: |
| 1320 |
- OpenID: [] |
| 1321 |
components: |
| 1322 |
securitySchemes: |
| 1323 |
OpenID: |
| 1324 |
type: openIdConnect |
| 1325 |
openIdConnectUrl: https://example.com/.well-known/openid-configuration |
| 1326 |
` |
| 1327 |
|
| 1328 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1329 |
m, _ := doc.BuildV3Model() |
| 1330 |
|
| 1331 |
called := false |
| 1332 |
authFn := func(ctx context.Context, input *config.AuthenticationInput) error { |
| 1333 |
called = true |
| 1334 |
assert.NotNil(t, ctx) |
| 1335 |
assert.Equal(t, "OpenID", input.SecuritySchemeName) |
| 1336 |
assert.Equal(t, "openIdConnect", input.SecurityScheme.Type) |
| 1337 |
assert.Empty(t, input.Scopes) |
| 1338 |
return nil |
| 1339 |
} |
| 1340 |
|
| 1341 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn)) |
| 1342 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1343 |
|
| 1344 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1345 |
assert.True(t, valid) |
| 1346 |
assert.Empty(t, validationErrors) |
| 1347 |
assert.True(t, called) |
| 1348 |
} |
| 1349 |
|
| 1350 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_ORSuccessAfterFirstFailure(t *testing.T) { |
| 1351 |
spec := `openapi: 3.1.0 |
| 1352 |
paths: |
| 1353 |
/products: |
| 1354 |
get: |
| 1355 |
security: |
| 1356 |
- OAuth2: |
| 1357 |
- read:products |
| 1358 |
- ApiKeyAuth: [] |
| 1359 |
components: |
| 1360 |
securitySchemes: |
| 1361 |
OAuth2: |
| 1362 |
type: oauth2 |
| 1363 |
flows: |
| 1364 |
clientCredentials: |
| 1365 |
tokenUrl: https://example.com/oauth/token |
| 1366 |
scopes: |
| 1367 |
read:products: Read products |
| 1368 |
ApiKeyAuth: |
| 1369 |
type: apiKey |
| 1370 |
in: header |
| 1371 |
name: X-API-Key |
| 1372 |
` |
| 1373 |
|
| 1374 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1375 |
m, _ := doc.BuildV3Model() |
| 1376 |
|
| 1377 |
calls := make(map[string]int) |
| 1378 |
authFn := func(_ context.Context, input *config.AuthenticationInput) error { |
| 1379 |
calls[input.SecuritySchemeName]++ |
| 1380 |
if input.SecuritySchemeName == "OAuth2" { |
| 1381 |
return stderrors.New("token missing") |
| 1382 |
} |
| 1383 |
return nil |
| 1384 |
} |
| 1385 |
|
| 1386 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn)) |
| 1387 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1388 |
|
| 1389 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1390 |
assert.True(t, valid) |
| 1391 |
assert.Empty(t, validationErrors) |
| 1392 |
assert.Equal(t, 1, calls["OAuth2"]) |
| 1393 |
assert.Equal(t, 1, calls["ApiKeyAuth"]) |
| 1394 |
} |
| 1395 |
|
| 1396 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_ANDPartialFailure(t *testing.T) { |
| 1397 |
spec := `openapi: 3.1.0 |
| 1398 |
paths: |
| 1399 |
/products: |
| 1400 |
get: |
| 1401 |
security: |
| 1402 |
- OAuth2: |
| 1403 |
- read:products |
| 1404 |
ApiKeyAuth: [] |
| 1405 |
components: |
| 1406 |
securitySchemes: |
| 1407 |
OAuth2: |
| 1408 |
type: oauth2 |
| 1409 |
flows: |
| 1410 |
clientCredentials: |
| 1411 |
tokenUrl: https://example.com/oauth/token |
| 1412 |
scopes: |
| 1413 |
read:products: Read products |
| 1414 |
ApiKeyAuth: |
| 1415 |
type: apiKey |
| 1416 |
in: header |
| 1417 |
name: X-API-Key |
| 1418 |
` |
| 1419 |
|
| 1420 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1421 |
m, _ := doc.BuildV3Model() |
| 1422 |
|
| 1423 |
calls := make(map[string]int) |
| 1424 |
authFn := func(_ context.Context, input *config.AuthenticationInput) error { |
| 1425 |
calls[input.SecuritySchemeName]++ |
| 1426 |
if input.SecuritySchemeName == "ApiKeyAuth" { |
| 1427 |
return stderrors.New("api key denied") |
| 1428 |
} |
| 1429 |
return nil |
| 1430 |
} |
| 1431 |
|
| 1432 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn)) |
| 1433 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1434 |
|
| 1435 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1436 |
assert.False(t, valid) |
| 1437 |
assert.Len(t, validationErrors, 1) |
| 1438 |
assert.Equal(t, "Authentication failed for security scheme 'ApiKeyAuth'", validationErrors[0].Message) |
| 1439 |
assert.Equal(t, "api key denied", validationErrors[0].Reason) |
| 1440 |
assert.Equal(t, 1, calls["OAuth2"]) |
| 1441 |
assert.Equal(t, 1, calls["ApiKeyAuth"]) |
| 1442 |
} |
| 1443 |
|
| 1444 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_ErrorReturned(t *testing.T) { |
| 1445 |
spec := `openapi: 3.1.0 |
| 1446 |
paths: |
| 1447 |
/products: |
| 1448 |
get: |
| 1449 |
security: |
| 1450 |
- OAuth2: |
| 1451 |
- read:products |
| 1452 |
components: |
| 1453 |
securitySchemes: |
| 1454 |
OAuth2: |
| 1455 |
type: oauth2 |
| 1456 |
flows: |
| 1457 |
clientCredentials: |
| 1458 |
tokenUrl: https://example.com/oauth/token |
| 1459 |
scopes: |
| 1460 |
read:products: Read products |
| 1461 |
` |
| 1462 |
|
| 1463 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1464 |
m, _ := doc.BuildV3Model() |
| 1465 |
|
| 1466 |
authFn := func(context.Context, *config.AuthenticationInput) error { |
| 1467 |
return stderrors.New("expired token") |
| 1468 |
} |
| 1469 |
|
| 1470 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn)) |
| 1471 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1472 |
|
| 1473 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1474 |
assert.False(t, valid) |
| 1475 |
assert.Len(t, validationErrors, 1) |
| 1476 |
assert.Equal(t, "Authentication failed for security scheme 'OAuth2'", validationErrors[0].Message) |
| 1477 |
assert.Equal(t, "expired token", validationErrors[0].Reason) |
| 1478 |
assert.Equal(t, "security", validationErrors[0].ValidationType) |
| 1479 |
assert.Equal(t, "oauth2", validationErrors[0].ValidationSubType) |
| 1480 |
assert.Equal(t, request.Method, validationErrors[0].RequestMethod) |
| 1481 |
assert.Equal(t, request.URL.Path, validationErrors[0].RequestPath) |
| 1482 |
assert.Equal(t, "/products", validationErrors[0].SpecPath) |
| 1483 |
} |
| 1484 |
|
| 1485 |
func TestParamValidator_ValidateSecurity_AuthenticationFunc_SkippedWhenSecurityValidationDisabled(t *testing.T) { |
| 1486 |
spec := `openapi: 3.1.0 |
| 1487 |
paths: |
| 1488 |
/products: |
| 1489 |
get: |
| 1490 |
security: |
| 1491 |
- OAuth2: |
| 1492 |
- read:products |
| 1493 |
components: |
| 1494 |
securitySchemes: |
| 1495 |
OAuth2: |
| 1496 |
type: oauth2 |
| 1497 |
flows: |
| 1498 |
clientCredentials: |
| 1499 |
tokenUrl: https://example.com/oauth/token |
| 1500 |
scopes: |
| 1501 |
read:products: Read products |
| 1502 |
` |
| 1503 |
|
| 1504 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 1505 |
m, _ := doc.BuildV3Model() |
| 1506 |
|
| 1507 |
called := false |
| 1508 |
authFn := func(context.Context, *config.AuthenticationInput) error { |
| 1509 |
called = true |
| 1510 |
return stderrors.New("should not be called") |
| 1511 |
} |
| 1512 |
|
| 1513 |
v := NewParameterValidator(&m.Model, config.WithAuthenticationFunc(authFn), config.WithoutSecurityValidation()) |
| 1514 |
request, _ := http.NewRequest(http.MethodGet, "https://things.com/products", nil) |
| 1515 |
|
| 1516 |
valid, validationErrors := v.ValidateSecurity(request) |
| 1517 |
assert.True(t, valid) |
| 1518 |
assert.Empty(t, validationErrors) |
| 1519 |
assert.False(t, called) |
| 1520 |
} |