Skip to content

Commit

Permalink
Improved url checks on AgenciaTransporte model to prevent XSS attacks…
Browse files Browse the repository at this point in the history
… with javascript urls.

------
Mejorada la comprobación de urls en el modelo de agencia de transporte para evitar ataques XSS mediante urls de tipo javascript.
  • Loading branch information
NeoRazorX committed May 16, 2022
1 parent f1ca50d commit 61ee9c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Core/Base/Utils.php
Expand Up @@ -135,6 +135,16 @@ public static function intval(?string $str): ?int
return $str === null ? null : (int)$str;
}

public static function isValidUrl(string $url): bool
{
// si la url está vacío o comienza por javascript: entonces no es una url válida
if (empty($url) || strpos($url, 'javascript:') === 0) {
return false;
}

return filter_var($url, FILTER_VALIDATE_URL) !== false;
}

/**
* This function converts:
* < to &lt;
Expand Down
2 changes: 1 addition & 1 deletion Core/Model/AgenciaTransporte.php
Expand Up @@ -93,7 +93,7 @@ public function test(): bool
$this->web = $utils->noHtml($this->web);

// check if the web is a valid url
if (!empty($this->web) && !filter_var($this->web, FILTER_VALIDATE_URL)) {
if (!empty($this->web) && false === self::toolBox()::utils()::isValidUrl($this->web)) {
self::toolBox()::i18nLog()->error('invalid-web');
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions Test/Core/Model/AgenciaTransporteTest.php
Expand Up @@ -59,6 +59,10 @@ public function testBadWeb()
$agency->nombre = 'Test Agency';
$agency->web = 'javascript:alert(origin)';
$this->assertFalse($agency->save(), 'agency-can-save-bad-web');

// otra url peligrosa
$agency->web = 'javascript://example.com//%0aalert(document.domain);//';
$this->assertFalse($agency->save(), 'agency-can-save-bad-web-2');
}

public function testGoodWeb()
Expand Down

0 comments on commit 61ee9c9

Please sign in to comment.