Skip to content

Fix: add filterXSSWithResult() to track removed tags and attributes (fixes #284) - #297

Merged
leizongmin merged 1 commit into
leizongmin:masterfrom
Joya-Biswas:feature/filter-xss-with-result
May 6, 2026
Merged

Fix: add filterXSSWithResult() to track removed tags and attributes (fixes #284)#297
leizongmin merged 1 commit into
leizongmin:masterfrom
Joya-Biswas:feature/filter-xss-with-result

Conversation

@Joya-Biswas

@Joya-Biswas Joya-Biswas commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #284

Problem
When filterXSS() sanitizes HTML, it returns only the clean output string. There is no built-in way to know which tags or attributes were removed during sanitization. Users who need this information — for example, to display validation errors to their users — must manually wire up onIgnoreTag and onIgnoreTagAttr callbacks every single time they use the library. This is repetitive, easy to get wrong, and not obvious from the documentation.

Solution
This PR adds a new exported function:
filterXSSWithResult(html, options)

It returns an object with two properties:

  • html — the sanitized HTML string, identical to what filterXSS() returns
  • removed — an array of objects, one for each tag or attribute that was removed

Example:

var xss = require("xss");

var result = xss.filterXSSWithResult(
  '<script>alert("xss")</script><a href="#" onclick="evil()">click</a>'
);
console.log(result.html);
// &lt;script&gt;alert("xss")&lt;/script&gt;<a href="#">click</a>
console.log(result.removed);
// [
//   { type: 'tag',  tag: 'script', html: '<script>',   isClosing: false },
//   { type: 'tag',  tag: 'script', html: '</script>',  isClosing: true  },
//   { type: 'attr', tag: 'a',      attr: 'onclick',    value: 'evil()' }
// ]

Notes

  • Fully backwards compatible — existing filterXSS() usage is completely unchanged
  • If the caller already provides onIgnoreTag or onIgnoreTagAttr in their options, those callbacks still fire correctly
  • Clean HTML with nothing to remove returns an empty removed array []
  • The change is confined to index.js — no changes to core filtering logic

A note on process

I'm a beginner contributor and this is one of my first open source contributions.
I used Claude as a learning assistant to help me understand
the existing codebase and the JavaScript concepts involved. All decisions about
what to change, where to change it, and how to verify it were made by me and
tested locally before submitting.

@Joya-Biswas Joya-Biswas reopened this Apr 17, 2026
@Joya-Biswas
Joya-Biswas force-pushed the feature/filter-xss-with-result branch 2 times, most recently from 8774ee5 to d23efd4 Compare April 17, 2026 20:53
@Joya-Biswas
Joya-Biswas force-pushed the feature/filter-xss-with-result branch from d23efd4 to 2c30203 Compare April 17, 2026 21:01
@Joya-Biswas Joya-Biswas changed the title feat: add filterXSSWithResult() to track removed tags and attributes (fixes #284) Fix: add filterXSSWithResult() to track removed tags and attributes (fixes #284) Apr 18, 2026
@leizongmin
leizongmin merged commit a8fe38d into leizongmin:master May 6, 2026
5 checks passed
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.

How to see what tags are removed?

2 participants