Skip to content

Commit 7fea7dc

Browse files
committed
Merge pull request #2 from jadejs/escape-html
Escape html
2 parents 887e676 + b4aff5e commit 7fea7dc

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ assert(stringify('foo') === '"foo"');
2121
assert(stringify('foo\u2028bar\u2029baz') === '"foo\\u2028bar\\u2029baz"');
2222
assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19T03:42:00.000Z")');
2323
assert(stringify({foo: 'bar'}) === '{"foo":"bar"}');
24+
assert(stringify(undefined) === 'undefined');
25+
assert(stringify(null) === 'null');
26+
assert(
27+
stringify({val: "</script><script>alert('bad actor')</script>"}) ===
28+
'{"val":"\\u003C\\u002Fscript\\u003E\\u003Cscript\\u003Ealert(\'bad actor\')\\u003C\\u002Fscript\\u003E"}'
29+
);
2430
```
2531

2632
## License

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ function stringify(obj) {
1010
}
1111
return JSON.stringify(obj)
1212
.replace(/\u2028/g, '\\u2028')
13-
.replace(/\u2029/g, '\\u2029');
13+
.replace(/\u2029/g, '\\u2029')
14+
.replace(/</g, '\\u003C')
15+
.replace(/>/g, '\\u003E')
16+
.replace(/\//g, '\\u002F');
1417
}

test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ assert(stringify(new Date('2014-12-19T03:42:00.000Z')) === 'new Date("2014-12-19
99
assert(stringify({foo: 'bar'}) === '{"foo":"bar"}');
1010
assert(stringify(undefined) === 'undefined');
1111
assert(stringify(null) === 'null');
12+
assert(
13+
stringify({val: "</script><script>alert('bad actor')</script>"}) ===
14+
'{"val":"\\u003C\\u002Fscript\\u003E\\u003Cscript\\u003Ealert(\'bad actor\')\\u003C\\u002Fscript\\u003E"}'
15+
);
1216

1317
console.log('tests passed');

0 commit comments

Comments
 (0)