Increased referer length to 1024 and ensured values are cropped before trying to insert in database

This commit is contained in:
Alejandro Celaya
2019-08-24 10:25:43 +02:00
parent b31236958b
commit 56d0383170
8 changed files with 121 additions and 7 deletions

View File

@@ -6,8 +6,14 @@ namespace Shlinkio\Shlink\Core\Model;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory;
use function substr;
final class Visitor
{
public const USER_AGENT_MAX_LENGTH = 512;
public const REFERER_MAX_LENGTH = 1024;
public const REMOTE_ADDRESS_MAX_LENGTH = 256;
/** @var string */
private $userAgent;
/** @var string */
@@ -17,9 +23,14 @@ final class Visitor
public function __construct(string $userAgent, string $referer, ?string $remoteAddress)
{
$this->userAgent = $userAgent;
$this->referer = $referer;
$this->remoteAddress = $remoteAddress;
$this->userAgent = $this->cropToLength($userAgent, self::USER_AGENT_MAX_LENGTH);
$this->referer = $this->cropToLength($referer, self::REFERER_MAX_LENGTH);
$this->remoteAddress = $this->cropToLength($remoteAddress, self::REMOTE_ADDRESS_MAX_LENGTH);
}
private function cropToLength(?string $value, int $length): ?string
{
return $value === null ? null : substr($value, 0, $length);
}
public static function fromRequest(ServerRequestInterface $request): self