Skip to content

Commit 61f4673

Browse files
committed
Make sure Easter egg substrings are not used as blog post slugs or talk actions
1 parent 416be91 commit 61f4673

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

app/src/EasterEgg/FourOhFourButFound.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,13 @@ public function sendItMaybe(Presenter $presenter): void
3838
}
3939
}
4040

41+
42+
/**
43+
* @return list<string>
44+
*/
45+
public function getRequestSubstrings(): array
46+
{
47+
return array_keys(self::TEMPLATES);
48+
}
49+
4150
}

app/src/Form/FormValidators.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MichalSpacekCz\Form;
55

66
use Contributte\Translation\Translator;
7+
use MichalSpacekCz\EasterEgg\FourOhFourButFound;
78
use Nette\Forms\Controls\TextInput;
89
use Nette\Forms\Form;
910

@@ -12,13 +13,16 @@
1213

1314
public function __construct(
1415
private Translator $translator,
16+
private FourOhFourButFound $fourOhFourButFound,
1517
) {
1618
}
1719

1820

1921
public function addValidateSlugRules(TextInput $input): void
2022
{
21-
$input->addRule(Form::Pattern, $this->translator->translate('messages.forms.validateSlugParamsError'), '[a-z0-9.,_-]+');
23+
$input
24+
->addRule(Form::Pattern, $this->translator->translate('messages.forms.validateSlugParamsError'), '[a-z0-9.,_-]+')
25+
->addRule(Form::IsNotIn, $this->translator->translate('messages.forms.validateSlugParamsEasterEgg'), $this->fourOhFourButFound->getRequestSubstrings());
2226
}
2327

2428
}

app/src/lang/messages.cs_CZ.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,4 @@ httpHeaders:
398398
headerNotSent: "hlavička neposlána"
399399
forms:
400400
validateSlugParamsError: "%label musí odpovídat formátu %d"
401+
validateSlugParamsEasterEgg: "%label nemůže být '%value', protože to spustí Easter egg"

app/src/lang/messages.en_US.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,4 @@ httpHeaders:
398398
headerNotSent: "header not sent"
399399
forms:
400400
validateSlugParamsError: "%label must match %d"
401+
validateSlugParamsEasterEgg: "%label can't be '%value', because that's used by an Easter egg"

app/tests/EasterEgg/FourOhFourButFoundTest.phpt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class FourOhFourButFoundTest extends TestCase
3939
['/etc/foo?file=..%2F..%2F..%2Fetc%2Fpasswd', 'rick:x:1337:1337:Astley'],
4040
['/etc/foo?file=../../../etc/passwd&foo/bar', 'rick:x:1337:1337:Astley'],
4141
['/etc/foo?file=..%2F..%2F..%2Fetc%2Fpasswd&foo/bar', 'rick:x:1337:1337:Astley'],
42+
['/?%adfoo', 'Parse error'],
43+
['/?%ad=/etc/passwd&bar', 'Parse error'],
4244
];
4345
}
4446

@@ -57,13 +59,8 @@ class FourOhFourButFoundTest extends TestCase
5759
$this->fourOhFourButFound->sendItMaybe($presenter);
5860
}));
5961
$response = $presenter->getResponse();
60-
if (!$response instanceof TextResponse) {
61-
Assert::fail('Response is of a wrong type ' . get_debug_type($response));
62-
} elseif (!is_string($response->getSource())) {
63-
Assert::fail('Source should be a string but is ' . get_debug_type($response->getSource()));
64-
} else {
65-
Assert::contains($contains, $response->getSource());
66-
}
62+
assert($response instanceof TextResponse && is_string($response->getSource()));
63+
Assert::contains($contains, $response->getSource());
6764
}
6865
}
6966

0 commit comments

Comments
 (0)