Skip to content
XSS Write-up Intermediate

What an injection into jQuery-selector can lead to

The survey creation feature allowed arbitrary strings as question names, which were later injected into a jQuery selector without sanitisation. An attacker could craft a question name that, when the survey was submitted, caused the selector to match a CSRF token input and include its value in the results, which the attacker could then read.

Anton Subbotin Follow

Published on systemweakness.com

Bug Bounty

Anton Subbotin (skavans), on systemweakness.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

The target was a survey feature on a private bug bounty program, so the author describes it in abstract terms. A survey owner picks questions from a pre-prepared list and includes them in a survey. When the survey is saved, the chosen questions are sent to the server as JSON. When a visitor opens the survey, that same JSON is embedded inside the HTML, and client-side code builds a form from it and later collects the answers.

The owner of the survey can then see, in their personal account, the email of each respondent together with the answers they submitted.

Setup

The program is private, so no hostnames, endpoints or account details are given. The researcher worked with two roles that the feature implies: a survey owner (the attacker) who creates a survey and reads the results, and a visitor (the victim) who opens the survey, fills it in and submits it. No specific tools are named. The jQuery version in use was, according to the report, the most recent one, which is why the author rules out XSS through the selector.

Finding it

The first observation was that the server did not validate the contents of the chosen_questions array. The owner is supposed to pick from a fixed list, but arbitrary strings could be added to the array and were saved successfully. The JSON sent on save looked like this:

JSON
{"chosen_questions": ["country", "gender", "favorite_movie"]}

On the client side, the questions become inputs in a form:

HTML
<form id=questions><input id=country><input id=gender><input id=favorite_movie></form>

and a submit function builds the results object by looking each input up with a jQuery selector:

JavaScript
var results = {};for (q of chosen_questions) {    results[q] = $('form#questions > input#' + q).val()}send_post('/results', results);

The question name is concatenated into the selector with no filtering. Since the jQuery version was current, the author discounted XSS and instead looked at how jQuery parses selectors. Testing different selector shapes showed that a selector such as $('#invalid,#valid') returns the element with id=valid when no element with id=invalid exists on the page. In other words, comma-separated selectors are evaluated one after another, and the first one that matches produces the returned element.

With an injection point inside the selector, the next step was to look for inputs on the survey page that hold sensitive data. The author found a CSRF token in another form on the page:

HTML
<input name=csrf value=123123123>

Exploitation

Step 1: create a survey with a crafted question

The attacker creates a survey and saves a question list containing a selector fragment:

JSON
{"chosen_questions": ["country", "gender", "INVALID,[name='csrf']"]}

The third entry is not a real question. It is a string that, once concatenated into the selector, adds a second selector targeting the CSRF input by its name attribute.

Step 2: the victim opens the survey and submits it

When a visitor opens the attacker's survey, fills in the fields and clicks Submit, the submit function iterates over the questions from the JSON, injects each one into the jQuery selector, finds the matching input and stores its value in results.

Step 3: the injected selector resolves to the CSRF input

For the third question, jQuery evaluates a selector of this shape:

JavaScript
$('form#questions > input#INVALID,[name="csrf"]')

The first part, form#questions > input#INVALID, matches nothing because there is no input with id INVALID, so that part is ignored. The second part, [name='csrf'], matches the input holding the CSRF token. That input is found, and its value is written into results as the answer to the third question.

Step 4: read the token from the results

After the form is submitted, the survey owner sees the victim's CSRF token in their personal account as the answer to the third question. The victim's email is attached to every answer automatically, so the attacker also learns which address the token belongs to.

Step 5: forge requests with the stolen token

With the token and the email, the attacker can send each victim a link to a specially crafted CSRF form that carries that particular user's token, and forge any request against the service on the victim's behalf.

Root cause

The server accepted arbitrary strings in chosen_questions instead of restricting them to the prepared list, and the client concatenated those strings directly into a jQuery selector. jQuery's comma-separated selector syntax then let an injected fragment add a second selector that matched an unrelated input on the page. The current jQuery version blocked the usual selector-based XSS, but it did not stop the selector from being redirected at another element, so the existing defence did not address the actual problem.

Impact

An attacker who can create a survey can obtain the CSRF token of any visitor who opens it and submits the form, together with that visitor's email address. With those, the attacker can forge requests against the service as that user. The attack requires the victim to open the attacker's survey, fill it in and submit it; the report does not describe any other precondition.

Fix

The report does not say what was changed. The changes it implies are server-side validation of chosen_questions against the prepared list, and avoiding string concatenation of question names into selectors on the client, for example by looking inputs up by a fixed attribute or by escaping the value before it reaches the selector.

Variants to test

  1. Any feature where user-controlled names are concatenated into a jQuery selector: try a value such as INVALID,[name='csrf'] and see whether an unrelated input's value comes back in the output.
  2. Lists of identifiers saved as JSON and rendered client-side: submit entries outside the allowed set and check whether they are stored and later used in DOM queries.
  3. Pages that hold tokens in inputs outside the main form: check whether a comma-separated selector can reach them from an injected fragment, with a positive result being the token value appearing where the attacker can read it.
  4. Features that attach the respondent's identity to submitted data: check whether a stolen token can be paired with an address, which turns a token leak into a targeted forgery.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

A security researcher found a way to steal a secret code from other people who filled in an online survey. The survey belonged to a private bug bounty program, which is a scheme where companies pay people to find security holes in their websites. Because the program was private, the researcher does not name the site or the exact addresses involved, and describes the feature in general terms.

The secret code was a CSRF token. A CSRF token is a short random value that a website puts into a form so it can tell that the form was really submitted by the person using the site, and not by some other website trying to trick them. If an attacker learns your token, they can build a form that looks legitimate to the site and make requests as if they were you.

The researcher found that the survey let the person who created it put any text they liked into the list of questions, and that text was later pasted into a piece of code that looks up fields on the page. By choosing the right text, the attacker could make that lookup grab the CSRF token instead of a survey answer. When the victim submitted the survey, the token was sent to the survey owner as if it were an answer. Since the survey owner also sees the respondent's email address, the attacker ended up with both the token and the address it belonged to.

Background: the survey feature

A survey owner picks questions from a pre-prepared list and includes them in a survey. For example, they might choose "country", "gender" and "favorite_movie". When the survey is saved, the chosen questions are sent to the server as JSON. JSON is a plain text format for structured data, a bit like a very simple spreadsheet written out as text, with names in quotes and values after them.

When a visitor opens the survey, that same JSON is embedded inside the HTML of the page. HTML is the language web pages are written in. Client-side code, meaning code that runs in the visitor's own browser rather than on the server, builds a form from the JSON and later collects the answers.

The survey owner can then log in to their personal account and see the email address of each respondent together with the answers they submitted.

Background: the two roles

The program is private, so no hostnames, endpoints or account details are given. The researcher worked with the two roles the feature implies:

  • A survey owner, who acts as the attacker: they create a survey and read the results.
  • A visitor, who acts as the victim: they open the survey, fill it in and submit it.

No specific tools are named. The jQuery version in use was, according to the report, the most recent one. jQuery is a widely used JavaScript library, a collection of ready-made code that websites load to do common tasks, and one of those tasks is finding elements on a page. Because the version was current, the author ruled out XSS through the selector. XSS, or cross-site scripting, is a class of attack where an attacker gets their own code to run inside someone else's page; Because the version was current, the author ruled out XSS through the selector.

Finding it

The first observation was that the server did not check what was inside the chosen_questions array. The owner is supposed to pick from a fixed list, but arbitrary strings could be added to the array and were saved successfully. The JSON sent on save looked like this:

JSON
{"chosen_questions": ["country", "gender", "favorite_movie"]}

Line by line: the outer braces mark the start and end of one JSON object, a bundle of named values. chosen_questions is the name of the field, and the square brackets mark a list. Inside the list are three text values in quotes, separated by commas: the names of the three questions the owner picked. The server is supposed to accept only names from its prepared list, but it did not check.

On the client side, the questions become inputs in a form. An input is a field on a page where you type something, such as a text box. The form looked like this:

HTML
<form id=questions><input id=country><input id=gender><input id=favorite_movie></form>

Line by line: <form id=questions> opens a form and gives it the name questions, so other code can find it. Each <input id=...> is one field, and its id is the question name, so the country field is called country, the gender field is called gender, and so on. </form> closes the form. The id is an identifier: a label that should be unique on the page, like a name tag on a coat.

A submit function then builds the results object by looking each input up with a jQuery selector:

JavaScript
var results = {};for (q of chosen_questions) {    results[q] = $('form#questions > input#' + q).val()}send_post('/results', results);

Line by line:

  1. var results = {}; creates an empty container called results, which will hold the answers.
  2. for (q of chosen_questions) { starts a loop that goes through the list of question names one at a time, putting the current name into q.
  3. results[q] = $('form#questions > input#' + q).val() is the important line. $('...') is jQuery's way of finding an element on the page. The text inside the quotes is a selector, a pattern that describes which element you want. Here the pattern is built by gluing together a fixed part, form#questions > input#, and the question name q. The > means "directly inside". So for the question country, the selector becomes form#questions > input#country, meaning the input called country inside the form called questions. .val() then reads the value typed into that field, and the result is stored in results under the question name.
  4. } closes the loop.
  5. send_post('/results', results); sends the collected answers to the server at the path /results.

The question name is glued into the selector with no filtering. Since the jQuery version was current, the author discounted XSS and instead looked at how jQuery parses selectors. Testing different selector shapes showed that a selector such as $('#invalid,#valid') returns the element with id=valid when no element with id=invalid exists on the page. In other words, comma-separated selectors are evaluated one after another, and the first one that matches produces the returned element. A selector is a search pattern, and a comma inside it means "or": try this pattern, and if nothing matches, try the next one.

With an injection point inside the selector, the next step was to look for inputs on the survey page that hold sensitive data. The author found a CSRF token in another form on the page:

HTML
<input name=csrf value=123123123>

Line by line: this is a single input field. name=csrf gives it the name csrf, and value=123123123 is the token itself, a stand-in value in the report. The token is a secret that the site uses to check that a form submission really came from the person using the site.

Exploitation

Step 1: create a survey with a crafted question

The attacker creates a survey and saves a question list containing a selector fragment:

JSON
{"chosen_questions": ["country", "gender", "INVALID,[name='csrf']"]}

Line by line: the list still starts with two ordinary questions, country and gender. The third entry is not a real question. It is a string that, once glued into the selector, adds a second selector targeting the CSRF input by its name attribute. An attribute is an extra piece of information attached to an element, such as its name or its value. The comma inside the string is what makes jQuery treat the rest as an alternative pattern.

Step 2: the victim opens the survey and submits it

When a visitor opens the attacker's survey, fills in the fields and clicks Submit, the submit function goes through the questions from the JSON one by one, glues each one into the jQuery selector, finds the matching input and stores its value in results.

Step 3: the injected selector resolves to the CSRF input

For the third question, jQuery evaluates a selector of this shape:

JavaScript
$('form#questions > input#INVALID,[name="csrf"]')

Line by line: the first part, form#questions > input#INVALID, matches nothing because there is no input with id INVALID, so that part is ignored. The comma means "or try the next pattern". The second part, [name='csrf'], matches any element whose name attribute is csrf, which is the input holding the CSRF token. That input is found, and its value is written into results as the answer to the third question. The attacker never needed to know the token in advance: the victim's own browser looked it up and handed it over.

Step 4: read the token from the results

After the form is submitted, the survey owner sees the victim's CSRF token in their personal account as the answer to the third question. The victim's email is attached to every answer automatically, so the attacker also learns which address the token belongs to. That pairing is what turns a leaked token into a targeted attack: the attacker knows exactly whose token it is.

Step 5: forge requests with the stolen token

With the token and the email, the attacker can send each victim a link to a specially crafted CSRF form that carries that particular user's token, and forge any request against the service on the victim's behalf. Forging a request means sending one that the site accepts as genuine, because it carries the right token.

Root cause

The server accepted arbitrary strings in chosen_questions instead of restricting them to the prepared list, and the client glued those strings directly into a jQuery selector. jQuery's comma-separated selector syntax then let an injected fragment add a second selector that matched an unrelated input on the page. The current jQuery version blocked the usual selector-based XSS, but it did not stop the selector from being redirected at another element, so the existing defence did not address the actual problem.

Impact

An attacker who can create a survey can obtain the CSRF token of any visitor who opens it and submits the form, together with that visitor's email address. With those, the attacker can forge requests against the service as that user. The attack requires the victim to open the attacker's survey, fill it in and submit it; the report does not describe any other precondition.

Fix

The changes it implies are server-side validation of chosen_questions against the prepared list, and avoiding string concatenation of question names into selectors on the client, for example by looking inputs up by a fixed attribute or by escaping the value before it reaches the selector. Validation means checking the input against what is allowed and rejecting anything else; escaping means treating special characters as plain text so they cannot change the meaning of the code around them.

Variants to test

  1. Any feature where user-controlled names are glued into a jQuery selector: try a value such as INVALID,[name='csrf'] and see whether an unrelated input's value comes back in the output.
  2. Lists of identifiers saved as JSON and rendered client-side: submit entries outside the allowed set and check whether they are stored and later used in DOM queries. The DOM is the browser's in-memory model of the page, the tree of elements that code searches through.
  3. Pages that hold tokens in inputs outside the main form: check whether a comma-separated selector can reach them from an injected fragment, with a positive result being the token value appearing where the attacker can read it.
  4. Features that attach the respondent's identity to submitted data: check whether a stolen token can be paired with an address, which turns a token leak into a targeted forgery.

Glossary

  • API: a defined way for one piece of software to ask another for something, like a service counter with a fixed menu of requests.
  • Attribute: extra information attached to an HTML element, such as its name or value.
  • Bug bounty program: a scheme where an organisation pays researchers for finding and reporting security problems in its systems.
  • Client-side: code that runs in the visitor's own browser rather than on the server.
  • CSRF: cross-site request forgery, an attack where a malicious site makes a victim's browser send a request to another site as if the victim had made it.
  • CSRF token: a random value placed in a form so the site can check that the submission really came from the person using the site.
  • DOM: the browser's in-memory tree of the page's elements, which code searches through.
  • Escaping: treating special characters as plain text so they cannot change the meaning of surrounding code.
  • Form: a set of input fields on a page that a visitor fills in and submits.
  • HTML: the language web pages are written in.
  • id: a label for an element that should be unique on the page.
  • Input: a field on a page where a visitor types or selects something.
  • JavaScript: the programming language that runs in web browsers.
  • jQuery: a widely used JavaScript library that provides ready-made code for common tasks, including finding elements on a page.
  • JSON: a plain text format for structured data, with named fields and values.
  • Selector: a pattern that describes which element or elements on a page you want to find.
  • Server: the computer that stores the site's data and answers requests from browsers.
  • Validation: checking input against what is allowed and rejecting anything else.
  • XSS: cross-site scripting, an attack where an attacker gets their own code to run inside someone else's page.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.