From c2b2f96f07c97c15597cd972b1cd672238d87cdc Mon Sep 17 00:00:00 2001 From: Joel Lee Date: Wed, 18 Sep 2024 23:08:23 +0200 Subject: [PATCH] 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. --- internal/api/errorcodes.go | 1 + internal/api/hooks.go | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/api/errorcodes.go b/internal/api/errorcodes.go index f6a890566..d9a982224 100644 --- a/internal/api/errorcodes.go +++ b/internal/api/errorcodes.go @@ -76,6 +76,7 @@ const ( ErrorCodeHookTimeout ErrorCode = "hook_timeout" ErrorCodeHookTimeoutAfterRetry ErrorCode = "hook_timeout_after_retry" ErrorCodeHookPayloadOverSizeLimit ErrorCode = "hook_payload_over_size_limit" + ErrorCodeHookPayloadInvalidContentType ErrorCode = "hook_payload_invalid_content_type" ErrorCodeRequestTimeout ErrorCode = "request_timeout" ErrorCodeMFAPhoneEnrollDisabled ErrorCode = "mfa_phone_enroll_not_enabled" ErrorCodeMFAPhoneVerifyDisabled ErrorCode = "mfa_phone_verify_not_enabled" diff --git a/internal/api/hooks.go b/internal/api/hooks.go index 7b89dc561..3a5512d40 100644 --- a/internal/api/hooks.go +++ b/internal/api/hooks.go @@ -137,18 +137,21 @@ func (a *API) runHTTPHook(r *http.Request, hookConfig conf.ExtensibilityPointCon } defer rsp.Body.Close() - // Header.Get is case insensitive - contentType := rsp.Header.Get("Content-Type") - mediaType, _, err := mime.ParseMediaType(contentType) - if err != nil { - return nil, internalServerError("Invalid Content-Type header") - } - if mediaType != "application/json" { - return nil, internalServerError("Invalid JSON response. Received content-type: " + contentType) - } switch rsp.StatusCode { case http.StatusOK, http.StatusNoContent, http.StatusAccepted: + // Header.Get is case insensitive + contentType := rsp.Header.Get("Content-Type") + if contentType == "" { + return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, "Invalid Content-Type: Missing Content-Type header") + } + mediaType, _, err := mime.ParseMediaType(contentType) + if err != nil { + return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, fmt.Sprintf("Invalid Content-Type header: %s", err.Error())) + } + if mediaType != "application/json" { + return nil, badRequestError(ErrorCodeHookPayloadInvalidContentType, "Invalid JSON response. Received content-type: "+contentType) + } if rsp.Body == nil { return nil, nil }