Skip to content
Myr

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

3 files+90 added−2 removed

modified:src/helper/css/common.ts+6 added-1 removed
53 unmodified lines
54}5556const isValidClassName = (name: string): boolean => /^-?[_a-zA-Z][_a-zA-Z0-9-]*$/.test(name)5758// `<`, `{`, `}` 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.60const hasUnsafeSelectorChar = (name: string): boolean => /[<{}]/.test(name)6162// CSS-wide keywords that are invalid as @keyframes names per the spec63const RESERVED_KEYFRAME_NAMES = new Set([64 'default',65 'inherit',
181 unmodified lines
247 [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 lines
modified:src/helper/css/index.test.tsx+73 added
127 unmodified lines
128 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 })132133 it('Should escape external class name to prevent XSS via attribute injection', async () => {134 const btn = css`135 border-radius: 4px;136 `137138 const template = (139 <>140 <Style />141 <h1 class={cx(btn, '" onmouseover=alert(1) x="')}>Hello!</h1>142 </>143 )144 // the injected `"` is escaped to `&quot;`, so it cannot close the class attribute145 const result = await toString(template)146 expect(result).toContain('class="css-3467431616 &quot; onmouseover=alert(1) x=&quot;"')147 })148149 it('Should keep special characters in external class name working in attribute context', async () => {150 const btn = css`151 border-radius: 4px;152 `153154 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 [&amp;&gt;*]:p-1"')162 })163164 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 `173174 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 })183184 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 })206207 describe('minify', () => {208 const data: [string, Promise<string>, string][] = [
426 unmodified lines
modified:src/helper/css/index.ts+11 added-1 removed
4 unmodified lines
56import { raw } from '../../helper/html'7import { DOM_RENDERER } from '../../jsx/constants'8import { createCssJsxDomObjects } from '../../jsx/dom/css'9import { escapeToBuffer } from '../../utils/html'10import type { HtmlEscapedCallback, HtmlEscapedString } from '../../utils/html'11import type {12 ClassNameSlug,13 CssClassName as CssClassNameCommon,
131 unmodified lines
145146 return Promise.resolve(raw('', [appendStyle]))147 }148149 // 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