Skip to content

Commit a602c57

Browse files
authored
Merge pull request #32 from Rich-Harris/modernise
Modernise
2 parents 10c0371 + b7df7f6 commit a602c57

16 files changed

+418
-1268
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
# cancel in-progress runs on new commits to same PR (gitub.event.number)
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.event.number || github.sha }}
12+
cancel-in-progress: true
13+
14+
jobs:
15+
Tests:
16+
runs-on: ${{ matrix.os }}
17+
timeout-minutes: 30
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
node-version: [16]
22+
os: [ubuntu-latest]
23+
steps:
24+
- run: git config --global core.autocrlf false
25+
- uses: actions/checkout@v3
26+
- uses: pnpm/[email protected]
27+
- uses: actions/setup-node@v3
28+
with:
29+
node-version: ${{ matrix.node-version }}
30+
cache: pnpm
31+
- run: pnpm install --frozen-lockfile
32+
- run: pnpm test

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
# devalue changelog
22

3+
## 3.0.0
4+
5+
- Convert to ESM
6+
- Change `import devalue` to `import { devalue }`
7+
- Support `BigInt`
8+
39
## 2.0.1
410

5-
* Prevent regex XSS vulnerability in non-Node environments
11+
- Prevent regex XSS vulnerability in non-Node environments
612

713
## 2.0.0
814

9-
* Change license to MIT
15+
- Change license to MIT
1016

1117
## 1.1.1
1218

13-
* Prevent object key XSS vulnerability ([#19](https://github.com/Rich-Harris/devalue/issues/19))
19+
- Prevent object key XSS vulnerability ([#19](https://github.com/Rich-Harris/devalue/issues/19))
1420

1521
## 1.1.0
1622

17-
* Escape lone surrogates ([#13](https://github.com/Rich-Harris/devalue/issues/13))
23+
- Escape lone surrogates ([#13](https://github.com/Rich-Harris/devalue/issues/13))
1824

1925
## 1.0.4
2026

21-
* Smaller output ([#10](https://github.com/Rich-Harris/devalue/pull/10))
27+
- Smaller output ([#10](https://github.com/Rich-Harris/devalue/pull/10))
2228

2329
## 1.0.3
2430

25-
* Detect POJOs cross-realm ([#7](https://github.com/Rich-Harris/devalue/pull/7))
26-
* Error on symbolic keys ([#7](https://github.com/Rich-Harris/devalue/pull/7))
31+
- Detect POJOs cross-realm ([#7](https://github.com/Rich-Harris/devalue/pull/7))
32+
- Error on symbolic keys ([#7](https://github.com/Rich-Harris/devalue/pull/7))
2733

2834
## 1.0.2
2935

30-
* Fix global name for UMD build
36+
- Fix global name for UMD build
3137

3238
## 1.0.1
3339

34-
* XSS mitigation ([#1](https://github.com/Rich-Harris/devalue/issues/1))
40+
- XSS mitigation ([#1](https://github.com/Rich-Harris/devalue/issues/1))
3541

3642
## 1.0.0
3743

38-
* First release
44+
- First release

README.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@
22

33
Like `JSON.stringify`, but handles
44

5-
* cyclical references (`obj.self = obj`)
6-
* repeated references (`[value, value]`)
7-
* `undefined`, `Infinity`, `NaN`, `-0`
8-
* regular expressions
9-
* dates
10-
* `Map` and `Set`
5+
- cyclical references (`obj.self = obj`)
6+
- repeated references (`[value, value]`)
7+
- `undefined`, `Infinity`, `NaN`, `-0`
8+
- regular expressions
9+
- dates
10+
- `Map` and `Set`
11+
- `BigInt`
1112

1213
Try it out on [runkit.com](https://npm.runkit.com/devalue).
1314

1415
## Goals:
1516

16-
* Performance
17-
* Security (see [XSS mitigation](#xss-mitigation))
18-
* Compact output
19-
17+
- Performance
18+
- Security (see [XSS mitigation](#xss-mitigation))
19+
- Compact output
2020

2121
## Non-goals:
2222

23-
* Human-readable output
24-
* Stringifying functions or non-POJOs
25-
23+
- Human-readable output
24+
- Stringifying functions or non-POJOs
2625

2726
## Usage
2827

2928
```js
30-
import devalue from 'devalue';
29+
import { devalue } from 'devalue';
3130

3231
let obj = { a: 1, b: 2 };
3332
obj.c = 3;
@@ -40,7 +39,6 @@ devalue(obj); // '(function(a){a.a=1;a.b=2;a.c=3;a.self=a;return a}({}))'
4039

4140
If `devalue` encounters a function or a non-POJO, it will throw an error.
4241

43-
4442
## XSS mitigation
4543

4644
Say you're server-rendering a page and want to serialize some state, which could include user input. `JSON.stringify` doesn't protect against XSS attacks:
@@ -62,7 +60,10 @@ Which would result in this:
6260
```html
6361
<script>
6462
// NEVER DO THIS
65-
var preloaded = {"userinput":"</script><script src='https://evil.com/mwahaha.js'>"};
63+
var preloaded = {"userinput":"
64+
</script>
65+
<script src="https://evil.com/mwahaha.js">
66+
"};
6667
</script>
6768
```
6869
@@ -77,43 +78,43 @@ const template = `
7778
7879
```html
7980
<script>
80-
var preloaded = {userinput:"\\u003C\\u002Fscript\\u003E\\u003Cscript src=\'https:\\u002F\\u002Fevil.com\\u002Fmwahaha.js\'\\u003E"};
81+
var preloaded = {
82+
userinput:
83+
"\\u003C\\u002Fscript\\u003E\\u003Cscript src='https:\\u002F\\u002Fevil.com\\u002Fmwahaha.js'\\u003E"
84+
};
8185
</script>
8286
```
8387
8488
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`:
8589
8690
```js
87-
const value = (0,eval)('(' + str + ')');
91+
const value = (0, eval)('(' + str + ')');
8892
```
8993
90-
9194
## Other security considerations
9295
9396
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.
9497
95-
When using `eval`, ensure that you call it *indirectly* so that the evaluated code doesn't have access to the surrounding scope:
98+
When using `eval`, ensure that you call it _indirectly_ so that the evaluated code doesn't have access to the surrounding scope:
9699
97100
```js
98101
{
99102
const sensitiveData = 'Setec Astronomy';
100103
eval('sendToEvilServer(sensitiveData)'); // pwned :(
101-
(0,eval)('sendToEvilServer(sensitiveData)'); // nice try, evildoer!
104+
(0, eval)('sendToEvilServer(sensitiveData)'); // nice try, evildoer!
102105
}
103106
```
104107
105108
Using `new Function(code)` is akin to using indirect eval.
106109
107-
108110
## See also
109111
110-
* [lave](https://github.com/jed/lave) by Jed Schmidt
111-
* [arson](https://github.com/benjamn/arson) by Ben Newman
112-
* [tosource](https://github.com/marcello3d/node-tosource) by Marcello Bastéa-Forte
113-
* [serialize-javascript](https://github.com/yahoo/serialize-javascript) by Eric Ferraiuolo
114-
* [jsesc](https://github.com/mathiasbynens/jsesc) by Mathias Bynens
115-
* [superjson](https://github.com/blitz-js/superjson) by Blitz
116-
112+
- [lave](https://github.com/jed/lave) by Jed Schmidt
113+
- [arson](https://github.com/benjamn/arson) by Ben Newman
114+
- [tosource](https://github.com/marcello3d/node-tosource) by Marcello Bastéa-Forte
115+
- [serialize-javascript](https://github.com/yahoo/serialize-javascript) by Eric Ferraiuolo
116+
- [jsesc](https://github.com/mathiasbynens/jsesc) by Mathias Bynens
117+
- [superjson](https://github.com/blitz-js/superjson) by Blitz
117118
118119
## License
119120

appveyor.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)