Skip to content

Commit c3732e8

Browse files
committed
fix: added check on valid HTML attributes, closes #2093
1 parent 5e5e237 commit c3732e8

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
This is a log of major user-visible changes in each phpMyFAQ release.
88

9-
### phpMyFAQ v3.1.3 - 2022-XX-XX
9+
### phpMyFAQ v3.1.3 - 2022-04-XX
1010

11+
- fixed login via LDAP or ActiveDirectory (Thorsten)
1112
- fixed minor bugs (Thorsten)
1213

1314
### phpMyFAQ v3.1.2 - 2022-03-16

phpmyfaq/src/phpMyFAQ/Filter.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,40 @@ public static function removeAttributes(string $html = ''): string
125125

126126
foreach ($attributes[0] as $attribute) {
127127
$attributeName = stristr($attribute, '=', true);
128-
if (!in_array($attributeName, $keep)) {
128+
if (self::isAttribute($attributeName) && !in_array($attributeName, $keep)) {
129129
$html = str_replace(' ' . $attribute, '', $html);
130130
}
131131
}
132132

133133
return $html;
134134
}
135+
136+
/**
137+
* @param string $attribute
138+
* @return bool
139+
*/
140+
private static function isAttribute(string $attribute): bool
141+
{
142+
$globalAttributes = [
143+
'autocomplete', 'autofocus', 'disabled', 'list', 'name', 'readonly', 'required', 'tabindex', 'type',
144+
'value', 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', 'id',
145+
'lang', 'style', 'tabindex', 'title', 'inputmode', 'is', 'itemid', 'itemprop', 'itemref', 'itemscope',
146+
'itemtype', 'lang', 'slot', 'spellcheck', 'translate', 'autofocus', 'disabled', 'form', 'multiple', 'name',
147+
'required', 'size', 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength',
148+
'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', 'onmouseenter',
149+
'onmouseleave', 'onafterprint', 'onbeforeprint', 'onbeforeunload', 'onhashchange', 'onmessage', 'onoffline',
150+
'ononline', 'onpopstate', 'onpagehide', 'onpageshow', 'onresize', 'onunload', 'ondevicemotion',
151+
'ondeviceorientation', 'onabort', 'onblur', 'oncanplay', 'oncanplaythrough', 'onchange', 'onclick',
152+
'oncontextmenu', 'ondblclick', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover',
153+
'ondragstart', 'ondrop', 'ondurationchange', 'onemptied', 'onended', 'onerror', 'onfocus', 'oninput',
154+
'oninvalid', 'onkeydown', 'onkeypress', 'onkeyup', 'onload', 'onloadeddata', 'onloadedmetadata',
155+
'onloadstart', 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup',
156+
'onmozfullscreenchange', 'onmozfullscreenerror', 'onpause', 'onplay', 'onplaying', 'onprogress',
157+
'onratechange', 'onreset', 'onscroll', 'onseeked', 'onseeking', 'onselect', 'onshow', 'onstalled',
158+
'onsubmit', 'onsuspend', 'ontimeupdate', 'onvolumechange', 'onwaiting', 'oncopy', 'oncut', 'onpaste',
159+
'onbeforescriptexecute', 'onafterscriptexecute'
160+
];
161+
162+
return in_array($attribute, $globalAttributes);
163+
}
135164
}

phpmyfaq/src/phpMyFAQ/Utils.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
88
* obtain one at http://mozilla.org/MPL/2.0/.
99
*
10-
* @package phpMyFAQ
10+
* @package phpMyFAQ
1111
* @author Thorsten Rinne <[email protected]>
1212
* @author Matteo Scaramuccia <[email protected]>
1313
* @copyright 2005-2022 phpMyFAQ Team
1414
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15-
* @link https://www.phpmyfaq.de
16-
* @since 2005-11-01
15+
* @link https://www.phpmyfaq.de
16+
* @since 2005-11-01
1717
*/
1818

1919
namespace phpMyFAQ;
@@ -185,9 +185,9 @@ public static function setHighlightedString(string $string, string $highlight):
185185
*/
186186
public static function highlightNoLinks(array $matches): string
187187
{
188-
$prefix = isset($matches[3]) ? $matches[3] : '';
189-
$item = isset($matches[4]) ? $matches[4] : '';
190-
$postfix = isset($matches[5]) ? $matches[5] : '';
188+
$prefix = $matches[3] ?? '';
189+
$item = $matches[4] ?? '';
190+
$postfix = $matches[5] ?? '';
191191

192192
if (!empty($item) && !self::isForbiddenElement($item)) {
193193
return sprintf(
@@ -260,13 +260,11 @@ public static function parseUrl(string $string): string
260260
$string = str_replace($protocols, '', $string);
261261
$string = str_replace('www.', 'http://www.', $string);
262262
$string = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $string);
263-
$string = preg_replace(
263+
return preg_replace(
264264
'/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/',
265265
'<a href="mailto:$1">$1</a>',
266266
$string
267267
);
268-
269-
return $string;
270268
}
271269

272270
/**

tests/phpMyFAQ/FilterTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Test suite for Filter class
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2022 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2022-04-08
16+
*/
17+
18+
namespace phpMyFAQ;
19+
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @testdox A Filter
24+
*/
25+
class FilterTest extends TestCase
26+
{
27+
/**
28+
* @testdox removes unwanted attributes
29+
*/
30+
public function testRemoveAttributes()
31+
{
32+
$expected = '<a href="#">phpMyFAQ</a>';
33+
$actual = Filter::removeAttributes($expected);
34+
$this->assertEquals($expected, $actual);
35+
36+
$expected = '<a href="#">phpMyFAQ</a>';
37+
$toTest = '<a href="#" onchange="bar()">phpMyFAQ</a>';
38+
$actual = Filter::removeAttributes($toTest);
39+
$this->assertEquals($expected, $actual);
40+
41+
$expected = '<a href="#">phpMyFAQ</a>';
42+
$toTest = '<a href="#" disabled="disabled">phpMyFAQ</a>';
43+
$actual = Filter::removeAttributes($toTest);
44+
$this->assertEquals($expected, $actual);
45+
46+
$expected = 'To: sslEnabledProtocols="TLSv1.2"';
47+
$actual = Filter::removeAttributes($expected);
48+
$this->assertEquals($expected, $actual);
49+
}
50+
}

0 commit comments

Comments
 (0)