since it is actually not very popular to use the Origin / X-Frame-Options HTTP header, and I don’t think the new CSP in Firefox would be better (overhead, complexity, etc.). I want to make an offer for a new version of JavaScript / ECMA.
But first, I publish this idea so that you can say it is bad. I call it simple jsPolicy :
Everyone who uses JavaScript has placed scripts in their html header. So why don't we use them to add our policies to control all subsequent scenarios. Example:
<html> <head> <title>Example</title> <script> window.policy.inner = ["\nfunction foo(bar) {\n return bar;\n}\n", "foo(this);"]; </script> </head> <body> <script> function foo(bar) { return bar; } </script> <a href="#" onclick="foo(this);">Click Me</a> <script> alert('XSS'); </script> </body> </html>
Now the browser compares <scripts> .innerHTML and onclick.value with those specified in the policy, and therefore the last script element is not executed (ignored).
Of course, it won't be useful to double all the embedded code, so instead we will use checksums. Example:
crc32("\nfunction foo(bar) {\n return bar;\n}\n");
Results for "1077388790"
And now a complete example:
if (typeof window.policy != 'undefined') { window.policy.inner = ["1077388790", "2501246156"]; window.policy.url = ["http://code.jquery.com/jquery*.js","http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"]; window.policy.relative = ["js/*.js"]; window.policy.report = ["api/xssreport.php"]; }
The browser only needs to be compared if the checksum of the built-in script is set to policy.inner or if the URL script.src is suitable for policy.url.
Note. The idea of politics. Relative - allow only local scripts:
window.policy.url = false; window.policy.relative = ["js/*.js"];
Note. policy.report should be almost the same as with CSP (sends blocked scripts and URLs in api):
https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-unofficial-draft-20110315.html#violation-report-syntax
Important:
- A policy cannot be set twice (otherwise it will issue a warning) = constant
- To think about it: a policy can only be set in the head (otherwise it issues a warning)
- This policy is only used to test scripts that are part of the html source, and not those that are hosted on the fly. example:
document.write ('<script src = "http://code.jquery.com/jquery-1.5.2.min.js"> </ scr' + 'ipt>');
You do not need the definition of policy.url for "http: //code.jquery.com ..." as the checksum of policy.inner checked the full source of the script. This means that the source is loaded, even if the policy.url parameter is set to false (yes, it is still protected!). This ensures easy use of the policy. - If one of the policies is missing, there are no restrictions. This means an empty policy. The result resolves all local files. This ensures backward compatibility.
- If one of the policies is set to false, use is not allowed (by default, this is true). example:
policy.inner = false;
This prohibits any inline scripts. - The policy ignores only prohibited scripts and issues a console warning (an error stops the execution of permitted scripts, and this is not required).
I think this would make XSS impossible, and instead of CSP, it would also avoid permanent XSS (since no one ever overwrites the Policy), and it would be much easier to update.
What do you think?
EDIT:
Here is an example made in Javascript:
http://www.programmierer-forum.de/php/js-policy-against-xss.php
Of course, we cannot control the execution of the script, but it shows how it can work if there is a browser compatible with jsPolicy.
EDIT2:
I don’t think I'm talking about coding a small javascript function to detect xss! My jsPolicy idea should be part of the new JavaScript engine. You can compare it with the php installation placed in the .htaccess file. You cannot change this parameter at runtime. The same requirements apply to jsPolicy. You can call it global setting .
jsPolicy in a nutshell:
HTML parser -> send scripts in JavaScript Engine -> compare with jsPolicy -> allowed?
A) yes, execution using the JavaScript Engine
B) no, ignored and sends a report to the webmaster
EDIT3 :
Referring to Mike's comment , this is also possible:
window.policy.eval = false;