Skip to content

Commit 5a8794a

Browse files
committed
fix: check input encodings
1 parent 2b2faaf commit 5a8794a

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/middleware.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,53 @@ import {
2222
ZstdCompressionStream,
2323
} from './streams'
2424

25+
function checkCompressEncodings(encodings: CompressionEncoding[]) {
26+
const unsupportedEncoding: string | undefined = encodings.find(
27+
(enc) => !ACCEPTED_ENCODINGS.includes(enc),
28+
)
29+
if (unsupportedEncoding) {
30+
throw new Error(`Invalid compression encoding: ${unsupportedEncoding}`)
31+
}
32+
}
33+
2534
function checkResposeType(c: Context) {
26-
// skip no content
35+
// NOTE: skip no content
2736
if (!c.res.body) {
2837
throw Error
2938
}
3039

31-
// skip head request
40+
// NOTE: skip head request
3241
if (c.req.method === 'HEAD') {
3342
throw Error
3443
}
3544
}
3645

3746
function checkResponseCompressible(c: Context, threshold: number) {
38-
// skip already encoded
47+
// NOTE: skip already encoded
3948
if (c.res.headers.has('Content-Encoding')) {
4049
throw Error
4150
}
4251

4352
const contentLength = Number(c.res.headers.get('Content-Length'))
4453

45-
// skip small size content
54+
// NOTE: skip small size content
4655
if (contentLength && contentLength < threshold) {
4756
throw Error
4857
}
4958

50-
// skip un-compressible content
59+
// NOTE: skip un-compressible content
5160
if (!shouldCompress(c.res)) {
5261
throw Error
5362
}
5463

55-
// skip un-transformable content
64+
// NOTE: skip un-transformable content
5665
if (!shouldTransform(c.res)) {
5766
throw Error
5867
}
5968
}
6069

6170
function checkResponseFilter(c: Context, filter: CompressionFilter | null | undefined) {
62-
// skip by filter callback result or already compressing runtimes
71+
// NOTE: skip by callback result or if an already compressing runtime
6372
if (filter != null) {
6473
if (!filter(c)) {
6574
throw Error
@@ -75,7 +84,6 @@ function getAcceptedEncoding(c: Context, encodings: CompressionEncoding[]) {
7584
if (!acceptedEncoding) {
7685
return
7786
}
78-
7987
return encodings.find((enc) => acceptedEncoding.includes(enc))
8088
}
8189

@@ -89,16 +97,19 @@ export function compress({
8997
options = {},
9098
filter,
9199
}: CompressOptions = {}): MiddlewareHandler {
92-
// NOTE: uses `encoding` as the only compression scheme
100+
// NOTE: use `encoding` as the only compression scheme
93101
if (encoding) {
94102
encodings = [encoding]
95103
}
96104
options = { ...options, level: zlibLevel }
97105

106+
// NOTE: fail if unsupported encodings
107+
checkCompressEncodings(encodings)
108+
98109
return async function compress(c, next) {
99110
await next()
100111

101-
// skip checks failed
112+
// NOTE: skip if checks failed
102113
try {
103114
checkResposeType(c)
104115
checkResponseCompressible(c, threshold)
@@ -109,7 +120,7 @@ export function compress({
109120

110121
const enc = getAcceptedEncoding(c, encodings)
111122

112-
// skip no accepted encoding
123+
// NOTE: skip if no accepted encoding
113124
if (!enc) {
114125
return
115126
}

0 commit comments

Comments
 (0)