Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Stricter parsing of status line #217

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/llhttp/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const NODES: ReadonlyArray<string> = [
'res_http_minor',
'res_http_end',
'res_after_version',
'res_status_code',
'res_status_code_digit_1',
'res_status_code_digit_2',
'res_status_code_digit_3',
'res_status_code_otherwise',
'res_status_start',
'res_status',
Expand Down Expand Up @@ -334,16 +336,30 @@ export class HTTP {
));

n('res_after_version')
.match(' ', this.update('status_code', 0, 'res_status_code'))
.match(' ', this.update('status_code', 0, 'res_status_code_digit_1'))
.otherwise(p.error(ERROR.INVALID_VERSION,
'Expected space after version'));

n('res_status_code')
n('res_status_code_digit_1')
.select(NUM_MAP, this.mulAdd('status_code', {
overflow: p.error(ERROR.INVALID_STATUS, 'Response overflow'),
success: 'res_status_code',
}, { base: 10, signed: false, max: 999 }))
.otherwise(n('res_status_code_otherwise'));
overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
success: 'res_status_code_digit_2',
}))
.otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));

n('res_status_code_digit_2')
.select(NUM_MAP, this.mulAdd('status_code', {
overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
success: 'res_status_code_digit_3',
}))
.otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));

n('res_status_code_digit_3')
.select(NUM_MAP, this.mulAdd('status_code', {
overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
success: 'res_status_code_otherwise',
}))
.otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));

n('res_status_code_otherwise')
.match(' ', n('res_status_start'))
Expand Down
50 changes: 50 additions & 0 deletions test/response/invalid.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,54 @@ off=8 version complete
off=13 len=2 span[status]="OK"
off=17 status complete
off=18 error code=30 reason="Unexpected space after start line"
```

### Extra space between HTTP version and status code

<!-- meta={"type": "response-only"} -->
```http
HTTP/1.1 200 OK


```

```log
off=0 message begin
off=5 len=3 span[version]="1.1"
off=8 version complete
off=9 error code=13 reason="Invalid status code"
```

### Extra space between status code and reason

<!-- meta={"type": "response-only"} -->
```http
HTTP/1.1 200 OK


```

```log
off=0 message begin
off=5 len=3 span[version]="1.1"
off=8 version complete
off=13 len=3 span[status]=" OK"
off=18 status complete
off=20 headers complete status=200 v=1/1 flags=0 content_length=0
```

### One-digit status code

<!-- meta={"type": "response-only"} -->
```http
HTTP/1.1 2 OK


```

```log
off=0 message begin
off=5 len=3 span[version]="1.1"
off=8 version complete
off=10 error code=13 reason="Invalid status code"
```