Skip to content

Fix XSS in EncodingVisualizer HTML attributes#2216

Open
varneeth01 wants to merge 1 commit into
huggingface:mainfrom
varneeth01:main
Open

Fix XSS in EncodingVisualizer HTML attributes#2216
varneeth01 wants to merge 1 commit into
huggingface:mainfrom
varneeth01:main

Conversation

@varneeth01

Copy link
Copy Markdown

What does this PR do?

This PR fixes an HTML attribute injection vulnerability in
bindings/python/py_src/tokenizers/tools/visualizer.py.

While visible text was already escaped by the earlier fix in #1937, several
data-* attribute values were still constructed directly from untrusted token
or annotation text. A malicious value containing quotes could terminate the
attribute and inject additional HTML attributes or elements when the generated
HTML was rendered.

This change:

  • escapes all user-controlled HTML attribute values using
    html.escape(..., quote=True)
  • quotes the previously unquoted data-stoken attribute
  • preserves existing rendering behavior
  • adds regression coverage to prevent future regressions

Fixes

Fixes #2210

Testing

  • Added regression tests covering malicious token text
  • Added regression tests covering malicious annotation labels
  • Verified that injected quotes are escaped and cannot create new HTML attributes
  • Verified that normal rendering remains unchanged

@varneeth01

Copy link
Copy Markdown
Author

Escape all user-controlled values inserted into HTML data-* attributes in EncodingVisualizer. Previously, visible text was escaped but attribute values were not, which could allow malformed HTML or script injection if token or annotation values contained special characters.

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.

EncodingVisualizer: data-* attributes built from token text are not HTML-escaped (XSS gap left by #1937)

1 participant