Skip to content
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ this flag, control characters (except for NULL, CR & LF) will be accepted in hea
This does not create any known security issue, but does allow content considered 'invalid' by
[RFC 9110](https://www.rfc-editor.org/rfc/rfc9110#name-field-values) and so should be avoided by default.

### `void llhttp_set_lenient_optional_sp_after_status(llhttp_t* parser, int enabled)`

Enables/disables lenient handling of spaces after status code

As per [RFC 9112](https://www.rfc-editor.org/info/rfc9112/#section-4-8):
> A server MUST send the space that separates the status-code from the reason-phrase even when the reason-phrase is absent (i.e., the status-line would end with the space).

This flag allows status code with no trailing space.

## Build Instructions

Make sure you have [Node.js](https://nodejs.org/), npm and npx installed. Then under project directory run:
Expand Down
1 change: 1 addition & 0 deletions src/llhttp/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const LENIENT_FLAGS = {
OPTIONAL_CR_BEFORE_LF: 1 << 8,
SPACES_AFTER_CHUNK_SIZE: 1 << 9,
HEADER_VALUE_RELAXED: 1 << 10,
OPTIONAL_SP_AFTER_STATUS: 1 << 11,
} as const;

export const STATUSES = {
Expand Down
22 changes: 10 additions & 12 deletions src/llhttp/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const NODES = [
'res_status_code_digit_1',
'res_status_code_digit_2',
'res_status_code_digit_3',
'res_status_code_otherwise',
'res_status_trailing_space',
'res_status_start',
'res_status',
'res_line_almost_done',
Expand Down Expand Up @@ -385,25 +385,23 @@ export class HTTP {
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',
success: 'res_status_trailing_space',
}))
.otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));

const onStatusComplete = this.invokePausable(
'on_status_complete', ERROR.CB_STATUS_COMPLETE, n('headers_start'),
);

n('res_status_code_otherwise')
n('res_status_trailing_space')
.match(' ', n('res_status_start'))
.match('\r', n('res_line_almost_done'))
.match(
'\n',
checkIfAllowLFWithoutCR(
onStatusComplete,
p.error(ERROR.INVALID_STATUS, 'Invalid response status'),
),
)
.otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid response status'));
.otherwise(
this.testLenientFlags(
LENIENT_FLAGS.OPTIONAL_SP_AFTER_STATUS,
{ 1: n('res_line_almost_done') },
p.error(ERROR.INVALID_STATUS, 'Invalid response status')
)
);

n('res_status_start')
.otherwise(span.status.start(n('res_status')));
Expand Down
8 changes: 8 additions & 0 deletions src/native/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ void llhttp_set_lenient_header_value_relaxed(llhttp_t* parser, int enabled) {
}
}

void llhttp_set_lenient_optional_sp_after_status(llhttp_t* parser, int enabled) {
if (enabled) {
parser->lenient_flags |= LENIENT_OPTIONAL_SP_AFTER_STATUS;
} else {
parser->lenient_flags &= ~LENIENT_OPTIONAL_SP_AFTER_STATUS;
}
}

/* Callbacks */


Expand Down
12 changes: 12 additions & 0 deletions src/native/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled);
LLHTTP_EXPORT
void llhttp_set_lenient_header_value_relaxed(llhttp_t* parser, int enabled);

/* Enables/disables lenient handling of spaces after status code
*
* As per RFC 9112:
* A server MUST send the space that separates the status-code from the
* reason-phrase even when the reason-phrase is absent
* (i.e., the status-line would end with the space).
*
* This flag allows status code with no trailing space.
*/
LLHTTP_EXPORT
void llhttp_set_lenient_optional_sp_after_status(llhttp_t* parser, int enabled);

#ifdef __cplusplus
} /* extern "C" */
#endif
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ void llhttp__test_init_response_lenient_header_value_relaxed(llparse_t* s) {
s->lenient_flags |= LENIENT_HEADER_VALUE_RELAXED;
}

void llhttp__test_init_request_lenient_optional_sp_after_status(llparse_t* s) {
llhttp__test_init_request(s);
s->lenient_flags |= LENIENT_OPTIONAL_SP_AFTER_STATUS;
}

void llhttp__test_init_response_lenient_optional_sp_after_status(llparse_t* s) {
llhttp__test_init_response(s);
s->lenient_flags |= LENIENT_OPTIONAL_SP_AFTER_STATUS;
}


void llhttp__test_finish(llparse_t* s) {
llparse__print(NULL, NULL, "finish=%d", s->finish);
Expand Down
19 changes: 18 additions & 1 deletion test/response/invalid.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ off=5 len=3 span[version]="1.0"
off=8 version complete
off=8 error code=9 reason="Expected space after version"
```
-->

### Tab after HTTP version

Expand Down Expand Up @@ -266,6 +265,24 @@ off=18 status complete
off=20 headers complete status=200 v=1/1 flags=0 content_length=0
```

### No space after status code (no reason)

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


```

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

### One-digit status code

<!-- meta={"type": "response"} -->
Expand Down
19 changes: 0 additions & 19 deletions test/response/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,6 @@ off=24 status complete
off=26 headers complete status=404 v=1/1 flags=0 content_length=0
```

## No reason phrase

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


```

```log
off=0 message begin
off=0 len=4 span[protocol]="HTTP"
off=4 protocol complete
off=5 len=3 span[version]="1.1"
off=8 version complete
off=14 status complete
off=16 headers complete status=301 v=1/1 flags=0 content_length=0
```

## Empty reason phrase after space

<!-- meta={"type": "response"} -->
Expand Down