|
| 1 | +// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package parameters |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "github.com/pb33f/libopenapi-validator/errors" |
| 9 | + "github.com/pb33f/libopenapi-validator/helpers" |
| 10 | + "github.com/pb33f/libopenapi-validator/paths" |
| 11 | + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 12 | + "net/http" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +func (v *paramValidator) ValidateSecurity(request *http.Request) (bool, []*errors.ValidationError) { |
| 17 | + |
| 18 | + // find path |
| 19 | + var pathItem *v3.PathItem |
| 20 | + var errs []*errors.ValidationError |
| 21 | + if v.pathItem == nil { |
| 22 | + pathItem, errs, _ = paths.FindPath(request, v.document) |
| 23 | + if pathItem == nil || errs != nil { |
| 24 | + v.errors = errs |
| 25 | + return false, errs |
| 26 | + } |
| 27 | + } else { |
| 28 | + pathItem = v.pathItem |
| 29 | + } |
| 30 | + |
| 31 | + // extract security for the operation |
| 32 | + security := helpers.ExtractSecurityForOperation(request, pathItem) |
| 33 | + |
| 34 | + if security == nil { |
| 35 | + return true, nil |
| 36 | + } |
| 37 | + |
| 38 | + for _, sec := range security { |
| 39 | + for secName, _ := range sec.Requirements { |
| 40 | + |
| 41 | + // look up security from components |
| 42 | + secScheme := v.document.Components.SecuritySchemes[secName] |
| 43 | + if secScheme != nil { |
| 44 | + |
| 45 | + switch strings.ToLower(secScheme.Type) { |
| 46 | + case "http": |
| 47 | + switch strings.ToLower(secScheme.Scheme) { |
| 48 | + case "basic", "bearer", "digest": |
| 49 | + // check for an authorization header |
| 50 | + if request.Header.Get("Authorization") == "" { |
| 51 | + return false, []*errors.ValidationError{ |
| 52 | + { |
| 53 | + Message: fmt.Sprintf("Authorization header for '%s' scheme", secScheme.Scheme), |
| 54 | + Reason: "Authorization header was not found", |
| 55 | + ValidationType: "security", |
| 56 | + ValidationSubType: secScheme.Scheme, |
| 57 | + SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 58 | + SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 59 | + HowToFix: "Add an 'Authorization' header to this request", |
| 60 | + }, |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + case "apikey": |
| 66 | + // check if the api key is in the request |
| 67 | + if secScheme.In == "header" { |
| 68 | + if request.Header.Get(secScheme.Name) == "" { |
| 69 | + return false, []*errors.ValidationError{ |
| 70 | + { |
| 71 | + Message: fmt.Sprintf("API Key %s not found in header", secScheme.Name), |
| 72 | + Reason: "API Key not found in http header for security scheme 'apiKey' with type 'header'", |
| 73 | + ValidationType: "security", |
| 74 | + ValidationSubType: "apiKey", |
| 75 | + SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 76 | + SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 77 | + HowToFix: fmt.Sprintf("Add the API Key via '%s' as a header of the request", secScheme.Name), |
| 78 | + }, |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if secScheme.In == "query" { |
| 83 | + if request.URL.Query().Get(secScheme.Name) == "" { |
| 84 | + copyUrl := *request.URL |
| 85 | + fixed := ©Url |
| 86 | + q := fixed.Query() |
| 87 | + q.Add(secScheme.Name, "your-api-key") |
| 88 | + fixed.RawQuery = q.Encode() |
| 89 | + |
| 90 | + return false, []*errors.ValidationError{ |
| 91 | + { |
| 92 | + Message: fmt.Sprintf("API Key %s not found in query", secScheme.Name), |
| 93 | + Reason: "API Key not found in URL query for security scheme 'apiKey' with type 'query'", |
| 94 | + ValidationType: "security", |
| 95 | + ValidationSubType: "apiKey", |
| 96 | + SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 97 | + SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 98 | + HowToFix: fmt.Sprintf("Add an API Key via '%s' to the query string "+ |
| 99 | + "of the URL, for example '%s'", secScheme.Name, fixed.String()), |
| 100 | + }, |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + if secScheme.In == "cookie" { |
| 105 | + cookies := request.Cookies() |
| 106 | + cookieFound := false |
| 107 | + for _, cookie := range cookies { |
| 108 | + if cookie.Name == secScheme.Name { |
| 109 | + cookieFound = true |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + if !cookieFound { |
| 114 | + return false, []*errors.ValidationError{ |
| 115 | + { |
| 116 | + Message: fmt.Sprintf("API Key %s not found in cookies", secScheme.Name), |
| 117 | + Reason: "API Key not found in http request cookies for security scheme 'apiKey' with type 'cookie'", |
| 118 | + ValidationType: "security", |
| 119 | + ValidationSubType: "apiKey", |
| 120 | + SpecLine: sec.GoLow().Requirements.ValueNode.Line, |
| 121 | + SpecCol: sec.GoLow().Requirements.ValueNode.Column, |
| 122 | + HowToFix: fmt.Sprintf("Submit an API Key '%s' as a cookie with the request", secScheme.Name), |
| 123 | + }, |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return true, nil |
| 132 | +} |
0 commit comments