fix: sanitize dangerouslySetInnerHTML with DOMPurify to prevent XSS#33
Merged
abhizipstack merged 1 commit intomainfrom Apr 2, 2026
Merged
fix: sanitize dangerouslySetInnerHTML with DOMPurify to prevent XSS#33abhizipstack merged 1 commit intomainfrom
abhizipstack merged 1 commit intomainfrom
Conversation
Add DOMPurify sanitization to the ANSI log renderer in no-code-model. The parseLog function converts ANSI escape codes to HTML via ansi-to-html, then renders with dangerouslySetInnerHTML. Without sanitization, malicious content in logs could execute scripts. - Install dompurify dependency - Wrap ansiToHtml.toHtml() output with DOMPurify.sanitize() - Only instance of dangerouslySetInnerHTML in the entire frontend Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
| Filename | Overview |
|---|---|
| frontend/src/ide/editor/no-code-model/no-code-model.jsx | Wraps ansiToHtml.toHtml(log) with DOMPurify.sanitize() in parseLog before rendering via dangerouslySetInnerHTML; correctly addresses the XSS vulnerability. |
| frontend/package.json | Adds dompurify ^3.3.3 as a production dependency, correctly placed in dependencies rather than devDependencies. |
| frontend/package-lock.json | Adds dompurify 3.3.3 lock entry; incidentally removes @testing-library/dom and typescript peer-dep entries that npm re-resolved as no longer needed during install. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[logsInfo data from API] --> B["parseLog(el)"]
B --> C["ansiToHtml.toHtml(log)\n(ANSI → raw HTML string)"]
C --> D["DOMPurify.sanitize(html)\n✅ strips scripts & event handlers\n✅ preserves span/style for colors"]
D --> E["dangerouslySetInnerHTML\n{ __html: sanitizedHtml }"]
E --> F[Safe DOM render in browser]
style D fill:#d4edda,stroke:#28a745,color:#000
style C fill:#fff3cd,stroke:#ffc107,color:#000
Prompt To Fix All With AI
This is a comment left during a code review.
Path: frontend/src/ide/editor/no-code-model/no-code-model.jsx
Line: 362
Comment:
**Consider explicitly allowing `style` attribute in DOMPurify config**
`ansi-to-html` generates `<span style="color:...">` elements for ANSI color codes. DOMPurify 3.x does allow `style` in its default `ALLOWED_ATTR` list, so this works correctly today. However, the current call relies implicitly on that default. To make the intent explicit and guard against future DOMPurify configuration changes or version upgrades that might alter defaults, consider passing an explicit config:
```suggestion
const parseLog = (log) =>
DOMPurify.sanitize(ansiToHtml.toHtml(log), {
ALLOWED_TAGS: ["span", "br"],
ALLOWED_ATTR: ["style"],
});
```
This ensures ANSI colour spans are always preserved while keeping the allowlist as tight as possible.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: sanitize dangerouslySetInnerHTML wi..." | Re-trigger Greptile
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.
What
dangerouslySetInnerHTMLoutput in no-code-model log renderer using DOMPurifyWhy
dangerouslySetInnerHTMLrenders ANSI-converted HTML without sanitizationdangerouslySetInnerHTMLin the entire frontend codebaseHow
dompurifydependencyansiToHtml.toHtml(log)output withDOMPurify.sanitize()before renderingCan this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
Database Migrations
Env Config
Relevant Docs
Related Issues or PRs
Dependencies Versions
Notes on Testing
Screenshots
N/A
Checklist
I have read and understood the Contribution Guidelines.