Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions lib/Service/ColumnTypes/TextLinkBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@
namespace OCA\Tables\Service\ColumnTypes;

use OCA\Tables\Db\Column;
use OCA\Tables\Errors\BadRequestError;
use OCP\IL10N;
use Psr\Log\LoggerInterface;

class TextLinkBusiness extends SuperBusiness {

public function __construct(
LoggerInterface $logger,
private IL10N $n,
) {
parent::__construct($logger);
}

/**
* @param mixed $value (string|null)
* @param Column $column
Expand Down Expand Up @@ -96,4 +106,19 @@ public function canBeParsed($value, Column $column): bool {
preg_match('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $value, $matches);
return !empty($matches);
}

public function validateValue(mixed $value, Column $column, string $userId, int $tableId, ?int $rowId): void {
$data = json_decode($value, true);

// Only allow URLs that start with http or https
if (isset($data['value']) && !preg_match('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $data['value'])) {
throw new BadRequestError(
'Column "' . $column->getTitle() . '" contains an invalid protocol. Only http and https are allowed.',
translatedMessage: $this->n->t(
'Column "%s" contains an invalid protocol. Only http and https are allowed.',
[$column->getTitle()]
),
);
}
}
}
25 changes: 24 additions & 1 deletion tests/unit/Service/ColumnTypes/TextLinkBusinessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\Tables\Service\ColumnTypes;

use OCA\Tables\Db\Column;
use OCP\IL10N;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

Expand All @@ -18,7 +19,8 @@ class TextLinkBusinessTest extends TestCase {

public function setUp(): void {
$this->textLinkBusiness = new TextLinkBusiness(
$this->createMock(LoggerInterface::class)
$this->createMock(LoggerInterface::class),
$this->createMock(IL10N::class)
);

$this->column = $this->createMock(Column::class);
Expand Down Expand Up @@ -90,4 +92,25 @@ public function testParseValue() {
'providerId' => 'url',
]), $column));
}

public function testValidateValue() {
// Assert that no exception is thrown for valid values
try {
$this->textLinkBusiness->validateValue(json_encode([
'title' => 'Test link',
'value' => 'https://nextcloud.com',
'providerId' => 'url',
]), $this->column, 'userId', 1, null);
} catch (\Exception $e) {
$this->fail('validateValue threw an exception for valid input: ' . $e->getMessage());
}

// Assert that exception is thrown for invalid values
$this->expectException(\OCA\Tables\Errors\BadRequestError::class);
$this->textLinkBusiness->validateValue(json_encode([
'title' => 'Test link',
'value' => 'invalidurl',
'providerId' => 'url',
]), $this->column, 'userId', 1, null);
}
}
Loading