Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: Do not allow OBS fold in headers by default. #350

Merged
merged 1 commit into from
Apr 4, 2024
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
13 changes: 8 additions & 5 deletions src/llhttp/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,14 @@ export class HTTP {
'Missing expected LF after header value'));

n('header_value_lws')
.peek([ ' ', '\t' ],
this.load('header_state', {
[HEADER_STATE.TRANSFER_ENCODING_CHUNKED]:
this.resetHeaderState(span.headerValue.start(n('header_value_start'))),
}, span.headerValue.start(n('header_value_start'))))
.peek(
[ ' ', '\t' ],
this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
1: this.load('header_state', {
[HEADER_STATE.TRANSFER_ENCODING_CHUNKED]:
this.resetHeaderState(span.headerValue.start(n('header_value_start'))),
}, span.headerValue.start(n('header_value_start'))),
}, p.error(ERROR.INVALID_HEADER_TOKEN, 'Unexpected whitespace after header value')))
.otherwise(this.setHeaderFlags(onHeaderValueComplete));

const checkTrailing = this.testFlags(FLAGS.TRAILING, {
Expand Down
2 changes: 2 additions & 0 deletions test/md-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const http: IFixtureMap = {
'request-lenient-version': buildMode( 'loose', 'request-lenient-version'),
'response': buildMode('loose', 'response'),
'response-finish': buildMode('loose', 'response-finish'),
'response-lenient-headers': buildMode( 'loose', 'response-lenient-headers'),
'response-lenient-keep-alive': buildMode( 'loose', 'response-lenient-keep-alive'),
'response-lenient-version': buildMode( 'loose', 'response-lenient-version'),
'url': buildMode('loose', 'url'),
Expand All @@ -101,6 +102,7 @@ const http: IFixtureMap = {
'request-lenient-version': buildMode( 'strict', 'request-lenient-version'),
'response': buildMode('strict', 'response'),
'response-finish': buildMode('strict', 'response-finish'),
'response-lenient-headers': buildMode('strict', 'response-lenient-headers'),
'response-lenient-keep-alive': buildMode('strict', 'response-lenient-keep-alive'),
'response-lenient-version': buildMode( 'strict', 'response-lenient-version'),
'url': buildMode('strict', 'url'),
Expand Down
4 changes: 2 additions & 2 deletions test/request/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ off=75 message complete

### Multiple tokens with folding

<!-- meta={"type": "request"} -->
<!-- meta={"type": "request-lenient-headers"} -->
```http
GET /demo HTTP/1.1
Host: example.com
Expand Down Expand Up @@ -397,7 +397,7 @@ off=75 error code=22 reason="Pause on CONNECT/Upgrade"

### Multiple tokens with folding, LWS, and CRLF

<!-- meta={"type": "request"} -->
<!-- meta={"type": "request-lenient-headers"} -->
```http
GET /demo HTTP/1.1
Connection: keep-alive, \r\n upgrade
Expand Down
72 changes: 72 additions & 0 deletions test/request/invalid.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,76 @@ off=0 message begin
off=4 len=1 span[url]="/"
off=6 url complete
off=14 error code=9 reason="Invalid HTTP version"
```

### Spaces before headers

<!-- meta={ "type": "request" } -->

```http
POST /hello HTTP/1.1
Host: localhost
Foo: bar
Content-Length: 38

GET /bye HTTP/1.1
Host: localhost


```

```log
off=0 message begin
off=5 len=6 span[url]="/hello"
off=12 url complete
off=22 len=4 span[header_field]="Host"
off=27 header_field complete
off=28 len=9 span[header_value]="localhost"
off=39 header_value complete
off=39 len=3 span[header_field]="Foo"
off=43 header_field complete
off=44 len=3 span[header_value]="bar"
off=49 error code=10 reason="Unexpected whitespace after header value"
```

### Spaces before headers (lenient)

<!-- meta={ "type": "request-lenient-headers" } -->

```http
POST /hello HTTP/1.1
Host: localhost
Foo: bar
Content-Length: 38

GET /bye HTTP/1.1
Host: localhost


```

```log
off=0 message begin
off=5 len=6 span[url]="/hello"
off=12 url complete
off=22 len=4 span[header_field]="Host"
off=27 header_field complete
off=28 len=9 span[header_value]="localhost"
off=39 header_value complete
off=39 len=3 span[header_field]="Foo"
off=43 header_field complete
off=44 len=3 span[header_value]="bar"
off=49 len=19 span[header_value]=" Content-Length: 38"
off=70 header_value complete
off=72 headers complete method=3 v=1/1 flags=0 content_length=0
off=72 message complete
off=72 message begin
off=76 len=4 span[url]="/bye"
off=81 url complete
off=91 len=4 span[header_field]="Host"
off=96 header_field complete
off=97 len=9 span[header_value]="localhost"
off=108 header_value complete
off=110 headers complete method=1 v=1/1 flags=0 content_length=0
off=110 message complete
```
2 changes: 1 addition & 1 deletion test/request/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ off=61 message complete

See nodejs/test/parallel/test-http-headers-obstext.js

<!-- meta={"type": "request"} -->
<!-- meta={"type": "request-lenient-headers"} -->
```http
GET / HTTP/1.1
X-SSL-Nonsense: -----BEGIN CERTIFICATE-----
Expand Down
2 changes: 1 addition & 1 deletion test/request/transfer-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ off=50 error code=12 reason="Invalid character in chunk size"

## Invalid OBS fold after chunked value

<!-- meta={"type": "request", "mode": "strict"} -->
<!-- meta={"type": "request-lenient-headers", "mode": "strict"} -->
```http
PUT /url HTTP/1.1
Transfer-Encoding: chunked
Expand Down
2 changes: 1 addition & 1 deletion test/response/transfer-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ off=78 len=1 span[body]=lf

## Invalid OBS fold after chunked value

<!-- meta={"type": "response" } -->
<!-- meta={"type": "response-lenient-headers"} -->
```http
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"outDir": "./lib",
"declaration": true,
"pretty": true,
"sourceMap": true
"sourceMap": true,
"skipLibCheck": true
},
"include": [
"src/**/*.ts"
Expand Down
Loading