Skip to content

Commit c2b2f96

Browse files
authored
fix: add additional information around errors for missing content type header (#1576)
## What kind of change does this PR introduce? Adds additional information around errors related to missing content-type headers so that developers can unblock themselves.
1 parent 25d9874 commit c2b2f96

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

internal/api/errorcodes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const (
7676
ErrorCodeHookTimeout ErrorCode = "hook_timeout"
7777
ErrorCodeHookTimeoutAfterRetry ErrorCode = "hook_timeout_after_retry"
7878
ErrorCodeHookPayloadOverSizeLimit ErrorCode = "hook_payload_over_size_limit"
79+
ErrorCodeHookPayloadInvalidContentType ErrorCode = "hook_payload_invalid_content_type"
7980
ErrorCodeRequestTimeout ErrorCode = "request_timeout"
8081
ErrorCodeMFAPhoneEnrollDisabled ErrorCode = "mfa_phone_enroll_not_enabled"
8182
ErrorCodeMFAPhoneVerifyDisabled ErrorCode = "mfa_phone_verify_not_enabled"

internal/api/hooks.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,21 @@ func (a *API) runHTTPHook(r *http.Request, hookConfig conf.ExtensibilityPointCon
137137
}
138138

139139
defer rsp.Body.Close()
140-
// Header.Get is case insensitive
141-
contentType := rsp.Header.Get("Content-Type")
142-
mediaType, _, err := mime.ParseMediaType(contentType)
143-
if err != nil {
144-
return nil, internalServerError("Invalid Content-Type header")
145-
}
146-
if mediaType != "application/json" {
147-
return nil, internalServerError("Invalid JSON response. Received content-type: " + contentType)
148-
}
149140

150141
switch rsp.StatusCode {
151142
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
143+
// Header.Get is case insensitive
144+
contentType := rsp.Header.Get("Content-Type")
145+
if contentType == "" {
146+
return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, "Invalid Content-Type: Missing Content-Type header")
147+
}
148+
mediaType, _, err := mime.ParseMediaType(contentType)
149+
if err != nil {
150+
return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, fmt.Sprintf("Invalid Content-Type header: %s", err.Error()))
151+
}
152+
if mediaType != "application/json" {
153+
return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, "Invalid JSON response. Received content-type: "+contentType)
154+
}
152155
if rsp.Body == nil {
153156
return nil, nil
154157
}

0 commit comments

Comments
 (0)