Skip to content

Commit 82cc07e

Browse files
committed
Security: Remove on* attributes through new filter of HTML Purifier
Fix advisory GHSA-gw58-89f7-4xgj
1 parent 241c569 commit 82cc07e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

main/inc/lib/formvalidator/FormValidator.class.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/* For licensing terms, see /license.txt */
44

5+
use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\RemoveOnAttributes;
56
use Chamilo\UserBundle\Entity\User;
67

78
/**
@@ -2107,7 +2108,5 @@ function plain_url_filter($html, $mode = NO_HTML)
21072108
*/
21082109
function attr_on_filter(string $html): string
21092110
{
2110-
$pattern = '/\s*on\w+=(?:"[^"]*"|\'[^\']*\'|[^\s>]+)/i';
2111-
2112-
return preg_replace($pattern, '', $html);
2111+
return RemoveOnAttributes::filter($html);
21132112
}

main/inc/lib/security.lib.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* For licensing terms, see /license.txt */
44

55
use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\AllowIframes;
6+
use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\RemoveOnAttributes;
67
use ChamiloSession as Session;
78

89
/**
@@ -347,8 +348,16 @@ public static function remove_XSS($var, int $user_status = null, bool $filter_te
347348
$config->set('Core.ConvertDocumentToFragment', false);
348349
$config->set('Core.RemoveProcessingInstructions', true);
349350

351+
$customFilters = [
352+
new RemoveOnAttributes(),
353+
];
354+
350355
if (api_get_setting('enable_iframe_inclusion') == 'true') {
351-
$config->set('Filter.Custom', [new AllowIframes()]);
356+
$customFilters[] = new AllowIframes();
357+
}
358+
359+
if ($customFilters) {
360+
$config->set('Filter.Custom', $customFilters);
352361
}
353362

354363
// Shows _target attribute in anchors
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
namespace Chamilo\CoreBundle\Component\HTMLPurifier\Filter;
6+
7+
use HTMLPurifier_Filter;
8+
9+
class RemoveOnAttributes extends HTMLPurifier_Filter
10+
{
11+
public $name = 'RemoveOnAttributes';
12+
13+
public function preFilter($html, $config, $context)
14+
{
15+
return self::filter($html);
16+
}
17+
18+
public static function filter($html)
19+
{
20+
$pattern = '/\s*on\w+=(?:"[^"]*"|\'[^\']*\'|[^\s>]+)/i';
21+
22+
return preg_replace($pattern, '', $html);
23+
}
24+
}

0 commit comments

Comments
 (0)