Skip to content

Commit 07f9043

Browse files
authored
Action params can also be arrays of whatever, not just strings (#474)
Docblocks now reflect this. This reverts e297212 of #443 which means no exception is being thrown on non-string params when some bots scan try to exploit some bugs with arrays.
2 parents fdb8784 + 41ab924 commit 07f9043

13 files changed

+18
-181
lines changed

app/config/services.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ services:
33
cliArgs:
44
type: MichalSpacekCz\Application\Cli\CliArgs
55
imported: true
6-
- MichalSpacekCz\Application\ComponentParameters
76
- MichalSpacekCz\Application\Error
87
- MichalSpacekCz\Application\LinkGenerator
98
localeLinkGenerator: MichalSpacekCz\Application\Locale\LocaleLinkGenerator(languages: %locales.languages%)

app/src/Application/AppRequest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace MichalSpacekCz\Application;
55

66
use MichalSpacekCz\Application\Exceptions\NoOriginalRequestException;
7-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
87
use MichalSpacekCz\ShouldNotHappenException;
98
use Nette\Application\Request;
109
use Throwable;
@@ -28,26 +27,6 @@ public function getOriginalRequest(?Request $request): Request
2827
}
2928

3029

31-
/**
32-
* @return array<string, string|null>
33-
* @throws NoOriginalRequestException
34-
* @throws ParameterNotStringException
35-
*/
36-
public function getOriginalRequestStringParameters(?Request $request): array
37-
{
38-
$params = [];
39-
foreach ($this->getOriginalRequest($request)->getParameters() as $name => $value) {
40-
$name = (string)$name;
41-
if ($value === null || is_string($value)) {
42-
$params[$name] = $value;
43-
} else {
44-
throw new ParameterNotStringException($name, get_debug_type($value));
45-
}
46-
}
47-
return $params;
48-
}
49-
50-
5130
public function getException(Request $request): Throwable
5231
{
5332
$e = $request->getParameter('exception');

app/src/Application/ComponentParameters.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/src/Application/Exceptions/ParameterNotStringException.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/Application/LinkGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(
1919
/**
2020
* Same as `Nette\Application\LinkGenerator::link()` but will always return just string, not string|null.
2121
*
22-
* @param array<int|string, string|null> $args
22+
* @param array<array-key, mixed> $args
2323
* @throws InvalidLinkException
2424
*/
2525
public function link(string $destination, array $args = [], ?NetteLinkGenerator $linkGenerator = null): string

app/src/Application/Locale/LocaleLinkGenerator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(
4040
* Generates localized URLs.
4141
*
4242
* @param string $destination destination in format "[[[module:]presenter:]action] [#fragment]"
43-
* @param array<string, list<string>|array<string, string|null>> $params of locale => [position|name => value]
43+
* @param array<string, list<string>|array<array-key, mixed>> $params of locale => [position|name => value]
4444
* @return array<string, LocaleLink> of locale => URL
4545
* @throws InvalidLinkException
4646
*/
@@ -70,8 +70,8 @@ public function links(string $destination, array $params = []): array
7070
/**
7171
* Return default params for all locales.
7272
*
73-
* @param array<string, string|null> $params
74-
* @return array<string, array<string, string|null>>
73+
* @param array<array-key, mixed> $params
74+
* @return array<string, array<array-key, mixed>>
7575
*/
7676
public function defaultParams(array $params): array
7777
{
@@ -114,9 +114,9 @@ public function allLinks(string $destination, array $params = []): array
114114

115115

116116
/**
117-
* @param array<string, list<string>|array<string, string|null>> $params
117+
* @param array<string, list<string>|array<array-key, mixed>> $params
118118
* @param string $locale
119-
* @return list<string>|array<string, string|null>
119+
* @return list<string>|array<array-key, mixed>
120120
*/
121121
private function getParams(array $params, string $locale): array
122122
{

app/src/Training/TrainingLocales.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function getLocaleActions(string $action): array
4747
/**
4848
* Translated locale parameters for trainings.
4949
*
50-
* @param array<string, string|null> $defaultParams
51-
* @return array<string, array<string, string|null>>
50+
* @param array<array-key, mixed> $defaultParams
51+
* @return array<string, array<array-key, mixed>>
5252
*/
5353
public function getLocaleLinkParams(?string $trainingAction, array $defaultParams): array
5454
{

app/src/Www/Presenters/BasePresenter.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace MichalSpacekCz\Www\Presenters;
55

66
use DateTimeInterface;
7-
use MichalSpacekCz\Application\ComponentParameters;
8-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
97
use MichalSpacekCz\Application\Locale\LocaleLink;
108
use MichalSpacekCz\Application\Locale\LocaleLinkGenerator;
119
use MichalSpacekCz\Css\CriticalCss;
@@ -37,8 +35,6 @@ abstract class BasePresenter extends Presenter
3735

3836
private CriticalCssFactory $criticalCssFactory;
3937

40-
private ComponentParameters $componentParameters;
41-
4238
private FourOhFourButFound $fourOhFourButFound;
4339

4440

@@ -87,15 +83,6 @@ public function injectCriticalCssFactory(CriticalCssFactory $criticalCssFactory)
8783
}
8884

8985

90-
/**
91-
* @internal
92-
*/
93-
public function injectComponentParameters(ComponentParameters $componentParameters): void
94-
{
95-
$this->componentParameters = $componentParameters;
96-
}
97-
98-
9986
/**
10087
* @internal
10188
*/
@@ -117,9 +104,6 @@ protected function startup(): void
117104
}
118105

119106

120-
/**
121-
* @throws ParameterNotStringException
122-
*/
123107
#[Override]
124108
public function beforeRender(): void
125109
{
@@ -138,8 +122,7 @@ protected function getLocaleLinksGeneratorDestination(): string
138122

139123

140124
/**
141-
* @return array<string, array<string, string|null>>
142-
* @throws ParameterNotStringException
125+
* @return array<string, array<array-key, mixed>>
143126
*/
144127
protected function getLocaleLinksGeneratorParams(): array
145128
{
@@ -170,12 +153,11 @@ protected function getLocaleLinkAction(): string
170153
/**
171154
* Default parameters for locale links.
172155
*
173-
* @return array<string, array<string, string|null>>
174-
* @throws ParameterNotStringException
156+
* @return array<string, array<array-key, mixed>>
175157
*/
176158
protected function getLocaleLinkParams(): array
177159
{
178-
return $this->localeLinkGenerator->defaultParams($this->componentParameters->getStringParameters($this));
160+
return $this->localeLinkGenerator->defaultParams($this->getParameters());
179161
}
180162

181163

app/src/Www/Presenters/CompanyTrainingsPresenter.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace MichalSpacekCz\Www\Presenters;
55

66
use Contributte\Translation\Translator;
7-
use MichalSpacekCz\Application\ComponentParameters;
8-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
97
use MichalSpacekCz\Formatter\TexyFormatter;
108
use MichalSpacekCz\Training\Company\CompanyTrainings;
119
use MichalSpacekCz\Training\Discontinued\DiscontinuedTrainings;
@@ -32,7 +30,6 @@ public function __construct(
3230
private readonly TrainingReviews $trainingReviews,
3331
private readonly Prices $prices,
3432
private readonly Translator $translator,
35-
private readonly ComponentParameters $componentParameters,
3633
) {
3734
parent::__construct();
3835
}
@@ -72,13 +69,12 @@ public function actionTraining(string $name): void
7269
/**
7370
* Translated locale parameters for trainings.
7471
*
75-
* @return array<string, array<string, string|null>>
76-
* @throws ParameterNotStringException
72+
* @return array<string, array<array-key, mixed>>
7773
*/
7874
#[Override]
7975
protected function getLocaleLinkParams(): array
8076
{
81-
return $this->trainingLocales->getLocaleLinkParams($this->trainingAction, $this->componentParameters->getStringParameters($this));
77+
return $this->trainingLocales->getLocaleLinkParams($this->trainingAction, $this->getParameters());
8278
}
8379

8480
}

app/src/Www/Presenters/ErrorPresenter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Contributte\Translation\Translator;
77
use MichalSpacekCz\Application\AppRequest;
88
use MichalSpacekCz\Application\Exceptions\NoOriginalRequestException;
9-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
109
use MichalSpacekCz\Application\Locale\LocaleLink;
1110
use MichalSpacekCz\Application\Locale\LocaleLinkGenerator;
1211
use MichalSpacekCz\ShouldNotHappenException;
@@ -112,15 +111,14 @@ protected function getLocaleLinkAction(): string
112111
/**
113112
* Get original parameters for locale links.
114113
*
115-
* @return array<string, array<string, string|null>>
114+
* @return array<string, array<array-key, mixed>>
116115
* @throws NoOriginalRequestException
117-
* @throws ParameterNotStringException
118116
*/
119117
#[Override]
120118
protected function getLocaleLinkParams(): array
121119
{
122-
$params = $this->appRequest->getOriginalRequestStringParameters($this->getRequest());
123-
return $this->localeLinkGenerator->defaultParams($params);
120+
$requestParam = $this->appRequest->getOriginalRequest($this->getRequest());
121+
return $this->localeLinkGenerator->defaultParams($requestParam->getParameters());
124122
}
125123

126124
}

app/src/Www/Presenters/TrainingsPresenter.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace MichalSpacekCz\Www\Presenters;
55

66
use Contributte\Translation\Translator;
7-
use MichalSpacekCz\Application\ComponentParameters;
8-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
97
use MichalSpacekCz\CompanyInfo\CompanyInfo;
108
use MichalSpacekCz\Form\TrainingApplicationFormFactory;
119
use MichalSpacekCz\Form\TrainingApplicationPreliminaryFormFactory;
@@ -66,7 +64,6 @@ public function __construct(
6664
private readonly Translator $translator,
6765
private readonly Session $sessionHandler,
6866
private readonly Robots $robots,
69-
private readonly ComponentParameters $componentParameters,
7067
) {
7168
parent::__construct();
7269
}
@@ -308,13 +305,12 @@ protected function createComponentOtherUpcomingDatesList(): UpcomingTrainingDate
308305
/**
309306
* Translated locale parameters for trainings.
310307
*
311-
* @return array<string, array<string, string|null>>
312-
* @throws ParameterNotStringException
308+
* @return array<string, array<array-key, mixed>>
313309
*/
314310
#[Override]
315311
protected function getLocaleLinkParams(): array
316312
{
317-
return $this->trainingLocales->getLocaleLinkParams($this->trainingAction, $this->componentParameters->getStringParameters($this));
313+
return $this->trainingLocales->getLocaleLinkParams($this->trainingAction, $this->getParameters());
318314
}
319315

320316

app/tests/Application/AppRequestTest.phpt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use DateTime;
88
use Error;
99
use Exception;
1010
use MichalSpacekCz\Application\Exceptions\NoOriginalRequestException;
11-
use MichalSpacekCz\Application\Exceptions\ParameterNotStringException;
1211
use MichalSpacekCz\ShouldNotHappenException;
1312
use MichalSpacekCz\Test\TestCaseRunner;
1413
use Nette\Application\Request;
@@ -64,26 +63,6 @@ class AppRequestTest extends TestCase
6463
}
6564

6665

67-
public function testGetOriginalRequestStringParameters(): void
68-
{
69-
$original = new Request('bar', params: ['foo' => 'bar', 1 => 'one']);
70-
$request = new Request('foo');
71-
$request->setParameters(['request' => $original]);
72-
Assert::same(['foo' => 'bar', '1' => 'one'], $this->appRequest->getOriginalRequestStringParameters($request));
73-
}
74-
75-
76-
public function testGetOriginalRequestStringParametersException(): void
77-
{
78-
$original = new Request('bar', params: ['foo' => 'bar', 'one' => 1]);
79-
$request = new Request('foo');
80-
$request->setParameters(['request' => $original]);
81-
Assert::exception(function () use ($request): void {
82-
$this->appRequest->getOriginalRequestStringParameters($request);
83-
}, ParameterNotStringException::class, "Component parameter 'one' is not a string but it's a int");
84-
}
85-
86-
8766
public function testGetExceptionNoException(): void
8867
{
8968
Assert::exception(function (): void {

app/tests/Application/ComponentParametersTest.phpt

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)