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: jsx tag name validation * perf: cache valid names to improve performance * fix: reject parser-control tag name prefixes Reject tag names starting with `!` or `?` (e.g. `!--`, `!DOCTYPE`, `?xml`) so programmatic JSX input can't emit HTML comment / doctype / processing-instruction openers that break parser context downstream.

authored by Taku Amano · committed by GitHub · parent 027e3df · Browse files · 4 files+180 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+180 added−15 removed

  • tests:+128 added−13 removed
  • other:+52 added−2 removed

4 files+180 added−15 removed

modified:src/jsx/base.ts+9 added-1 removed
8 unmodified lines
9import type {10 JSX as HonoJSX,11 IntrinsicElements as IntrinsicElementsDefined,12} from './intrinsic-elements'13import { isValidAttributeName, normalizeIntrinsicElementKey, styleObjectForEach } from './utils'13import {14 isValidAttributeName,15 isValidTagName,16 normalizeIntrinsicElementKey,17 styleObjectForEach,18} from './utils'1920// eslint-disable-next-line @typescript-eslint/no-explicit-any21export type Props = Record<string, any>22export type FC<P = Props> = {
116 unmodified lines
139 children: Child[]140 isEscaped: true = true as const141 localContexts?: LocalContexts142 constructor(tag: string | Function, props: Props, children: Child[]) {143 if (typeof tag !== 'function' && !isValidTagName(tag)) {144 throw new Error(`Invalid JSX tag name: ${tag}`)145 }146 this.tag = tag147 this.props = props148 this.children = children149 }
304 unmodified lines
modified:src/jsx/index.test.tsx+52 added-1 removed
1 unmodified line
2/* eslint-disable @typescript-eslint/no-explicit-any */3import { html } from '../helper/html'4import { Hono } from '../hono'5import { Suspense, renderToReadableStream } from './streaming'6import DefaultExport, { Fragment, StrictMode, createContext, memo, useContext, version } from '.'6import DefaultExport, {7 Fragment,8 StrictMode,9 createContext,10 createElement,11 jsx,12 memo,13 useContext,14 version,15} from '.'16import type { Context, FC, PropsWithChildren } from '.'1718interface SiteData {19 title: string
158 unmodified lines
178 const template = <link>https://example.com</link>179 expect(template.toString()).toBe('<link>https://example.com</link>')180 })181182 describe('programmatic tag names', () => {183 it('Should throw when jsx() receives a tag name that can break out of the HTML context', () => {184 const payloads = [185 '><script>alert(1)</script><x',186 'div onmouseover="alert(1)" x=',187 'div></div><img src=x onerror=alert(1)><div',188 '!--',189 '!DOCTYPE',190 '?xml',191 ]192193 for (const tag of payloads) {194 expect(() => jsx(tag, {}, 'hello')).toThrow(`Invalid JSX tag name: ${tag}`)195 }196 })197198 it('Should throw when createElement() receives a tag name that can break out of the HTML context', () => {199 expect(() => createElement('div onmouseover="alert(1)" x=', {}, 'hello')).toThrow(200 'Invalid JSX tag name: div onmouseover="alert(1)" x='201 )202 })203204 it('Should throw when jsx() receives an invalid non-primitive tag name', () => {205 expect(() => jsx(new String('div') as never, {}, 'hello')).toThrow(206 'Invalid JSX tag name: div'207 )208 expect(() =>209 jsx({ toString: () => 'div onmouseover="alert(1)" x=' } as never, {}, 'hello')210 ).toThrow('Invalid JSX tag name: div onmouseover="alert(1)" x=')211 })212213 it('Should allow compatible custom and namespaced tag names', () => {214 expect(jsx('custom-element', {}, 'hello').toString()).toBe(215 '<custom-element>hello</custom-element>'216 )217 expect(jsx('foo:bar', {}, 'hello').toString()).toBe('<foo:bar>hello</foo:bar>')218 expect(jsx('x.foo', {}, 'hello').toString()).toBe('<x.foo>hello</x.foo>')219 expect(jsx('x_bar', {}, 'hello').toString()).toBe('<x_bar>hello</x_bar>')220 expect(jsx('é', {}, 'hello').toString()).toBe('<é>hello</é>')221 })222 })223224 it('Props value is null', () => {225 const template = <span data-hello={null}>Hello</span>226 expect(template.toString()).toBe('<span>Hello</span>')227 })
1013 unmodified lines
modified:src/jsx/utils.test.ts+76 added-12 removed
modified:src/jsx/utils.ts+43 added-1 removed
11 unmodified lines
12 normalizeElementKeyMap.get(key) || key1314// eslint-disable-next-line no-control-regex15const invalidAttributeNameCharRe = /[\s"'<>/=`\\\x00-\x1f\x7f-\x9f]/16const validAttributeNameCache = new Set<string>()17const validAttributeNameCacheMax = 102418// reject HTML parser-control tag starters ('!' for comments/doctype, '?' for19// processing instructions) in addition to the shared invalid-char set20// eslint-disable-next-line no-control-regex21const invalidTagNameCharRe = /^[!?]|[\s"'<>/=`\\\x00-\x1f\x7f-\x9f]/22const validTagNameCache = new Set<unknown>()23const validTagNameCacheMax = 2562425const cacheValidName = (cache: Set<unknown>, max: number, name: string): void => {26 if (cache.size >= max) {27 cache.clear()28 }29 cache.add(name)30}3132export const isValidTagName = (name: unknown): name is string => {33 if (validTagNameCache.has(name)) {34 return true35 }36 if (typeof name !== 'string') {37 return false38 }39 if (name.length === 0) {40 return true41 }42 if (invalidTagNameCharRe.test(name)) {43 return false44 }45 cacheValidName(validTagNameCache, validTagNameCacheMax, name)46 return true47}4849export const isValidAttributeName = (name: string): boolean => {50 if (validAttributeNameCache.has(name)) {51 return true52 }53 const len = name.length54 if (len === 0) {55 return false56 }
10 unmodified lines
67 c === 0x3a // :68 )69 ) {70 // non-fast-path character found — fall back to regex for the full name35 return !invalidAttributeNameCharRe.test(name)71 if (!invalidAttributeNameCharRe.test(name)) {72 cacheValidName(validAttributeNameCache, validAttributeNameCacheMax, name)73 return true74 } else {75 return false76 }77 }78 }79 cacheValidName(validAttributeNameCache, validAttributeNameCacheMax, name)80 return true81}8283export const styleObjectForEach = (
22 unmodified lines