Skip to content

Commit 061b263

Browse files
committed
Filter the headers
1 parent ce92b11 commit 061b263

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/HttpWorker.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,27 @@ private function hydrateRequest(Request $request, array $context): void
124124
\parse_str($context['rawQuery'], $request->query);
125125

126126
$request->attributes = (array)($context['attributes'] ?? []);
127-
$request->headers = (array)($context['headers'] ?? []);
127+
$request->headers = $this->filterHeaders((array)($context['headers'] ?? []));
128128
$request->cookies = (array)($context['cookies'] ?? []);
129129
$request->uploads = (array)($context['uploads'] ?? []);
130130
$request->parsed = (bool)$context['parsed'];
131131
}
132+
133+
/**
134+
* @param array<mixed, mixed> $headers
135+
*
136+
* @return array<string, mixed>
137+
*/
138+
private function filterHeaders(array $headers): array
139+
{
140+
foreach ($headers as $key => $_) {
141+
if (!\is_string($key) || $key === '') {
142+
// ignore invalid header names or values (otherwise, the worker will be crashed)
143+
// @see: <https://git.io/JzjgJ>
144+
unset($headers[$key]);
145+
}
146+
}
147+
148+
return $headers;
149+
}
132150
}

src/PSR7Worker.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ protected function mapRequest(Request $httpRequest, array $server): ServerReques
188188
}
189189

190190
foreach ($httpRequest->headers as $name => $value) {
191-
try {
192-
$request = $request->withHeader($name, $value);
193-
} catch (\InvalidArgumentException $e) {
194-
// ignore invalid header names or values (otherwise, the worker will be crashed)
195-
// @see: Nyholm/psr7 <https://git.io/JzjgJ>
196-
}
191+
$request = $request->withHeader($name, $value);
197192
}
198193

199194
if ($httpRequest->parsed) {

0 commit comments

Comments
 (0)