@@ -56,6 +56,45 @@ describe('render', () => {
5656 expect ( render ( < div foo = { 0 } /> ) ) . to . equal ( `<div foo="0"></div>` ) ;
5757 } ) ;
5858
59+ describe ( 'attribute name sanitization' , ( ) => {
60+ it ( 'should omit attributes with invalid names' , ( ) => {
61+ let rendered = render ( h ( 'div' , {
62+ '<a' : '1' ,
63+ 'a>' : '1' ,
64+ 'foo"bar' : '1' ,
65+ '"hello"' : '1'
66+ } ) ) ;
67+ expect ( rendered ) . to . equal ( `<div></div>` ) ;
68+ } ) ;
69+
70+ it ( 'should mitigate attribute name injection' , ( ) => {
71+ let rendered = render ( h ( 'div' , {
72+ '></div><script>alert("hi")</script>' : '' ,
73+ 'foo onclick' : 'javascript:alert()' ,
74+ a : 'b'
75+ } ) ) ;
76+ expect ( rendered ) . to . equal ( `<div a="b"></div>` ) ;
77+ } ) ;
78+
79+ it ( 'should allow emoji attribute names' , ( ) => {
80+ let rendered = render ( h ( 'div' , {
81+ 'a;b' : '1' ,
82+ 'a🧙b' : '1'
83+ } ) ) ;
84+ expect ( rendered ) . to . equal ( `<div a;b="1" a🧙b="1"></div>` ) ;
85+ } ) ;
86+ } ) ;
87+
88+ it ( 'should throw for invalid nodeName values' , ( ) => {
89+ expect ( ( ) => render ( h ( 'div' ) ) ) . not . to . throw ( ) ;
90+ expect ( ( ) => render ( h ( 'x-💩' ) ) ) . not . to . throw ( ) ;
91+ expect ( ( ) => render ( h ( 'a b' ) ) ) . to . throw ( / < a b > / ) ;
92+ expect ( ( ) => render ( h ( 'a\0b' ) ) ) . to . throw ( / < a \0 b > / ) ;
93+ expect ( ( ) => render ( h ( 'a>' ) ) ) . to . throw ( / < a > > / ) ;
94+ expect ( ( ) => render ( h ( '<' ) ) ) . to . throw ( / < < > / ) ;
95+ expect ( ( ) => render ( h ( '"' ) ) ) . to . throw ( / < " > / ) ;
96+ } ) ;
97+
5998 it ( 'should collapse collapsible attributes' , ( ) => {
6099 let rendered = render ( < div class = "" style = "" foo = { true } bar /> ) ,
61100 expected = `<div class style foo bar></div>` ;
0 commit comments