Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow extensions with dots, Extension filter checks full extension #2219

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions webapp/migrations/Version20231120225210.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231120225210 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove leading dots from extensions in the `language` table';
}

public function up(Schema $schema): void
{
$languages = $this->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;
}
}
6 changes: 6 additions & 0 deletions webapp/src/Entity/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>')]
private array $extensions = [];

Expand Down
5 changes: 3 additions & 2 deletions webapp/src/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '.' . $extension;
$extensionLength = strlen($extensionWithDot);
if (substr($file->getClientOriginalName(), -$extensionLength) === $extensionWithDot) {
ItsNiklas marked this conversation as resolved.
Show resolved Hide resolved
$matchesExtension = true;
break;
}
Expand Down
4 changes: 3 additions & 1 deletion webapp/templates/jury/partials/language_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
addExtension($extensionsHolder, $addExtensionButton);
});

$extensionsHolder.find('div').each(function() {
$extensionsHolder.find('div:not(.invalid-feedback)').each(function() {
addDeleteLink($(this));
});

Expand All @@ -37,8 +37,10 @@
var $removeFormButton = $('<button type="button" class="btn btn-danger"><i class="fas fa-trash"></i></button>');
var $inputGroup = $('<div class="input-group"></div>');
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) {
Expand Down
Loading