Last active
May 30, 2020 21:23
-
-
Save nicjansma/6f3209a53079f483abb5226156a2054b to your computer and use it in GitHub Desktop.
ResourceTiming leak to parent frames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Placed in any cross-origin IFRAMEs | |
// | |
if (window !== window.top) { | |
if (typeof window.PerformanceObserver !== "function") { | |
return; | |
} | |
// Listen for all ResourceTimings, repeating them to the parent window | |
var observer = new PerformanceObserver(function(entries) { | |
window.top.postMessage({ | |
type: "resourcetiming", | |
entries: JSON.stringify(entries.getEntries()), | |
origin: window.performance.timeOrigin || window.performance.timing.navigationStart | |
}, "*"); | |
}); | |
observer.observe({entryTypes: ['resource']}); | |
// repeat any sub-frame's ResourceTiming data | |
window.addEventListener("message", function(event) { | |
if (event.data && event.data.type === "resourcetiming") { | |
window.top.postMessage(event.data, "*"); | |
} | |
}, false); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Placed in the top-level window | |
// | |
// list of all ResourceTimings from cross-origin IFRAMEs | |
var iframeEntries = []; | |
window.addEventListener("message", function(event) { | |
if (event.data && event.data.type === "resourcetiming") { | |
// RT entries are JSON.stringify'd | |
var entries = JSON.parse(event.data.entries); | |
var myOrigin = window.performance.timeOrigin || window.performance.timing.navigationStart; | |
var offsetTime = event.data.origin - myOrigin; | |
// update each entry's startTime by the offset of the two frame's time origins | |
for (var i = 0; i < entries.length; i++) { | |
entries[i].startTime += offsetTime; | |
} | |
Array.prototype.push.apply(iframeEntries, entries); | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment