You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use `stringify` and `parse` when evaluating JavaScript isn't an option.
65
+
40
66
## Error handling
41
67
42
-
If `devalue` encounters a function or a non-POJO, it will throw an error. You can find where in the input data the offending value lives by inspecting `error.path`:
68
+
If `uneval` or `stringify` encounters a function or a non-POJO, it will throw an error. You can find where in the input data the offending value lives by inspecting `error.path`:
Using `devalue`, we're protected against that attack:
113
+
Using `uneval` or `stringify`, we're protected against that attack:
88
114
89
115
```js
90
116
const template = `
91
117
<script>
92
-
var preloaded = ${devalue(state)};
118
+
var preloaded = ${uneval(state)};
93
119
</script>`;
94
120
```
95
121
@@ -102,15 +128,15 @@ const template = `
102
128
</script>
103
129
```
104
130
105
-
This, along with the fact that `devalue` bails on functions and non-POJOs, stops attackers from executing arbitrary code. Strings generated by `devalue` can be safely deserialized with `eval` or `new Function`:
131
+
This, along with the fact that `uneval` and `stringify` bail on functions and non-POJOs, stops attackers from executing arbitrary code. Strings generated by `uneval` can be safely deserialized with `eval` or `new Function`:
106
132
107
133
```js
108
134
const value = (0, eval)('(' + str + ')');
109
135
```
110
136
111
137
## Other security considerations
112
138
113
-
While `devalue` prevents the XSS vulnerability shown above, meaning you can use it to send data from server to client, **you should not send user data from client to server** using the same method. Since it has to be evaluated, an attacker that successfully submitted data that bypassed `devalue` would have access to your system.
139
+
While `uneval` prevents the XSS vulnerability shown above, meaning you can use it to send data from server to client, **you should not send user data from client to server** using the same method. Since it has to be evaluated, an attacker that successfully submitted data that bypassed `uneval` would have access to your system.
114
140
115
141
When using `eval`, ensure that you call it _indirectly_ so that the evaluated code doesn't have access to the surrounding scope:
116
142
@@ -127,7 +153,7 @@ Using `new Function(code)` is akin to using indirect eval.
127
153
## See also
128
154
129
155
- [lave](https://github.com/jed/lave) by Jed Schmidt
130
-
- [arson](https://github.com/benjamn/arson) by Ben Newman
156
+
- [arson](https://github.com/benjamn/arson) by Ben Newman. The `stringify`/`parse` approach in `devalue` was inspired by `arson`
131
157
- [tosource](https://github.com/marcello3d/node-tosource) by Marcello Bastéa-Forte
132
158
- [serialize-javascript](https://github.com/yahoo/serialize-javascript) by Eric Ferraiuolo
133
159
- [jsesc](https://github.com/mathiasbynens/jsesc) by Mathias Bynens
0 commit comments