Skip to content

Commit 14cae90

Browse files
author
pooya parsa
committed
prevent double escaping
1 parent 751ed46 commit 14cae90

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ function escapeUnsafeChars(str: string) {
230230
}
231231

232232
function safeKey(key: string) {
233-
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : JSON.stringify(escapeUnsafeChars(key));
233+
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : escapeUnsafeChars(JSON.stringify(key));
234234
}
235235

236236
function safeProp(key: string) {
237-
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? `.${key}` : `[${JSON.stringify(escapeUnsafeChars(key))}]`;
237+
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? `.${key}` : `[${escapeUnsafeChars(JSON.stringify(key))}]`;
238238
}
239239

240240
function stringifyString(str: string) {

test/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('devalue', () => {
9191
test(
9292
'Dangerous key',
9393
{ '<svg onload=alert("xss_works")>': 'bar' },
94-
'{"\\\\u003Csvg onload=alert(\\"xss_works\\")\\\\u003E":"bar"}'
94+
'{"\\u003Csvg onload=alert(\\"xss_works\\")\\u003E":"bar"}'
9595
)
9696
});
9797

0 commit comments

Comments
 (0)