Skip to content

Commit b6b00bb

Browse files
authored
[Fix] use unrestricted http client only for non-safe requests (#3847)
* exposed a MethodIsSafe() to reuse it in OpsGenie Analyzer. Use Restricted Client for non-safe APIs. * Renamed MethodIsSafe to IsMethodSafe for more clarity
1 parent d509097 commit b6b00bb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pkg/analyzer/analyzers/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type AnalyzerRoundTripper struct {
114114

115115
func (r AnalyzerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
116116
resp, err := r.parent.RoundTrip(req)
117-
if err != nil || methodIsSafe(req.Method) {
117+
if err != nil || IsMethodSafe(req.Method) {
118118
return resp, err
119119
}
120120
// Check that unsafe methods did NOT return a valid status code.
@@ -126,7 +126,7 @@ func (r AnalyzerRoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
126126

127127
// methodIsSafe is a helper method to check whether the HTTP method is safe according to MDN Web Docs.
128128
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods#safe_idempotent_and_cacheable_request_methods
129-
func methodIsSafe(method string) bool {
129+
func IsMethodSafe(method string) bool {
130130
switch strings.ToUpper(method) {
131131
case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace:
132132
return true

pkg/analyzer/analyzers/opsgenie/opsgenie.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,16 @@ func (h *HttpStatusTest) RunTest(cfg *config.Config, headers map[string]string)
132132
}
133133

134134
// Create new HTTP request
135-
client := analyzers.NewAnalyzeClientUnrestricted(cfg)
135+
var client *http.Client
136+
137+
// Non-safe Opsgenie APIs are asynchronous and always return 202 if credential has the permission.
138+
// For Safe API Methods, use the restricted client
139+
if analyzers.IsMethodSafe(h.Method) {
140+
client = analyzers.NewAnalyzeClient(cfg)
141+
} else {
142+
client = analyzers.NewAnalyzeClientUnrestricted(cfg)
143+
}
144+
136145
req, err := http.NewRequest(h.Method, h.Endpoint, data)
137146
if err != nil {
138147
return false, err

0 commit comments

Comments
 (0)