Skip to content

Commit

Permalink
RenderingHelper: Replace object links before html injection
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 12, 2024
1 parent 01d6721 commit f170f90
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions library/Jira/Web/RenderingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,11 @@ public function getIssueComment($author, string $time, string $body): array
*/
public function formatBody(string $body): HtmlString
{
$html = Html::wantHtml($body)->render();

// This is safe.
return new HtmlString($this->replaceLinks($html) ?? '');
}
$urls = [];

/**
* Replace object urls in the given string with link elements
*
* @param string $string
*
* @return ?string
*/
protected function replaceLinks(string $string): ?string
{
return preg_replace_callback('/\[([^|]+)\|([^]]+)]/', function ($match) {
$url = Url::fromPath(htmlspecialchars_decode($match[2]));
// Replace object urls in the given string with link elements
$body = preg_replace_callback('/\[([^|]+)\|([^]]+)]/', function ($match) use (&$urls) {
$url = Url::fromPath($match[2]);
$link = new Link(
$match[1],
$url,
Expand Down Expand Up @@ -176,8 +164,19 @@ protected function replaceLinks(string $string): ?string
$this->setHostLink($hostLink);
}

return $link->render();
}, $string);
$urls[] = $link->render();

return '$objectLink' . (count($urls) - 1) . '$';
}, $body);

$html = Html::wantHtml($body)->render();

foreach ($urls as $i => $url) {
$html = str_replace('$objectLink' . $i . '$', $url, $html);
}

// This is safe.
return new HtmlString($html);
}

public function linkToJira($caption, $url, $attributes = [])
Expand Down

0 comments on commit f170f90

Please sign in to comment.