Why does Google add while 1 at the start of their JSON responses

0 votes
With the help of code, can you explain why Google adds while(1) at the start of their JSON responses?
Jan 9 in Java-Script by Ashutosh
• 14,020 points
25 views

1 answer to this question.

0 votes

Google often adds while(1); (or a similar infinite loop statement) at the start of their JSON responses to prevent JSON hijacking.

Reason: JSON Hijacking Prevention

JSON data can be evaluated as JavaScript code using the eval() function.

If a malicious site includes the JSON response directly in a <script> tag, it could potentially expose sensitive data.

By adding while(1);, the JSON response becomes invalid JavaScript and cannot be executed directly.

Example:

Response from Google:

while(1);{ "data": "example" }

This invalidates the response when parsed as JavaScript, protecting it from being exploited as a data source by malicious scripts.

answered Jan 10 by Navya

Related Questions In Java-Script

0 votes
1 answer

Hi, how does one kill the whatsapp message loop with a code instead of closing whatsapp-web?

Hello @Felix , Use this code: <script> function simulateMouseEvents(element, eventName) ...READ MORE

answered Oct 20, 2020 in Java-Script by Niroj
• 82,840 points
2,463 views
0 votes
0 answers

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

I was trying to change a string ...READ MORE

May 9, 2022 in Java-Script by Kichu
• 19,040 points
6,908 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Is JavaScript a case-sensitive language?

Yes, JavaScript is a case sensitive language.  The language ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
5,989 views
0 votes
1 answer
0 votes
1 answer

How to sum the values of a javascript object

Certainly, I'd be happy to help you ...READ MORE

answered Sep 25, 2023 in Java-Script by Edureka
• 12,690 points
4,480 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP