Skip to content

Commit

Permalink
fix: remove check for content-length (supabase#1700)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Not all responses are gonna contain the `Content-Length` header.
According to the HTTP/2 spec, it is not required to return the
content-length header in the response. If the "Transfer Encoding" is
chunked, the content-length header will also not be present as the
response is sent in chunks and the content-length is unknown initially.

## What is the current behavior?

Please link any relevant issues here.

## What is the new behavior?

Feel free to include screenshots if it includes visual changes.

## Additional context

Add any other context or screenshots.
  • Loading branch information
kangmingtay authored Aug 2, 2024
1 parent 250d92f commit 81b332d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
1 change: 0 additions & 1 deletion internal/api/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const (
ErrorCodeHookTimeout ErrorCode = "hook_timeout"
ErrorCodeHookTimeoutAfterRetry ErrorCode = "hook_timeout_after_retry"
ErrorCodeHookPayloadOverSizeLimit ErrorCode = "hook_payload_over_size_limit"
ErrorCodeHookPayloadUnknownSize ErrorCode = "hook_payload_unknown_size"
ErrorCodeRequestTimeout ErrorCode = "request_timeout"
ErrorCodeMFAPhoneEnrollDisabled ErrorCode = "mfa_phone_enroll_not_enabled"
ErrorCodeMFAPhoneVerifyDisabled ErrorCode = "mfa_phone_verify_not_enabled"
Expand Down
15 changes: 7 additions & 8 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,17 @@ func (a *API) runHTTPHook(r *http.Request, hookConfig conf.ExtensibilityPointCon
if rsp.Body == nil {
return nil, nil
}
contentLength := rsp.ContentLength
if contentLength == -1 {
return nil, unprocessableEntityError(ErrorCodeHookPayloadUnknownSize, "Payload size not known")
}
if contentLength >= PayloadLimit {
return nil, unprocessableEntityError(ErrorCodeHookPayloadOverSizeLimit, fmt.Sprintf("Payload size is: %d bytes exceeded size limit of %d bytes", contentLength, PayloadLimit))
}
limitedReader := io.LimitedReader{R: rsp.Body, N: contentLength}
limitedReader := io.LimitedReader{R: rsp.Body, N: PayloadLimit}
body, err := io.ReadAll(&limitedReader)
if err != nil {
return nil, err
}
if limitedReader.N <= 0 {
// check if the response body still has excess bytes to be read
if n, _ := rsp.Body.Read(make([]byte, 1)); n > 0 {
return nil, unprocessableEntityError(ErrorCodeHookPayloadOverSizeLimit, fmt.Sprintf("Payload size exceeded size limit of %d bytes", PayloadLimit))
}
}
return body, nil
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
retryAfterHeader := rsp.Header.Get("retry-after")
Expand Down

0 comments on commit 81b332d

Please sign in to comment.