Skip to content

Fix: Preserve Redirection Links for Images in Emails #18397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions phpunit/functional/ToolboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,58 @@ public function testConvertTagToImageAlreadyInLink()
$expected_result,
\Toolbox::convertTagToImage($content_text, $item, $docs_data)
);

$content_text2 = <<<HTML
<a href="http://example.org/" target="_blank"><img id="{$img_1_tag}" width="10" height="10" /></a>
HTML;

$expected_result2 = <<<HTML
<a href="http://example.org/" target="_blank"><img alt="{$img_1_tag}" width="10" src="{$image_1_src}" /></a>
HTML;

// Processed data is expected to be sanitized, and expected result should remain sanitized
$this->assertEquals(
Sanitizer::sanitize($expected_result2),
\Toolbox::convertTagToImage(Sanitizer::sanitize($content_text2), $item, $docs_data)
);

// Processed data may also be escaped using Toolbox::addslashes_deep(), and expected result should be escaped too
$this->assertEquals(
\Toolbox::addslashes_deep($expected_result2),
\Toolbox::convertTagToImage(\Toolbox::addslashes_deep($content_text2), $item, $docs_data)
);

// Processed data may also be not sanitized, and expected result should not be sanitized
$this->assertEquals(
$expected_result2,
\Toolbox::convertTagToImage($content_text2, $item, $docs_data)
);

$content_text3 = <<<HTML
<a href="http://example.org/">Some Content<div><img id="{$img_1_tag}" width="10" height="10" /><p>Content</p></div></a>
HTML;

$expected_result3 = <<<HTML
<a href="http://example.org/">Some Content<div><img alt="{$img_1_tag}" width="10" src="{$image_1_src}" /><p>Content</p></div></a>
HTML;

// Processed data is expected to be sanitized, and expected result should remain sanitized
$this->assertEquals(
Sanitizer::sanitize($expected_result3),
\Toolbox::convertTagToImage(Sanitizer::sanitize($content_text3), $item, $docs_data)
);

// Processed data may also be escaped using Toolbox::addslashes_deep(), and expected result should be escaped too
$this->assertEquals(
\Toolbox::addslashes_deep($expected_result3),
\Toolbox::convertTagToImage(\Toolbox::addslashes_deep($content_text3), $item, $docs_data)
);

// Processed data may also be not sanitized, and expected result should not be sanitized
$this->assertEquals(
$expected_result3,
\Toolbox::convertTagToImage($content_text3, $item, $docs_data)
);
}

public static function shortenNumbers()
Expand Down
6 changes: 3 additions & 3 deletions src/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2834,11 +2834,11 @@ public static function convertTagToImage($content_text, CommonDBTM $item, $doc_d
$width = $img_infos[0];
$height = $img_infos[1];
}

// Avoids creating a link within a link, when the image is already in an <a> tag
$add_link_tmp = $add_link;
if ($add_link) {
$pattern = '/<a[^>]*>[^<>]*?<img[^>]+' . preg_quote($image['tag'], '/') . '[^<]+>[^<>]*?<\/a>/s';
// Try to detect any unclosed `<a>` tag that preced the `<img>` tag
$pattern = '/<a[^>]*>((?!<\/a>).)*<img[^>]*' . preg_quote($image['tag'], '/') . '/s';
if (preg_match($pattern, $content_text)) {
$add_link_tmp = false;
}
Expand All @@ -2849,7 +2849,7 @@ public static function convertTagToImage($content_text, CommonDBTM $item, $doc_d
$width,
$height,
$add_link_tmp,
$object_url_param
$object_url_param,
);
if (empty($new_image)) {
$new_image = '#' . $image['tag'] . '#';
Expand Down