Skip to content

Commit e5cf898

Browse files
authored
Added a note regarding the use of new Function (#158)
1 parent b72e99f commit e5cf898

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fast-json-stringify-uglified obj x 9,073,607 ops/sec ±0.41% (94 runs sampled)
4242
- <a href="#long">`Long integers`</a>
4343
- <a href="#uglify">`Uglify`</a>
4444
- <a href="#nullable">`Nullable`</a>
45+
- <a href="#caveat">`Caveat`</a>
4546
- <a href="#acknowledgements">`Acknowledgements`</a>
4647
- <a href="#license">`License`</a>
4748

@@ -96,7 +97,7 @@ Supported types:
9697
* `'boolean'`
9798
* `'null'`
9899

99-
And nested ones, too.
100+
And nested ones, too.
100101

101102
<a name="specific"></a>
102103
#### Specific use cases
@@ -108,7 +109,7 @@ And nested ones, too.
108109

109110
<a name="required"></a>
110111
#### Required
111-
You can set specific fields of an object as required in your schema by adding the field name inside the `required` array in your schema.
112+
You can set specific fields of an object as required in your schema by adding the field name inside the `required` array in your schema.
112113
Example:
113114
```javascript
114115
const schema = {
@@ -129,7 +130,7 @@ If the object to stringify is missing the required field(s), `fast-json-stringif
129130

130131
<a name="missingFields"></a>
131132
#### Missing fields
132-
If a field *is present* in the schema (and is not required) but it *is not present* in the object to stringify, `fast-json-stringify` will not write it in the final string.
133+
If a field *is present* in the schema (and is not required) but it *is not present* in the object to stringify, `fast-json-stringify` will not write it in the final string.
133134
Example:
134135
```javascript
135136
const stringify = fastJson({
@@ -176,9 +177,9 @@ console.log(stringify({nickname: 'my-nickname'})) // '{"nickname":"my-nickname"}
176177

177178
<a name="patternProperties"></a>
178179
#### Pattern properties
179-
`fast-json-stringify` supports pattern properties as defined by JSON schema.
180-
*patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: `{ type: 'type' }`.
181-
*patternProperties* will work only for the properties that are not explicitly listed in the properties object.
180+
`fast-json-stringify` supports pattern properties as defined by JSON schema.
181+
*patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: `{ type: 'type' }`.
182+
*patternProperties* will work only for the properties that are not explicitly listed in the properties object.
182183
Example:
183184
```javascript
184185
const stringify = fastJson({
@@ -211,13 +212,13 @@ console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"
211212

212213
<a name="additionalProperties"></a>
213214
#### Additional properties
214-
`fast-json-stringify` supports additional properties as defined by JSON schema.
215-
*additionalProperties* must be an object or a boolean, declared in this way: `{ type: 'type' }`.
215+
`fast-json-stringify` supports additional properties as defined by JSON schema.
216+
*additionalProperties* must be an object or a boolean, declared in this way: `{ type: 'type' }`.
216217
*additionalProperties* will work only for the properties that are not explicitly listed in the *properties* and *patternProperties* objects.
217218

218219
If *additionalProperties* is not present or is set to `false`, every property that is not explicitly listed in the *properties* and *patternProperties* objects,will be ignored, as described in <a href="#missingFields">Missing fields</a>.
219220
Missing fields are ignored to avoid having to rewrite objects before serializing. However, other schema rules would throw in similar situations.
220-
If *additionalProperties* is set to `true`, it will be used by `JSON.stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible.
221+
If *additionalProperties* is set to `true`, it will be used by `JSON.stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible.
221222
Example:
222223
```javascript
223224
const stringify = fastJson({
@@ -327,8 +328,8 @@ console.log(stringify({
327328
328329
<a name="ref"></a>
329330
#### Reuse - $ref
330-
If you want to reuse a definition of a value, you can use the property `$ref`.
331-
The value of `$ref` must be a string in [JSON Pointer](https://tools.ietf.org/html/rfc6901) format.
331+
If you want to reuse a definition of a value, you can use the property `$ref`.
332+
The value of `$ref` must be a string in [JSON Pointer](https://tools.ietf.org/html/rfc6901) format.
332333
Example:
333334
```javascript
334335
const schema = {
@@ -364,7 +365,7 @@ const schema = {
364365

365366
const stringify = fastJson(schema)
366367
```
367-
If you need to use an external definition, you can pass it as an option to `fast-json-stringify`.
368+
If you need to use an external definition, you can pass it as an option to `fast-json-stringify`.
368369
Example:
369370
```javascript
370371
const schema = {
@@ -488,6 +489,16 @@ Otherwise, instead of raising an error, null values will be coerced as follows:
488489
- `string` -> `""`
489490
- `boolean` -> `false`
490491
492+
<a name="caveat"></a>
493+
## Caveat
494+
495+
In order to achieve lowest cost/highest performance redaction `fast-json-stringify`
496+
creates and compiles a function (using the `Function` constructor) on initialization.
497+
While the `schema` is currently validated for any developer errors, it's recommended against
498+
allowing user input to directly supply a schema.
499+
It can't be guaranteed that allowing user input for the schema couldn't feasibly expose an attack
500+
vector.
501+
491502
<a name="acknowledgements"></a>
492503
## Acknowledgements
493504

0 commit comments

Comments
 (0)