Repository navigation
fix: enforce single-property validation in CSSStyleDeclaration.setProperty #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,166 @@ function CSSStyleDeclaration(elt) { | |
| } | ||
|
|
||
| const IMPORTANT_BANG = '!important'; | ||
| const INVALID_PROPERTY_NAME_RE = /[\s:;'"(){}\[\]\/\\!]/; | ||
| const SIMPLE_VALUE_RE = /^[^;'"()[\]{}/\\!]+$/; | ||
|
|
||
| // Sticky CSS Syntax Level 3 token matchers (disjoint first-sets -> O(n), no ReDoS) | ||
| const CSS_WS_RE = /[ \t\n\r\f]+/y; | ||
| const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//y; | ||
| const CSS_STRING_RE = /"(?:\\(?:\r\n|[\s\S])|[^"\\\n\r\f])*"|'(?:\\(?:\r\n|[\s\S])|[^'\\\n\r\f])*'/y; | ||
| const CSS_IDENT_RE = /(?:[a-zA-Z0-9_-]|[^\x00-\x7f]|\\(?:[0-9a-fA-F]{1,6}(?:\r\n|[ \t\n\r\f])?|[^\n\r\f]))+/y; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of notes on the token regexes:
|
||
| const CSS_QUOTED_URL_OPEN_RE = /\([ \t\n\r\f]*(?=['"])/y; | ||
| const CSS_UNQUOTED_URL_RE = /\([ \t\n\r\f]*(?:(?:\\[^\n\r\f]|[^'"()\\ \t\n\r\f\x00-\x08\x0b\x0e-\x1f\x7f])+[ \t\n\r\f]*)?\)/y; | ||
| const MATCHING_CLOSE = { '(': ')', '[': ']', '{': '}' }; | ||
|
|
||
| function isValidPropertyName(property) { | ||
| return property.length > 0 && !INVALID_PROPERTY_NAME_RE.test(property); | ||
| } | ||
|
|
||
| /** | ||
| * Validates that a CSS property value represents a single declaration value | ||
| * without top-level semicolons, unbalanced delimiters/quotes/comments, | ||
| * malformed tokens, or trailing `!important` priority. | ||
| */ | ||
| function isValidStyleValue(value) { | ||
| if (SIMPLE_VALUE_RE.test(value)) { | ||
| return true; | ||
| } | ||
|
|
||
| const len = value.length; | ||
| const stack = []; | ||
| let prevToken = ''; | ||
| let lastToken = ''; | ||
| let i = 0; | ||
|
|
||
| while (i < len) { | ||
| const ch = value[i]; | ||
|
|
||
| // Whitespace (ignored for !important tracking) | ||
| CSS_WS_RE.lastIndex = i; | ||
| if (CSS_WS_RE.exec(value)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On SSR hot paths, The main overhead in this loop comes from:
Switching to Nit: |
||
| i = CSS_WS_RE.lastIndex; | ||
| continue; | ||
| } | ||
|
|
||
| // CSS comment: /* ... */ (unclosed /* is invalid) | ||
| if (ch === '/' && value[i + 1] === '*') { | ||
| CSS_COMMENT_RE.lastIndex = i; | ||
| if (!CSS_COMMENT_RE.exec(value)) { | ||
| return false; | ||
| } | ||
| i = CSS_COMMENT_RE.lastIndex; | ||
| continue; | ||
| } | ||
|
|
||
| // String token: "..." or '...' (unclosed or unescaped newline is invalid) | ||
| if (ch === '"' || ch === "'") { | ||
| CSS_STRING_RE.lastIndex = i; | ||
| if (!CSS_STRING_RE.exec(value)) { | ||
| return false; | ||
| } | ||
| if (stack.length === 0) { | ||
| prevToken = lastToken; | ||
| lastToken = '"'; | ||
| } | ||
| i = CSS_STRING_RE.lastIndex; | ||
| continue; | ||
| } | ||
|
|
||
| // Top-level semicolon is invalid in a single-property value | ||
| if (ch === ';' && stack.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Brackets: (), [], {} | ||
| if (ch === '(' || ch === '[' || ch === '{') { | ||
| stack.push(MATCHING_CLOSE[ch]); | ||
| if (stack.length === 1) { | ||
| prevToken = lastToken; | ||
| lastToken = ch; | ||
| } | ||
| i++; | ||
| continue; | ||
| } | ||
| if (ch === ')' || ch === ']' || ch === '}') { | ||
| if (stack.length === 0 || stack.pop() !== ch) { | ||
| return false; | ||
| } | ||
| if (stack.length === 0) { | ||
| prevToken = lastToken; | ||
| lastToken = ch; | ||
| } | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| // Ident / number / dimension / escape sequence | ||
| CSS_IDENT_RE.lastIndex = i; | ||
| const identMatch = CSS_IDENT_RE.exec(value); | ||
| if (identMatch) { | ||
| const token = identMatch[0]; | ||
| i = CSS_IDENT_RE.lastIndex; | ||
|
|
||
| if (value[i] === '(') { | ||
| // Reject escaped function names (e.g. `\75 rl(...)`) that could disguise a <url-token> | ||
| if (token.includes('\\')) { | ||
| return false; | ||
| } | ||
| if (token.toLowerCase() === 'url') { | ||
| CSS_QUOTED_URL_OPEN_RE.lastIndex = i; | ||
| if (CSS_QUOTED_URL_OPEN_RE.exec(value)) { | ||
| stack.push(')'); | ||
| if (stack.length === 1) { | ||
| prevToken = lastToken; | ||
| lastToken = 'url('; | ||
| } | ||
| i = CSS_QUOTED_URL_OPEN_RE.lastIndex; | ||
| continue; | ||
| } | ||
| CSS_UNQUOTED_URL_RE.lastIndex = i; | ||
| if (!CSS_UNQUOTED_URL_RE.exec(value)) { | ||
| return false; | ||
| } | ||
| if (stack.length === 0) { | ||
| prevToken = lastToken; | ||
| lastToken = 'url()'; | ||
| } | ||
| i = CSS_UNQUOTED_URL_RE.lastIndex; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (stack.length === 0) { | ||
| prevToken = lastToken; | ||
| lastToken = token; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // Trailing or invalid backslash escape (e.g., `\` at EOF or `\` + newline) | ||
| if (ch === '\\') { | ||
| return false; | ||
| } | ||
|
|
||
| // Other punctuation / delimiters | ||
| if (stack.length === 0) { | ||
| prevToken = lastToken; | ||
| lastToken = ch; | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| if (stack.length !== 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // Reject trailing `!important` (including escaped forms like `!\69 mportant`) | ||
| if (prevToken === '!' && (lastToken.toLowerCase() === 'important' || lastToken.includes('\\'))) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Utility function for parsing style declarations | ||
| // Pass in a string like "margin-left: 5px; border-style: solid" | ||
|
|
@@ -156,13 +316,11 @@ CSSStyleDeclaration.prototype = Object.create(Object.prototype, { | |
|
|
||
| // String coercion | ||
| if (value !== NO_CHANGE) { | ||
| value = "" + value; | ||
| } | ||
|
|
||
| value = value.trim(); | ||
| if (value === "") { | ||
| this.removeProperty(property); | ||
| return; | ||
| value = ("" + value).trim(); | ||
| if (value === "") { | ||
| this.removeProperty(property); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (priority !== "" && priority !== NO_CHANGE && | ||
|
|
@@ -181,30 +339,19 @@ CSSStyleDeclaration.prototype = Object.create(Object.prototype, { | |
| delete styles.priority[property]; | ||
| } | ||
| } else { | ||
| // We don't just accept the property value. Instead | ||
| // we parse it to ensure that it is something valid. | ||
| // If it contains a semicolon it is invalid, except base64-encoded | ||
| // URLs (e.g. `background-image: url(data:image/png;base64,...`) | ||
| if (value.includes(";") && !value.includes("data:")) return; | ||
|
|
||
| var newprops = parseStyles(property + ":" + value); | ||
| if (Object.getOwnPropertyNames(newprops.property).length === 0) { | ||
| return; // no valid property found | ||
| // Validate that `property` and `value` represent a single CSS declaration | ||
| // without top-level semicolons, unbalanced delimiters/quotes/comments, or | ||
| // trailing `!important` priority. | ||
| if (!isValidPropertyName(property) || !isValidStyleValue(value)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things here:
|
||
| return; | ||
| } | ||
| if (Object.getOwnPropertyNames(newprops.priority).length !== 0) { | ||
| return; // if the value included '!important' it wasn't valid. | ||
| } | ||
|
|
||
| // XXX handle shorthand properties | ||
|
|
||
| for (var p in newprops.property) { | ||
| styles.property[p] = newprops.property[p]; | ||
| if (priority === NO_CHANGE) { | ||
| continue; | ||
| } else if (priority !== "") { | ||
| styles.priority[p] = "important"; | ||
| } else if (styles.priority[p]) { | ||
| delete styles.priority[p]; | ||
| styles.property[property] = value; | ||
| if (priority !== NO_CHANGE) { | ||
| if (priority !== "") { | ||
| styles.priority[property] = "important"; | ||
| } else if (styles.priority[property]) { | ||
| delete styles.priority[property]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2638,3 +2638,127 @@ exports.nativeEventDispatch = function() { | |
| createdKeyEvent.type.should.equal('keyup'); | ||
| createdKeyEvent.key.should.equal('a'); | ||
| }; | ||
|
|
||
| exports.styleSetPropertyValidation = function() { | ||
| function setStyle(prop, val, priority) { | ||
| var doc = domino.createDocument('<div></div>'); | ||
| var el = doc.querySelector('div'); | ||
| if (priority !== undefined) { | ||
| el.style.setProperty(prop, val, priority); | ||
| } else { | ||
| el.style.setProperty(prop, val); | ||
| } | ||
| return el.getAttribute('style'); | ||
| } | ||
|
|
||
| // Reject multi-property injection (including `data:` guard bypasses and parser differentials) | ||
| var rejected = [ | ||
| // data: substring bypasses | ||
| ['color', "red; position: fixed; inset: 0px; z-index: 2147483647; background-image: url('https://attacker.example/phish.png?data:')"], | ||
| ['width', '100px; position: fixed; top: 10px; z-index: 2147483646; --bypass: data:; --pad: 1px'], | ||
| ['--accent', 'blue; position: fixed; top: 20px; z-index: 2147483645; --bypass: data:'], | ||
| // Top-level semicolons | ||
| ['color', 'red; position: fixed'], | ||
| ['color', 'red;'], | ||
| ['color', '; position: fixed'], | ||
| ['color', 'red; color: blue'], | ||
| ['background-image', 'url(data:image/png;base64,AAA); position: fixed'], | ||
| ['background-image', 'url("data:image/png;base64,AAA"); position: fixed'], | ||
| // Parentheses inside quotes / unmatched delimiters | ||
| ['color', "'(' ; position: fixed"], | ||
| ['color', '"(" ; position: fixed'], | ||
| ['background-image', 'url("("); position: fixed'], | ||
| ['color', 'red) ; position: fixed'], | ||
| ['color', 'red] ; position: fixed'], | ||
| ['color', 'red} ; position: fixed'], | ||
| ['color', 'rgb(1, 2'], | ||
| ['grid-template-columns', '[main-start'], | ||
| ['--x', '{'], | ||
| ['--x', '([)]'], | ||
| // Unclosed strings, escaped backslashes before closing quote, unescaped newlines in strings | ||
| ['content', '"hello'], | ||
| ['content', "'hello"], | ||
| ['content', '"hello\\"'], | ||
| ['content', '"foo\\\\" ; position: fixed'], | ||
| ['content', "'foo\\\\' ; position: fixed"], | ||
| ['content', '"foo\n; position: fixed; --x: "'], | ||
| ['content', '"foo\r; position: fixed; --x: "'], | ||
| ['content', '"foo\f; position: fixed; --x: "'], | ||
| ['content', "'foo\n; position: fixed; --x: '"], | ||
| // CSS comments | ||
| ['color', "/* ' */ ; position: fixed"], | ||
| ['color', '/* ( */ ; position: fixed'], | ||
| ['color', 'red /* unclosed comment'], | ||
| // Unquoted url(...) parser differentials | ||
| ['background-image', 'url(https://example.com/*); position: fixed; --a: /* */ )'], | ||
| ['background-image', "url(https://example.com/'foo); position: fixed; --a: '"], | ||
| ['background-image', 'url(https://example.com/"foo); position: fixed; --a: "'], | ||
| ['background-image', 'url(https://example.com/(foo); position: fixed; --a: )'], | ||
| ['background-image', 'url(https://example.com/a b)'], | ||
| ['background-image', 'url(https://example.com/a\nb)'], | ||
| ['background-image', 'url(data:image/png;base64,AAA'], | ||
| // Escaped url( obfuscation | ||
| ['background-image', '\\75 rl(https://example.com/*); position: fixed; --a: /* */ )'], | ||
| ['background-image', '\\75\\72\\6c(https://example.com/*); position: fixed; --a: /* */ )'], | ||
| // Trailing / invalid backslashes | ||
| ['color', 'red\\'], | ||
| ['color', 'red\\\\\\'], | ||
| ['color', 'red\\\n; position: fixed'], | ||
| // !important inside value | ||
| ['color', 'red !important'], | ||
| ['color', 'red !IMPORTANT'], | ||
| ['color', 'red ! important'], | ||
| ['color', 'red !/*c*/important /* trailing */'], | ||
| ['color', 'red !\\69 mportant'], | ||
| // Invalid property names | ||
| ['color: red; position', 'fixed'], | ||
| ['color; position', 'fixed'], | ||
| ['', 'red'], | ||
| ]; | ||
|
|
||
| rejected.forEach(function(entry) { | ||
| (setStyle(entry[0], entry[1]) === null).should.equal(true); | ||
| }); | ||
|
|
||
| // Accept legitimate single-property CSS values (including semicolons inside strings/urls/parens/comments) | ||
| var accepted = [ | ||
| ['color', 'red', 'color: red;'], | ||
| ['width', '100px', 'width: 100px;'], | ||
| ['width', 'calc(100% - 20px)', 'width: calc(100% - 20px);'], | ||
| ['color', 'var(--my-color, rgba(0, 0, 0, 0.5))', 'color: var(--my-color, rgba(0, 0, 0, 0.5));'], | ||
| ['background-image', 'url(data:image/png;base64,iVBORw0KGgo=)', 'background-image: url(data:image/png;base64,iVBORw0KGgo=);'], | ||
| ['background-image', 'URL(data:image/png;base64,iVBORw0KGgo=)', 'background-image: URL(data:image/png;base64,iVBORw0KGgo=);'], | ||
| ['background-image', 'url( data:image/svg+xml;charset=utf-8,%3Csvg%3E%3C/svg%3E )', 'background-image: url( data:image/svg+xml;charset=utf-8,%3Csvg%3E%3C/svg%3E );'], | ||
| ['background-image', 'url("data:image/png;base64,iVBORw0KGgo=")', 'background-image: url("data:image/png;base64,iVBORw0KGgo=");'], | ||
| ['background-image', "url('data:image/png;base64,iVBORw0KGgo=')", "background-image: url('data:image/png;base64,iVBORw0KGgo=');"], | ||
| ['background-image', 'url("https://example.com/a;b=1")', 'background-image: url("https://example.com/a;b=1");'], | ||
| ['background-image', 'url("foo\\"bar;baz")', 'background-image: url("foo\\"bar;baz");'], | ||
| ['background-image', 'url("foo\\\\")', 'background-image: url("foo\\\\");'], | ||
| ['background-image', 'url()', 'background-image: url();'], | ||
| ['background-image', 'linear-gradient(to right, red, blue), url(data:image/png;base64,AAA)', 'background-image: linear-gradient(to right, red, blue), url(data:image/png;base64,AAA);'], | ||
| ['content', '"a; b"', 'content: "a; b";'], | ||
| ['content', "'hello; world'", "content: 'hello; world';"], | ||
| ['content', '"foo !important"', 'content: "foo !important";'], | ||
| ['font-family', '"Helvetica Neue", Arial, sans-serif', 'font-family: "Helvetica Neue", Arial, sans-serif;'], | ||
| ['grid-template-columns', '[main-start] 1fr [main-end]', 'grid-template-columns: [main-start] 1fr [main-end];'], | ||
| ['color', 'red /* comment ; with semicolon */', 'color: red /* comment ; with semicolon */;'], | ||
| ['--custom', '"a; b"', '--custom: "a; b";'], | ||
| ['--custom', '(a; b)', '--custom: (a; b);'], | ||
| ['font-family', 'Foo\\;Bar', 'font-family: Foo\\;Bar;'], | ||
| ]; | ||
|
|
||
| accepted.forEach(function(entry) { | ||
| setStyle(entry[0], entry[1]).should.equal(entry[2]); | ||
| }); | ||
|
|
||
| // Explicit priority argument still works | ||
| setStyle('color', 'red', 'important').should.equal('color: red !important;'); | ||
|
|
||
| // Proxy property assignment enforces the same validation | ||
| var doc = domino.createDocument('<a id="badge">hi</a>'); | ||
| var a = doc.getElementById('badge'); | ||
| a.style.color = "red; position: fixed; inset: 0px; z-index: 2147483647; background-image: url('https://attacker.example/phish.png?data:')"; | ||
| (a.getAttribute('style') === null).should.equal(true); | ||
| a.style.backgroundImage = 'url(data:image/png;base64,iVBORw0KGgo=)'; | ||
| a.getAttribute('style').should.equal('background-image: url(data:image/png;base64,iVBORw0KGgo=);'); | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add tests for:
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things on property name handling:
parseStyles()initializesproperty: {}andpriority: {}as plain objects, soObject.prototypekeys collide. Callingel.style.setProperty('constructor', 'red')reads inheritedObject.prototype.constructorin_serialize()and emitsstyle="constructor: red !function Object() { [native code] };". Also,el.style.setProperty('__proto__', 'red')regresses from returning early (getAttribute('style') === null) to writingstyle="", becauseisValidPropertyName('__proto__')passes and_serialize()still runs after the silent assignment. Can we initialize both withObject.create(null)inparseStyles()?INVALID_PROPERTY_NAME_REis a denylist, it still accepts invalid CSS property names like'--','123','@import','a<b', and'\x00'. Sincepropertyis already lowercased insetProperty(), can we switch to an allowlist regex instead?