|
| 1 | +From 0dd4dd541c665fb292d664f77604ba694726f298 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Jacob Hoffman-Andrews < [email protected]> |
| 3 | +Date: Thu, 7 Mar 2024 14:25:21 -0800 |
| 4 | +Subject: [PATCH] v2: backport decompression limit fix (#109) |
| 5 | + |
| 6 | +Backport from #107. |
| 7 | +Modified to apply to vendored code by : Kavya Sree Kaitepalli < [email protected]> |
| 8 | +--- |
| 9 | + vendor/gopkg.in/square/go-jose.v2/crypter.go | 6 ++++ |
| 10 | + vendor/gopkg.in/square/go-jose.v2/encoding.go | 21 +++++++++--- |
| 11 | + 2 files changed, 141 insertions(+), 4 deletions(-) |
| 12 | + |
| 13 | +diff --git a/vendor/gopkg.in/square/go-jose.v2/crypter.go b/vendor/gopkg.in/square/go-jose.v2/crypter.go |
| 14 | +index 73aab0f..0ae2e5e 100644 |
| 15 | +--- a/vendor/gopkg.in/square/go-jose.v2/crypter.go |
| 16 | ++++ b/vendor/gopkg.in/square/go-jose.v2/crypter.go |
| 17 | +@@ -406,6 +406,9 @@ func (ctx *genericEncrypter) Options() EncrypterOptions { |
| 18 | + // Decrypt and validate the object and return the plaintext. Note that this |
| 19 | + // function does not support multi-recipient, if you desire multi-recipient |
| 20 | + // decryption use DecryptMulti instead. |
| 21 | ++// |
| 22 | ++// Automatically decompresses plaintext, but returns an error if the decompressed |
| 23 | ++// data would be >250kB or >10x the size of the compressed data, whichever is larger. |
| 24 | + func (obj JSONWebEncryption) Decrypt(decryptionKey interface{}) ([]byte, error) { |
| 25 | + headers := obj.mergedHeaders(nil) |
| 26 | + |
| 27 | +@@ -470,6 +473,9 @@ func (obj JSONWebEncryption) Decrypt(decryptionKey interface{}) ([]byte, error) |
| 28 | + // with support for multiple recipients. It returns the index of the recipient |
| 29 | + // for which the decryption was successful, the merged headers for that recipient, |
| 30 | + // and the plaintext. |
| 31 | ++// |
| 32 | ++// Automatically decompresses plaintext, but returns an error if the decompressed |
| 33 | ++// data would be >250kB or >3x the size of the compressed data, whichever is larger. |
| 34 | + func (obj JSONWebEncryption) DecryptMulti(decryptionKey interface{}) (int, Header, []byte, error) { |
| 35 | + globalHeaders := obj.mergedHeaders(nil) |
| 36 | + |
| 37 | +diff --git a/vendor/gopkg.in/square/go-jose.v2/encoding.go b/vendor/gopkg.in/square/go-jose.v2/encoding.go |
| 38 | +index 40b688b..636f6c8 100644 |
| 39 | +--- a/vendor/gopkg.in/square/go-jose.v2/encoding.go |
| 40 | ++++ b/vendor/gopkg.in/square/go-jose.v2/encoding.go |
| 41 | +@@ -21,6 +21,7 @@ import ( |
| 42 | + "compress/flate" |
| 43 | + "encoding/base64" |
| 44 | + "encoding/binary" |
| 45 | ++ "fmt" |
| 46 | + "io" |
| 47 | + "math/big" |
| 48 | + "strings" |
| 49 | +@@ -85,7 +86,7 @@ func decompress(algorithm CompressionAlgorithm, input []byte) ([]byte, error) { |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +-// Compress with DEFLATE |
| 54 | ++// deflate compresses the input. |
| 55 | + func deflate(input []byte) ([]byte, error) { |
| 56 | + output := new(bytes.Buffer) |
| 57 | + |
| 58 | +@@ -97,15 +98,27 @@ func deflate(input []byte) ([]byte, error) { |
| 59 | + return output.Bytes(), err |
| 60 | + } |
| 61 | + |
| 62 | +-// Decompress with DEFLATE |
| 63 | ++// inflate decompresses the input. |
| 64 | ++// |
| 65 | ++// Errors if the decompressed data would be >250kB or >10x the size of the |
| 66 | ++// compressed data, whichever is larger. |
| 67 | + func inflate(input []byte) ([]byte, error) { |
| 68 | + output := new(bytes.Buffer) |
| 69 | + reader := flate.NewReader(bytes.NewBuffer(input)) |
| 70 | + |
| 71 | +- _, err := io.Copy(output, reader) |
| 72 | +- if err != nil { |
| 73 | ++ maxCompressedSize := 10 * int64(len(input)) |
| 74 | ++ if maxCompressedSize < 250000 { |
| 75 | ++ maxCompressedSize = 250000 |
| 76 | ++ } |
| 77 | ++ |
| 78 | ++ limit := maxCompressedSize + 1 |
| 79 | ++ n, err := io.CopyN(output, reader, limit) |
| 80 | ++ if err != nil && err != io.EOF { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | ++ if n == limit { |
| 84 | ++ return nil, fmt.Errorf("uncompressed data would be too large (>%d bytes)", maxCompressedSize) |
| 85 | ++ } |
| 86 | + |
| 87 | + err = reader.Close() |
| 88 | + return output.Bytes(), err |
0 commit comments