From 88fa2e6505b987d882b207771e961b4f778d3f80 Mon Sep 17 00:00:00 2001 From: ItsNiklas Date: Sat, 18 Nov 2023 22:24:04 +0100 Subject: [PATCH 1/4] Extension filter checks full extension, including dot --- webapp/src/Service/SubmissionService.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/webapp/src/Service/SubmissionService.php b/webapp/src/Service/SubmissionService.php index 73f656e9e5..c2e0520963 100644 --- a/webapp/src/Service/SubmissionService.php +++ b/webapp/src/Service/SubmissionService.php @@ -530,8 +530,9 @@ public function submitSolution( if ($source !== 'shadowing' && $language->getFilterCompilerFiles()) { $matchesExtension = false; foreach ($language->getExtensions() as $extension) { - $extensionLength = strlen($extension); - if (substr($file->getClientOriginalName(), -$extensionLength) === $extension) { + $extensionWithDot = '.' . ltrim($extension, '.'); + $extensionLength = strlen($extensionWithDot); + if (substr($file->getClientOriginalName(), -$extensionLength) === $extensionWithDot) { $matchesExtension = true; break; } From 0cddbb00c7fe7cf10d04896029daaf8021ef142a Mon Sep 17 00:00:00 2001 From: ItsNiklas Date: Tue, 21 Nov 2023 00:31:13 +0100 Subject: [PATCH 2/4] Assert extensions don't start with a dot Including migration, fix invalid-feedback divs --- webapp/migrations/Version20231120225210.php | 54 +++++++++++++++++++ webapp/src/Entity/Language.php | 6 +++ .../jury/partials/language_form.html.twig | 4 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 webapp/migrations/Version20231120225210.php diff --git a/webapp/migrations/Version20231120225210.php b/webapp/migrations/Version20231120225210.php new file mode 100644 index 0000000000..4f2bedd3cc --- /dev/null +++ b/webapp/migrations/Version20231120225210.php @@ -0,0 +1,54 @@ +connection->fetchAllAssociative('SELECT langid, extensions FROM language'); + + foreach ($languages as $language) { + $extensions = json_decode($language['extensions'], true); + + $updated = false; + foreach ($extensions as &$extension) { + if (strpos($extension, '.') === 0) { + $extension = ltrim($extension, '.'); + $updated = true; + } + } + + if ($updated) { + $newExtensionsJson = json_encode($extensions); + $this->connection->executeQuery('UPDATE language SET extensions = :extensions WHERE langid = :langid', [ + 'extensions' => $newExtensionsJson, + 'langid' => $language['langid'], + ]); + } + } + } + + public function down(Schema $schema): void + { + // This migration is not reversible + } + + public function isTransactional(): bool + { + return false; + } +} diff --git a/webapp/src/Entity/Language.php b/webapp/src/Entity/Language.php index 2ff25e0e0d..4d2e76627e 100644 --- a/webapp/src/Entity/Language.php +++ b/webapp/src/Entity/Language.php @@ -53,6 +53,12 @@ class Language extends BaseApiEntity options: ['comment' => 'List of recognized extensions (JSON encoded)'] )] #[Assert\NotBlank] + #[Assert\All([ + new Assert\Regex([ + 'pattern' => '/^[^.]/', + 'message' => 'The extension should not start with a dot.' + ]) + ])] #[Serializer\Type('array')] private array $extensions = []; diff --git a/webapp/templates/jury/partials/language_form.html.twig b/webapp/templates/jury/partials/language_form.html.twig index 82e9d7b1ad..7409b3958f 100644 --- a/webapp/templates/jury/partials/language_form.html.twig +++ b/webapp/templates/jury/partials/language_form.html.twig @@ -18,7 +18,7 @@ addExtension($extensionsHolder, $addExtensionButton); }); - $extensionsHolder.find('div').each(function() { + $extensionsHolder.find('div:not(.invalid-feedback)').each(function() { addDeleteLink($(this)); }); @@ -37,8 +37,10 @@ var $removeFormButton = $(''); var $inputGroup = $('
'); var $formControl = $extensionDiv.find('.form-control'); + var $feedbackBlock = $extensionDiv.find('.invalid-feedback'); $inputGroup.append($formControl); $inputGroup.append($removeFormButton); + $inputGroup.append($feedbackBlock); $extensionDiv.html($inputGroup); $removeFormButton.on('click', function(e) { From 8812ee9b9759f89d414ebdfde59174c49750d67a Mon Sep 17 00:00:00 2001 From: ItsNiklas Date: Tue, 21 Nov 2023 00:56:35 +0100 Subject: [PATCH 3/4] Submission: Don't ltrim extension --- webapp/src/Service/SubmissionService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/Service/SubmissionService.php b/webapp/src/Service/SubmissionService.php index c2e0520963..9d503fffee 100644 --- a/webapp/src/Service/SubmissionService.php +++ b/webapp/src/Service/SubmissionService.php @@ -530,7 +530,7 @@ public function submitSolution( if ($source !== 'shadowing' && $language->getFilterCompilerFiles()) { $matchesExtension = false; foreach ($language->getExtensions() as $extension) { - $extensionWithDot = '.' . ltrim($extension, '.'); + $extensionWithDot = '.' . $extension; $extensionLength = strlen($extensionWithDot); if (substr($file->getClientOriginalName(), -$extensionLength) === $extensionWithDot) { $matchesExtension = true; From 3b81fe28ffdeb15c27427ea3de1679fa7513364d Mon Sep 17 00:00:00 2001 From: ItsNiklas Date: Thu, 23 Nov 2023 10:31:43 +0100 Subject: [PATCH 4/4] Submission: Use `str_ends_with` --- webapp/src/Service/SubmissionService.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/webapp/src/Service/SubmissionService.php b/webapp/src/Service/SubmissionService.php index 9d503fffee..c53cd17e45 100644 --- a/webapp/src/Service/SubmissionService.php +++ b/webapp/src/Service/SubmissionService.php @@ -530,9 +530,7 @@ public function submitSolution( if ($source !== 'shadowing' && $language->getFilterCompilerFiles()) { $matchesExtension = false; foreach ($language->getExtensions() as $extension) { - $extensionWithDot = '.' . $extension; - $extensionLength = strlen($extensionWithDot); - if (substr($file->getClientOriginalName(), -$extensionLength) === $extensionWithDot) { + if (str_ends_with($file->getClientOriginalName(), '.' . $extension)) { $matchesExtension = true; break; }