Stored XSS with two different parameters
The comment endpoint HTML-encoded the comment field but failed to encode the first name field, which was limited to 12 characters. By splitting a script payload across the first name and comment fields, an attacker could execute stored JavaScript in the browser of anyone viewing the comment.
Published on joelmcg1993.medium.com
Today I will share a story of how to exploit an XSS vulnerability using two different parameters.
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 private client application, so the author does not name the host, the endpoint or the product. What the report does describe is a feature: users can upload pictures and comment on them, and every other user can see those comments. The comment form has a single input field, comment.
When a comment is submitted, two values are written to the database: the commenter's first name and the value of the comment parameter. Both are then rendered back on the page inside a fixed block of markup, so anything that reaches the browser sits between HTML tags. The report shows the rendered structure as:
<p>
<h3> `YourFirstName`</h3>
<br>
`CommentValue`
</p>
The first name is not set on the comment form. It comes from the account settings page, where the user can edit it, and that field accepts at most 12 characters.
Setup
No tooling, browser version or account roles are described beyond the two inputs the author controls: the first name in the settings page and the comment field on a picture. The precondition for the whole attack is simply an account that can post a comment on a picture that other users, including administrators, will view.
Finding it
The comment field HTML-encodes what the author calls dangerous characters, so < becomes < and > becomes >. The author presents this as the right way to protect against XSS, then questions whether it is. The author's reasoning at this point is that the only remaining input is the second parameter, the first name.
To test how the first name is handled, the author set it to a value containing both angle brackets:
<>Joel
On the profile page those characters were HTML-encoded. On the comment endpoint they were not. After posting a comment, the page came back with the first name rendered literally:
<p>
<h3>`<>Joel`</h3>
<br>
`whatever`
</p>
So the same stored value is encoded in one view and emitted raw in another. That inconsistency is the bug: the comment endpoint trusts the first name.
Exploitation
The obstacle is the 12-character limit on the first name. The author first tried short payloads, including:
<svg/onload=alert(1)>
That is too long. A search led to brutelogic's post on the shortest reflected XSS payload, but that payload is 15 characters, still over the limit. The conclusion was that neither parameter could carry a complete payload alone, so the exploit had to be split across both.
Step 1: open a JavaScript comment in the first name
JavaScript supports both line comments and block comments:
//This is a comment per line
/*
Multiple comments
on multiple lines
*/
The author used the block form to start a comment inside a script tag. The first name was set to:
<script>/*
It fits within the 12-character limit. It opens a script element and immediately starts a block comment, which means everything the browser parses next, until the comment is closed, is ignored by the JavaScript engine.
Step 2: close the comment and run code in the comment field
The comment value was set to:
*/alert(document.domain)//
The leading */ closes the block comment opened in the first name. The call alert(document.domain) is the payload. The trailing // starts a line comment so that whatever markup follows is ignored by the JavaScript parser.
Step 3: load the page
When the page was visited, the XSS fired. The resulting source looked like this:
<p>
<h3>`<script>/*</h3><br>*/alert(document.domain)//</p></script>`</h3>
</p>

Reading that output: the <script> tag is opened inside the h3, the /* starts the block comment, the </h3><br> that the template emits next is swallowed by that comment, the */ in the comment field closes it, the alert runs, and the // comments out the </p> and everything after it. The </script> that closes the element is not present in either input.
Root cause
The browser repairs malformed HTML before rendering it. It reads the markup and checks that tags are correctly closed; where they are not, it inserts the missing closing tags so the document can be parsed without errors. Because of that, the payload does not need to supply </script>: the browser adds it itself. In this case the browser appends </script></h3></p> after the injected code, which is why the script element is valid even though neither parameter contains a closing tag.
The underlying mistake is that the first name was encoded on the profile page but not on the comment endpoint. The comment field's encoding was correct and did its job; the second parameter bypassed it entirely. The 12-character limit only forced the payload to be split, not prevented. The report does not describe the server-side code, so the exact place where encoding was skipped is not shown.
Impact
This is stored XSS: the first name and the comment are saved in the database and rendered to every user who views the picture, so any user, including administrators, who visits the page triggers the script. The author notes that stored XSS is more critical than reflected XSS for exactly this reason. The demonstrated payload only calls alert(document.domain); the report does not show any other action taken with the execution.
Fix
The fix it argues for is that every user-supplied parameter must be properly filtered or encoded on every endpoint, not just on the page where it is first displayed. The fix it argues for is that every user-supplied parameter must be properly filtered or encoded on every endpoint.
Variants to test
- Same value, different views. Put a marker such as
<>in a profile field and view it on every page that renders it: profile, comments, listings, emails, admin panels. A positive result is the raw characters appearing in the HTML source on any one of those views while the others show<>. - Split payloads across two fields. Where one field is length-limited and another is encoded, try opening a construct in the short field and closing it in the long one, as here with
<script>/*and*/alert(1)//. A positive result is the script executing with no complete tag in either field. - Length-limit arithmetic. Count the characters a field actually accepts and look for the shortest construct that opens an execution context within that budget. A positive result is any input at or under the limit that changes how the following markup is parsed.
- Browser tag repair. Submit markup with unclosed tags and inspect the DOM the browser builds. A positive result is the browser inserting a closing tag that turns your input into a valid script element.
- Comment syntax as a bridge. Try
/*,*/and//in one field to neutralise the template markup emitted between the two injection points. A positive result is the intervening tags disappearing from the executed script rather than causing a syntax error.
What happened, in plain words
A website let people upload pictures and leave comments on them. Every visitor could see those comments. The site took two pieces of text from each commenter: the comment itself, and the person's first name, which was set elsewhere in the account settings. Both were saved in the site's database and shown back on the picture page.
The site tried to protect the comment field by turning the characters < and > into harmless text codes, so nobody could sneak a new piece of code into a comment. That is the right idea. But the first name field was not protected in the same way on the comment page. It was protected on the profile page, just not on the comment page. That gap let the author combine the two fields into one working attack, even though the first name could only be 12 characters long.
This is called stored cross-site scripting, or stored XSS. "Stored" means the harmful text is saved on the server and shown to everyone who later opens the page, not just to the person who typed it.
The basics you need first
Before the steps, a few ideas from zero.
A web page is text that a browser reads. When you open a page, your browser downloads a document written in HTML. HTML is a markup language: it uses tags such as <p> for a paragraph and <h3> for a heading to describe what each piece of text is. Tags usually come in pairs, an opening tag like <p> and a closing tag like </p>, with content in between.
A browser is forgiving. If a page has a tag that was opened but never closed, the browser does not give up. It quietly adds the missing closing tag so the page can still be displayed. This repair behaviour matters a lot later.
JavaScript is the code that runs in the page. HTML describes what is on the page; JavaScript makes it do things, like react to a click or show a pop-up. JavaScript is placed inside a <script> tag. When the browser meets a <script> tag, it stops treating the text as page content and starts treating it as code to run.
XSS means getting your own JavaScript into someone else's page. If a site shows text you typed without cleaning it first, you can type JavaScript instead of ordinary text, and the browser will run it as if the site itself had written it. The classic demonstration is a pop-up box showing the site's address, written as alert(document.domain). alert shows a message; document.domain is the address of the current page. Seeing that box proves code ran.
There are three common kinds of XSS. Reflected XSS comes from the current request: you send something, the site echoes it straight back. Stored XSS comes from the site's database: your text is saved and shown to other people later. DOM-based XSS lives in the browser-side code rather than the server-side code. This story is the stored kind.
Encoding is the usual defence. If a site turns < into < and > into >, the browser shows those characters as text instead of treating them as the start and end of a tag. The text is displayed, but it cannot become code. This is called HTML encoding, and it is the correct way to stop XSS.
A parameter is a named piece of data sent to a server. When you fill in a form and press submit, the browser sends the field names and their values to the server. The comment form had one field, named comment. The first name was a second value the site also knew about, taken from your account settings.
The target
The author cannot name the site, the address or the product, because it was a private client. What can be described is the feature: users upload pictures, and anyone can comment on them, and everyone can see the comments. The comment form has a single input field, comment.
When a comment is submitted, two values are written to the database: the commenter's first name and the value of the comment parameter. Both are then shown back on the page inside a fixed block of markup, so whatever reaches the browser sits between HTML tags. The report shows the structure like this:
<p>
<h3> `YourFirstName`</h3>
<br>
`CommentValue`
</p>
Line by line:
<p>opens a paragraph, the outer container for the whole block.<h3>opens a heading, andYourFirstNameis where the saved first name is placed.</h3>closes the heading.<br>inserts a line break.CommentValueis where the saved comment is placed.</p>closes the paragraph.
The backticks in the report are just placeholders showing where each value lands; they are not part of the page.
The first name is not typed on the comment form. It comes from the account settings page, where the user can edit it, and that field accepts at most 12 characters.
Setting up
The report describes no special tools, browser version or account roles beyond the two inputs the author controls: the first name in the settings page and the comment field on a picture. The only precondition for the whole attack is an account that can post a comment on a picture that other users, including administrators, will look at.
Finding the weakness
The comment field HTML-encodes what the author calls dangerous characters, so < becomes < and > becomes >. The author calls this the right way to protect against XSS, then asks whether it is. The author's reasoning at this point is simple: if the comment is locked down, the only other input left is the first name.
To test how the first name is handled, the author set it to a value containing both angle brackets:
<>Joel
This is a probe, not an attack. It asks one question: does the site clean up these two characters, or does it pass them through untouched? On the profile page those characters were HTML-encoded. On the comment page they were not. After posting a comment, the page came back with the first name rendered literally:
<p>
<h3>`<>Joel`</h3>
<br>
`whatever`
</p>
Line by line:
<p>opens the paragraph block.<h3>opens the heading, and inside it the raw<>Joelappears. The angle brackets were not turned into<and>here.</h3>closes the heading,<br>adds a line break, andwhateverstands in for the comment text.</p>closes the paragraph.
So the same saved value is encoded in one view and emitted raw in another. That inconsistency is the bug: the comment page trusts the first name.
Exploiting it
The obstacle is the 12-character limit on the first name. The author first tried short payloads, including:
<svg/onload=alert(1)>
This is a complete little attack in one line. <svg> starts an image-like element, onload= attaches code to run when it loads, and alert(1) is the code. But it is too long for a 12-character field. A search led to brutelogic's post on the shortest reflected XSS payload, but that payload is 15 characters, still over the limit. The conclusion was that neither field could carry a complete payload alone, so the exploit had to be split across both.
Step 1: open a JavaScript comment in the first name
JavaScript supports two styles of comment, text the browser ignores when running code. A line comment runs to the end of the line:
//This is a comment per line
Here // tells JavaScript to ignore everything after it on that line.
A block comment spans from an opening marker to a closing marker:
/*
Multiple comments
on multiple lines
*/
Here /* starts the ignored section and */ ends it, so everything in between is skipped.
The author used the block form to start a comment inside a script tag. The first name was set to:
<script>/*
Line by line:
<script>opens a script element, which tells the browser that what follows is JavaScript to run./*starts a block comment, so the JavaScript engine will ignore everything until it finds*/.
It fits within the 12-character limit. It opens a script element and immediately starts a block comment, which means everything the browser parses next, until the comment is closed, is ignored by the JavaScript engine.
Step 2: close the comment and run code in the comment field
The comment value was set to:
*/alert(document.domain)//
Line by line:
*/closes the block comment that the first name opened. From here on, JavaScript pays attention again.alert(document.domain)is the payload: it shows a pop-up containing the address of the current page, which proves code ran.//starts a line comment, so whatever markup follows is ignored by the JavaScript parser.
Step 3: load the page
When the page was visited, the XSS fired. The resulting source looked like this:
<p>
<h3>`<script>/*</h3><br>*/alert(document.domain)//</p></script>`</h3>
</p>

Reading that output: the <script> tag is opened inside the h3, the /* starts the block comment, the </h3><br> that the page template emits next is swallowed by that comment, the */ in the comment field closes it, the alert runs, and the // comments out the </p> and everything after it. The </script> that closes the element is not present in either input.
Why it worked
The browser repairs malformed HTML before rendering it. It reads the markup and checks that tags are correctly closed; where they are not, it inserts the missing closing tags so the document can be parsed without errors. Because of that, the payload does not need to supply </script>: the browser adds it itself. In this case the browser appends </script></h3></p> after the injected code, which is why the script element is valid even though neither field contains a closing tag.
The underlying mistake is that the first name was encoded on the profile page but not on the comment page. The comment field's encoding was correct and did its job; the second parameter bypassed it entirely. The 12-character limit only forced the payload to be split, not prevented. The report does not describe the server-side code, so the exact place where encoding was skipped is not shown.
Impact
This is stored XSS: the first name and the comment are saved in the database and shown to every user who views the picture, so any user, including administrators, who visits the page triggers the script. The author notes that stored XSS is more critical than reflected XSS for exactly this reason. The demonstrated payload only calls alert(document.domain); the report does not show any other action taken with the execution.
Fix
The fix it argues for is that every user-supplied parameter must be properly filtered or encoded on every page where it is shown, not just on the page where it is first displayed. The fix it argues for is that every user-supplied parameter must be properly filtered or encoded on every endpoint.
Variants to test
- Same value, different views. Put a marker such as
<>in a profile field and view it on every page that renders it: profile, comments, listings, emails, admin panels. A positive result is the raw characters appearing in the HTML source on any one of those views while the others show<>. - Split payloads across two fields. Where one field is length-limited and another is encoded, try opening a construct in the short field and closing it in the long one, as here with
<script>/*and*/alert(1)//. A positive result is the script executing with no complete tag in either field. - Length-limit arithmetic. Count the characters a field actually accepts and look for the shortest construct that opens an execution context within that budget. A positive result is any input at or under the limit that changes how the following markup is parsed.
- Browser tag repair. Submit markup with unclosed tags and inspect the DOM the browser builds. A positive result is the browser inserting a closing tag that turns your input into a valid script element.
- Comment syntax as a bridge. Try
/*,*/and//in one field to neutralise the template markup emitted between the two injection points. A positive result is the intervening tags disappearing from the executed script rather than causing a syntax error.
Glossary
- HTML: the markup language that describes the structure of a web page, using tags such as
<p>and<h3>. - Tag: a piece of HTML markup, usually an opening tag like
<p>and a closing tag like</p>around some content. - Browser: the program you use to open web pages; it reads HTML and runs JavaScript.
- JavaScript: the programming language that runs inside a web page to make it interactive.
<script>tag: the HTML tag that tells the browser the text inside it is JavaScript to run.- XSS (cross-site scripting): getting your own JavaScript to run inside someone else's web page, usually by typing it into a field the site shows back without cleaning it.
- Reflected XSS: XSS where the harmful text comes from the current request and is echoed straight back.
- Stored XSS: XSS where the harmful text is saved in the site's database and shown to other users later.
- DOM-based XSS: XSS that lives in the browser-side code rather than the server-side code.
- Payload: the piece of text or code that carries out the attack.
- Parameter: a named piece of data sent to a server, such as a form field.
- Endpoint: the address on a server that receives a request and returns a response.
- HTML encoding: turning characters like
<and>into codes like<and>so the browser shows them as text instead of treating them as markup. - Database: the storage where a site keeps data such as comments and names so it can show them again later.
alert: a JavaScript function that shows a pop-up message; used here to prove code ran.document.domain: a JavaScript value holding the address of the current page.- Comment (in code): text that the JavaScript engine ignores;
//starts a line comment and/*...*/marks a block comment. - Block comment: a comment that starts with
/*and ends with*/, ignoring everything in between. - Line comment: a comment that starts with
//and ignores the rest of that line. - Character limit: a cap on how many characters a field accepts; here 12 for the first name.
- DOM: the browser's in-memory model of the page, built from the HTML after the browser has repaired any malformed tags.
- Administrator: a user with extra rights on a site, often able to see and manage other users' content.