codemod/dom-innerhtml-xss-to-textcontent
Replace dangerous innerHTML assignments with user data to safe textContent or DOM manipulation alternatives in browser JavaScript
# DOM innerHTML XSS to textContent
[](https://codemod.ai)
[](https://cwe.mitre.org/data/definitions/79.html)
A security-focused codemod that prevents XSS vulnerabilities by replacing dangerous `innerHTML` assignments with safe alternatives like `textContent` or proper DOM manipulation methods.
## 🚨 Security Issue
Using `innerHTML` with user-controlled data creates Cross-Site Scripting (XSS) vulnerabilities. This codemod identifies and fixes patterns where user input could be executed as HTML, replacing them with safe alternatives.
**CWE-79:** Cross-site Scripting (XSS) - https://cwe.mitre.org/data/definitions/79.html
## Installation
```bash
# Run directly from registry
npx codemod@latest run @codemod/dom-innerhtml-xss-to-textcontent
# Or run locally
npx codemod@latest run -w workflow.yaml
```
## What it does
### ✅ Transforms dangerous patterns
**Before:**
```javascript
// Direct innerHTML assignment with user data
element.innerHTML = userInput;
document.getElementById('output').innerHTML = formData.value;
// Template literals with user data
target.innerHTML = `<span>${userValue}</span>`;
// Appending user content
div.innerHTML += userComment;
// URL parameters
container.innerHTML = new URLSearchParams(window.location.search).get('content');
```
**After:**
```javascript
// Safe textContent assignment
element.textContent = userInput;
document.getElementById('output').textContent = formData.value;
// Safe textContent for template literals
target.textContent = userValue;
// Safe text node creation and appending
const textNode = document.createTextNode(userComment);
div.appendChild(textNode);
// Safe URL parameter handling
container.textContent = new URLSearchParams(window.location.search).get('content');
```
### ❌ Preserves safe patterns
The codemod will **NOT** transform these safe patterns:
```javascript
// Static HTML content
element.innerHTML = '<p>Static content</p>';
// Constants (uppercase naming convention)
container.innerHTML = SAFE_HTML_CONSTANT;
// Sanitized content
output.innerHTML = DOMPurify.sanitize(userInput);
// Already using textContent
element.textContent = userInput;
```
## Examples
### Form Input Handling
```javascript
// Before - XSS vulnerable
const userInput = document.getElementById('comment').value;
document.getElementById('display').innerHTML = userInput;
// After - XSS safe
const userInput = document.getElementById('comment').value;
document.getElementById('display').textContent = userInput;
```
### URL Parameter Display
```javascript
// Before - XSS vulnerable
const message = new URLSearchParams(location.search).get('msg');
document.getElementById('alert').innerHTML = message;
// After - XSS safe
const message = new URLSearchParams(location.search).get('msg');
document.getElementById('alert').textContent = message;
```
### API Response Handling
```javascript
// Before - XSS vulnerable
fetch('/api/user/name')
.then(r => r.json())
.then(data => element.innerHTML = data.name);
// After - XSS safe
fetch('/api/user/name')
.then(r => r.json())
.then(data => element.textContent = data.name);
```
## Scope
This codemod targets:
- ✅ JavaScript files in browser contexts
- ✅ TypeScript files targeting browser environments
- ✅ HTML files with inline JavaScript
- ✅ Frontend framework components using direct DOM access
- ✅ Files with URL parameter parsing or form input handling
- ✅ API response handlers that update DOM content
## Limitations
- Does not analyze data flow to determine if variables contain user input
- Uses heuristics to detect potentially dangerous assignments
- Conservative approach: transforms all non-static innerHTML assignments
- May require manual review for complex HTML generation use cases
## Development
```bash
# Test the transformation
pnpm test
# Run on specific files
npx codemod@latest jssg run -l typescript ./scripts/codemod.ts src/
# Update test snapshots
pnpm test -u
```
## References
- [OWASP XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html)
- [MDN innerHTML Security Considerations](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#security_considerations)
- [CWE-79: Cross-site Scripting](https://cwe.mitre.org/data/definitions/79.html)
## License
MIT