What Is XSSI and How Do Tokens Protect APIs?

Cross-Site Script Inclusion (XSSI) is a web security vulnerability that allows an attacker’s website to steal sensitive, authenticated data from another application by abusing the way web browsers handle the <script> tag. Because script tags are exempt from the Same-Origin Policy (SOP), an external domain can include dynamic, user-specific JavaScript or JSON resources and capture their contents. Implementing unpredictable token mechanisms protects JavaScript data APIs by requiring requests to carry cryptographically secure tokens or custom headers, which malicious third-party <script> tags cannot generate or supply.

Understanding Cross-Site Script Inclusion (XSSI)

The Same-Origin Policy typically prevents one website from reading data hosted on another origin. However, the HTML <script> tag is fundamentally designed to allow cross-origin execution to load libraries, analytics, and frameworks.

XSSI occurs when an application exposes sensitive user data through endpoints that output executable JavaScript, JSONP, or improperly formatted JSON. When an authenticated user visits an attacker-controlled webpage, the malicious site includes a script tag pointing directly to the vulnerable data API:

<script src="https://example.com/api/user-profile.js"></script>

When the victim’s browser requests this script, it automatically includes the victim’s session cookies. If the API responds with JavaScript containing sensitive data (such as global variables or executable functions), the attacker’s script can read that data directly from the browser’s execution context.

Variations of XSSI Exploitation

How Token Mechanisms Protect JavaScript Data APIs

Because the browser’s default behavior sends cookies automatically with cross-origin <script> requests, relying solely on cookie-based authentication leaves dynamic JavaScript APIs vulnerable. Token mechanisms effectively neutralize this threat.

1. Synchronizer Token Pattern (Anti-CSRF / Anti-XSSI Tokens)

Token-based defense involves issuing a unique, cryptographically random token tied to the user’s current session. When the client application requests sensitive data from an API:

2. Custom HTTP Headers and Bearer Tokens

Modern web APIs frequently use stateless authorization tokens (such as JSON Web Tokens) transmitted through custom HTTP headers (e.g., Authorization: Bearer <token> or X-Requested-With).

HTML <script> tags can only perform standard GET requests and cannot set custom HTTP headers. Consequently: * The cross-origin <script> tag cannot attach custom headers automatically. * If an attacker attempts to use fetch() or XMLHttpRequest to set custom headers, the browser enforces Cross-Origin Resource Sharing (CORS) rules. * Unless the API explicitly permits the attacker’s origin via CORS headers, the browser blocks the request before any data can be read.

Complementary Mitigations for API Protection

While token mechanisms provide strong protection, implementing defense-in-depth ensures complete immunity against XSSI: