Skip to content

Commit

Permalink
Request: Fix Referrer getter for malformed data + test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed Mar 2, 2022
1 parent 1731439 commit b182d4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,16 @@ public function getHeaders(): array
*/
public function getReferer(): ?UrlImmutable
{
return isset($this->headers['referer'])
? new UrlImmutable($this->headers['referer'])
: null;
if (!isset($this->headers['referer'])) {
return null;
}

try {
return new UrlImmutable($this->headers['referer']);
} catch (Nette\InvalidArgumentException $e) {
trigger_error("Unable to parse Malformed Referer URI: {$this->headers['referer']}");
return null;
}
}


Expand Down
40 changes: 40 additions & 0 deletions tests/Http/Request.referer.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Test: Nette\Http\Request headers.
*/

declare(strict_types=1);

use Nette\Http;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('', function () {
$request = new Http\Request(new Http\UrlScript);

Assert::null($request->getReferer());
});


test('', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, [
'referer' => 'http://nette.org:8080/file.php?q=search',
]);

Assert::same('http://nette.org:8080/file.php?q=search', $request->getReferer()->getAbsoluteUrl());
});


test('', function () {
$request = new Http\Request(new Http\UrlScript, null, null, null, [
'referer' => '/////',
]);

Assert::error(function () use ($request) {
Assert::null($request->getReferer());
}, E_USER_NOTICE, 'Unable to parse Malformed Referer URI: /////');
});

0 comments on commit b182d4e

Please sign in to comment.