CVE-2025-59526: Mailgen HTML Injection Vulnerability Vulnerability

MODERATE
Advisory Identifier: GHSA-j2xj-h7w5-r7vp
CVSS 5.3
CWE-79
Published
Advisory Summary

CVE-2025-59526 is a MODERATE (CVSS 5.3) html injection vulnerability vulnerability (CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')) affecting Mailgen libraries in the npm ecosystem. The issue involves html injection vulnerability in plaintext e-mails. Remediation is available by upgrading affected packages to version 2.0.30.

CVE-2025-59526 at a Glance

CVE Identifier CVE-2025-59526
GHSA Identifier GHSA-j2xj-h7w5-r7vp
Severity MODERATE
CVSS v3.1 Base Score 5.3
CWE Classification CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
Affected Product Mailgen
Package Ecosystem npm
Published Date
Last Updated

Security Impact

CVSS: 5.3

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality None (C:N)
Integrity Low (I:L)
Availability None (A:N)

How to Fix CVE-2025-59526

To remediate this vulnerability, upgrade all affected Mailgen dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

mailgen fixed: 2.0.30
npm install mailgen@2.0.30
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

npm ls mailgen

How to Check if You Are Affected

To determine whether your Node.js application is affected by CVE-2025-59526, inspect your project dependencies using npm:

npm ls mailgen

Analyze the terminal output to locate any version of Mailgen in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 2.0.30), your application is affected.

Note: The npm ls mailgen command inspects dependencies declared in your package.json and lockfile; it does not perform automated runtime dynamic scanning.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'), where the product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users. In Mailgen, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

# HTML Injection and XSS Filter Bypass in Plaintext Emails ### Summary An HTML injection vulnerability in plaintext emails generated by Mailgen has been discovered. Your project is affected if you use the `Mailgen.generatePlaintext(email);` method and pass in user-generated content. The issue was discovered and reported by Edoardo Ottavianelli (@edoardottt). ### Vulnerability Analysis The following function (inside `index.js`) is intended to strip all HTML content to produce a plaintext string. ```javascript // 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, ""); } // Strip all HTML tags from plaintext output output = output.replace(/<.+?>/g, ''); // Decode HTML entities such as &copy; output = he.decode(output); // All done! return output; }; ``` The process fails because it first converts HTML break tags to newlines and then attempts to strip HTML tags with a regular expression. Using a break tag inside another HTML tag can deceive the filter, allowing HTML content to be injected into the email. A valid payload is: `<img<br> src=xyz onerror=alert(1)>`. ### Proof of Concept ```javascript var Mailgen = require('mailgen'); var mailGenerator = new Mailgen({ theme: 'default', product: { name: 'Mailgen', link: 'https://mailgen.js/' } }); var email = { body: { name: 'John <img<br> src=xyz onerror=alert(document.body.innerHTML)> 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', text: 'Confirm your account', link: 'secret-link' } }, outro: 'Need help, or have questions? Just reply to this email, we\'d love to help.' } }; // Generate the plaintext version of the e-mail var emailText = mailGenerator.generatePlaintext(email); // Optionally, preview the generated plaintext e-mail require('fs').writeFileSync('emailText.txt', emailText, 'utf8'); ``` **Resulting output file (`emailText.txt`):** ```html Hi John <img src=xyz 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. ``` ### Mitigation The vulnerability has been patched in commit [741a019](https://github.com/eladnava/mailgen/commit/741a0190ddae0f408b22ae3b5f0f4c3f5cf4f11d) and released to npm in version `2.0.30`. Thanks to Edoardo Ottavianelli (@edoardottt) for discovering and reporting this vulnerability.

CVE-2025-59526 Affected Mailgen Versions

1 component(s)
Fixed Versions:
2.0.30
Product / Package Ecosystem Affected Versions Fixed Version
mailgen
npm
< 2.0.30
2.0.30

References

Frequently Asked Questions

What is CVE-2025-59526?
CVE-2025-59526 is a security vulnerability in Mailgen involving HTML injection vulnerability in plaintext e-mails under CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting').
Which Mailgen versions are affected?
Affected versions include releases prior to 2.0.30 across affected packages: mailgen.
What version fixes CVE-2025-59526?
Upgrading to 2.0.30 (or the latest patched release for your respective branch) resolves the vulnerability.
Is mailgen affected?
Yes, mailgen is affected in releases prior to 2.0.30. Upgrade your dependency to 2.0.30 or later.
What is the CVSS score of CVE-2025-59526?
CVE-2025-59526 has a CVSS v3.1 base score of 5.3 (MODERATE) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N.
What CWE is associated with CVE-2025-59526?
CVE-2025-59526 is classified under CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.).
How can I check npm dependencies for CVE-2025-59526?
Run the dependency tree command: 'npm ls mailgen'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-j2xj-h7w5-r7vp",
  "modified" : "2025-09-23T20:50:37Z",
  "published" : "2025-09-22T18:03:47Z",
  "aliases" : [ "CVE-2025-59526" ],
  "summary" : "Mailgen: HTML injection vulnerability in plaintext e-mails",
  "details" : "# HTML Injection and XSS Filter Bypass in Plaintext Emails\n\n### Summary\nAn HTML injection vulnerability in plaintext emails generated by Mailgen has been discovered. Your project is affected if you use the `Mailgen.generatePlaintext(email);` method and pass in user-generated content. The issue was discovered and reported by Edoardo Ottavianelli (@edoardottt).\n\n### Vulnerability Analysis\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('An error was encountered while loading the plaintext theme.');\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 <br /> tag as a regex pattern\n    var breakTag = /(?:\\<br\\s*\\/?\\>)/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, '');\n\n        // Replace html break tags with linebreaks\n        output = output.replace(breakTag, '\\n');\n\n        // Remove plaintext theme indentation (tabs or spaces in the beginning of each line)\n        output = output.replace(/^(?: |\\t)*/gm, \"\");\n    }\n\n    // Strip all HTML tags from plaintext output\n    output = output.replace(/<.+?>/g, '');\n\n    // Decode HTML entities such as &copy;\n    output = he.decode(output);\n\n    // All done!\n    return output;\n};\n```\n\nThe process fails because it first converts HTML break tags to newlines and then attempts to strip HTML tags with a regular expression. Using a break tag inside another HTML tag can deceive the filter, allowing HTML content to be injected into the email.\n\nA valid payload is: `<img<br> src=xyz onerror=alert(1)>`.\n\n### Proof of Concept\n\n```javascript\nvar Mailgen = require('mailgen');\n\nvar mailGenerator = new Mailgen({\n    theme: 'default',\n    product: {\n        name: 'Mailgen',\n        link: 'https://mailgen.js/'\n    }\n});\n\nvar email = {\n    body: {\n        name: 'John <img<br> src=xyz onerror=alert(document.body.innerHTML)> Appleseed',\n        intro: 'Welcome to Mailgen! We\\'re very excited to have you on board.',\n        action: {\n            instructions: 'To get started with Mailgen, please click here:',\n            button: {\n                color: '#22BC66',\n                text: 'Confirm your account',\n                link: 'secret-link'\n            }\n        },\n        outro: 'Need help, or have questions? Just reply to this email, we\\'d love to help.'\n    }\n};\n\n// Generate the plaintext version of the e-mail\nvar emailText = mailGenerator.generatePlaintext(email);\n\n// Optionally, preview the generated plaintext e-mail\nrequire('fs').writeFileSync('emailText.txt', emailText, 'utf8');\n```\n\n**Resulting output file (`emailText.txt`):**\n\n```html\nHi John <img\nsrc=xyz onerror=alert(document.body.innerHTML)> Appleseed,\n\nWelcome to Mailgen! We're 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'd love to help.        \n\nYours truly,  \nMailgen\n\n© 2025 Mailgen. All rights reserved.\n```\n\n### Mitigation\nThe vulnerability has been patched in commit [741a019](https://github.com/eladnava/mailgen/commit/741a0190ddae0f408b22ae3b5f0f4c3f5cf4f11d) and released to npm in version `2.0.30`.\n\nThanks to Edoardo Ottavianelli (@edoardottt) for discovering and reporting this vulnerability.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "npm",
      "name" : "mailgen"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "2.0.30"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/eladnava/mailgen/security/advisories/GHSA-j2xj-h7w5-r7vp"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2025-59526"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/eladnava/mailgen/commit/741a0190ddae0f408b22ae3b5f0f4c3f5cf4f11d"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/eladnava/mailgen"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-79" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2025-09-22T18:03:47Z",
    "nvd_published_at" : "2025-09-22T20:15:39Z"
  }
}