Skip to content

Commit e938e43

Browse files
committed
fix(Security): improve regex and quote handling in strip_image_tags()
1 parent 3ad5144 commit e938e43

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Release Date: Unreleased
1414

1515
## Bugs Fixed
1616

17+
- **Security:** Fixed a bug in `strip_image_tags()` where an invalid PCRE character class caused attribute injection and improper quote handling, and ensured tags without `src` are stripped.
1718
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
1819
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
1920

‎system/Helpers/security_helper.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ function sanitize_filename(string $filename, bool $relativePath = false): string
8989
*/
9090
function strip_image_tags(string $str): string
9191
{
92-
return preg_replace(
92+
return (string) preg_replace(
9393
[
94-
'#<img[\s/]+.*?src\s*=\s*(["\'])([^\\1]+?)\\1.*?\>#i',
95-
'#<img[\s/]+.*?src\s*=\s*?(([^\s"\'=<>`]+)).*?\>#i',
94+
'#<img[\s/]+[^>]*?src\s*=\s*(?|(["\'])(.*?)\1|()([^\s"\'=<>`]+))[^>]*>#is',
95+
'#<img(?:\s[^>]*|/[^>]*|)>#is',
96+
],
97+
[
98+
'$2',
99+
'',
96100
],
97-
'\\2',
98101
$str,
99102
);
100103
}

‎tests/system/Helpers/SecurityHelperTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,34 @@ public function testStripImageTags(): void
208208
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif'));
209209

210210
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src="http://example.com/spacer.gif" alt="Who needs CSS when you have a spacer.gif?" />'));
211+
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src=\'http://example.com/spacer.gif\' alt="test" />'));
212+
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src=http://example.com/spacer.gif alt="test" />'));
213+
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src = "http://example.com/spacer.gif" />'));
214+
$this->assertSame('http://example.com/test.png', strip_image_tags('<img class="my-class" id="my-img" src="http://example.com/test.png">'));
215+
$this->assertSame('http://example.com/test.jpg', strip_image_tags('<img alt="test\'s photo" src="http://example.com/test.jpg">'));
216+
$this->assertSame('http://example.com/test.jpg', strip_image_tags('<img alt=\'test "quoted" photo\' src=\'http://example.com/test.jpg\'>'));
217+
218+
// Empty src or tags without src should be stripped
219+
$this->assertSame('', strip_image_tags('<img src="" onerror="alert(1)">'));
220+
$this->assertSame('', strip_image_tags('<img src=\'\' onerror=\'alert(1)\'>'));
221+
$this->assertSame('', strip_image_tags('<img onerror="alert(1)">'));
222+
$this->assertSame('', strip_image_tags('<img alt="no image">'));
223+
$this->assertSame('Hello World', strip_image_tags('Hello <img> World'));
224+
$this->assertSame('Hello World', strip_image_tags('Hello <img/> World'));
225+
$this->assertSame('Hello World', strip_image_tags('Hello <img > World'));
226+
227+
// Multiline img tag
228+
$this->assertSame('http://example.com/test.png', strip_image_tags("<img\n class=\"foo\"\n src=\"http://example.com/test.png\"\n alt=\"bar\">"));
229+
230+
// Intervening text between multiple img tags must not be swallowed
231+
$this->assertSame('Text1 Text2 bar.jpg Text3', strip_image_tags('Text1 <img alt="foo"> Text2 <img src="bar.jpg"> Text3'));
232+
$this->assertSame('Start 1.jpg Middle 2.jpg End', strip_image_tags('Start <img src="1.jpg"> Middle <img src="2.jpg"> End'));
233+
234+
// Attribute injection and quote handling
235+
$this->assertSame('test', strip_image_tags('<img src="test" onmouseover="alert(1)">'));
236+
$this->assertSame('test', strip_image_tags('<img src=\'test\' onmouseover=\'alert(1)\'>'));
237+
$this->assertSame('test', strip_image_tags('<img src=\'test\' onmouseover="alert(1)">'));
238+
$this->assertSame('test', strip_image_tags('<img src="test" onmouseover=\'alert(1)\'>'));
211239
}
212240

213241
public function testEncodePhpTags(): void

0 commit comments

Comments
 (0)