File tree Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Expand file tree Collapse file tree 2 files changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ describe('Attributes to String', () => {
63
63
64
64
const resultString = attributeToString ( attr ) ;
65
65
66
- expect ( resultString ) . toEqual ( ' style=\"text-align:left; \" rows=\"4\" cols=\"2\" colWidths=\"250, 250\" <ls=\""></p><h1>test</h1><p class="\" ' )
66
+ expect ( resultString ) . toEqual ( ' style=\"text-align:left; \" rows=\"4\" cols=\"2\" colWidths=\"250, 250\"' )
67
67
done ( ) ;
68
68
} ) ;
69
69
it ( 'Should handle object attribute values correctly' , done => {
@@ -125,4 +125,16 @@ describe('Attributes to String', () => {
125
125
expect ( resultString ) . toEqual ( ' safeKey="<script>alert(xss)</script>"' ) ;
126
126
done ( ) ;
127
127
} ) ;
128
+ it ( 'Should ignore attributes with forbidden characters in keys' , done => {
129
+ const attr = {
130
+ "validKey" : "safeValue" ,
131
+ 'in"valid' : "should be ignored" ,
132
+ "another>invalid" : "should also be ignored"
133
+ } as Attributes ;
134
+
135
+ const resultString = attributeToString ( attr ) ;
136
+
137
+ expect ( resultString ) . toEqual ( ' validKey="safeValue"' ) ;
138
+ done ( ) ;
139
+ } ) ;
128
140
} )
Original file line number Diff line number Diff line change @@ -60,11 +60,7 @@ export function attributeToString(attributes: Attributes): string {
60
60
let result = '' ;
61
61
for ( const key in attributes ) {
62
62
if ( Object . prototype . hasOwnProperty . call ( attributes , key ) ) {
63
- // Sanitize the key to prevent HTML injection
64
- const sanitizedKey = replaceHtmlEntities ( key ) ;
65
-
66
- // Skip keys that contain forbidden characters (even after sanitization)
67
- if ( forbiddenAttrChars . some ( char => sanitizedKey . includes ( char ) ) ) {
63
+ if ( forbiddenAttrChars . some ( char => key . includes ( char ) ) ) {
68
64
continue ;
69
65
}
70
66
let value = attributes [ key ] ;
@@ -76,14 +72,13 @@ export function attributeToString(attributes: Attributes): string {
76
72
if ( Object . prototype . hasOwnProperty . call ( value , subKey ) ) {
77
73
const subValue = value [ subKey ] ;
78
74
if ( subValue != null && subValue !== '' ) {
79
- elementString += `${ replaceHtmlEntities ( subKey ) } :${ replaceHtmlEntities ( String ( subValue ) ) } ; ` ;
75
+ elementString += `${ subKey } :${ subValue } ; ` ;
80
76
}
81
77
}
82
78
}
83
79
value = elementString ;
84
80
}
85
- // Sanitize the value to prevent HTML injection
86
- result += ` ${ sanitizedKey } ="${ replaceHtmlEntities ( String ( value ) ) } "` ;
81
+ result += ` ${ key } ="${ replaceHtmlEntities ( String ( value ) ) } "` ;
87
82
}
88
83
}
89
84
return result ;
You can’t perform that action at this time.
0 commit comments