Skip to content

Commit 5b683b5

Browse files
Bruno Herfstredonkulus
Bruno Herfst
authored andcommitted
Add unsafe flag for straight serialisation (#37)
* Add unsafe flag for straight serialisation * Update index.js Fixed indentation * Added tests and updated docs * Fixed formatting and bumped version * Formatting (fix last space too) * Update package.json * Remove venv * Catch typo... ... and clarifying that options is an object, * Fixed more spelling errors I caught a few old ones too :)
1 parent 2b1e4c7 commit 5b683b5

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

README.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ Serialize JavaScript to a _superset_ of JSON that includes regular expressions,
1111

1212
The code in this package began its life as an internal module to [express-state][]. To expand its usefulness, it now lives as `serialize-javascript` — an independent package on npm.
1313

14-
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps** or **dates**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client.
14+
You're probably wondering: **What about `JSON.stringify()`!?** We've found that sometimes we need to serialize JavaScript **functions**, **regexps** or **dates**. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. But this module is also great for communicating between node processes.
15+
16+
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element.
17+
18+
> **HTML characters and JavaScript line terminators are escaped automatically.**
1519
16-
The string returned from this package's single export function is literal JavaScript which can be saved to a `.js` file, or be embedded into an HTML document by making the content of a `<script>` element. **HTML charaters and JavaScript line terminators are escaped automatically.**
1720

1821
## Installation
1922

@@ -67,9 +70,11 @@ The above will produce the following string, HTML-escaped output which is safe t
6770
'{"haxorXSS":"\\u003C\\u002Fscript\\u003E"}'
6871
```
6972

73+
> You can pass an optional `unsafe` argument to `serialize()` for straight serialization.
74+
7075
### Options
7176

72-
The `serialize()` function accepts `options` as its second argument. There are two options, both default to being `undefined`:
77+
The `serialize()` function accepts an `options` object as its second argument. All options are being defaulted to `undefined`:
7378

7479
#### `options.space`
7580

@@ -89,9 +94,17 @@ This option is a signal to `serialize()` that the object being serialized does n
8994
serialize(obj, {isJSON: true});
9095
```
9196

97+
#### `options.unsafe`
98+
99+
This option is to signal `serialize()` that we want to do a straight conversion, without the XSS protection. This options needs to be explicitly set to `true`. HTML characters and JavaScript line terminators will not be escaped. You will have to roll your own.
100+
101+
```js
102+
serialize(obj, {unsafe: true});
103+
```
104+
92105
## Deserializing
93106

94-
For some use cases you might also need to deserialize the string. This is explicitely not part of this module. However, you can easily write it yourself:
107+
For some use cases you might also need to deserialize the string. This is explicitly not part of this module. However, you can easily write it yourself:
95108

96109
```js
97110
function deserialize(serializedJavascript){

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function escapeUnsafeChars(unsafeChar) {
3030
module.exports = function serialize(obj, options) {
3131
options || (options = {});
3232

33-
// Backwards-compatability for `space` as the second argument.
33+
// Backwards-compatibility for `space` as the second argument.
3434
if (typeof options === 'number' || typeof options === 'string') {
3535
options = {space: options};
3636
}
@@ -87,7 +87,9 @@ module.exports = function serialize(obj, options) {
8787
// Replace unsafe HTML and invalid JavaScript line terminator chars with
8888
// their safe Unicode char counterpart. This _must_ happen before the
8989
// regexps and functions are serialized and added back to the string.
90-
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
90+
if (options.unsafe !== true) {
91+
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
92+
}
9193

9294
if (functions.length === 0 && regexps.length === 0 && dates.length === 0) {
9395
return str;

test/benchmark/serialize.js

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ new Benchmark.Suite('simpleObj', suiteConfig)
3939
.add('serialize( simpleObj, {isJSON: true} )', function () {
4040
serialize(simpleObj, {isJSON: true});
4141
})
42+
.add('serialize( simpleObj, {unsafe: true} )', function () {
43+
serialize(simpleObj, {unsafe: true});
44+
})
45+
.add('serialize( simpleObj, {unsafe: true, isJSON: true} )', function () {
46+
serialize(simpleObj, {unsafe: true, isJSON: true});
47+
})
4248
.add('serialize( simpleObj )', function () {
4349
serialize(simpleObj);
4450
})

test/unit/serialize.js

+18
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ describe('serialize( obj )', function () {
219219
expect(serialize(fn, {isJSON: true})).to.equal('undefined');
220220
expect(serialize([1], {isJSON: true, space: 2})).to.equal('[\n 1\n]');
221221
});
222+
223+
it('should accept a `unsafe` option', function () {
224+
expect(serialize('foo', {unsafe: true})).to.equal('"foo"');
225+
expect(serialize('foo', {unsafe: false})).to.equal('"foo"');
226+
227+
function fn() { return true; }
228+
229+
expect(serialize(fn)).to.equal('function fn() { return true; }');
230+
expect(serialize(fn, {unsafe: false})).to.equal('function fn() { return true; }');
231+
expect(serialize(fn, {unsafe: undefined})).to.equal('function fn() { return true; }');
232+
expect(serialize(fn, {unsafe: "true"})).to.equal('function fn() { return true; }');
233+
234+
expect(serialize(fn, {unsafe: true})).to.equal('function fn() { return true; }');
235+
expect(serialize(["1"], {unsafe: false, space: 2})).to.equal('[\n "1"\n]');
236+
expect(serialize(["1"], {unsafe: true, space: 2})).to.equal('[\n "1"\n]');
237+
expect(serialize(["<"], {space: 2})).to.equal('[\n "\\u003C"\n]');
238+
expect(serialize(["<"], {unsafe: true, space: 2})).to.equal('[\n "<"\n]');
239+
});
222240
});
223241

224242
describe('backwards-compatability', function () {

0 commit comments

Comments
 (0)