Skip to content

Commit 3813eb9

Browse files
Merge pull request #1747 from specialtactics/bugfix/deal-with-weird-exceptions
Deal with bad actors who throw http exceptions with invalid codes
2 parents 42c5b78 + 5a51774 commit 3813eb9

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Exception/Handler.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected function genericResponse(Throwable $exception)
183183

184184
$response = $this->newResponseArray();
185185

186-
array_walk_recursive($response, function (&$value, $key) use ($exception, $replacements) {
186+
array_walk_recursive($response, function (&$value, $key) use ($replacements) {
187187
if (Str::startsWith($value, ':') && isset($replacements[$value])) {
188188
$value = $replacements[$value];
189189
}
@@ -203,11 +203,23 @@ protected function genericResponse(Throwable $exception)
203203
*/
204204
protected function getStatusCode(Throwable $exception)
205205
{
206+
$statusCode = null;
207+
206208
if ($exception instanceof ValidationException) {
207-
return $exception->status;
209+
$statusCode = $exception->status;
210+
} elseif ($exception instanceof HttpExceptionInterface) {
211+
$statusCode = $exception->getStatusCode();
212+
} else {
213+
// By default throw 500
214+
$statusCode = 500;
215+
}
216+
217+
// Be extra defensive
218+
if ($statusCode < 100 || $statusCode > 500) {
219+
$statusCode = 500;
208220
}
209221

210-
return $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
222+
return $statusCode;
211223
}
212224

213225
/**

0 commit comments

Comments
 (0)