Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Modules\Addons\BankPayments\Models\BankMailbox;
use Modules\Addons\BankPayments\Services\BankMailboxClient;
use Modules\Addons\BankPayments\Services\BankPaymentsSettings;
use Modules\Addons\BankPayments\Services\ImapBankMailboxClient;

class BankMailboxController extends BankPaymentsController
{
Expand Down Expand Up @@ -128,7 +129,7 @@ private function validated(Request $request, ?BankMailbox $mailbox = null): arra
'required', 'string', 'max:100',
Rule::unique('bank_mailboxes', 'name')->ignore($mailbox?->id),
],
'host' => ['required', 'string', 'max:255'],
'host' => ['required', 'string', 'max:255', 'regex:'.ImapBankMailboxClient::HOST_PATTERN],
'port' => ['required', 'integer', 'between:1,65535'],
'encryption' => ['required', Rule::in(['ssl', 'tls', 'none'])],
'username' => ['required', 'string', 'max:255'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ImapBankMailboxClient implements BankMailboxClient
/** @var Connection|false|null */
private $stream = null;

/** A host name or an IPv4 address: letters, digits, dots and hyphens. */
public const HOST_PATTERN = '/^[A-Za-z0-9](?:[A-Za-z0-9.-]{0,253}[A-Za-z0-9])?$/';

private ?int $connectedMailboxId = null;

private string $lastError = '';
Expand Down Expand Up @@ -152,6 +155,12 @@ private function connect(BankMailbox $mailbox)
return false;
}

if (! preg_match(self::HOST_PATTERN, (string) $mailbox->host)) {
$this->lastError = __('bank-payments::messages.invalid_host');

return false;
}

if ($this->stream && $this->connectedMailboxId === $mailbox->id) {
return $this->stream;
}
Expand Down
1 change: 1 addition & 0 deletions modules/Addons/BankPayments/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'connection_ok' => 'The IMAP mailbox connection works.',
'connection_failed' => 'Could not connect to the IMAP mailbox.',
'imap_missing' => 'The PHP imap extension is not installed, so the mailbox cannot be read.',
'invalid_host' => 'The mailbox server address is not a valid host name.',
'only_test_mode' => 'This version of the addon only supports test mode.',
'addon_inactive' => 'The bank payments addon is not active.',
'no_active_mailboxes' => 'There are no active mailboxes to sync.',
Expand Down
1 change: 1 addition & 0 deletions modules/Addons/BankPayments/lang/pl/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'connection_ok' => 'Połączenie z skrzynką IMAP działa.',
'connection_failed' => 'Nie udało się połączyć ze skrzynką IMAP.',
'imap_missing' => 'Rozszerzenie PHP imap nie jest zainstalowane, więc nie można odczytać skrzynki.',
'invalid_host' => 'Adres serwera skrzynki nie jest prawidłową nazwą hosta.',
'only_test_mode' => 'Ta wersja dodatku obsługuje wyłącznie tryb testowy.',
'addon_inactive' => 'Dodatek Płatności bankowe nie jest aktywny.',
'no_active_mailboxes' => 'Brak aktywnych skrzynek do synchronizacji.',
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/BankPayments/BankMailboxManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,25 @@ function bootBankPaymentsAddonForMailbox(): void

expect(BankMailbox::where('name', 'Unknown bank')->exists())->toBeFalse();
});

it('accepts only a plain host name for the mailbox server', function () {
$fields = [
'name' => 'Host check',
'port' => 993,
'encryption' => 'ssl',
'username' => 'bank@example.test',
'password' => 'super-secret',
'folder' => 'INBOX',
'bank' => 'nest_bank',
];

foreach (['imap.example.test/novalidate-cert', 'imap.example.test:143', '{imap.example.test}', 'imap example.test'] as $host) {
$this->post(route('admin.bank-payments.mailboxes.store'), $fields + ['host' => $host])
->assertSessionHasErrors('host');
}

$this->post(route('admin.bank-payments.mailboxes.store'), $fields + ['host' => 'imap-1.example.test'])
->assertSessionHasNoErrors();

expect(BankMailbox::where('host', 'imap-1.example.test')->exists())->toBeTrue();
});
Loading