codemod/window-location-user-input-validation
Transform client-side JavaScript that assigns user-controlled data to window.location properties to include URL validation
# Window Location User Input Validation Codemod
This codemod automatically adds URL validation to JavaScript/TypeScript code that assigns user-controlled data to `window.location` properties, helping to prevent open redirect vulnerabilities and XSS attacks.
## Security Impact
**Vulnerability Type**: CWE-601 (URL Redirection to Untrusted Site) and OWASP A03:2021 (Injection)
Direct assignment of user input to `window.location.href` or `window.location` can lead to:
- Open redirect attacks allowing attackers to redirect users to malicious sites
- XSS attacks via `javascript:` URLs
- Phishing attacks through legitimate-looking but malicious redirects
## What It Does
This codemod identifies potentially dangerous location assignments and wraps them with URL validation:
### Before
```javascript
function handleRedirect(userInput) {
window.location.href = userInput;
}
```
### After
```javascript
function isValidURL(url) {
try {
const parsed = new URL(url, window.location.origin);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}
function handleRedirect(userInput) {
if (isValidURL(userInput)) {
window.location.href = userInput;
} else {
console.warn('Invalid URL provided:', userInput);
}
}
```
## Patterns Transformed
- `window.location.href = userInput`
- `window.location = redirectUrl`
- `location.href = params.get('redirect')`
- `location = new URLSearchParams(...).get('next')`
- Template literals and string concatenation in location assignments
## Patterns Excluded
The codemod intelligently skips:
- Hardcoded string literals (`window.location.href = '/dashboard'`)
- Relative paths starting with `/` or `./`
- Assignments already wrapped in validation
- Test files (`*.test.js`, `*.spec.js`, `__tests__/`)
## Installation & Usage
```bash
# Run directly via npx
npx codemod@latest run window-location-user-input-validation
# Or install and run
npm install -g @codemod/window-location-user-input-validation
codemod run @codemod/window-location-user-input-validation
```
## Examples
### Template Literal Assignment
```javascript
// Before
window.location.href = `${baseUrl}${userPath}`;
// After
const fullUrl = `${baseUrl}${userPath}`;
if (isValidURL(fullUrl)) {
window.location.href = fullUrl;
} else {
console.warn('Invalid URL constructed:', fullUrl);
}
```
### Parameter-based Redirect
```javascript
// Before
location.href = params.get('redirect');
// After
const redirectParam = params.get('redirect');
if (redirectParam && isValidURL(redirectParam)) {
location.href = redirectParam;
} else {
console.warn('Invalid redirect URL provided:', redirectParam);
}
```
## Validation Logic
The generated `isValidURL` function:
- Uses the browser's native `URL()` constructor for parsing
- Only allows HTTP and HTTPS protocols (blocks `javascript:`, `data:`, etc.)
- Handles relative URLs by resolving against `window.location.origin`
- Gracefully handles malformed URLs with try-catch
## Limitations
- Does not validate against allowlists of trusted domains
- May require manual review for complex redirect logic
- Generated validation is basic - consider enhanced validation for production use
## Security References
- [CWE-601: URL Redirection to Untrusted Site](https://cwe.mitre.org/data/definitions/601.html)
- [OWASP Top 10 2021 - A03 Injection](https://owasp.org/Top10/A03_2021-Injection/)
- [OWASP Cheat Sheet: Unvalidated Redirects and Forwards Prevention](https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html)
## Development
```bash
# Test the transformation
pnpm test
# Update test snapshots
pnpm test -u
# Validate the workflow
npx codemod@latest validate -w workflow.yaml
```
## License
MIT