DOM Clobbering
A technique where HTML elements with id or name attributes overwrite JavaScript variables or DOM properties, potentially enabling XSS or bypassing security checks.
DOM Clobbering is a technique where HTML elements with specific id or name attributes can overwrite global JavaScript variables or DOM properties. This happens because browsers automatically create global references to elements with IDs, which can conflict with expected JavaScript objects.
How It Works
When an HTML element has an id attribute, browsers create a global variable with that name pointing to the element. If JavaScript code expects a certain global variable or property to exist, an attacker can inject HTML that "clobbers" (overwrites) that variable with an HTML element.
Common Targets
- Global variables referenced by scripts
window.name,document.body- Security-related objects like CSRF tokens
- Configuration objects
Prevention
- Use
const/letinstead of relying on global variables - Check
typeofof objects before use - Use strict equality checks
- Freeze security-critical objects
- Sanitize user HTML to remove dangerous id/name attributes