Re:Throw 1; < don't be evil
by willscott on Thursday November 25, 2010 @06:55PM
(#1941285)
Presumably on the google website the script is loaded via an xmlhttp request which then strips the initial text and evals the rest. By added the initial throw 1; they prevent other sites from including the script, since it won't do anything.
[ Parent
| on HN ]
Re:Throw 1; < don't be evil
by stanleydrew on Thursday November 25, 2010 @07:07PM
(#1941300)
Indeed. See this: http://google-gruyere.appspot.com/part3#3__cross_site_script...
Re:Throw 1; < don't be evil
by SimonPStevens on Friday November 26, 2010 @04:59AM
(#1942037)
Cool site. I've never seen that before. Learn about web security by breaking it.
What about hackthissite.org?
Re:Throw 1; < don't be evil
by eddieplan9 on Friday November 26, 2010 @08:36AM
(#1942348)
That's extremely smart. XMLHttpRequest protects you via the same origin policy. But there are other ways (such as JSONP) to load JavaScript and bypass the same origin policy. It's not like you cannot opt out of things like JSONP, but this trick adds another layer of protection and is particularly useful in fighting XSSI.
If another site would really want to include the script, it could also strip the initial text. Is the purpose only to avoid people from including the script by mistake?
You can't make a XHR request to another domain. You only can include it via a <script> tag and that is going to fail.
Didn't know that. Thanks!