Skip to content

Commit 4a6fecb

Browse files
authored
Merge pull request #6604 from kenjis/fix-safe_mailto-csp
fix: safe_mailto() does not work with CSP
2 parents cf99e61 + ad369b1 commit 4a6fecb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

system/Helpers/url_helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ function safe_mailto(string $email, string $title = '', $attributes = ''): strin
369369
$x = array_reverse($x);
370370

371371
// improve obfuscation by eliminating newlines & whitespace
372-
$output = '<script type="text/javascript">'
372+
$cspNonce = csp_script_nonce();
373+
$cspNonce = $cspNonce ? ' ' . $cspNonce : $cspNonce;
374+
$output = '<script type="text/javascript"' . $cspNonce . '>'
373375
. 'var l=new Array();';
374376

375377
foreach ($x as $i => $value) {

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,24 @@ public function safeMailtoPatterns()
468468
*/
469469
public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = '')
470470
{
471-
$request = Services::request($this->config);
471+
$request = Services::incomingrequest($this->config);
472472
$request->uri = new URI('http://example.com/');
473473

474474
Services::injectMock('request', $request);
475475

476476
$this->assertSame($expected, safe_mailto($email, $title, $attributes));
477477
}
478478

479+
public function testSafeMailtoWithCsp()
480+
{
481+
$this->config->CSPEnabled = true;
482+
Factories::injectMock('config', 'App', $this->config);
483+
484+
$html = safe_mailto('[email protected]', 'Foo');
485+
486+
$this->assertMatchesRegularExpression('/<script .*?nonce="\w+?".*?>/u', $html);
487+
}
488+
479489
// Test auto_link
480490

481491
public function autolinkUrls()

0 commit comments

Comments
 (0)