00:00

#0905

JSONP Callback Validation

Easy+50 XPA03:2021 InjectionCWE-79
XSSJSONPCallback

Scenario

Your API supports JSONP by wrapping the JSON response in a caller-supplied callback name: callbackName({...}).

The validateCallback function currently accepts any string, allowing an attacker to supply );alert(1);// as the callback name.

This causes the rendered response to execute );alert(1);//({...}) in any browser that loads the JSONP endpoint, resulting in XSS.

JSONP is still widely used in legacy APIs. Without strict callback validation any XSS filter can be bypassed because the payload is served from a trusted first-party domain.

Your Tasks

  1. Fix validateCallback to only accept valid JavaScript identifier characters.
  2. Allow callbacks matching /^[a-zA-Z_$][a-zA-Z0-9_$.]*$/ (dots permitted for namespaced callbacks like jQuery.fn).
  3. Throw an error with message 'Invalid callback' for any other input; return the callback unchanged if valid.

Examples

Example 1Malicious callback (bug)

validateCallback(');alert(1);//')
// returns ');alert(1);//' — XSS!

Example 2Blocked (fix)

validateCallback(');alert(1);//')
// throws Error('Invalid callback')

Constraints

  • Throw exactly 'Invalid callback' — no other message.
  • Valid callbacks may contain dots (e.g. jQuery.ajax.success).
  • Empty strings must also be rejected.

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run