Skip to content

fix: escape image alt text#3896

Merged
UziTech merged 1 commit intomarkedjs:masterfrom
UziTech:image-alt
Feb 17, 2026
Merged

fix: escape image alt text#3896
UziTech merged 1 commit intomarkedjs:masterfrom
UziTech:image-alt

Conversation

@UziTech
Copy link
Member

@UziTech UziTech commented Feb 15, 2026

Marked version: 17.0.2

Description

Escape image alt text. This bug was introduced in v15.0.0

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

@vercel
Copy link

vercel bot commented Feb 15, 2026

@UziTech is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @UziTech, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical security vulnerability by ensuring that image alt text is properly escaped when rendered. This prevents malicious scripts from being injected and executed via unescaped alt attributes, thereby enhancing the overall security of the application. The change includes new tests to confirm the fix and prevent regressions.

Highlights

  • Security Fix: Implemented escaping for image alt text to prevent potential Cross-Site Scripting (XSS) vulnerabilities, specifically addressing a bug introduced in v15.0.0.
  • Test Coverage: Added new test files (image_alt.html and image_alt.md) to validate the fix and ensure proper escaping of image alt text.
Changelog
  • src/Renderer.ts
    • Modified the image rendering logic to apply HTML escaping to the alt attribute content.
  • test/specs/new/image_alt.html
    • Added a new HTML test case to verify the escaping of malicious alt text.
  • test/specs/new/image_alt.md
    • Added a new Markdown test case to verify the escaping of malicious alt text.
Activity
  • No activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@vercel
Copy link

vercel bot commented Feb 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview, Comment Feb 15, 2026 4:35am

Request Review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a critical cross-site scripting (XSS) vulnerability by properly escaping the alt text of images. The change in src/Renderer.ts correctly applies the escape function to the image's alt attribute, preventing malicious content from being executed. The addition of new test files (image_alt.md and image_alt.html) provides a solid regression test for this vulnerability. The fix is correct, concise, and follows best practices for handling user-provided content in HTML attributes. The change is excellent and I have no suggestions for improvement.

href = cleanHref;

let out = `<img src="${href}" alt="${text}"`;
let out = `<img src="${href}" alt="${escape(text)}"`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if escape() is the right thing to use here because its deprecated.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape

But looks like the title below also does the same thing so we can go with it for now as the quick fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The escape function here is not the global escape function but a helper function we define.

export function escape(html: string, encode?: boolean) {

This has been confusing before. I will update the name of the function so we don't keep getting confused by it in the future.

@UziTech UziTech merged commit 909fe44 into markedjs:master Feb 17, 2026
8 checks passed
github-actions bot pushed a commit that referenced this pull request Feb 17, 2026
## [17.0.3](v17.0.2...v17.0.3) (2026-02-17)

### Bug Fixes

* escape image alt text ([#3896](#3896)) ([909fe44](909fe44))
@UziTech UziTech deleted the image-alt branch March 6, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants