Skip to content

Commit 7596c64

Browse files
tatsuhiro-tindutny
authored andcommitted
http: require digits in TE chunk size
The chunk `\r\n\r\n` should not be treated as `0\r\n\r\n`, and should error.
1 parent bd3d224 commit 7596c64

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/llhttp/http.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const NODES: ReadonlyArray<string> = [
6868
'headers_done',
6969

7070
'chunk_size_start',
71+
'chunk_size_digit',
7172
'chunk_size',
7273
'chunk_size_otherwise',
7374
'chunk_size_almost_done',
@@ -589,13 +590,20 @@ export class HTTP {
589590
.skipTo(n('eof'));
590591

591592
n('chunk_size_start')
592-
.otherwise(this.update('content_length', 0, 'chunk_size'));
593+
.otherwise(this.update('content_length', 0, 'chunk_size_digit'));
594+
595+
const addContentLength = this.mulAdd('content_length', {
596+
overflow: p.error(ERROR.INVALID_CHUNK_SIZE, 'Chunk size overflow'),
597+
success: 'chunk_size',
598+
}, { signed: false, base: 0x10 });
599+
600+
n('chunk_size_digit')
601+
.select(HEX_MAP, addContentLength)
602+
.otherwise(p.error(ERROR.INVALID_CHUNK_SIZE,
603+
'Invalid character in chunk size'));
593604

594605
n('chunk_size')
595-
.select(HEX_MAP, this.mulAdd('content_length', {
596-
overflow: p.error(ERROR.INVALID_CHUNK_SIZE, 'Chunk size overflow'),
597-
success: 'chunk_size',
598-
}, { signed: false, base: 0x10 }))
606+
.select(HEX_MAP, addContentLength)
599607
.otherwise(n('chunk_size_otherwise'));
600608

601609
n('chunk_size_otherwise')

test/request/transfer-encoding.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,28 @@ off=117 headers complete method=3 v=1/1 flags=20 content_length=5
253253
off=117 len=5 span[body]="World"
254254
off=122 message complete
255255
```
256+
257+
## Missing last-chunk
258+
259+
<!-- meta={"type": "request"} -->
260+
```http
261+
PUT /url HTTP/1.1
262+
Transfer-Encoding: chunked
263+
264+
3
265+
foo
266+
267+
268+
```
269+
270+
```log
271+
off=0 message begin
272+
off=4 len=4 span[url]="/url"
273+
off=19 len=17 span[header_field]="Transfer-Encoding"
274+
off=38 len=7 span[header_value]="chunked"
275+
off=49 headers complete method=4 v=1/1 flags=8 content_length=0
276+
off=52 chunk header len=3
277+
off=52 len=3 span[body]="foo"
278+
off=57 chunk complete
279+
off=57 error code=12 reason="Invalid character in chunk size"
280+
```

0 commit comments

Comments
 (0)