SSTI in Bug Bounty Program: The Time I Played with Handlebars and Broke Stuff
A subdomain reflected the callback parameter without sanitisation, allowing JavaScript injection. The attacker could execute arbitrary JavaScript in the victim's browser, potentially leading to XSS, though full remote code execution was not achieved.
Published on medium.com
Hey, everybody! 🎉 I’m super excited to share this wild bug I recently found in a public bug bounty program. This one was a fun ride, so grab some popcorn and let me tell you all about it.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target was a public bug bounty program whose scope covered an entire wildcard domain, *.target.tld. Within that scope the researcher found a subdomain, referred to here as mb.target.tld, that hosted a small search feature: a form that let a visitor look up device capabilities by device ID. The page fetched its data from a backend endpoint and rendered one of the returned fields, SOME_OTHER_INFO, into the page.
Setup
No accounts, roles or special tooling are described. The work started from the public scope and a search engine, and continued with manual requests to the endpoint and inspection of the responses. The only precondition was that the device capability lookup endpoint was reachable on the in-scope subdomain.
Finding it
The first step was subdomain hunting across the wide scope, starting with a Google dork restricted to the program's domain and excluding out-of-scope hosts:
site:*.target.tld -out_of_scope
Among the results was mb.target.tld. Loading the page showed the device capability search form, and the researcher began probing it.
While exploring the page, a comment in one of the JavaScript files stood out:
Seems that result format/handling/etc has been changed I did some not too funny fixes to make it work here as well - onAlldevices - result data is string, eval it - onSingleDevice - without 'callback' response is lost somewhere
The comment mentions eval and a callback, which was enough to justify a closer look at how the form talked to the backend.
Exploitation
Step 1: capture the original request
Submitting the form produced a request of this shape:
The response wrapped the JSON data in the value of the callback parameter:
eval({"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"INFO"});
The page printed SOME_OTHER_INFO: INFO from that response. The callback parameter was reflected into a JavaScript context, and the page evaluated the result.
Step 2: confirm that the callback is reflected unsanitised
The callback value was changed to an arbitrary identifier:
The response came back with the same identifier in place of eval:
terrestrial({"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"INFO"});
Nothing was printed on the page, likely because the call raised an error, but the response confirmed that the input was placed into the response without sanitisation and executed.
Step 3: change what the page displays
The next goal was to control the rendered output. The SOME_OTHER_INFO field was modified inside the callback value:
The page displayed SOME_OTHER_INFO: Terrestrial, so the attacker-controlled value reached the page through the template.
Step 4: confirm server-side template evaluation
With Handlebars apparently in use, a simple arithmetic expression was placed in the field:
The template engine evaluated 4*4 and the page printed 16, which confirmed template injection rather than plain reflection.
Step 5: reach the JavaScript engine
More complex expressions were then tried in the callback parameter:
callback=(("SOME_OTHER_INFO":(function(){return+this;})()});//
This printed [object Object]. The next payload reached the constructor chain:
callback=(("SOME_OTHER_INFO":(function(){return+this.constructor.constructor;})()});//
That printed function anonymous() {}, which showed that the payload was interacting with the underlying JavaScript engine.
Step 6: enumerate the window object
The following payload was injected:
callback=(("SOME_OTHER_INFO":(function(){return+Object.keys(this);})()});//
It returned properties of the window object, which allowed the DOM to be explored. The keys seen included close, stop, focus, blur, open, alert, confirm, prompt,...
Step 7: attempt escalation to command execution
After reviewing the results, the researcher found no access to child_process to run command line code, so the issue could not be escalated to full remote code execution. The report attributes this either to the target being sandboxed or to security restrictions. Even so, the template injection was clear and could have led to more serious consequences.
Root cause
The endpoint takes the callback parameter and writes it into the response body unescaped, in a JavaScript context. The developer comment in the JavaScript file describes exactly this design: result data is treated as a string and passed to eval, and the callback parameter is required for the single-device response to work. Because the callback value is also processed by a template engine, expressions inside it are evaluated, which turns a JSONP-style callback reflection into template injection. No sanitisation or validation of the callback value is described, and the template engine version is not stated in the report.
Impact
An attacker could control the content rendered on the page and execute JavaScript expressions in the template engine's context. In this instance the researcher could not reach child_process, so arbitrary command execution was not demonstrated. The report notes that the restriction may have been a sandbox or other security restrictions, and that the vulnerability still posed a significant risk and was worth reporting. The report does not describe any bounty amount or remediation.
Fix
The report's lessons are to always sanitise and validate user data, especially when dealing with callbacks and template engines such as Handlebars, and to keep libraries up to date, since older versions of template engines often have known vulnerabilities. The report does not say what the program actually changed.
Variants to test
- JSONP-style callback parameters on other endpoints. Send a request with
callback=set to a unique identifier and check whether the response wraps the JSON in that identifier. A positive result is the identifier appearing verbatim in the response body. - Template expressions in reflected fields. Where a reflected value is rendered by a template engine, submit an arithmetic expression such as
4*4in the field. A positive result is the page printing16rather than the literal text. - Constructor access from an evaluated context. Try
(function(){return+this.constructor.constructor;})()in a reflected JavaScript context. A positive result is afunction anonymous() {}-style response, which indicates access to theFunctionconstructor. - Global object enumeration. Try
(function(){return+Object.keys(this);})()to list properties of the global object. A positive result is a list ofwindowproperties such asalert,openorprompt. - Escalation to process access. From a confirmed JavaScript execution context, check whether
child_processor equivalent process APIs are reachable. A positive result is the ability to require or access the module, which would allow command execution; a negative result suggests a sandbox or other restriction.
What happened, in plain words
A security researcher was taking part in a public bug bounty program. A bug bounty program is an arrangement where a company says: "if you find a security problem in our systems and tell us about it responsibly, we may pay you a reward." The company publishes a list of what is in scope, meaning which websites and services you are allowed to test.
In this case the scope was very wide: every subdomain of one domain, written as *.target.tld. A subdomain is a name that sits in front of the main domain, like shop.example.com next to example.com. The * is a wildcard meaning "anything here".
The researcher found a small search page on one subdomain, which we will call mb.target.tld. The page had a form where you could look up information about a device (for example a phone brand) by typing its ID number. Behind the form, the page asked a server for the data and then showed one of the returned fields on screen. That field was called SOME_OTHER_INFO.
While reading the page's JavaScript files (the code that runs in your browser to make a page interactive), the researcher found a comment left by a developer. The comment mentioned eval, a JavaScript function that takes a piece of text and runs it as code. That hint led the researcher to test how the form talked to the server, and it turned out the server was putting part of the request straight into its reply without cleaning it, and that reply was being run as code. This is a class of bug called template injection: user input ends up being treated as part of a template that is evaluated, instead of being treated as harmless text.
The researcher could change what the page displayed and could get JavaScript expressions evaluated, but could not go all the way to running operating system commands on the server. The report was still worth filing because the flaw was real and could have led to worse.
The setup
The report does not describe any accounts, logins or special tools. The researcher started from the public scope list and a search engine, then sent requests to the endpoint by hand and read the replies. The only precondition was that the device capability lookup endpoint was reachable on the in-scope subdomain.
A few terms used from here on:
- An endpoint is a specific address on a server that accepts a particular kind of request, like a service window in a building. You send a request to that address and get a reply.
- An API (application programming interface) is the general name for a set of endpoints a program offers so other programs (or a web page) can ask it for data or ask it to do something. The form on the page is the friendly front; the API is what it talks to behind the scenes.
- A request is the message your browser sends to a server. A response is the message the server sends back.
- JSON is a simple text format for structured data, made of names and values in curly braces, like
{"name":"value"}. It is how many APIs send data back.
Finding it
The first step was to look for subdomains across the wide scope. The researcher used a Google dork, which is just a search query written with special operators to narrow results. The query was:
site:*.target.tld -out_of_scope
Line by line:
site:*.target.tldtells Google to only show pages on any subdomain oftarget.tld.-out_of_scopetells Google to leave out results that contain the textout_of_scope, which is a way of filtering out pages that are not part of the program.
Among the results was mb.target.tld. Opening that page showed the device capability search form, and the researcher started poking at it to see how it behaved.
While exploring the page, a comment in one of the JavaScript files stood out:
Seems that result format/handling/etc has been changed I did some not too funny fixes to make it work here as well - onAlldevices - result data is string, eval it - onSingleDevice - without 'callback' response is lost somewhere
What this comment is saying, in plain words:
- The way results are formatted and handled has changed at some point.
- The developer made some quick fixes to keep this page working.
- For the "all devices" case, the result data arrives as a string (plain text) and the code runs it with
eval. - For the "single device" case, if there is no
callbackvalue, the response gets lost somewhere.
The comment mentions eval and a callback. eval is the JavaScript function that runs text as code, and a callback here is a name the server is expected to wrap its data in. That was enough of a hint to look closely at how the form talks to the backend.
Exploitation
Step 1: capture the original request
Submitting the form produced a request of this shape:
Line by line:
GETis the type of request: "please give me this resource"./[REDACTED_PATH]/DeviceCapabilityDetailsis the path on the server being asked for. The first part is hidden here.?id=1starts the query string, which is the part after the question mark that carries extra details. Here it says the device ID is1.&callback=evaladds a second detail: thecallbackvalue iseval.HTTP/1.1is the version of the web protocol being used.
The response wrapped the JSON data in the value of the callback parameter:
eval({"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"INFO"});
Line by line:
eval(is the callback value, placed at the start.{"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"INFO"}is the JSON data: device ID 1, brand Alcatel, and a field calledSOME_OTHER_INFOwith the valueINFO.);closes the call.
So the server took whatever was in callback and put it in front of the data. The page then printed SOME_OTHER_INFO: INFO from that response. In other words, the callback parameter was being reflected into a JavaScript context, and the page evaluated the result. "Reflected" means your input comes back to you in the reply. "JavaScript context" means the reply is treated as code by the browser, not as plain text.
Step 2: confirm that the callback is reflected unsanitised
The callback value was changed to an arbitrary identifier:
Line by line:
- Same request as before, except
callback=terrestrialinstead ofcallback=eval. terrestrialis just a made-up word, used as a name to see whether the server echoes it back.
The response came back with the same identifier in place of eval:
terrestrial({"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"INFO"});
Line by line:
terrestrial(is the value that was sent, placed at the start of the reply.- The JSON data follows unchanged.
);closes the call.
Nothing was printed on the page, likely because the call raised an error: terrestrial is not a function the browser knows, so trying to call it fails. But the reply confirmed that the input was placed into the response without sanitisation and then executed. "Sanitisation" means cleaning input so it cannot do anything harmful, for example by escaping special characters. Here nothing was cleaned.
Step 3: change what the page displays
The next goal was to control the rendered output. The SOME_OTHER_INFO field was modified inside the callback value:
Line by line:
callback=eval(starts the callback witheval, so the reply will be run as code.{"Id":1,"Brand":"Alcatel", "SOME_OTHER_INFO":"Terrestrial"}is a JSON object supplied by the researcher, with the field changed toTerrestrial.)closes theevalcall.;//ends the statement and starts a comment. In JavaScript,//means "ignore the rest of this line". This is useful because the server will add its own);after the input, and the comment makes sure that trailing part does not break the code.
The page displayed SOME_OTHER_INFO: Terrestrial, so the attacker-controlled value reached the page through the template. A template is a page with blanks that get filled in with data; a template engine is the software that fills those blanks. If the blanks are filled with user input that is treated as instructions rather than text, you get template injection.
Step 4: confirm server-side template evaluation
With Handlebars apparently in use, a simple arithmetic expression was placed in the field:
Line by line:
- Same structure as before, but
SOME_OTHER_INFOis now4*4instead of a word. 4*4is a multiplication, which a template engine or JavaScript will calculate as16.);//closes the call and comments out the rest.
Handlebars is a template engine often used with JavaScript on the server. The template engine evaluated 4*4 and the page printed 16. That is the key difference between plain reflection (where you would see the literal text 4*4) and template injection (where the expression is actually computed).
Step 5: reach the JavaScript engine
More complex expressions were then tried in the callback parameter:
callback=(("SOME_OTHER_INFO":(function(){return+this;})()});//
Line by line:
callback=starts the parameter.(("SOME_OTHER_INFO":is a fragment of an object literal, with the field name.(function(){return+this;})()defines a small function and immediately calls it. Inside,thisrefers to the current object, and the+in front is a trick to convert it to a number or string.});closes the object and the call.//comments out the rest of the line.
This printed [object Object]. That is what JavaScript shows when you turn an object into text without saying how: it is a generic label meaning "some object". It told the researcher that code was running and that this was an object.
The next payload reached the constructor chain:
callback=(("SOME_OTHER_INFO":(function(){return+this.constructor.constructor;})()});//
Line by line:
- Same shape as before.
this.constructoris the function that created the current object..constructoragain gives the function that created that function, which in JavaScript is theFunctionconstructor, the built-in tool that can create new functions from text.return+converts the result to text so it can be shown.
That printed function anonymous() {}, which showed that the payload was interacting with the underlying JavaScript engine. function anonymous() {} is the text form of a function with no name and an empty body, which is what the Function constructor produces.
Step 6: enumerate the window object
The following payload was injected:
callback=(("SOME_OTHER_INFO":(function(){return+Object.keys(this);})()});//
Line by line:
Object.keys(this)asks JavaScript for the names of all the properties on the current object.thisin this context is the global object, which in a browser is calledwindow.return+turns the list into text so it can be printed.
It returned properties of the window object, which allowed the DOM to be explored. The DOM is the browser's model of the page: every element, button and text box. The keys seen included close, stop, focus, blur, open, alert, confirm, prompt,... Those are names of browser functions, for example alert shows a pop-up and open opens a new window. Seeing them confirmed that the code was running in a browser-like environment and could reach page-level features.
Step 7: attempt escalation to command execution
After reviewing the results, the researcher found no access to child_process to run command line code, so the issue could not be escalated to full remote code execution. child_process is a part of Node.js, a JavaScript runtime often used on servers, that lets a program start other programs on the machine. Remote code execution (RCE) means an attacker can run their own commands on the server, which is the most serious outcome. The report attributes the block either to the target being sandboxed or to security restrictions. A sandbox is a restricted environment that limits what code can do, a bit like a playpen. Even so, the template injection was clear and could have led to more serious consequences.
Root cause
The endpoint takes the callback parameter and writes it into the response body unescaped, in a JavaScript context. The developer comment in the JavaScript file describes exactly this design: result data is treated as a string and passed to eval, and the callback parameter is required for the single-device response to work. Because the callback value is also processed by a template engine, expressions inside it are evaluated, which turns a JSONP-style callback reflection into template injection. JSONP is an older technique where a page asks a server for data and the server wraps the data in a function name supplied by the page, so the browser runs it as a script. No sanitisation or validation of the callback value is described, and the template engine version is not stated in the report.
Impact
An attacker could control the content rendered on the page and execute JavaScript expressions in the template engine's context. In this instance the researcher could not reach child_process, so arbitrary command execution was not demonstrated. The report notes that the restriction may have been a sandbox or other security restrictions, and that the vulnerability still posed a significant risk and was worth reporting. The report does not describe any bounty amount or remediation.
Fix
The report's lessons are to always sanitise and validate user data, especially when dealing with callbacks and template engines such as Handlebars, and to keep libraries up to date, since older versions of template engines often have known vulnerabilities.
Variants to test
- JSONP-style callback parameters on other endpoints. Send a request with
callback=set to a unique identifier and check whether the response wraps the JSON in that identifier. A positive result is the identifier appearing verbatim in the response body. - Template expressions in reflected fields. Where a reflected value is rendered by a template engine, submit an arithmetic expression such as
4*4in the field. A positive result is the page printing16rather than the literal text. - Constructor access from an evaluated context. Try
(function(){return+this.constructor.constructor;})()in a reflected JavaScript context. A positive result is afunction anonymous() {}-style response, which indicates access to theFunctionconstructor. - Global object enumeration. Try
(function(){return+Object.keys(this);})()to list properties of the global object. A positive result is a list ofwindowproperties such asalert,openorprompt. - Escalation to process access. From a confirmed JavaScript execution context, check whether
child_processor equivalent process APIs are reachable. A positive result is the ability to require or access the module, which would allow command execution; a negative result suggests a sandbox or other restriction.
Glossary
- API: a set of addresses a program offers so other programs can ask it for data or actions.
- Bug bounty program: a scheme where a company rewards people for reporting security problems responsibly.
- Callback: a name a page asks a server to wrap data in, so the browser runs the data as a script.
- child_process: a part of Node.js that lets a program start other programs on the machine.
- DOM: the browser's model of the page, made of elements like buttons and text boxes.
- Endpoint: a specific address on a server that accepts a particular kind of request.
- eval: a JavaScript function that runs text as code.
- Function constructor: a built-in JavaScript tool that creates new functions from text.
- GET: the request type that asks a server to give back a resource.
- Handlebars: a template engine often used with JavaScript.
- JSON: a text format for structured data made of names and values.
- JSONP: an older technique where a server wraps data in a function name supplied by the page.
- Node.js: a JavaScript runtime often used to run JavaScript on servers.
- Query string: the part of a URL after the question mark that carries extra details.
- RCE (remote code execution): the ability for an attacker to run their own commands on a server.
- Reflection: when your input comes back to you in the server's reply.
- Sandbox: a restricted environment that limits what code can do.
- Sanitisation: cleaning input so it cannot do anything harmful.
- Scope: the list of systems a bug bounty program allows you to test.
- Server-side template injection (SSTI): a bug where user input is treated as part of a template the server evaluates.
- Subdomain: a name in front of a main domain, like
shopinshop.example.com. - Template engine: software that fills blanks in a page template with data.
- Wildcard: a
*symbol meaning "anything here". - window: the global object in a browser, holding page-level functions like
alertandopen.