News for hackers. Stuff that matters.
Re:Why does Google append while(1); in front of their JSON r... by mmahemoff on Monday February 04, 2013 @08:29PM (#5168462)
This works because the browser will run any third-party script, but it won't expose the raw string constituting the script.

So a malicious website can't steal that content by putting the URL in a script. It will never get to see what's in the script and only a same-domain script could parse the whole contents, by making a XHR call, and strip out the "while 1;" part. In contrast, a regular JSONP string is specifically designed for the third party to read it (by including code to call a callback function).

A lot of people still don't realise you shouldn't be serving private data as JSONP. Thankfully, CORS lets modern browsers go cross-browser safely, even with private data if things are configured right.

Re:Why does Google append while(1); in front of their JSON r... by y0ghur7_xxx on Tuesday February 05, 2013 @08:26AM (#5170498)
> Thankfully, CORS lets modern browsers go cross-browser

> safely, even with private data if things are configured right.

CORS does nothing to prevent CSRF attacks, see[1]. We still need to protect against CSRF.

[1]http://www.w3.org/TR/cors/#security

Re:Why does Google append while(1); in front of their JSON r... by facorreia on Tuesday February 05, 2013 @05:45AM (#5170039)
"Thankfully, CORS lets modern browsers go cross-browser safely, even with private data if things are configured right."

Would you clarify or add some pointers to where we can learn about CORS benefits for private data? CSRF is a huge but often underestimated issue.

Re:Why does Google append while(1); in front of their JSON r... by mmahemoff on Tuesday February 05, 2013 @09:14AM (#5170696)
Actually as yoghur points out, CORS doesn't protect against CSRF. A token must be passed for any privileged calls.

That said, a benefit of CORS is the ability to do non-GET methods. So it's safer in the sense of HTTP idempotence, ie you can change server state safely, using POSTs, DELETEs, and so on. You'd still need to pass a token, however.

(Of course I meant cross-domain, not cross-browser :).

Re:Why does Google append while(1); in front of their JSON r... by byroot on Tuesday February 05, 2013 @09:10AM (#5170680)
There is not CORS benefits for private data if your CORS endpoint have a cookie based authentication.
Re:Why does Google append while(1); in front of their JSON r... by kwamenum86 on Monday February 04, 2013 @10:28PM (#5168846)
Actually this prevents XSSI (cross-site script inclusion) not XSRF (cross-site request forgery).

XSRF vulnerabilities allow an attacker to issue web requests on a user's behalf; oftentimes though people only call it a vulnerability when you can issue a request that changes some state. And XSRF is usually prevented by including a hard to guess token with the request that is tied to the user and allows you to determine whether the request was generated by an authorized party. Please don't try to prevent XSRF by using while(1) :)

Re:Why does Google append while(1); in front of their JSON r... by seag on Monday February 04, 2013 @08:41PM (#5168508)
You don't append in front, you prepend
Re:Why does Google append while(1); in front of their JSON r... by knowtheory on Monday February 04, 2013 @08:53PM (#5168560)
I'm a bit perplex why you've chosen to pedantize this particular word choice since "prepend" itself is a portmanteau derived by mashing "pre" and "append" together.

Perhaps the trio of "affix", "prefix" and "suffix" might be more useful, since there is no "postpend" or "suppend" to accompany "append" and "prepend".

Regardless, the Google ngram chart of these various words is pretty interesting: http://books.google.com/ngrams/graph?content=prepend%2Cappen...

Re:Why does Google append while(1); in front of their JSON r... by bradleyjg on Monday February 04, 2013 @09:25PM (#5168663)
Re:Why does Google append while(1); in front of their JSON r... by caipre on Tuesday February 05, 2013 @02:43PM (#5172779)
I had no idea 'prepend' was used so infrequently.
Re:Why does Google append while(1); in front of their JSON r... by jacquesm on Monday February 04, 2013 @08:48PM (#5168539)
Why does Google append their JSON responses to while(1); ?
Re:Why does Google append while(1); in front of their JSON r... by comex on Monday February 04, 2013 @10:08PM (#5168787)
That carries the implication "why not simply return 'while(1)'?".
Re:Why does Google append while(1); in front of their JSON r... by rmc on Tuesday February 05, 2013 @04:34AM (#5169894)
No, you prefix.
Re:Why does Google append while(1); in front of their JSON r... by matthuggins on Tuesday February 05, 2013 @12:16PM (#5171807)
Don't call seag a prefix. :(
Re:Why does Google append while(1); in front of their JSON r... by zem on Tuesday February 05, 2013 @03:49AM (#5169789)
and what do you see as the difference between appending in front and prepending?
Re:Why does Google append while(1); in front of their JSON r... by jeremysmyth on Tuesday February 05, 2013 @04:21AM (#5169867)
Nothing, if you're prepared to say you can pop something onto a stack and push it off.
Re:Why does Google append while(1); in front of their JSON r... by zem on Tuesday February 05, 2013 @05:07PM (#5173599)
what about a deque? the push-pop/shift-unshift distinction is unnecessarily confusing when something doesn't have stack semantics.
Re:Why does Google append while(1); in front of their JSON r... by patorjk on Monday February 04, 2013 @08:23PM (#5168439)
If you look at the gmail ajax calls, you'll also see they sometimes put a number in front of their data. Example:

3

[1, 2, 3, 0]

If you execute that in your JS console, it'll return undefined, which threw me for a loop a couple days ago when I saw it. I ended up having it ask on StackOverflow too and it's because JavaScript will interpret it as a property look up on that number object (it will evaluate to 3[0], which evaluates to undefined).

Re:Why does Google append while(1); in front of their JSON r... by Hello71 on Monday February 04, 2013 @09:51PM (#5168740)
Something comma operator something.
Re:Why does Google append while(1); in front of their JSON r... by nodesocket on Tuesday February 05, 2013 @03:24AM (#5169725)
This doesn't protect again using an <img> tag to accomplish the same thing correct? Assume a malicious user wanted to delete something and the URI was formatted like: http://mydomain.com/delete/some-id-here. Then assuming the user is logged in, a simple <img src="http://www.mydomain.com/delete/some-id-here /> on a third party site would execute the request successfully correct?
Re:Why does Google append while(1); in front of their JSON r... by hendi_ on Tuesday February 05, 2013 @03:37AM (#5169758)
Yes. This is why you never perform any actions which change something over GET. Use POST (or PUT or DELETE) for them.
Re:Why does Google append while(1); in front of their JSON r... by nostrademons on Tuesday February 05, 2013 @04:29AM (#5169883)
This is one of the reasons why you never perform any actions which change something over GET. The main reason is so your site doesn't get deleted when the Googlebot visits. (Or more generally, so that web crawlers and other robots don't accidentally mutate the site.)

Using POST is not sufficient; you also need to include an XSRF token that only the requesting page knows. It's very possible to send a POST to a third-party site with about one line of Javascript. (In the relatively early days of Reddit, I wrote a page that upvoted itself by having a JS handler in an invisible iframe resubmit the same link over again, using the visitor's login credentials. At the time, submitting the same link twice on Reddit counted as an upvote, and so merely visiting the page would upvote the link.)

Re:Why does Google append while(1); in front of their JSON r... by hendi_ on Tuesday February 05, 2013 @05:06AM (#5169964)
You're right, I only focused on that one single reason for my post since I was in a hurry.

Security issues are obviously more complex than one-line explanations. Thank you for your more elaborate reply!

Re:Why does Google append while(1); in front of their JSON r... by rmc on Tuesday February 05, 2013 @04:32AM (#5169892)
Yes, that solves that. But you should use CSRF. Otherwise someone could have a <form method=POST action="http://mysite.example.com/delete>…</form>; on their site and a javascript that will submit the form upon page load.
Re:Why does Google append while(1); in front of their JSON r... by fhars on Tuesday February 05, 2013 @03:38AM (#5169760)
If that works then the developer responsible for mydomain.com should be reprimanded for wilful incompetence. And that isn't even the problem the while(1); trick is meant to mitigate.
Re:Why does Google append while(1); in front of their JSON r... by brown9-2 on Tuesday February 05, 2013 @10:13AM (#5171010)
The scenario you mentioned is one of the things people mean when they talk about cross-site request forgery.
Re:Why does Google append while(1); in front of their JSON r... by hayksaakian on Monday February 04, 2013 @08:14PM (#5168404)
> closed

Good 'ol stackoverflow

Re:Why does Google append while(1); in front of their JSON r... by duskwuff on Monday February 04, 2013 @08:20PM (#5168425)
Closed as a duplicate of this equivalent question, which has an equally good answer:

http://stackoverflow.com/questions/871505/

Which is, itself a duplicate of this question, which has an even better answer:

http://stackoverflow.com/questions/3146798/

Re:Why does Google append while(1); in front of their JSON r... by tikhonj on Monday February 04, 2013 @08:19PM (#5168421)
Really, the knee-jerk response to every single closed question on SO isn't terribly useful.

First off, you can still read the question and vote on answers. Since there are already several answers, adding another one would not be very useful anyhow.

Moreover, it's closed as a duplicate. There's an essentially identical question--with essentially the same answer--that's quite a bit older. It makes sense to point people to it so that there's one centralized resource on the topic.

Re:Why does Google append while(1); in front of their JSON r... by dhimes on Monday February 04, 2013 @08:22PM (#5168435)
As a duplicate of What??? That's always been my question. I've found it beyond dumb that they don't put the thing they want you to go to at that message. And, they should make it look like a link. Just my $0.02.
Re:Why does Google append while(1); in front of their JSON r... by tikhonj on Monday February 04, 2013 @08:27PM (#5168459)
There's a link at the top of the question, in a gray box.

However, I certainly see how it would be useful to have the link in the closed message as well.

Re:Why does Google append while(1); in front of their JSON r... by dhimes on Tuesday February 05, 2013 @08:20AM (#5170477)
The thing that says, "Possible duplicate?" Once they confirm it they should change the wording to say "Duplicate" if that's the one they mean. It just looks like it's unfinished this way.
Re:Why does Google append while(1); in front of their JSON r... by _pmf_ on Tuesday February 05, 2013 @06:52AM (#5170180)
Wikipedia has shown us what ridiculous bureaucracy does to a community; it's sad that Stackoverflow has been turned into a utterly useless site despite the lessons that could have been learned from Wikipedia.
Re:Why does Google append while(1); in front of their JSON r... by T-hawk on Tuesday February 05, 2013 @10:18AM (#5171038)
It's the Iron Law of Bureaucracy, coined in that form by Jerry Pournelle. http://www.jerrypournelle.com/reports/jerryp/iron.html

Every organization becomes controlled by those who serve the organization itself, as opposed to those who perform the actual goal or service of the organization. Schools are controlled by administrators, not the teachers. Wikipedia and other community-sourced internet sites are run by those who desired to acquire power over Wikipedia, not by those who desire to contribute content.

Re:Why does Google append while(1); in front of their JSON r... by icebraining on Tuesday February 05, 2013 @11:02AM (#5171319)
Stackoverflow has been turned into a utterly useless site

It has? It's news to me; I'm constantly finding relevant, useful information whenever I search for almost anything in the area of software development.

Re:Why does Google append while(1); in front of their JSON r... by sil3ntmac on Monday February 04, 2013 @08:43PM (#5168517)
Wait.. so any JSON API that doesn't do this is technically vulnerable to CSRF? Mind blown.
Re:Why does Google append while(1); in front of their JSON r... by gdix on Tuesday February 05, 2013 @01:06AM (#5169423)
No. Unblow your mind. Any service which returns sensitive data in JSON via an HTTP get based on the user's cookie is vulnerable. At worst, an attacker would just be able collect the response of a GET on behalf of a visitor to the attacker's site if the user is on a really old browser.
Re:Why does Google append while(1); in front of their JSON r... by apendleton on Monday February 04, 2013 @08:54PM (#5168564)
This method only works for HTTP GETs, and can't get around requirements to present CSRF tokens with the request. This is one of several possible strategies for guarding against CSRF.
Re:Why does Google append while(1); in front of their JSON r... by ville on Monday February 04, 2013 @09:05PM (#5168595)
Wouldn't also using a custom HTTP header instead of cookies for passing a session identifier prevent this attack? A script tag can not set headers, right?
Re:Why does Google append while(1); in front of their JSON r... by thezilch on Monday February 04, 2013 @10:25PM (#5168837)
Re:Why does Google append while(1); in front of their JSON r... by deliminator on Tuesday February 05, 2013 @01:05AM (#5169421)
Correct me if I'm wrong, but does this not merely allow an attacker to cause the browser to make requests with custom headers? That is, it does not allow the attacker to gain the CSRF token, and if the CSRF token is not known to the attacker, he would still not be able to make API requests.
Re:Why does Google append while(1); in front of their JSON r... by kwamenum86 on Tuesday February 05, 2013 @12:07AM (#5169259)
Not CSRF. XSSI. It's actually completely different
Re:Why does Google append while(1); in front of their JSON r... by twistedpair on Tuesday February 05, 2013 @01:01PM (#5172084)
This can really be broken because you can redefine the numeric constructor of 1 to return false and thus get around the while(1). However, if for(;;) was used, then you cannot get around it. ;)
Re:Why does Google append while(1); in front of their JSON r... by aleem on Tuesday February 05, 2013 @01:45PM (#5172390)
I'm curious--how would you redefine it? Could you share a code snippet or reference?
Re:Why does Google append while(1); in front of their JSON r... by ahrjay on Tuesday February 05, 2013 @04:34PM (#5173365)
Source maps recommend a similar technique to mitigate against XSSI attacks by prepending ")]}" to your source map http://www.html5rocks.com/en/tutorials/developertools/source...
Re:Why does Google append while(1); in front of their JSON r... by swah on Tuesday February 05, 2013 @01:29PM (#5172275)
I think this why Flask only allows top-level objects, not arrays. Am I right?

(http://flask.pocoo.org/docs/security/#json-security)

Re:Why does Google append while(1); in front of their JSON r... by boop0x6 on Tuesday February 05, 2013 @11:59AM (#5171691)
I wonder how many times this vulnerability has been exploited before they've implemented this hack.
Re:Why does Google append while(1); in front of their JSON r... by dpup on Tuesday February 05, 2013 @03:08PM (#5172932)
If I remember correctly, it was first discovered pretty soon after a version of Gecko was introduced with support for setters/getters, and Gecko had minority market share at that time.

http://jeremiahgrossman.blogspot.com/2006/01/advanced-web-at...

Re:Why does Google append while(1); in front of their JSON r... by homakov on Tuesday February 05, 2013 @08:11AM (#5170441)
it was known like 4 years ago. I prefer to just use /
Re:Why does Google append while(1); in front of their JSON r... by martinced on Monday February 04, 2013 @08:27PM (#5168457)
That such hacks are used as security measures makes me think that we've heard far from the last JavaScript exploit.

Now I find that workaround sweet: but it's still a total kludge and that such kludges are used just shows how poorly security has been conceived from the ground up in browsers / JavaScript.

Re:Why does Google append while(1); in front of their JSON r... by tptacek on Monday February 04, 2013 @09:03PM (#5168592)
Along the same lines, see how you feel about browser security in general after reading: http://lcamtuf.coredump.cx/postxss/
Re:Why does Google append while(1); in front of their JSON r... by a3_nm on Tuesday February 05, 2013 @03:26AM (#5169729)
There's a great book about browser security by the same author: _The Tangled Web_ <http://lcamtuf.coredump.cx/tangled/>;
Re:Why does Google append while(1); in front of their JSON r... by saturdayplace on Tuesday February 05, 2013 @11:12AM (#5171388)
Thank you for this link. As someone just getting started in web app security this was eye-opening.
Re:Why does Google append while(1); in front of their JSON r... by olalonde on Monday February 04, 2013 @10:16PM (#5168812)
Note that this bug doesn't affect modern browsers AFAIK (just tested on Chrome). Firefox fixed this bug back in 2007 for instance.
Re:Why does Google append while(1); in front of their JSON r... by blablabla123 on Tuesday February 05, 2013 @06:35AM (#5170151)
I agree... I mean look at JSONP, that's basically the inverse hack. After all: what's the point of enforcing same-origin-policy in AJAX but ignoring it for <script>?
Re:Why does Google append while(1); in front of their JSON r... by mmahemoff on Tuesday February 05, 2013 @09:52AM (#5170865)
The actual reason for this is web advertising took off like wildfire, using 3rd party scripts. It was too late to reverse by the time people got concerned about the security model.

Content Security Policy makes all this much more robust in modern browsers.

Re:Why does Google append while(1); in front of their JSON r... by dspeyer on Tuesday February 05, 2013 @01:03PM (#5172092)
JSONP lets the server turn off the same-origin policy. If XRFing your data is dangerous, don't use JSONP. But often we don't care about XRF, and the person writing the server is well-placed to know.