Course contentsShow
← Security
Lesson 1061 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

Bypassing SOP with JSONP

Understanding JSONP as a legacy cross-origin communication technique and its inherent security vulnerabilities.

Bypassing SOP with JSONP

You'll learn: How JSONP, a legacy technique for cross-origin data fetching, unintentionally creates serious security vulnerabilities by bypassing the Same-Origin Policy.

What Is JSONP?

Before modern CORS existed, developers needed ways to fetch data from other domains. JSONP (JSON with Padding) exploited a loophole: while XMLHttpRequest is blocked by the Same-Origin Policy, <script> tags can load JavaScript from any origin.

JSONP works by wrapping JSON data in a function call. A server responds not with pure JSON, but with executable JavaScript that calls a callback function you specify:

// Client requests: https://api.example.com/data?callback=handleData
// Server responds with:
handleData({"user": "alice", "balance": 5000});

When your page includes this as a <script>, the browser executes it, calling your handleData function with the data.

Why JSONP Is Dangerous

The fundamental problem: JSONP completely bypasses the Same-Origin Policy because <script> tags execute code with full JavaScript privileges in your page's context.

Attack scenarios:

  1. Sensitive data theft: Any site can include your JSONP endpoint and steal user data if the endpoint doesn't validate the requester
  2. Callback manipulation: Attackers might manipulate callback names to execute arbitrary code
  3. No CSRF protection: JSONP requests automatically include cookies, making CSRF trivial
  4. Trust boundary violation: You're executing third-party code directly—no sandboxing

Think of JSONP like giving a stranger your house key instead of showing them a photo through the window. Modern CORS lets you control what data others can see, but JSONP lets them execute code in your home.

Key Takeaway: JSONP is a legacy hack that bypasses Same-Origin Policy by executing cross-origin JavaScript directly—never use it in modern applications; use CORS with proper validation instead.