Get a Pentest and security assessment of your IT network.

Cyber Security

XSS in Plain Text Emails

TL;DR

Yes, a plain text email can contain XSS injection, but it won’t execute directly within the email itself. The risk comes when that plain text is copied and pasted into a web application (like a forum post, comment box, or support ticket) without proper sanitisation. The injected script will then run in the context of that web application.

How XSS Works in Emails

XSS (Cross-Site Scripting) attacks rely on injecting malicious JavaScript code into a trusted website. Plain text emails themselves don’t interpret or execute JavaScript. However, they can act as carriers for the payload.

Steps to Understand and Mitigate the Risk

  1. The Scenario: Imagine someone receives an email with this content:
    <script>alert('XSS Vulnerability!');</script>

    If they copy this text and paste it into a vulnerable web form, the script could execute.

  2. Why Plain Text is Dangerous: Because plain text doesn’t automatically encode or escape characters, malicious code remains intact when copied. HTML emails often have some level of encoding that prevents immediate execution, but plain text does not.
  3. The Web Application’s Role: The vulnerability isn’t in the email itself; it’s in how the web application handles user-submitted data. If the application doesn’t properly sanitise or encode input before displaying it on a webpage, the XSS payload will be rendered and executed.
  4. Mitigation: Input Sanitisation & Output Encoding
    • Input Sanitisation (Server-Side): Before storing any user-submitted data (including content copied from emails), remove or escape potentially harmful characters. This is the most important step. Libraries and frameworks often provide functions for this.
      // Example in PHP - using htmlspecialchars
      $safe_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
    • Output Encoding (Server-Side): When displaying user-submitted data on a webpage, encode it to prevent the browser from interpreting it as code. Again, use appropriate encoding functions.
      // Example in PHP - using htmlspecialchars
      echo htmlspecialchars($user_data, ENT_QUOTES, 'UTF-8');
    • Content Security Policy (CSP): Implement a CSP to control the resources that the browser is allowed to load. This can help limit the damage caused by XSS attacks.
  5. Example Attack Flow:
    1. Attacker sends an email with malicious JavaScript code in plain text.
    2. Victim copies the code from the email.
    3. Victim pastes the code into a vulnerable web form (e.g., forum post).
    4. The web application stores the un-sanitised code.
    5. When another user views the forum post, the malicious JavaScript executes in their browser.
  6. Testing for XSS: Try pasting various XSS payloads into your web forms and see if they are rendered as code.
    • Simple alert box:
      <script>alert('XSS');</script>
    • Image tag with an onerror event:
      <img src="x" onerror="alert('XSS')">

Key Takeaways

  • Plain text emails don’t execute XSS directly.
  • The risk lies in the web applications that process content copied from these emails.
  • Always sanitise user input and encode output to prevent XSS vulnerabilities.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation