Prevent invalid URL parameters from flooding logs.

This commit is contained in:
Buster "Silver Eagle" Neece 2020-04-06 18:15:59 -05:00
parent 6fcc8e600f
commit c3e4b7f187
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
2 changed files with 36 additions and 3 deletions

View File

@ -0,0 +1,18 @@
<?php
namespace App\Exception;
use App\Exception;
use Psr\Log\LogLevel;
use Throwable;
class InvalidRequestAttribute extends Exception
{
public function __construct(
string $message = 'Invalid request attribute.',
int $code = 0,
Throwable $previous = null,
string $loggerLevel = LogLevel::DEBUG
) {
parent::__construct($message, $code, $previous, $loggerLevel);
}
}

View File

@ -8,6 +8,7 @@ use App\Radio;
use App\RateLimit;
use App\Session;
use App\View;
use InvalidArgumentException;
use Mezzio\Session\SessionInterface;
class ServerRequest extends \Slim\Http\ServerRequest
@ -179,10 +180,24 @@ class ServerRequest extends \Slim\Http\ServerRequest
protected function getAttributeOfClass($attr, $class_name)
{
$object = $this->serverRequest->getAttribute($attr);
if ($object instanceof $class_name) {
return $object;
if (empty($object)) {
throw new Exception\InvalidRequestAttribute(sprintf(
'Attribute "%s" is required and is empty in this request',
$attr
));
}
throw new Exception(sprintf('Attribute %s must be of type %s.', $attr, $class_name));
if (!($object instanceof $class_name)) {
throw new InvalidArgumentException(sprintf(
'Attribute "%s" must be of type "%s".',
$attr,
$class_name
));
}
return $object;
}
}