Previously asked here. Asking again on SO to try getting a better set of responses.
I have a service which allows the user to specify a callback function name that wraps the data being returned to support jsonp callbacks. I want to make sure that I am covering all of my bases in regards to preventing XSS attacks.
Note, I have read through the OWASP security checklist but none of the recommendations seem to directly address this question.
These are the currently supported methods for specifying the jsonp function where the function name is cbFn
and cbFn
is declared on its own, a method on an object, or being accessed from an object/array:
https://service.com/cbFn
https://service.com/?callback=cbFn
https://service.com/?callback=obj.cbFn
https://service.com/?callback=obj['cbFn']
https://service.com/?callback=obj[1]
These return:
cbFn({data: 'data being returned'})
obj.cbFn({data: 'data being returned'})
obj['cbFn']({data: 'data being returned'})
obj[1]({data: 'data being returned'})
However the following requests also work and are the known XSS issues I want to circumvent:
// executes an anonymous function
https://service.com/?callback=(()=%3E{alert(1)})
// replaces the user's callback function with our own
https://service.com/?callback=cbFn=((data)=>{alert(data)})
Is it enough to just replace/remove the characters ()=>
in the callback name to prevent XSS vulnerabilities? I want to allow the full valid javascript character set for function names, so restricting the valid character range to /[$_\w]+/
(alphanumeric plus $ and _) doesn't seem to be a good option.