Skip to content

Commit

Permalink
Fix handling of unquoted images src in mail collector
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and btry committed Sep 18, 2024
1 parent e4f7006 commit c308ce6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 38 deletions.
18 changes: 10 additions & 8 deletions src/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6189,16 +6189,18 @@ public function getRights($interface = 'central')
**/
public static function convertContentForTicket($html, $files, $tags)
{

preg_match_all("/src\s*=\s*['|\"](.+?)['|\"]/", $html, $matches, PREG_PATTERN_ORDER);
if (isset($matches[1]) && count($matches[1])) {
// Get all image src

foreach ($matches[1] as $src) {
$src_patterns = [
'src\s*=\s*"[^"]+"', // src="image.png"
"src\s*=\s*'[^']+'", // src='image.png'
'src\s*=[^\s>]+', // src=image.png
];
$matches = [];
if (preg_match_all('/(' . implode('|', $src_patterns) . ')/', $html, $matches, PREG_PATTERN_ORDER) > 0) {
foreach ($matches[0] as $src_attr) {
// Set tag if image matches
foreach ($files as $data => $filename) {
if (preg_match("/" . $data . "/i", $src)) {
$html = preg_replace("/<img[^>]*src=['|\"]" . preg_quote($src, '/') . "['|\"][^>]*\>/s", "<p>" . Document::getImageTag($tags[$filename]) . "</p>", $html);
if (preg_match("/" . $data . "/i", $src_attr)) {
$html = preg_replace("/<img[^>]*" . preg_quote($src_attr, '/') . "[^>]*>/s", "<p>" . Document::getImageTag($tags[$filename]) . "</p>", $html);
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/emails-tests/41-image-src-with-no-quote.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Date: Thu, 1 Dec 2022 11:23:48 +0200 (CEST)
From: Normal User <normal@glpi-project.org>
To: GLPI debug <unittests@glpi-project.org>
Subject: 41 - Image src without quotes
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_1757883_1359581901.1528365951028"

------=_Part_1757883_1359581901.1528365951028
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Image:
------=_Part_1757883_1359581901.1528365951028
Content-Type: multipart/related;
boundary="----=_Part_1757884_1267006027.1528365951028"
------=_Part_1757884_1267006027.1528365951028
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<html>
<body>
Image: <img src=cid:image001.png@01D90577.61DCAEC0>
</body>
</html>
------=_Part_1757884_1267006027.1528365951028
Content-Type: image/png; name=41-blue-dot.png
Content-Disposition: attachment; filename=41-blue-dot.png
Content-Transfer-Encoding: base64
Content-ID: <image001.png@01D90577.61DCAEC0>
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAAXNSR0IB2cksfwAAAAlwSFlzAAAL
EwAACxMBAJqcGAAAAANQTFRFAAD/injSVwAAAApJREFUeJxjYAAAAAIAAUivpHEAAAAASUVORK5C
YII=
------=_Part_1757884_1267006027.1528365951028--

------=_Part_1757883_1359581901.1528365951028--


83 changes: 53 additions & 30 deletions tests/functional/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4230,51 +4230,74 @@ protected function convertContentForTicketProvider(): iterable
'expected' => '',
];

// Content with embedded image.
yield [
'content' => <<<HTML
foreach (['"', "'", ''] as $quote_style) {
// `img` of embedded image that has only a `src` attribute.
yield [
'content' => <<<HTML
Here is the screenshot:
<img src="screenshot.png" />
<img src={$quote_style}screenshot.png{$quote_style}>
blabla
HTML
,
'files' => [
'screenshot.png' => 'screenshot.png',
],
'tags' => [
'screenshot.png' => '9faff0a6-f37490bd-60e2af9721f420.96500246',
],
'expected' => <<<HTML
,
'files' => [
'screenshot.png' => 'screenshot.png',
],
'tags' => [
'screenshot.png' => '9faff0a6-f37490bd-60e2af9721f420.96500246',
],
'expected' => <<<HTML
Here is the screenshot:
<p>#9faff0a6-f37490bd-60e2af9721f420.96500246#</p>
blabla
HTML
,
];
,
];
// `img` of embedded image that has multiple attributes.
yield [
'content' => <<<HTML
Here is the screenshot:
<img id="img-id" src={$quote_style}screenshot.png{$quote_style} height="150" width="100" />
blabla
HTML
,
'files' => [
'screenshot.png' => 'screenshot.png',
],
'tags' => [
'screenshot.png' => '9faff0a6-f37490bd-60e2af9721f420.96500246',
],
'expected' => <<<HTML
Here is the screenshot:
<p>#9faff0a6-f37490bd-60e2af9721f420.96500246#</p>
blabla
HTML
,
];

// Content with leading external image that will not be replaced by a tag.
yield [
'content' => <<<HTML
<img src="http://test.glpi-project.org/logo.png" />
// Content with leading external image that will not be replaced by a tag.
yield [
'content' => <<<HTML
<img src={$quote_style}http://test.glpi-project.org/logo.png{$quote_style} />
Here is the screenshot:
<img src="img.jpg" />
<img src={$quote_style}img.jpg{$quote_style} />
blabla
HTML
,
'files' => [
'img.jpg' => 'img.jpg',
],
'tags' => [
'img.jpg' => '3eaff0a6-f37490bd-60e2a59721f420.96500246',
],
'expected' => <<<HTML
<img src="http://test.glpi-project.org/logo.png" />
,
'files' => [
'img.jpg' => 'img.jpg',
],
'tags' => [
'img.jpg' => '3eaff0a6-f37490bd-60e2a59721f420.96500246',
],
'expected' => <<<HTML
<img src={$quote_style}http://test.glpi-project.org/logo.png{$quote_style} />
Here is the screenshot:
<p>#3eaff0a6-f37490bd-60e2a59721f420.96500246#</p>
blabla
HTML
,
];
,
];
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/imap/MailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ function () use (&$msg) {
'40.1 - Empty content (multipart)',
'40.2 - Empty content (html)',
'40.3 - Empty content (plain text)',
'41 - Image src without quotes',
]
],
// Mails having "normal" user as observer (add_cc_to_observer = true)
Expand Down Expand Up @@ -943,6 +944,7 @@ function () use (&$msg) {
'1234567890_2' => 'text/plain',
'1234567890_3' => 'text/plain',
'37-red-dot.png' => 'image/png',
'41-blue-dot.png' => 'image/png',
];

$iterator = $DB->request(
Expand Down

0 comments on commit c308ce6

Please sign in to comment.