Skip to content

Commit 9088bdc

Browse files
committed
MAGETWO-45593: Magento back office persistent XSS vulnerability on order comments
- Fixed the bug from the patch provided.
1 parent 44e607c commit 9088bdc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

app/code/Magento/Sales/Helper/Admin.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,29 @@ public function escapeHtmlWithLinks($data, $allowedTags = null)
149149
$links = [];
150150
$i = 1;
151151
$data = str_replace('%', '%%', $data);
152-
$regexp = '@(<a[^>]*>(?:[^<]|<[^/]|</[^a]|</a[^>])*</a>)@';
152+
$regexp = "/<a\s[^>]*href\s*?=\s*?([\"\']??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU";
153153
while (preg_match($regexp, $data, $matches)) {
154-
$links[] = $matches[1];
155-
$data = str_replace($matches[1], '%' . $i . '$s', $data);
154+
//Revert the sprintf escaping
155+
$url = str_replace('%%', '%', $matches[2]);
156+
$text = str_replace('%%', '%', $matches[3]);
157+
//Check for an valid url
158+
if ($url) {
159+
$urlScheme = strtolower(parse_url($url, PHP_URL_SCHEME));
160+
if ($urlScheme !== 'http' && $urlScheme !== 'https') {
161+
$url = null;
162+
}
163+
}
164+
//Use hash tag as fallback
165+
if (!$url) {
166+
$url = '#';
167+
}
168+
//Recreate a minimalistic secure a tag
169+
$links[] = sprintf(
170+
'<a href="%s">%s</a>',
171+
htmlspecialchars($url, ENT_QUOTES, 'UTF-8', false),
172+
$this->escaper->escapeHtml($text)
173+
);
174+
$data = str_replace($matches[0], '%' . $i . '$s', $data);
156175
++$i;
157176
}
158177
$data = $this->escaper->escapeHtml($data, $allowedTags);

0 commit comments

Comments
 (0)