Repository navigation
Expand file tree
/
Copy pathdecompress.go
More file actions
175 lines (146 loc) · 5.08 KB
/
Copy pathdecompress.go
File metadata and controls
175 lines (146 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package middleware
import (
"compress/gzip"
"io"
"net/http"
"strings"
"sync"
"github.com/labstack/echo/v5"
)
// DecompressConfig defines the config for Decompress middleware.
type DecompressConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// GzipDecompressPool defines an interface to provide the sync.Pool used to create/store Gzip readers
GzipDecompressPool Decompressor
// MaxDecompressedSize limits the maximum size of decompressed request body in bytes.
// If the decompressed body exceeds this limit, the middleware returns HTTP 413 error.
// This prevents zip bomb attacks where small compressed payloads decompress to huge sizes.
// Default: 100 * MB (104,857,600 bytes)
// Set to -1 to disable limits (not recommended in production).
MaxDecompressedSize int64
}
// GZIPEncoding content-encoding header if set to "gzip", decompress body contents.
const GZIPEncoding string = "gzip"
// Decompressor is used to get the sync.Pool used by the middleware to get Gzip readers
type Decompressor interface {
gzipDecompressPool() sync.Pool
}
// DefaultGzipDecompressPool is the default implementation of Decompressor interface
type DefaultGzipDecompressPool struct {
}
func (d *DefaultGzipDecompressPool) gzipDecompressPool() sync.Pool {
return sync.Pool{New: func() any { return new(gzip.Reader) }}
}
// Decompress decompresses request body based if content encoding type is set to "gzip" with default config
//
// SECURITY: By default, this limits decompressed data to 100MB to prevent zip bomb attacks.
// To customize the limit, use DecompressWithConfig. To disable limits (not recommended in production),
// set MaxDecompressedSize to -1.
func Decompress() echo.MiddlewareFunc {
return DecompressWithConfig(DecompressConfig{})
}
// DecompressWithConfig returns a decompress middleware with config or panics on invalid configuration.
//
// SECURITY: If MaxDecompressedSize is not set (zero value), it defaults to 100MB to prevent
// DoS attacks via zip bombs. Set to -1 to explicitly disable limits if needed for your use case.
func DecompressWithConfig(config DecompressConfig) echo.MiddlewareFunc {
return toMiddlewareOrPanic(config)
}
// ToMiddleware converts DecompressConfig to middleware or returns an error for invalid configuration
func (config DecompressConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
if config.Skipper == nil {
config.Skipper = DefaultSkipper
}
if config.GzipDecompressPool == nil {
config.GzipDecompressPool = &DefaultGzipDecompressPool{}
}
// Apply secure default for decompression limit
if config.MaxDecompressedSize == 0 {
config.MaxDecompressedSize = 100 * MB
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
pool := config.GzipDecompressPool.gzipDecompressPool()
return func(c *echo.Context) error {
if config.Skipper(c) {
return next(c)
}
if !isGzipContentEncoding(c.Request().Header.Get(echo.HeaderContentEncoding)) {
return next(c)
}
i := pool.Get()
gr, ok := i.(*gzip.Reader)
if !ok || gr == nil {
if err, isErr := i.(error); isErr {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return echo.NewHTTPError(http.StatusInternalServerError, "unexpected type from gzip decompression pool")
}
defer pool.Put(gr)
b := c.Request().Body
defer b.Close()
if err := gr.Reset(b); err != nil {
if err == io.EOF { //ignore if body is empty
return next(c)
}
return err
}
// only Close gzip reader if it was set to a proper gzip source otherwise it will panic on close.
defer gr.Close()
// Apply decompression size limit to prevent zip bombs
if config.MaxDecompressedSize > 0 {
c.Request().Body = &limitedGzipReader{
Reader: gr,
remaining: config.MaxDecompressedSize,
limit: config.MaxDecompressedSize,
}
} else {
// -1 means explicitly unlimited (not recommended)
c.Request().Body = gr
}
c.Request().ContentLength = -1
return next(c)
}
}, nil
}
// isGzipContentEncoding reports whether Content-Encoding is gzip.
// Content codings are case-insensitive per RFC 9110 §8.4.1.
func isGzipContentEncoding(v string) bool {
return strings.EqualFold(v, GZIPEncoding)
}
// limitedGzipReader wraps a gzip reader with size limiting to prevent zip bombs
type limitedGzipReader struct {
*gzip.Reader
remaining int64
limit int64
}
func (r *limitedGzipReader) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if r.remaining < 0 {
return 0, echo.ErrStatusRequestEntityTooLarge
}
if r.remaining == 0 {
// Reaching the limit is valid if there is no more decompressed data.
var probe [1]byte
n, err = r.Reader.Read(probe[:])
if n > 0 {
r.remaining = -1
return 0, echo.ErrStatusRequestEntityTooLarge
}
return 0, err
}
// Limit the read to remaining bytes
if int64(len(p)) > r.remaining {
p = p[:r.remaining]
}
n, err = r.Reader.Read(p)
r.remaining -= int64(n)
return n, err
}
func (r *limitedGzipReader) Close() error {
return r.Reader.Close()
}