Skip to content

Commit 45c1bba

Browse files
author
Oleksii Korshenko
authored
MAGETWO-65598: [GitHub][PR] Prevent cross origin iframe content reading #8005
2 parents 43c2d62 + 7c80d73 commit 45c1bba

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

app/code/Magento/PageCache/view/frontend/web/js/page-cache.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ define([
4141
* @param {jQuery} element - Comment holder
4242
*/
4343
(function lookup(element) {
44-
if ($.nodeName(element, 'iframe') && $(element).prop('src').indexOf(window.location.hostname) === -1) {
45-
return [];
44+
// prevent cross origin iframe content reading
45+
if ($(element).prop('tagName') === 'IFRAME') {
46+
var iframeHostName = $('<a>').prop('href', $(element).prop('src'))
47+
.prop('hostname');
48+
49+
if (window.location.hostname !== iframeHostName) {
50+
return [];
51+
}
4652
}
47-
$(element).contents().each(function (index, el) {
48-
var hostName, iFrameHostName;
4953

54+
$(element).contents().each(function (index, el) {
5055
switch (el.nodeType) {
5156
case 1: // ELEMENT_NODE
5257
lookup(el);
@@ -57,14 +62,7 @@ define([
5762
break;
5863

5964
case 9: // DOCUMENT_NODE
60-
hostName = window.location.hostname;
61-
iFrameHostName = $('<a>')
62-
.prop('href', $(element).prop('src'))
63-
.prop('hostname');
64-
65-
if (hostName === iFrameHostName) {
66-
lookup($(el).find('body'));
67-
}
65+
lookup($(el).find('body'));
6866
break;
6967
}
7068
});

dev/tests/js/jasmine/tests/app/code/Magento/PageCache/frontend/js/page-cache.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ define([
6060

6161
it('on iframe from other host returns empty Array', function () {
6262
iframe.contents().find('body').html(comment);
63-
iframe.attr('src', '//' + host + '.otherHost/');
63+
iframe.attr('src', '//' + host + '.otherHost/?origin_url=' + host);
6464

6565
expect(iframe.comments().length).toEqual(0);
6666
});

0 commit comments

Comments
 (0)