Skip to content
XSS Write-up Intermediate

Stored XSS in GMail

The sender name in Gmail's basic HTML layout was not properly escaped when displaying a phishing alert, allowing an attacker to inject script. An attacker could send an email with a malicious sender name to execute JavaScript in the victim's browser without user interaction.

Michele Spagnuolo Follow

Published on blog.miki.it

Here I present a (reported and fixed) Stored XSS vulnerability I discovered in mail.google.com, which required no user interaction.

Michele Spagnuolo, on blog.miki.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.

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

GMail is available in two layouts: the standard JavaScript application and a basic HTML layout, reached at https://mail.google.com/?ui=html, which is intended for slow connections and older browsers. One of the features of the web interface is the phishing alert: a warning shown to the recipient when a message arrives from a source that looks suspicious, for example a message sent through a fake mailer or an open relay SMTP server.

The alert includes the name of the sender. That name is a header field supplied by whoever sends the message, so it is fully under the attacker's control. The vulnerability described here is a stored cross-site scripting issue in mail.google.com, in the basic HTML layout, caused by the phishing alert printing the sender name without correct escaping. It required no user interaction beyond opening the message.

Setup

No special account, role or tool is needed on the victim side: any GMail address works, and the victim only has to read mail in the basic HTML layout. On the attacker side the requirements are:

  1. A way to send mail that triggers the phishing alert, such as a well known fake mailer (the author names Emkei) or any open relay SMTP server.
  2. Control over the From field of the message, which is where the payload is placed.
  3. The ability to choose the message encoding, set to UTF-8.

The author does not give browser versions or GMail build identifiers.

Finding it

The researcher noticed that the phishing alert, when rendered in the basic HTML layout, did not escape the characters in the sender name correctly. The name is attacker-controlled data that arrives in a mail header and is then printed into the alert markup, so any HTML metacharacters in it are interpreted by the browser rather than displayed as text. Because the alert is generated from stored mail, the injected markup persists with the message.

Exploitation

Step 1: prepare a sender that triggers the phishing alert

Go to a well known fake mailer such as Emkei, or use any open relay SMTP server that triggers the phishing alert. The point of this step is to obtain a message whose origin makes GMail display the warning; the warning is the sink that renders the sender name.

Step 2: send the message with the payload in the From field

Send an email to the victim GMail address with the From field set to:

Text
<img src=# onerror=alert(document.cookie)>

The value is an img element whose src is #, which cannot load, so the onerror handler fires as soon as the browser parses the element. The handler calls alert(document.cookie), demonstrating script execution in the origin of mail.google.com and access to the victim's cookies for that origin.

Step 3: choose the encoding

Choose UTF-8 as encoding for the message. This keeps the payload characters intact through the mail pipeline.

Step 4: open GMail in the basic HTML layout

Open your Gmail in the basic HTML layout:

Text
https://mail.google.com/?ui=html

The ui=html parameter selects the basic HTML interface, which is the layout where the alert is rendered without correct escaping.

Step 5: open the received email

Open the received email. The phishing alert is displayed, the sender name is inserted into the page as markup, the broken image triggers onerror, and the script runs. The author's reaction in the write-up is simply: BOOM!

The XSS is stored: the payload lives in the message, so reopening the mail at any later time executes it again. No further interaction from the attacker is needed after delivery.

Root cause

The phishing alert in the basic HTML layout printed the sender name without proper sanitisation. The sender name is untrusted input taken from the From header of an incoming message, and it was placed into the alert's HTML without escaping the characters that have meaning in markup, so an <img> tag with an onerror handler was parsed as an element instead of being shown as text. The standard layout is not described as affected; the flaw is specific to the basic HTML rendering path. The report does not describe the code itself, so the exact escaping routine that was missing is not stated.

Impact

An attacker who can send mail that triggers the phishing alert can execute JavaScript in the context of mail.google.com when the victim opens the message in the basic HTML layout. The proof of concept reads document.cookie, so the immediate impact demonstrated is access to the victim's cookies for that origin. Because the payload is stored in the mailbox, the script runs every time the message is reopened, without any further action by the attacker. The report does not enumerate further consequences, and it does not state which cookies or session material were reachable in practice.

Fix

The vulnerability was fixed by Google in a matter of hours. The author received a reward and was listed in the Google Security Hall of Fame.

Variants to test

  1. Other alert and banner surfaces. Look for any warning, banner or badge that echoes message metadata (sender name, subject, reply-to, display name from the address book) in a webmail or messaging product. A positive result is that a From name containing <img src=# onerror=alert(document.cookie)> produces script execution when the message is opened.
  2. Alternate rendering paths. Test reduced or legacy interfaces, such as a basic HTML mode, a print view, a mobile lite view or a text-only client, since escaping is often implemented separately per renderer. A positive result is that the payload executes in one layout but not in the main application.
  3. Encoding variations. Repeat the delivery with different charsets and transfer encodings, as the author did with UTF-8, to see whether a particular encoding bypasses filtering or normalisation. A positive result is execution that only occurs with a specific encoding.
  4. Header fields beyond From. Try the same payload in other attacker-controlled headers that may be displayed in warnings, such as the display name portion of the address or a reply-to field. A positive result is that the injected element is parsed in the alert rather than displayed literally.
  5. Persistence check. Reopen the message in a new session to confirm the payload is stored rather than reflected. A positive result is that the script runs again on every open, which is the property the author highlights.

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. 10 min read.

What happened, in plain words

A security researcher found a way to make Gmail run code that an attacker wrote, just by having the victim open an email. The trick did not need the victim to click a link, download an attachment or type anything: opening the message was enough.

The problem was in a warning message that Gmail shows when it thinks an email looks suspicious. That warning includes the name of the person who sent the email. The name is written by the sender, so an attacker can put anything there, including pieces of HTML, the language web pages are written in. Gmail copied that name into the warning page without cleaning it up first, so the browser treated the attacker's text as part of the page and ran it as a small program.

This kind of bug is called cross-site scripting, or XSS. It is "stored" because the malicious text lives inside the email in the victim's mailbox, so it fires again every time the message is opened. Google fixed it within hours, paid the researcher a reward and listed him in its security hall of fame.

The product and the feature involved

Gmail is Google's webmail service. It can be used in two ways:

  • The standard layout, a full JavaScript application that behaves like a desktop program inside the browser.
  • The basic HTML layout, a simpler version meant for slow connections and older browsers. It is reached at a special address, https://mail.google.com/?ui=html.

One feature of the web interface is the phishing alert. "Phishing" is the practice of sending a message that pretends to come from someone trustworthy in order to trick the reader. When Gmail sees a message arriving from a source that looks suspicious, for example a message sent through a fake mailer or an open relay SMTP server, it shows the recipient a warning. That warning includes the name of the sender.

The sender name is a header field: a line of information at the top of every email, separate from the body, that carries details such as who sent it and to whom. The From header holds the sender's address and display name. Whoever sends the message fills that in, so it is fully under the attacker's control. Gmail was printing that name into the warning page without escaping it correctly, and that is the bug.

What you need to try it

On the victim side nothing special is required: any Gmail address works, and the victim only has to read mail in the basic HTML layout. On the attacker side you need three things:

  1. A way to send mail that triggers the phishing alert, such as a well known fake mailer (the author names Emkei) or any open relay SMTP server.
  2. Control over the From field of the message, because that is where the malicious text goes.
  3. The ability to choose the message encoding, set to UTF-8.

A few of those terms deserve a plain explanation:

  • A fake mailer is a website that lets you type in any sender address you like and send a message from it, without owning that address.
  • An SMTP server is the computer that moves email from one place to another, the postal sorting office of the internet. An open relay is one that will forward mail for anyone, without checking who the sender really is.
  • Encoding (or character set) is the rulebook a computer uses to turn letters and symbols into numbers it can store. UTF-8 is the most common one and can represent almost every character in every language, which matters here because the malicious text is made of symbols like < and >.

The author does not give browser versions or Gmail build identifiers.

How the researcher found it

The researcher noticed that the phishing alert, when shown in the basic HTML layout, did not escape the characters in the sender name correctly. "Escaping" means converting characters that have a special meaning in HTML into harmless text. In HTML, < and > mark the start and end of a tag, and a tag is an instruction to the browser such as "show an image here". If a name containing those characters is pasted into a page without escaping, the browser does not show them as text: it reads them as an instruction.

The sender name is data that arrives in a mail header and is then printed into the alert. Because the alert is generated from stored mail, the injected markup stays with the message instead of disappearing after one view.

Exploitation, step by step

Step 1: prepare a sender that triggers the phishing alert

Go to a well known fake mailer such as Emkei, or use any open relay SMTP server that triggers the phishing alert. The point of this step is to obtain a message whose origin makes Gmail display the warning. The warning is the place where the sender name gets rendered, so it is the target of the attack.

Step 2: send the message with the payload in the From field

Send an email to the victim Gmail address with the From field set to:

Text
<img src=# onerror=alert(document.cookie)>

Line by line, in plain words:

  • <img ...> is an HTML tag that tells the browser to display an image.
  • src=# says where the image file is. # is not a real address, so the image cannot load.
  • onerror=... is an instruction that says: if loading the image fails, run the code that follows.
  • alert(document.cookie) is that code. alert opens a pop-up box showing whatever is inside the brackets. document.cookie is the browser's way of reading the cookies that belong to the current website.

A cookie is a small piece of data a website asks the browser to keep and send back on every later visit, so the site can remember who you are. In a webmail service, cookies are part of how you stay logged in. Reading them from a page is a common way to prove that an attacker can run code in that page.

Because the image address cannot load, the onerror handler fires as soon as the browser reads the tag. The handler calls alert(document.cookie), which demonstrates that script is running inside mail.google.com and can reach the victim's cookies for that site.

Step 3: choose the encoding

Choose UTF-8 as the encoding for the message. This keeps the payload characters intact as the message travels through the mail system, so the symbols arrive exactly as they were typed instead of being mangled on the way.

Step 4: open Gmail in the basic HTML layout

Open your Gmail in the basic HTML layout:

Text
https://mail.google.com/?ui=html

Here https://mail.google.com/ is the address of Gmail, and ?ui=html is a parameter added to the end of the address. A parameter is a small piece of information passed to a website in its address, usually after a question mark, telling it how you want the page to behave. In this case ui=html asks Gmail for the basic HTML interface, which is the layout where the alert is rendered without correct escaping.

Step 5: open the received email

Open the received email. The phishing alert is displayed, the sender name is inserted into the page as markup, the broken image triggers onerror, and the script runs. The author's reaction in the write-up is simply: BOOM!

The XSS is stored: the payload lives in the message, so reopening the mail at any later time executes it again. No further interaction from the attacker is needed after delivery. Think of the difference between a forged letter that only works once and a forged letter that is kept in the drawer and read out loud every time someone opens it.

Why it worked

The phishing alert in the basic HTML layout printed the sender name without proper sanitisation. Sanitisation means cleaning data before using it, for example by turning dangerous characters into harmless ones. The sender name is untrusted input taken from the From header of an incoming message, and it was placed into the alert's HTML without escaping the characters that have meaning in markup. So an <img> tag with an onerror handler was parsed as an element instead of being shown as text.

The standard layout is not described as affected; the flaw is specific to the basic HTML rendering path. The report does not describe the code itself, so the exact escaping routine that was missing is not stated.

What an attacker could do

An attacker who can send mail that triggers the phishing alert can execute JavaScript in the context of mail.google.com when the victim opens the message in the basic HTML layout. JavaScript is the programming language that web pages use to make themselves interactive; running it inside a site means running it with that site's permissions.

The proof of concept reads document.cookie, so the immediate impact demonstrated is access to the victim's cookies for that site. Because the payload is stored in the mailbox, the script runs every time the message is reopened, without any further action by the attacker. The report does not enumerate further consequences, and it does not state which cookies or session material were reachable in practice.

The fix

The vulnerability was fixed by Google in a matter of hours. The author received a reward and was listed in the Google Security Hall of Fame.

Similar places worth testing

  1. Other alert and banner surfaces. Look for any warning, banner or badge that echoes message details (sender name, subject, reply-to, display name from the address book) in a webmail or messaging product. A positive result is that a From name containing <img src=# onerror=alert(document.cookie)> produces script execution when the message is opened.
  2. Alternate rendering paths. Test reduced or legacy interfaces, such as a basic HTML mode, a print view, a mobile lite view or a text-only client, since escaping is often implemented separately for each renderer. A positive result is that the payload executes in one layout but not in the main application.
  3. Encoding variations. Repeat the delivery with different character sets and transfer encodings, as the author did with UTF-8, to see whether a particular encoding bypasses filtering or normalisation. A positive result is execution that only occurs with a specific encoding.
  4. Header fields beyond From. Try the same payload in other attacker-controlled headers that may be displayed in warnings, such as the display name portion of the address or a reply-to field. A positive result is that the injected element is parsed in the alert rather than displayed literally.
  5. Persistence check. Reopen the message in a new session to confirm the payload is stored rather than reflected. A positive result is that the script runs again on every open, which is the property the author highlights.

Glossary

  • API: a defined way for one piece of software to ask another for information or actions, like a menu of requests a program accepts.
  • Basic HTML layout: a simplified version of Gmail meant for slow connections and older browsers, reached at https://mail.google.com/?ui=html.
  • Cookie: a small piece of data a website asks the browser to store and send back on later visits, often used to keep you logged in.
  • Cross-site scripting (XSS): a bug where a website lets an attacker's text be treated as code by the browser, so the attacker's script runs inside that site.
  • Encoding (character set): the rulebook that turns letters and symbols into numbers a computer can store; UTF-8 is a common one.
  • Escaping: converting characters that have a special meaning in HTML into harmless text so they are displayed rather than interpreted.
  • Fake mailer: a website that lets you send email with any sender address you choose, without owning it.
  • From header: the line at the top of an email that holds the sender's address and display name, supplied by whoever sends the message.
  • Header field: a line of information at the top of an email, separate from the body, carrying details such as sender and recipient.
  • HTML: the language web pages are written in, made of tags that tell the browser what to display.
  • JavaScript: the programming language web pages use to make themselves interactive.
  • Open relay SMTP server: a mail server that forwards email for anyone without checking who the sender really is.
  • Parameter: a small piece of information passed to a website in its address, usually after a question mark.
  • Phishing: sending a message that pretends to come from someone trustworthy in order to trick the reader.
  • Phishing alert: a warning Gmail shows when a message arrives from a source that looks suspicious.
  • Sanitisation: cleaning data before using it, for example by turning dangerous characters into harmless ones.
  • SMTP server: the computer that moves email from one place to another.
  • Stored XSS: an XSS bug where the malicious text is saved (for example in an email) and runs again every time it is viewed.
  • Tag: an instruction in HTML, written between < and >, such as one that tells the browser to show an image.
  • onerror handler: an instruction attached to an element that runs code when loading it fails.
  • document.cookie: the browser's way of reading the cookies that belong to the current website.

More by Michele Spagnuolo

View all

1 more disclosed report, best first.

More from Google

View all

The newest disclosed reports for this program.

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.