Skip to content

Commit e31735c

Browse files
committed
UrlScript: added argument validation
1 parent 6a02df3 commit e31735c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Http/UrlScript.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Nette\Http;
1111

12+
use Nette;
13+
1214

1315
/**
1416
* Immutable representation of a URL with application base-path.
@@ -89,8 +91,12 @@ public function getPathInfo(): string
8991
protected function build(): void
9092
{
9193
parent::build();
92-
$this->scriptPath = $this->scriptPath ?: $this->getPath();
94+
$path = $this->getPath();
95+
$this->scriptPath = $this->scriptPath ?: $path;
9396
$pos = strrpos($this->scriptPath, '/');
94-
$this->basePath = $pos === false ? '' : substr($this->scriptPath, 0, $pos + 1);
97+
if ($pos === false || strncmp($this->scriptPath, $path, $pos + 1)) {
98+
throw new Nette\InvalidArgumentException("ScriptPath '$this->scriptPath' doesn't match path '$path'");
99+
}
100+
$this->basePath = substr($this->scriptPath, 0, $pos + 1);
95101
}
96102
}

tests/Http/UrlScript.error.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Http\UrlScript;
6+
use Tester\Assert;
7+
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::exception(function () {
13+
new UrlScript('http://nette.org/file.php?q=search', '/a/');
14+
}, Nette\InvalidArgumentException::class);
15+
16+
17+
Assert::exception(function () {
18+
new UrlScript('http://nette.org/file.php?q=search', 'a');
19+
}, Nette\InvalidArgumentException::class);
20+
21+
22+
Assert::exception(function () {
23+
new UrlScript('http://nette.org/dir/', '/d/');
24+
}, Nette\InvalidArgumentException::class);
25+
26+
27+
Assert::exception(function () {
28+
new UrlScript('http://nette.org/dir/', '/dir/index/');
29+
}, Nette\InvalidArgumentException::class);

0 commit comments

Comments
 (0)