@plotdb/csurf 0.0.0-stage → 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +192 -0
- package/LICENSE +23 -0
- package/README.md +327 -2
- package/index.js +297 -0
- package/package.json +52 -4
package/HISTORY.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
1.11.1 / 2026-11-28
|
|
2
|
+
===================
|
|
3
|
+
|
|
4
|
+
* fork of the archived `csurf` as `@plotdb/csurf`, maintained by plotdb
|
|
5
|
+
* deps: cookie@^0.7.2
|
|
6
|
+
- fix GHSA-pxg6-pf52-xh8x: cookie name, path and domain accept out of bounds characters
|
|
7
|
+
* devDependencies: csurf@1.11.0
|
|
8
|
+
- kept only so `npm audit` keeps reporting advisories filed against the original `csurf`
|
|
9
|
+
|
|
10
|
+
1.11.0 / 2020-01-18
|
|
11
|
+
===================
|
|
12
|
+
|
|
13
|
+
* deps: cookie@0.4.0
|
|
14
|
+
- Add `SameSite=None` support
|
|
15
|
+
* deps: http-errors@~1.7.3
|
|
16
|
+
- deps: inherits@2.0.4
|
|
17
|
+
|
|
18
|
+
1.10.0 / 2019-04-22
|
|
19
|
+
===================
|
|
20
|
+
|
|
21
|
+
* deps: csrf@3.1.0
|
|
22
|
+
- Remove `base64-url` dependency
|
|
23
|
+
- deps: tsscmp@1.0.6
|
|
24
|
+
- deps: uid-safe@2.1.5
|
|
25
|
+
* deps: http-errors@~1.7.2
|
|
26
|
+
- Make `message` property enumerable for `HttpError`s
|
|
27
|
+
- Set constructor name when possible
|
|
28
|
+
- deps: depd@~1.1.2
|
|
29
|
+
- deps: inherits@2.0.3
|
|
30
|
+
- deps: setprototypeof@1.1.1
|
|
31
|
+
- deps: statuses@'>= 1.5.0 < 2'
|
|
32
|
+
* perf: remove argument reassignment
|
|
33
|
+
* perf: use plain object for internal cookie options
|
|
34
|
+
|
|
35
|
+
1.9.0 / 2016-05-27
|
|
36
|
+
==================
|
|
37
|
+
|
|
38
|
+
* Pass invalid csrf token error to `next()` instead of throwing
|
|
39
|
+
* Pass misconfigured error to `next()` instead of throwing
|
|
40
|
+
* Provide misconfigured error when using cookies without cookie-parser
|
|
41
|
+
* deps: cookie@0.3.1
|
|
42
|
+
- Add `sameSite` option
|
|
43
|
+
- Fix cookie `Max-Age` to never be a floating point number
|
|
44
|
+
- Improve error message when `expires` is not a `Date`
|
|
45
|
+
- Throw better error for invalid argument to parse
|
|
46
|
+
- Throw on invalid values provided to `serialize`
|
|
47
|
+
- perf: enable strict mode
|
|
48
|
+
- perf: hoist regular expression
|
|
49
|
+
- perf: use for loop in parse
|
|
50
|
+
- perf: use string concatination for serialization
|
|
51
|
+
* deps: csrf@~3.0.3
|
|
52
|
+
- Use `tsscmp` module for timing-safe token verification
|
|
53
|
+
- deps: base64-url@1.2.2
|
|
54
|
+
- deps: rndm@1.2.0
|
|
55
|
+
- deps: uid-safe@2.1.1
|
|
56
|
+
* deps: http-errors@~1.5.0
|
|
57
|
+
- Add `HttpError` export, for `err instanceof createError.HttpError`
|
|
58
|
+
- Support new code `421 Misdirected Request`
|
|
59
|
+
- Use `setprototypeof` module to replace `__proto__` setting
|
|
60
|
+
- deps: inherits@2.0.1
|
|
61
|
+
- deps: statuses@'>= 1.3.0 < 2'
|
|
62
|
+
- perf: enable strict mode
|
|
63
|
+
* perf: enable strict mode
|
|
64
|
+
* perf: remove argument reassignment
|
|
65
|
+
|
|
66
|
+
1.8.3 / 2015-06-10
|
|
67
|
+
==================
|
|
68
|
+
|
|
69
|
+
* deps: cookie@0.1.3
|
|
70
|
+
- Slight optimizations
|
|
71
|
+
|
|
72
|
+
1.8.2 / 2015-05-09
|
|
73
|
+
==================
|
|
74
|
+
|
|
75
|
+
* deps: csrf@~3.0.0
|
|
76
|
+
- deps: uid-safe@~2.0.0
|
|
77
|
+
|
|
78
|
+
1.8.1 / 2015-05-03
|
|
79
|
+
==================
|
|
80
|
+
|
|
81
|
+
* deps: csrf@~2.0.7
|
|
82
|
+
- Fix compatibility with `crypto.DEFAULT_ENCODING` global changes
|
|
83
|
+
|
|
84
|
+
1.8.0 / 2015-04-07
|
|
85
|
+
==================
|
|
86
|
+
|
|
87
|
+
* Add `sessionKey` option
|
|
88
|
+
|
|
89
|
+
1.7.0 / 2015-02-15
|
|
90
|
+
==================
|
|
91
|
+
|
|
92
|
+
* Accept `CSRF-Token` and `XSRF-Token` request headers
|
|
93
|
+
* Default `cookie.path` to `'/'`, if using cookies
|
|
94
|
+
* deps: cookie-signature@1.0.6
|
|
95
|
+
* deps: csrf@~2.0.6
|
|
96
|
+
- deps: base64-url@1.2.1
|
|
97
|
+
- deps: uid-safe@~1.1.0
|
|
98
|
+
* deps: http-errors@~1.3.1
|
|
99
|
+
- Construct errors using defined constructors from `createError`
|
|
100
|
+
- Fix error names that are not identifiers
|
|
101
|
+
- Set a meaningful `name` property on constructed errors
|
|
102
|
+
|
|
103
|
+
1.6.6 / 2015-01-31
|
|
104
|
+
==================
|
|
105
|
+
|
|
106
|
+
* deps: csrf@~2.0.5
|
|
107
|
+
- deps: base64-url@1.2.0
|
|
108
|
+
- deps: uid-safe@~1.0.3
|
|
109
|
+
|
|
110
|
+
1.6.5 / 2015-01-08
|
|
111
|
+
==================
|
|
112
|
+
|
|
113
|
+
* deps: csrf@~2.0.4
|
|
114
|
+
- deps: uid-safe@~1.0.2
|
|
115
|
+
|
|
116
|
+
1.6.4 / 2014-12-30
|
|
117
|
+
==================
|
|
118
|
+
|
|
119
|
+
* deps: csrf@~2.0.3
|
|
120
|
+
- Slight speed improvement for `verify`
|
|
121
|
+
- deps: base64-url@1.1.0
|
|
122
|
+
- deps: rndm@~1.1.0
|
|
123
|
+
* deps: http-errors@~1.2.8
|
|
124
|
+
- Fix stack trace from exported function
|
|
125
|
+
|
|
126
|
+
1.6.3 / 2014-11-09
|
|
127
|
+
==================
|
|
128
|
+
|
|
129
|
+
* deps: csrf@~2.0.2
|
|
130
|
+
- deps: scmp@1.0.0
|
|
131
|
+
* deps: http-errors@~1.2.7
|
|
132
|
+
- Remove duplicate line
|
|
133
|
+
|
|
134
|
+
1.6.2 / 2014-10-14
|
|
135
|
+
==================
|
|
136
|
+
|
|
137
|
+
* Fix cookie name when using `cookie: true`
|
|
138
|
+
* deps: http-errors@~1.2.6
|
|
139
|
+
- Fix `expose` to be `true` for `ClientError` constructor
|
|
140
|
+
- Use `inherits` instead of `util`
|
|
141
|
+
- deps: statuses@1
|
|
142
|
+
|
|
143
|
+
1.6.1 / 2014-09-05
|
|
144
|
+
==================
|
|
145
|
+
|
|
146
|
+
* deps: cookie-signature@1.0.5
|
|
147
|
+
|
|
148
|
+
1.6.0 / 2014-09-03
|
|
149
|
+
==================
|
|
150
|
+
|
|
151
|
+
* Set `code` property on CSRF token errors
|
|
152
|
+
|
|
153
|
+
1.5.0 / 2014-08-24
|
|
154
|
+
==================
|
|
155
|
+
|
|
156
|
+
* Add `ignoreMethods` option
|
|
157
|
+
|
|
158
|
+
1.4.1 / 2014-08-22
|
|
159
|
+
==================
|
|
160
|
+
|
|
161
|
+
* Use `csrf-tokens` instead of `csrf`
|
|
162
|
+
|
|
163
|
+
1.4.0 / 2014-07-30
|
|
164
|
+
==================
|
|
165
|
+
|
|
166
|
+
* Support changing `req.session` after `csurf` middleware
|
|
167
|
+
- Calling `res.csrfToken()` after `req.session.destroy()` will now work
|
|
168
|
+
|
|
169
|
+
1.3.0 / 2014-07-03
|
|
170
|
+
==================
|
|
171
|
+
|
|
172
|
+
* Add support for environments without `res.cookie` (connect@3)
|
|
173
|
+
|
|
174
|
+
1.2.2 / 2014-06-18
|
|
175
|
+
==================
|
|
176
|
+
|
|
177
|
+
* deps: csrf-tokens@~2.0.0
|
|
178
|
+
|
|
179
|
+
1.2.1 / 2014-06-09
|
|
180
|
+
==================
|
|
181
|
+
|
|
182
|
+
* Refactor to use `csrf-tokens` module
|
|
183
|
+
|
|
184
|
+
1.2.0 / 2014-05-13
|
|
185
|
+
==================
|
|
186
|
+
|
|
187
|
+
* Add support for double-submit cookie
|
|
188
|
+
|
|
189
|
+
1.1.0 / 2014-04-06
|
|
190
|
+
==================
|
|
191
|
+
|
|
192
|
+
* Add constant-time string compare
|
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
|
|
4
|
+
Copyright (c) 2014-2016 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
a copy of this software and associated documentation files (the
|
|
8
|
+
'Software'), to deal in the Software without restriction, including
|
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,328 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @plotdb/csurf
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fork of the archived [csurf](https://github.com/expressjs/csurf), maintained by plotdb for dependency fixes. API is unchanged.
|
|
4
|
+
|
|
5
|
+
[![NPM Version][npm-version-image]][npm-url]
|
|
6
|
+
[![NPM Downloads][npm-downloads-image]][node-url]
|
|
7
|
+
[![Build status][travis-image]][travis-url]
|
|
8
|
+
[![Test coverage][coveralls-image]][coveralls-url]
|
|
9
|
+
|
|
10
|
+
Node.js [CSRF][wikipedia-csrf] protection middleware.
|
|
11
|
+
|
|
12
|
+
Requires either a session middleware or [cookie-parser](https://www.npmjs.com/package/cookie-parser) to be initialized first.
|
|
13
|
+
|
|
14
|
+
* If you are setting the ["cookie" option](#cookie) to a non-`false` value,
|
|
15
|
+
then you must use [cookie-parser](https://www.npmjs.com/package/cookie-parser)
|
|
16
|
+
before this module.
|
|
17
|
+
* Otherwise, you must use a session middleware before this module. For example:
|
|
18
|
+
- [express-session](https://www.npmjs.com/package/express-session)
|
|
19
|
+
- [cookie-session](https://www.npmjs.com/package/cookie-session)
|
|
20
|
+
|
|
21
|
+
If you have questions on how this module is implemented, please read
|
|
22
|
+
[Understanding CSRF](https://github.com/pillarjs/understanding-csrf).
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
27
|
+
[npm registry](https://www.npmjs.com/). Installation is done using the
|
|
28
|
+
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
$ npm install @plotdb/csurf
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
<!-- eslint-disable no-unused-vars -->
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
var csurf = require('@plotdb/csurf')
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### csurf([options])
|
|
43
|
+
|
|
44
|
+
Create a middleware for CSRF token creation and validation. This middleware
|
|
45
|
+
adds a `req.csrfToken()` function to make a token which should be added to
|
|
46
|
+
requests which mutate state, within a hidden form field, query-string etc.
|
|
47
|
+
This token is validated against the visitor's session or csrf cookie.
|
|
48
|
+
|
|
49
|
+
#### Options
|
|
50
|
+
|
|
51
|
+
The `csurf` function takes an optional `options` object that may contain
|
|
52
|
+
any of the following keys:
|
|
53
|
+
|
|
54
|
+
##### cookie
|
|
55
|
+
|
|
56
|
+
Determines if the token secret for the user should be stored in a cookie
|
|
57
|
+
or in `req.session`. Storing the token secret in a cookie implements
|
|
58
|
+
the [double submit cookie pattern][owsap-csrf-double-submit].
|
|
59
|
+
Defaults to `false`.
|
|
60
|
+
|
|
61
|
+
When set to `true` (or an object of options for the cookie), then the module
|
|
62
|
+
changes behavior and no longer uses `req.session`. This means you _are no
|
|
63
|
+
longer required to use a session middleware_. Instead, you do need to use the
|
|
64
|
+
[cookie-parser](https://www.npmjs.com/package/cookie-parser) middleware in
|
|
65
|
+
your app before this middleware.
|
|
66
|
+
|
|
67
|
+
When set to an object, cookie storage of the secret is enabled and the
|
|
68
|
+
object contains options for this functionality (when set to `true`, the
|
|
69
|
+
defaults for the options are used). The options may contain any of the
|
|
70
|
+
following keys:
|
|
71
|
+
|
|
72
|
+
- `key` - the name of the cookie to use to store the token secret
|
|
73
|
+
(defaults to `'_csrf'`).
|
|
74
|
+
- `path` - the path of the cookie (defaults to `'/'`).
|
|
75
|
+
- `signed` - indicates if the cookie should be signed (defaults to `false`).
|
|
76
|
+
- `secure` - marks the cookie to be used with HTTPS only (defaults to
|
|
77
|
+
`false`).
|
|
78
|
+
- `maxAge` - the number of seconds after which the cookie will expire
|
|
79
|
+
(defaults to session length).
|
|
80
|
+
- `httpOnly` - flags the cookie to be accessible only by the web server
|
|
81
|
+
(defaults to `false`).
|
|
82
|
+
- `sameSite` - sets the same site policy for the cookie(defaults to
|
|
83
|
+
`false`). This can be set to `'strict'`, `'lax'`, `'none'`, or `true`
|
|
84
|
+
(which maps to `'strict'`).
|
|
85
|
+
- `domain` - sets the domain the cookie is valid on(defaults to current
|
|
86
|
+
domain).
|
|
87
|
+
|
|
88
|
+
##### ignoreMethods
|
|
89
|
+
|
|
90
|
+
An array of the methods for which CSRF token checking will disabled.
|
|
91
|
+
Defaults to `['GET', 'HEAD', 'OPTIONS']`.
|
|
92
|
+
|
|
93
|
+
##### sessionKey
|
|
94
|
+
|
|
95
|
+
Determines what property ("key") on `req` the session object is located.
|
|
96
|
+
Defaults to `'session'` (i.e. looks at `req.session`). The CSRF secret
|
|
97
|
+
from this library is stored and read as `req[sessionKey].csrfSecret`.
|
|
98
|
+
|
|
99
|
+
If the ["cookie" option](#cookie) is not `false`, then this option does
|
|
100
|
+
nothing.
|
|
101
|
+
|
|
102
|
+
##### value
|
|
103
|
+
|
|
104
|
+
Provide a function that the middleware will invoke to read the token from
|
|
105
|
+
the request for validation. The function is called as `value(req)` and is
|
|
106
|
+
expected to return the token as a string.
|
|
107
|
+
|
|
108
|
+
The default value is a function that reads the token from the following
|
|
109
|
+
locations, in order:
|
|
110
|
+
|
|
111
|
+
- `req.body._csrf` - typically generated by the `body-parser` module.
|
|
112
|
+
- `req.query._csrf` - a built-in from Express.js to read from the URL
|
|
113
|
+
query string.
|
|
114
|
+
- `req.headers['csrf-token']` - the `CSRF-Token` HTTP request header.
|
|
115
|
+
- `req.headers['xsrf-token']` - the `XSRF-Token` HTTP request header.
|
|
116
|
+
- `req.headers['x-csrf-token']` - the `X-CSRF-Token` HTTP request header.
|
|
117
|
+
- `req.headers['x-xsrf-token']` - the `X-XSRF-Token` HTTP request header.
|
|
118
|
+
|
|
119
|
+
## Example
|
|
120
|
+
|
|
121
|
+
### Simple express example
|
|
122
|
+
|
|
123
|
+
The following is an example of some server-side code that generates a form
|
|
124
|
+
that requires a CSRF token to post back.
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
var cookieParser = require('cookie-parser')
|
|
128
|
+
var csrf = require('@plotdb/csurf')
|
|
129
|
+
var bodyParser = require('body-parser')
|
|
130
|
+
var express = require('express')
|
|
131
|
+
|
|
132
|
+
// setup route middlewares
|
|
133
|
+
var csrfProtection = csrf({ cookie: true })
|
|
134
|
+
var parseForm = bodyParser.urlencoded({ extended: false })
|
|
135
|
+
|
|
136
|
+
// create express app
|
|
137
|
+
var app = express()
|
|
138
|
+
|
|
139
|
+
// parse cookies
|
|
140
|
+
// we need this because "cookie" is true in csrfProtection
|
|
141
|
+
app.use(cookieParser())
|
|
142
|
+
|
|
143
|
+
app.get('/form', csrfProtection, function (req, res) {
|
|
144
|
+
// pass the csrfToken to the view
|
|
145
|
+
res.render('send', { csrfToken: req.csrfToken() })
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
app.post('/process', parseForm, csrfProtection, function (req, res) {
|
|
149
|
+
res.send('data is being processed')
|
|
150
|
+
})
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Inside the view (depending on your template language; handlebars-style
|
|
154
|
+
is demonstrated here), set the `csrfToken` value as the value of a hidden
|
|
155
|
+
input field named `_csrf`:
|
|
156
|
+
|
|
157
|
+
```html
|
|
158
|
+
<form action="/process" method="POST">
|
|
159
|
+
<input type="hidden" name="_csrf" value="{{csrfToken}}">
|
|
160
|
+
|
|
161
|
+
Favorite color: <input type="text" name="favoriteColor">
|
|
162
|
+
<button type="submit">Submit</button>
|
|
163
|
+
</form>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### Using AJAX
|
|
167
|
+
|
|
168
|
+
When accessing protected routes via ajax both the csrf token will need to be
|
|
169
|
+
passed in the request. Typically this is done using a request header, as adding
|
|
170
|
+
a request header can typically be done at a central location easily without
|
|
171
|
+
payload modification.
|
|
172
|
+
|
|
173
|
+
The CSRF token is obtained from the `req.csrfToken()` call on the server-side.
|
|
174
|
+
This token needs to be exposed to the client-side, typically by including it in
|
|
175
|
+
the initial page content. One possibility is to store it in an HTML `<meta>` tag,
|
|
176
|
+
where value can then be retrieved at the time of the request by JavaScript.
|
|
177
|
+
|
|
178
|
+
The following can be included in your view (handlebar example below), where the
|
|
179
|
+
`csrfToken` value came from `req.csrfToken()`:
|
|
180
|
+
|
|
181
|
+
```html
|
|
182
|
+
<meta name="csrf-token" content="{{csrfToken}}">
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The following is an example of using the
|
|
186
|
+
[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to post
|
|
187
|
+
to the `/process` route with the CSRF token from the `<meta>` tag on the page:
|
|
188
|
+
|
|
189
|
+
<!-- eslint-env browser -->
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
// Read the CSRF token from the <meta> tag
|
|
193
|
+
var token = document.querySelector('meta[name="csrf-token"]').getAttribute('content')
|
|
194
|
+
|
|
195
|
+
// Make a request using the Fetch API
|
|
196
|
+
fetch('/process', {
|
|
197
|
+
credentials: 'same-origin', // <-- includes cookies in the request
|
|
198
|
+
headers: {
|
|
199
|
+
'CSRF-Token': token // <-- is the csrf token as a header
|
|
200
|
+
},
|
|
201
|
+
method: 'POST',
|
|
202
|
+
body: {
|
|
203
|
+
favoriteColor: 'blue'
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### Single Page Application (SPA)
|
|
209
|
+
|
|
210
|
+
Many SPA frameworks like Angular have CSRF support built in automatically.
|
|
211
|
+
Typically they will reflect the value from a specific cookie, like
|
|
212
|
+
`XSRF-TOKEN` (which is the case for Angular).
|
|
213
|
+
|
|
214
|
+
To take advantage of this, set the value from `req.csrfToken()` in the cookie
|
|
215
|
+
used by the SPA framework. This is only necessary to do on the route that
|
|
216
|
+
renders the page (where `res.render` or `res.sendFile` is called in Express,
|
|
217
|
+
for example).
|
|
218
|
+
|
|
219
|
+
The following is an example for Express of a typical SPA response:
|
|
220
|
+
|
|
221
|
+
<!-- eslint-disable no-undef -->
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
app.all('*', function (req, res) {
|
|
225
|
+
res.cookie('XSRF-TOKEN', req.csrfToken())
|
|
226
|
+
res.render('index')
|
|
227
|
+
})
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Ignoring Routes
|
|
231
|
+
|
|
232
|
+
**Note** CSRF checks should only be disabled for requests that you expect to
|
|
233
|
+
come from outside of your website. Do not disable CSRF checks for requests
|
|
234
|
+
that you expect to only come from your website. An existing session, even if
|
|
235
|
+
it belongs to an authenticated user, is not enough to protect against CSRF
|
|
236
|
+
attacks.
|
|
237
|
+
|
|
238
|
+
The following is an example of how to order your routes so that certain endpoints
|
|
239
|
+
do not check for a valid CSRF token.
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
var cookieParser = require('cookie-parser')
|
|
243
|
+
var csrf = require('@plotdb/csurf')
|
|
244
|
+
var bodyParser = require('body-parser')
|
|
245
|
+
var express = require('express')
|
|
246
|
+
|
|
247
|
+
// create express app
|
|
248
|
+
var app = express()
|
|
249
|
+
|
|
250
|
+
// create api router
|
|
251
|
+
var api = createApiRouter()
|
|
252
|
+
|
|
253
|
+
// mount api before csrf is appended to the app stack
|
|
254
|
+
app.use('/api', api)
|
|
255
|
+
|
|
256
|
+
// now add csrf and other middlewares, after the "/api" was mounted
|
|
257
|
+
app.use(bodyParser.urlencoded({ extended: false }))
|
|
258
|
+
app.use(cookieParser())
|
|
259
|
+
app.use(csrf({ cookie: true }))
|
|
260
|
+
|
|
261
|
+
app.get('/form', function (req, res) {
|
|
262
|
+
// pass the csrfToken to the view
|
|
263
|
+
res.render('send', { csrfToken: req.csrfToken() })
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
app.post('/process', function (req, res) {
|
|
267
|
+
res.send('csrf was required to get here')
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
function createApiRouter () {
|
|
271
|
+
var router = new express.Router()
|
|
272
|
+
|
|
273
|
+
router.post('/getProfile', function (req, res) {
|
|
274
|
+
res.send('no csrf to get here')
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
return router
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Custom error handling
|
|
282
|
+
|
|
283
|
+
When the CSRF token validation fails, an error is thrown that has
|
|
284
|
+
`err.code === 'EBADCSRFTOKEN'`. This can be used to display custom
|
|
285
|
+
error messages.
|
|
286
|
+
|
|
287
|
+
```js
|
|
288
|
+
var bodyParser = require('body-parser')
|
|
289
|
+
var cookieParser = require('cookie-parser')
|
|
290
|
+
var csrf = require('@plotdb/csurf')
|
|
291
|
+
var express = require('express')
|
|
292
|
+
|
|
293
|
+
var app = express()
|
|
294
|
+
app.use(bodyParser.urlencoded({ extended: false }))
|
|
295
|
+
app.use(cookieParser())
|
|
296
|
+
app.use(csrf({ cookie: true }))
|
|
297
|
+
|
|
298
|
+
// error handler
|
|
299
|
+
app.use(function (err, req, res, next) {
|
|
300
|
+
if (err.code !== 'EBADCSRFTOKEN') return next(err)
|
|
301
|
+
|
|
302
|
+
// handle CSRF token errors here
|
|
303
|
+
res.status(403)
|
|
304
|
+
res.send('form tampered with')
|
|
305
|
+
})
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## References
|
|
309
|
+
|
|
310
|
+
- [Cross-side request forgery on Wikipedia][wikipedia-csrf]
|
|
311
|
+
- [OWASP Cross-Site Request Forgery Prevention Cheat Sheet][owsap-csrf]
|
|
312
|
+
|
|
313
|
+
[owsap-csrf]: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html
|
|
314
|
+
[owsap-csrf-double-submit]: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie
|
|
315
|
+
[wikipedia-csrf]: https://en.wikipedia.org/wiki/Cross-site_request_forgery
|
|
316
|
+
|
|
317
|
+
## License
|
|
318
|
+
|
|
319
|
+
[MIT](LICENSE)
|
|
320
|
+
|
|
321
|
+
[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/csurf/master
|
|
322
|
+
[coveralls-url]: https://coveralls.io/r/expressjs/csurf?branch=master
|
|
323
|
+
[node-url]: https://nodejs.org/en/download
|
|
324
|
+
[npm-downloads-image]: https://badgen.net/npm/dm/csurf
|
|
325
|
+
[npm-url]: https://npmjs.org/package/csurf
|
|
326
|
+
[npm-version-image]: https://badgen.net/npm/v/csurf
|
|
327
|
+
[travis-image]: https://badgen.net/travis/expressjs/csurf/master
|
|
328
|
+
[travis-url]: https://travis-ci.org/expressjs/csurf
|
package/index.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* csurf
|
|
3
|
+
* Copyright(c) 2011 Sencha Inc.
|
|
4
|
+
* Copyright(c) 2014 Jonathan Ong
|
|
5
|
+
* Copyright(c) 2014-2016 Douglas Christopher Wilson
|
|
6
|
+
* MIT Licensed
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Module dependencies.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
var Cookie = require('cookie')
|
|
17
|
+
var createError = require('http-errors')
|
|
18
|
+
var sign = require('cookie-signature').sign
|
|
19
|
+
var Tokens = require('csrf')
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Module exports.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
module.exports = csurf
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* CSRF protection middleware.
|
|
30
|
+
*
|
|
31
|
+
* This middleware adds a `req.csrfToken()` function to make a token
|
|
32
|
+
* which should be added to requests which mutate
|
|
33
|
+
* state, within a hidden form field, query-string etc. This
|
|
34
|
+
* token is validated against the visitor's session.
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} options
|
|
37
|
+
* @return {Function} middleware
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
function csurf (options) {
|
|
42
|
+
var opts = options || {}
|
|
43
|
+
|
|
44
|
+
// get cookie options
|
|
45
|
+
var cookie = getCookieOptions(opts.cookie)
|
|
46
|
+
|
|
47
|
+
// get session options
|
|
48
|
+
var sessionKey = opts.sessionKey || 'session'
|
|
49
|
+
|
|
50
|
+
// get value getter
|
|
51
|
+
var value = opts.value || defaultValue
|
|
52
|
+
|
|
53
|
+
// token repo
|
|
54
|
+
var tokens = new Tokens(opts)
|
|
55
|
+
|
|
56
|
+
// ignored methods
|
|
57
|
+
var ignoreMethods = opts.ignoreMethods === undefined
|
|
58
|
+
? ['GET', 'HEAD', 'OPTIONS']
|
|
59
|
+
: opts.ignoreMethods
|
|
60
|
+
|
|
61
|
+
if (!Array.isArray(ignoreMethods)) {
|
|
62
|
+
throw new TypeError('option ignoreMethods must be an array')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// generate lookup
|
|
66
|
+
var ignoreMethod = getIgnoredMethods(ignoreMethods)
|
|
67
|
+
|
|
68
|
+
return function csrf (req, res, next) {
|
|
69
|
+
// validate the configuration against request
|
|
70
|
+
if (!verifyConfiguration(req, sessionKey, cookie)) {
|
|
71
|
+
return next(new Error('misconfigured csrf'))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// get the secret from the request
|
|
75
|
+
var secret = getSecret(req, sessionKey, cookie)
|
|
76
|
+
var token
|
|
77
|
+
|
|
78
|
+
// lazy-load token getter
|
|
79
|
+
req.csrfToken = function csrfToken () {
|
|
80
|
+
var sec = !cookie
|
|
81
|
+
? getSecret(req, sessionKey, cookie)
|
|
82
|
+
: secret
|
|
83
|
+
|
|
84
|
+
// use cached token if secret has not changed
|
|
85
|
+
if (token && sec === secret) {
|
|
86
|
+
return token
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// generate & set new secret
|
|
90
|
+
if (sec === undefined) {
|
|
91
|
+
sec = tokens.secretSync()
|
|
92
|
+
setSecret(req, res, sessionKey, sec, cookie)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// update changed secret
|
|
96
|
+
secret = sec
|
|
97
|
+
|
|
98
|
+
// create new token
|
|
99
|
+
token = tokens.create(secret)
|
|
100
|
+
|
|
101
|
+
return token
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// generate & set secret
|
|
105
|
+
if (!secret) {
|
|
106
|
+
secret = tokens.secretSync()
|
|
107
|
+
setSecret(req, res, sessionKey, secret, cookie)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// verify the incoming token
|
|
111
|
+
if (!ignoreMethod[req.method] && !tokens.verify(secret, value(req))) {
|
|
112
|
+
return next(createError(403, 'invalid csrf token', {
|
|
113
|
+
code: 'EBADCSRFTOKEN'
|
|
114
|
+
}))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
next()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Default value function, checking the `req.body`
|
|
123
|
+
* and `req.query` for the CSRF token.
|
|
124
|
+
*
|
|
125
|
+
* @param {IncomingMessage} req
|
|
126
|
+
* @return {String}
|
|
127
|
+
* @api private
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
function defaultValue (req) {
|
|
131
|
+
return (req.body && req.body._csrf) ||
|
|
132
|
+
(req.query && req.query._csrf) ||
|
|
133
|
+
(req.headers['csrf-token']) ||
|
|
134
|
+
(req.headers['xsrf-token']) ||
|
|
135
|
+
(req.headers['x-csrf-token']) ||
|
|
136
|
+
(req.headers['x-xsrf-token'])
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Get options for cookie.
|
|
141
|
+
*
|
|
142
|
+
* @param {boolean|object} [options]
|
|
143
|
+
* @returns {object}
|
|
144
|
+
* @api private
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
function getCookieOptions (options) {
|
|
148
|
+
if (options !== true && typeof options !== 'object') {
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
var opts = Object.create(null)
|
|
153
|
+
|
|
154
|
+
// defaults
|
|
155
|
+
opts.key = '_csrf'
|
|
156
|
+
opts.path = '/'
|
|
157
|
+
|
|
158
|
+
if (options && typeof options === 'object') {
|
|
159
|
+
for (var prop in options) {
|
|
160
|
+
var val = options[prop]
|
|
161
|
+
|
|
162
|
+
if (val !== undefined) {
|
|
163
|
+
opts[prop] = val
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return opts
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Get a lookup of ignored methods.
|
|
173
|
+
*
|
|
174
|
+
* @param {array} methods
|
|
175
|
+
* @returns {object}
|
|
176
|
+
* @api private
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
function getIgnoredMethods (methods) {
|
|
180
|
+
var obj = Object.create(null)
|
|
181
|
+
|
|
182
|
+
for (var i = 0; i < methods.length; i++) {
|
|
183
|
+
var method = methods[i].toUpperCase()
|
|
184
|
+
obj[method] = true
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return obj
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get the token secret from the request.
|
|
192
|
+
*
|
|
193
|
+
* @param {IncomingMessage} req
|
|
194
|
+
* @param {String} sessionKey
|
|
195
|
+
* @param {Object} [cookie]
|
|
196
|
+
* @api private
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
function getSecret (req, sessionKey, cookie) {
|
|
200
|
+
// get the bag & key
|
|
201
|
+
var bag = getSecretBag(req, sessionKey, cookie)
|
|
202
|
+
var key = cookie ? cookie.key : 'csrfSecret'
|
|
203
|
+
|
|
204
|
+
if (!bag) {
|
|
205
|
+
throw new Error('misconfigured csrf')
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// return secret from bag
|
|
209
|
+
return bag[key]
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Get the token secret bag from the request.
|
|
214
|
+
*
|
|
215
|
+
* @param {IncomingMessage} req
|
|
216
|
+
* @param {String} sessionKey
|
|
217
|
+
* @param {Object} [cookie]
|
|
218
|
+
* @api private
|
|
219
|
+
*/
|
|
220
|
+
|
|
221
|
+
function getSecretBag (req, sessionKey, cookie) {
|
|
222
|
+
if (cookie) {
|
|
223
|
+
// get secret from cookie
|
|
224
|
+
var cookieKey = cookie.signed
|
|
225
|
+
? 'signedCookies'
|
|
226
|
+
: 'cookies'
|
|
227
|
+
|
|
228
|
+
return req[cookieKey]
|
|
229
|
+
} else {
|
|
230
|
+
// get secret from session
|
|
231
|
+
return req[sessionKey]
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Set a cookie on the HTTP response.
|
|
237
|
+
*
|
|
238
|
+
* @param {OutgoingMessage} res
|
|
239
|
+
* @param {string} name
|
|
240
|
+
* @param {string} val
|
|
241
|
+
* @param {Object} [options]
|
|
242
|
+
* @api private
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
function setCookie (res, name, val, options) {
|
|
246
|
+
var data = Cookie.serialize(name, val, options)
|
|
247
|
+
|
|
248
|
+
var prev = res.getHeader('set-cookie') || []
|
|
249
|
+
var header = Array.isArray(prev) ? prev.concat(data)
|
|
250
|
+
: [prev, data]
|
|
251
|
+
|
|
252
|
+
res.setHeader('set-cookie', header)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Set the token secret on the request.
|
|
257
|
+
*
|
|
258
|
+
* @param {IncomingMessage} req
|
|
259
|
+
* @param {OutgoingMessage} res
|
|
260
|
+
* @param {string} sessionKey
|
|
261
|
+
* @param {string} val
|
|
262
|
+
* @param {Object} [cookie]
|
|
263
|
+
* @api private
|
|
264
|
+
*/
|
|
265
|
+
|
|
266
|
+
function setSecret (req, res, sessionKey, val, cookie) {
|
|
267
|
+
if (cookie) {
|
|
268
|
+
// set secret on cookie
|
|
269
|
+
var value = val
|
|
270
|
+
|
|
271
|
+
if (cookie.signed) {
|
|
272
|
+
value = 's:' + sign(val, req.secret)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
setCookie(res, cookie.key, value, cookie)
|
|
276
|
+
} else {
|
|
277
|
+
// set secret on session
|
|
278
|
+
req[sessionKey].csrfSecret = val
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Verify the configuration against the request.
|
|
284
|
+
* @private
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
function verifyConfiguration (req, sessionKey, cookie) {
|
|
288
|
+
if (!getSecretBag(req, sessionKey, cookie)) {
|
|
289
|
+
return false
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (cookie && cookie.signed && !req.secret) {
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return true
|
|
297
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plotdb/csurf",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "CSRF token middleware",
|
|
4
|
+
"version": "1.11.1",
|
|
5
|
+
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Douglas Christopher Wilson <doug@somethingdoug.com>"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": "plotdb/csurf",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"cookie": "^0.7.2",
|
|
13
|
+
"cookie-signature": "1.0.6",
|
|
14
|
+
"csrf": "3.1.0",
|
|
15
|
+
"http-errors": "~1.7.3"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"body-parser": "1.19.0",
|
|
19
|
+
"connect": "3.7.0",
|
|
20
|
+
"cookie-parser": "1.4.4",
|
|
21
|
+
"cookie-session": "1.4.0",
|
|
22
|
+
"csurf": "1.11.0",
|
|
23
|
+
"eslint": "6.8.0",
|
|
24
|
+
"eslint-config-standard": "14.1.0",
|
|
25
|
+
"eslint-plugin-import": "2.20.0",
|
|
26
|
+
"eslint-plugin-markdown": "1.0.1",
|
|
27
|
+
"eslint-plugin-node": "11.0.0",
|
|
28
|
+
"eslint-plugin-promise": "4.2.1",
|
|
29
|
+
"eslint-plugin-standard": "4.0.1",
|
|
30
|
+
"mocha": "7.0.0",
|
|
31
|
+
"nyc": "15.0.0",
|
|
32
|
+
"supertest": "4.0.2"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">= 0.8.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"lint": "eslint --plugin markdown --ext js,md .",
|
|
39
|
+
"test": "mocha --check-leaks --reporter spec --bail test/",
|
|
40
|
+
"test-cov": "nyc --reporter=html --reporter=text npm test",
|
|
41
|
+
"test-travis": "nyc --reporter=text npm test"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"HISTORY.md",
|
|
45
|
+
"LICENSE",
|
|
46
|
+
"index.js"
|
|
47
|
+
],
|
|
48
|
+
"keywords": [
|
|
49
|
+
"csrf",
|
|
50
|
+
"tokens",
|
|
51
|
+
"middleware",
|
|
52
|
+
"express"
|
|
53
|
+
]
|
|
54
|
+
}
|