fix(subtitles): harden subtitle rendering against xss#1926
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens xgplayer-subtitles rendering to prevent DOM XSS by eliminating direct innerHTML insertion and introducing a whitelist-based inline-markup renderer for subtitles.
Changes:
- Added a safe inline subtitle renderer that only materializes allowlisted tags/attributes into DOM nodes.
- Updated subtitle parsing/encoding and the runtime renderer to route all subtitle text through the safe renderer (and to strip markup for word-by-word rendering).
- Added Jest coverage for XSS cases and refreshed the subtitle demo fixtures to include an XSS validation track and UI.
Reviewed changes
Copilot reviewed 11 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/xgplayer-subtitles/src/xss/safeInlineTags.js | Defines allowlisted inline/void tag names and normalization helpers. |
| packages/xgplayer-subtitles/src/xss/safeInlineRenderer.js | Implements safe parsing + DOM construction for inline subtitle markup. |
| packages/xgplayer-subtitles/src/parser.js | Encodes subtitle text to prevent raw HTML injection; avoids mis-classifying safe inline tags as language wrappers. |
| packages/xgplayer-subtitles/src/index.js | Replaces innerHTML rendering with appendSubtitleText(); strips markup before splitWords(). |
| packages/xgplayer-subtitles/README.md | Documents the new safe rendering behavior and supported tags. |
| packages/xgplayer-subtitles/tests/xss.spec.js | Adds automated tests asserting XSS payloads remain inert and allowlisted markup renders safely. |
| jest.config.js | Includes xgplayer-subtitles test glob in Jest’s testMatch. |
| fixtures/subtitle/shared/utils.js | Demo helpers for decoding/formatting subtitle cue text and checking for unsafe nodes. |
| fixtures/subtitle/shared/tracks.js | Demo tracks including a dedicated XSS test track and cases. |
| fixtures/subtitle/shared/timeline.js | Timeline UI component for the demo (inert text rendering). |
| fixtures/subtitle/index.js | Reworked demo logic/UI wiring for track switching + XSS status reporting. |
| fixtures/subtitle/index.html | Updated demo UI/markup/styles for the new controls, timeline, and status panel. |
| .gitignore | Un-ignores shared fixture JS helpers so the new demo modules are tracked. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
380
to
382
| // 动态构造语言匹配规则 | ||
| // eslint-disable-next-line no-useless-escape | ||
| const newReg = RegExp(`^<${tag}>(([\\s\\S])*?)<\/${tag}>$`) |
wudechang
approved these changes
Jun 8, 2026
llftt
approved these changes
Jun 8, 2026
xiong-001
approved these changes
Jun 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.