Skip to content

Commit 101e028

Browse files
brianquinlanCommit Queue
authored andcommitted
[io] Fix a bug where NUL was allowed in HTTP headers.
Bug:#56636 Change-Id: I88c579cfaaf0884cb3b582084b8739b060d8f439 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402541 Reviewed-by: Alexander Aprelev <[email protected]> Commit-Queue: Brian Quinlan <[email protected]>
1 parent be2aabd commit 101e028

File tree

3 files changed

+358
-172
lines changed

3 files changed

+358
-172
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ AOT snapshot can be used as follows to run DDC <dart-sdk>/bin/dartaotruntime
223223
release. Users should migrate to using `dart:js_interop` and `package:web`.
224224
See [#59716][].
225225

226+
#### `dart:io`
227+
228+
- `HttpException` will be thrown by `HttpClient` and `HttpServer` if a `NUL`
229+
(`0x00`) appears in a received HTTP header value.
230+
226231
#### `dart:svg`
227232

228233
- `dart:svg` is marked deprecated and will be removed in an upcoming release.

sdk/lib/_http/http_parser.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
683683
_state = _State.HEADER_VALUE_FOLD_OR_END;
684684
} else if (byte != _CharCode.SP && byte != _CharCode.HT) {
685685
// Start of new header value.
686-
_addWithValidation(_headerValue, byte);
686+
_addToHeaderValueWithValidation(_headerValue, byte);
687687
_state = _State.HEADER_VALUE;
688688
}
689689
break;
@@ -694,7 +694,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
694694
} else if (byte == _CharCode.LF) {
695695
_state = _State.HEADER_VALUE_FOLD_OR_END;
696696
} else {
697-
_addWithValidation(_headerValue, byte);
697+
_addToHeaderValueWithValidation(_headerValue, byte);
698698
}
699699
break;
700700

@@ -710,7 +710,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
710710
// prior to interpreting the field value or forwarding the
711711
// message downstream."
712712
// See https://www.rfc-editor.org/rfc/rfc7230#section-3.2.4
713-
_addWithValidation(_headerValue, _CharCode.SP);
713+
_addToHeaderValueWithValidation(_headerValue, _CharCode.SP);
714714
_state = _State.HEADER_VALUE_START; // Strips leading whitespace.
715715
} else {
716716
String headerField = String.fromCharCodes(_headerField);
@@ -1117,6 +1117,16 @@ class _HttpParser extends Stream<_HttpIncoming> {
11171117
}
11181118
}
11191119

1120+
void _addToHeaderValueWithValidation(List<int> list, int byte) {
1121+
// From RFC-9110:
1122+
// Field values containing CR, LF, or NUL characters are invalid and
1123+
// dangerous.
1124+
if (byte == 0 || byte == _CharCode.LF || byte == _CharCode.CR) {
1125+
throw HttpException("Illegal value $byte in HTTP header");
1126+
}
1127+
_addWithValidation(list, byte);
1128+
}
1129+
11201130
void _addWithValidation(List<int> list, int byte) {
11211131
_headersReceivedSize++;
11221132
if (_headersReceivedSize < _headerTotalSizeLimit) {

0 commit comments

Comments
 (0)