-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server/authorizer: Fix gzip payload handling. (#6825)
This PR fixes an issue where an OPA running authorization policies would be unable to handle gzipped request bodies. Example OPA CLI setup: opa run -s --authorization=basic Example request: echo -n '{}' | gzip | curl -H "Content-Encoding: gzip" --data-binary @- http://127.0.0.1:8181/v1/data This would result in unhelpful error messages, like: ```json { "code": "invalid_parameter", "message": "invalid character '\\x1f' looking for beginning of value" } ``` The cause was that the request body handling system in the `server/authorizer` package did not take gzipped payloads into account. The fix was to borrow the gzip request body handling function from `server/server.go`, to transparently decompress the body when needed. Fixes: #6804 Signed-off-by: Philip Conrad <[email protected]>
- Loading branch information
1 parent
c2cede7
commit 4e01537
Showing
4 changed files
with
175 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// Note(philipc): Originally taken from server/server.go | ||
func ReadMaybeCompressedBody(r *http.Request) (io.ReadCloser, error) { | ||
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") { | ||
gzReader, err := gzip.NewReader(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer gzReader.Close() | ||
bytesBody, err := io.ReadAll(gzReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return io.NopCloser(bytes.NewReader(bytesBody)), err | ||
} | ||
return r.Body, nil | ||
} |