Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(css): escape class names when rendering * fix(css): reject unsafe characters in external class names
authored by Taku Amano · committed by GitHub · parent d4853a8 · Browse files · 3 files·+90 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+90 added−2 removed
- tests:+73 added−0 removed
- other:+17 added−2 removed
Provenance
- Commit
cd3f6f7- Tree
f8c4f15- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
d4853a8
3 files changed+90 added−2 removed
modified:src/helper/css/common.ts+6 added-1 removed
53 unmodified lines54 }55 56 const isValidClassName = (name: string): boolean => /^-?[_a-zA-Z][_a-zA-Z0-9-]*$/.test(name)57 58+// `<`, `{`, `}` never appear in valid class names but can break out of a <style> element59+// or inject a CSS rule when an external cx() class name is used as a selector.60+const hasUnsafeSelectorChar = (name: string): boolean => /[<{}]/.test(name)61+62 // CSS-wide keywords that are invalid as @keyframes names per the spec63 const RESERVED_KEYFRAME_NAMES = new Set([64 'default',65 'inherit',181 unmodified lines247 [SELECTOR]: '',248 [CLASS_NAME]: '',249 [STYLE_STRING]: '',250 [SELECTORS]: [],247- [EXTERNAL_CLASS_NAMES]: [arg],251+ // drop names that are unsafe to emit as a selector252+ [EXTERNAL_CLASS_NAMES]: hasUnsafeSelectorChar(arg) ? [] : [arg],253 }254 }255 }256 77 unmodified linesmodified:src/helper/css/index.test.tsx+73 added
127 unmodified lines128 expect(await toString(template)).toBe(129 '<style id="hono-css">.css-3358636561{border-radius:4px;color:white}</style><h1 class="css-3358636561 external-class external-class2">Hello!</h1>'130 )131 })132+133+ it('Should escape external class name to prevent XSS via attribute injection', async () => {134+ const btn = css`135+ border-radius: 4px;136+ `137+138+ const template = (139+ <>140+ <Style />141+ <h1 class={cx(btn, '" onmouseover=alert(1) x="')}>Hello!</h1>142+ </>143+ )144+ // the injected `"` is escaped to `"`, so it cannot close the class attribute145+ const result = await toString(template)146+ expect(result).toContain('class="css-3467431616 " onmouseover=alert(1) x=""')147+ })148+149+ it('Should keep special characters in external class name working in attribute context', async () => {150+ const btn = css`151+ border-radius: 4px;152+ `153+154+ const template = (155+ <>156+ <Style />157+ <h1 class={cx(btn, '[&>*]:p-1')}>Hello!</h1>158+ </>159+ )160+ // escaped entities are decoded back to the original class name by the browser161+ expect(await toString(template)).toContain('class="css-3467431616 [&>*]:p-1"')162+ })163+164+ it('Should keep special characters in external class name working as a selector', async () => {165+ const btn = css`166+ border-radius: 4px;167+ `168+ const wrapper = css`169+ ${cx(btn, '[&>*]:p-1')} {170+ color: red;171+ }172+ `173+174+ const template = (175+ <>176+ <Style />177+ <div class={wrapper}>Hello!</div>178+ </>179+ )180+ // legitimate class names (incl. Tailwind arbitrary values) are kept verbatim in the selector181+ expect(await toString(template)).toContain('.css-3467431616 [&>*]:p-1{')182+ })183+184+ it('Should drop external class names that could break out of a style element', async () => {185+ const btn = css`186+ border-radius: 4px;187+ `188+ // `</style>` (HTML breakout) and `{}` (CSS rule injection) are dropped from the selector189+ for (const payload of ['</style><script>alert(1)</script>', 'a{}body{display:none}']) {190+ const wrapper = css`191+ ${cx(btn, payload)} {192+ color: red;193+ }194+ `195+ const result = await toString(196+ <>197+ <Style />198+ <div class={wrapper}>Hello!</div>199+ </>200+ )201+ expect(result).not.toContain('</style><script>')202+ expect(result).not.toContain('body{display:none}')203+ }204+ })205 })206 207 describe('minify', () => {208 const data: [string, Promise<string>, string][] = [426 unmodified linesmodified:src/helper/css/index.ts+11 added-1 removed
4 unmodified lines5 6 import { raw } from '../../helper/html'7 import { DOM_RENDERER } from '../../jsx/constants'8 import { createCssJsxDomObjects } from '../../jsx/dom/css'9+import { escapeToBuffer } from '../../utils/html'10 import type { HtmlEscapedCallback, HtmlEscapedString } from '../../utils/html'11 import type {12 ClassNameSlug,13 CssClassName as CssClassNameCommon,131 unmodified lines145 146 return Promise.resolve(raw('', [appendStyle]))147 }148 149+ // external class names from cx() are untrusted but the result is marked isEscaped,150+ // so escape it here. skip the buffer when there is nothing to escape.151+ const rawClassName = cssClassName[CLASS_NAME]152+ let escapedClassName = rawClassName153+ if (/[&<>'"]/.test(rawClassName)) {154+ const escapedBuffer: [string] = ['']155+ escapeToBuffer(rawClassName, escapedBuffer)156+ escapedClassName = escapedBuffer[0]157+ }148- const className = new String(cssClassName[CLASS_NAME]) as CssClassName158+ const className = new String(escapedClassName) as CssClassName159 Object.assign(className, cssClassName)160 ;(className as HtmlEscapedString).isEscaped = true161 ;(className as HtmlEscapedString).callbacks = [addClassNameToContext]162 const promise = Promise.resolve(className)100 unmodified lines