Skip to content

Commit f3bf4cb

Browse files
committed
Merge pull request #189 from magento-ogre/MAGETWO-45593-xss-vuln
[Ogres] Security Bug Fix
2 parents 44e607c + beb2038 commit f3bf4cb

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

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

+22-3
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);

app/code/Magento/Sales/Test/Unit/Helper/AdminTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,37 @@ public function escapeHtmlWithLinksDataProvider()
354354
'<a>some text in tags</a>',
355355
'<a>some text in tags</a>',
356356
'allowedTags' => ['a']
357-
]
357+
],
358+
'Not replacement with placeholders' => [
359+
"<a><script>alert(1)</script></a>",
360+
'<a>&lt;script&gt;alert(1)&lt;/script&gt;</a>',
361+
'allowedTags' => ['a']
362+
],
363+
'Normal usage, url escaped' => [
364+
'<a href=\"#\">Foo</a>',
365+
'<a href="#">Foo</a>',
366+
'allowedTags' => ['a']
367+
],
368+
'Normal usage, url not escaped' => [
369+
"<a href=http://example.com?foo=1&bar=2&baz[name]=BAZ>Foo</a>",
370+
'<a href="http://example.com?foo=1&amp;bar=2&amp;baz[name]=BAZ">Foo</a>',
371+
'allowedTags' => ['a']
372+
],
373+
'XSS test' => [
374+
"<a href=\"javascript&colon;alert(59)\">Foo</a>",
375+
'<a href="#">Foo</a>',
376+
'allowedTags' => ['a']
377+
],
378+
'Additional regex test' => [
379+
"<a href=\"http://example1.com\" href=\"http://example2.com\">Foo</a>",
380+
'<a href="http://example1.com">Foo</a>',
381+
'allowedTags' => ['a']
382+
],
383+
'Break of valid urls' => [
384+
"<a href=\"http://example.com?foo=text with space\">Foo</a>",
385+
'<a href="#">Foo</a>',
386+
'allowedTags' => ['a']
387+
],
358388
];
359389
}
360390
}

0 commit comments

Comments
 (0)