@fastify/cookie 6.0.0
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/.editorconfig +13 -0
- package/.github/dependabot.yml +13 -0
- package/.github/stale.yml +21 -0
- package/.github/workflows/ci.yml +49 -0
- package/.taprc +3 -0
- package/LICENSE +21 -0
- package/README.md +206 -0
- package/cookie.js +225 -0
- package/package.json +62 -0
- package/plugin.d.ts +116 -0
- package/plugin.js +110 -0
- package/signer.js +32 -0
- package/test/cookie-module.test.js +205 -0
- package/test/cookie.test.js +703 -0
- package/test/plugin.test-d.ts +191 -0
- package/test/signer.test.js +79 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: "github-actions"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "monthly"
|
|
7
|
+
open-pull-requests-limit: 10
|
|
8
|
+
|
|
9
|
+
- package-ecosystem: "npm"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "weekly"
|
|
13
|
+
open-pull-requests-limit: 10
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Number of days of inactivity before an issue becomes stale
|
|
2
|
+
daysUntilStale: 15
|
|
3
|
+
# Number of days of inactivity before a stale issue is closed
|
|
4
|
+
daysUntilClose: 7
|
|
5
|
+
# Issues with these labels will never be considered stale
|
|
6
|
+
exemptLabels:
|
|
7
|
+
- "discussion"
|
|
8
|
+
- "feature request"
|
|
9
|
+
- "bug"
|
|
10
|
+
- "help wanted"
|
|
11
|
+
- "plugin suggestion"
|
|
12
|
+
- "good first issue"
|
|
13
|
+
# Label to use when marking an issue as stale
|
|
14
|
+
staleLabel: stale
|
|
15
|
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
|
16
|
+
markComment: >
|
|
17
|
+
This issue has been automatically marked as stale because it has not had
|
|
18
|
+
recent activity. It will be closed if no further activity occurs. Thank you
|
|
19
|
+
for your contributions.
|
|
20
|
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
|
21
|
+
closeComment: false
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
'on':
|
|
3
|
+
push:
|
|
4
|
+
paths-ignore:
|
|
5
|
+
- docs/**
|
|
6
|
+
- '*.md'
|
|
7
|
+
pull_request:
|
|
8
|
+
paths-ignore:
|
|
9
|
+
- docs/**
|
|
10
|
+
- '*.md'
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ${{ matrix.os }}
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
node-version:
|
|
17
|
+
- 10
|
|
18
|
+
- 12
|
|
19
|
+
- 14
|
|
20
|
+
- 16
|
|
21
|
+
os:
|
|
22
|
+
- macos-latest
|
|
23
|
+
- ubuntu-latest
|
|
24
|
+
- windows-latest
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v3
|
|
27
|
+
- name: Use Node.js
|
|
28
|
+
uses: actions/setup-node@v3
|
|
29
|
+
with:
|
|
30
|
+
node-version: ${{ matrix.node-version }}
|
|
31
|
+
- name: Install Dependencies
|
|
32
|
+
run: |
|
|
33
|
+
npm install --ignore-scripts
|
|
34
|
+
- name: Lint
|
|
35
|
+
run: |
|
|
36
|
+
npm run lint:ci
|
|
37
|
+
- name: Run Tests
|
|
38
|
+
run: |
|
|
39
|
+
npm test
|
|
40
|
+
automerge:
|
|
41
|
+
needs: test
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
permissions:
|
|
44
|
+
pull-requests: write
|
|
45
|
+
contents: write
|
|
46
|
+
steps:
|
|
47
|
+
- uses: fastify/github-action-merge-dependabot@v3
|
|
48
|
+
with:
|
|
49
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/.taprc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Fastify
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# fastify-cookie
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/fastify-cookie)
|
|
5
|
+
[](https://snyk.io/test/github/fastify/fastify-cookie)
|
|
6
|
+
[](https://standardjs.com/)
|
|
7
|
+
|
|
8
|
+
A plugin for [Fastify](http://fastify.io/) that adds support for reading and
|
|
9
|
+
setting cookies.
|
|
10
|
+
|
|
11
|
+
This plugin's cookie parsing works via Fastify's `onRequest` hook. Therefore,
|
|
12
|
+
you should register it prior to any other `onRequest` hooks that will depend
|
|
13
|
+
upon this plugin's actions.
|
|
14
|
+
|
|
15
|
+
`fastify-cookie` [v2.x](https://github.com/fastify/fastify-cookie/tree/v2.x)
|
|
16
|
+
supports both Fastify@1 and Fastify@2.
|
|
17
|
+
`fastify-cookie` v3 only supports Fastify@2.
|
|
18
|
+
|
|
19
|
+
## Example
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const fastify = require('fastify')()
|
|
23
|
+
|
|
24
|
+
fastify.register(require('fastify-cookie'), {
|
|
25
|
+
secret: "my-secret", // for cookies signature
|
|
26
|
+
parseOptions: {} // options for parsing cookies
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
fastify.get('/', (req, reply) => {
|
|
30
|
+
const aCookieValue = req.cookies.cookieName
|
|
31
|
+
// `reply.unsignCookie()` is also available
|
|
32
|
+
const bCookie = req.unsignCookie(req.cookies.cookieSigned);
|
|
33
|
+
reply
|
|
34
|
+
.setCookie('foo', 'foo', {
|
|
35
|
+
domain: 'example.com',
|
|
36
|
+
path: '/'
|
|
37
|
+
})
|
|
38
|
+
.cookie('baz', 'baz') // alias for setCookie
|
|
39
|
+
.setCookie('bar', 'bar', {
|
|
40
|
+
path: '/',
|
|
41
|
+
signed: true
|
|
42
|
+
})
|
|
43
|
+
.send({ hello: 'world' })
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## TypeScript Example
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { FastifyCookieOptions } from 'fastify-cookie'
|
|
51
|
+
import cookie from 'fastify-cookie'
|
|
52
|
+
import fastify from 'fastify'
|
|
53
|
+
|
|
54
|
+
const app = fastify()
|
|
55
|
+
|
|
56
|
+
app.register(cookie, {
|
|
57
|
+
secret: "my-secret", // for cookies signature
|
|
58
|
+
parseOptions: {} // options for parsing cookies
|
|
59
|
+
} as FastifyCookieOptions)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Options
|
|
63
|
+
|
|
64
|
+
- `secret` (`String` | `Array` | `Object`):
|
|
65
|
+
- A `String` can be passed to use as secret to sign the cookie using [`cookie-signature`](http://npm.im/cookie-signature).
|
|
66
|
+
- An `Array` can be passed if key rotation is desired. Read more about it in [Rotating signing secret](#rotating-secret).
|
|
67
|
+
- More sophisticated cookie signing mechanisms can be implemented by supplying an `Object`. Read more about it in [Custom cookie signer](#custom-cookie-signer).
|
|
68
|
+
|
|
69
|
+
- `parseOptions`: An `Object` to pass as options to [cookie parse](https://github.com/jshttp/cookie#cookieparsestr-options).
|
|
70
|
+
|
|
71
|
+
## API
|
|
72
|
+
|
|
73
|
+
### Parsing
|
|
74
|
+
|
|
75
|
+
Cookies are parsed in the `onRequest` Fastify hook and attached to the request
|
|
76
|
+
as an object named `cookies`. Thus, if a request contains the header
|
|
77
|
+
`Cookie: foo=foo` then, within your handler, `req.cookies.foo` would equal
|
|
78
|
+
`'foo'`.
|
|
79
|
+
|
|
80
|
+
You can pass options to the [cookie parse](https://github.com/jshttp/cookie#cookieparsestr-options) by setting an object named `parseOptions` in the plugin config object.
|
|
81
|
+
|
|
82
|
+
### Sending
|
|
83
|
+
|
|
84
|
+
The method `setCookie(name, value, options)`, and its alias `cookie(name, value, options)`, are added to the `reply` object
|
|
85
|
+
via the Fastify `decorateReply` API. Thus, in a request handler,
|
|
86
|
+
`reply.setCookie('foo', 'foo', {path: '/'})` will set a cookie named `foo`
|
|
87
|
+
with a value of `'foo'` on the cookie path `/`.
|
|
88
|
+
|
|
89
|
+
+ `name`: a string name for the cookie to be set
|
|
90
|
+
+ `value`: a string value for the cookie
|
|
91
|
+
+ `options`: an options object as described in the [cookie serialize][cs] documentation
|
|
92
|
+
with a extra param "signed" for signed cookie
|
|
93
|
+
|
|
94
|
+
#### Securing the cookie
|
|
95
|
+
|
|
96
|
+
Following are _some_ of the precautions that should be taken to ensure the integrity of an application:
|
|
97
|
+
|
|
98
|
+
- It's important to use `options.httpOnly` cookies to prevent attacks like XSS.
|
|
99
|
+
- Use signed cookies (`options.signed`) to ensure they are not getting tampered with on client-side by an attacker.
|
|
100
|
+
- Use `__Host-` [Cookie Prefix](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Attributes) to avoid Cookie Tossing attacks.
|
|
101
|
+
- it's important to [use HTTPS for your website/app](https://letsencrypt.org/) to avoid a bunch of other potential security issues like [MITM](https://en.wikipedia.org/wiki/Man-in-the-middle_attack) etc.
|
|
102
|
+
|
|
103
|
+
### Clearing
|
|
104
|
+
|
|
105
|
+
The method `clearCookie(name, options)` is added to the `reply` object
|
|
106
|
+
via the Fastify `decorateReply` API. Thus, in a request handler,
|
|
107
|
+
`reply.clearCookie('foo', {path: '/'})` will clear a cookie named `foo`
|
|
108
|
+
on the cookie path `/`.
|
|
109
|
+
|
|
110
|
+
+ `name`: a string name for the cookie to be cleared
|
|
111
|
+
+ `options`: an options object as described in the [cookie serialize][cs]
|
|
112
|
+
documentation. Its optional to pass `options` object
|
|
113
|
+
|
|
114
|
+
### Manual cookie parsing
|
|
115
|
+
|
|
116
|
+
The method `parseCookie(cookieHeader)` is added to the `fastify` instance
|
|
117
|
+
via the Fastify `decorate` API. Thus, `fastify.parseCookie('sessionId=aYb4uTIhdBXC')`
|
|
118
|
+
will parse the raw cookie header and return an object `{ "sessionId": "aYb4uTIhdBXC" }`.
|
|
119
|
+
|
|
120
|
+
[cs]: https://www.npmjs.com/package/cookie#options-1
|
|
121
|
+
|
|
122
|
+
<a id="rotating-secret"></a>
|
|
123
|
+
### Rotating signing secret
|
|
124
|
+
|
|
125
|
+
Key rotation is when an encryption key is retired and replaced by generating a new cryptographic key. To implement rotation, supply an `Array` of keys to `secret` option.
|
|
126
|
+
|
|
127
|
+
**Example:**
|
|
128
|
+
```js
|
|
129
|
+
fastify.register(require('fastify-cookie'), {
|
|
130
|
+
secret: [key1, key2]
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The plugin will **always** use the first key (`key1`) to sign cookies. When parsing incoming cookies, it will iterate over the supplied array to see if any of the available keys are able to decode the given signed cookie. This ensures that any old signed cookies are still valid.
|
|
135
|
+
|
|
136
|
+
Note:
|
|
137
|
+
- Key rotation is **only** achieved by redeploying the server again with the new `secret` array.
|
|
138
|
+
- Iterating through all secrets is an expensive process, so the rotation list should contain as few keys as possible. Ideally, only the current key and the most recently retired key.
|
|
139
|
+
- Although previously signed cookies are valid even after rotation, cookies should be updated with the new key as soon as possible. See the following example for how to accomplish this.
|
|
140
|
+
|
|
141
|
+
**Example:**
|
|
142
|
+
```js
|
|
143
|
+
fastify.get('/', (req, reply) => {
|
|
144
|
+
const result = reply.unsignCookie(req.cookies.myCookie)
|
|
145
|
+
|
|
146
|
+
if (result.valid && result.renew) {
|
|
147
|
+
// Setting the same cookie again, this time plugin will sign it with a new key
|
|
148
|
+
reply.setCookie('myCookie', result.value, {
|
|
149
|
+
domain: 'example.com', // same options as before
|
|
150
|
+
path: '/',
|
|
151
|
+
signed: true
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
<a id="custom-cookie-signer"></a>
|
|
158
|
+
### Custom cookie signer
|
|
159
|
+
|
|
160
|
+
The `secret` option optionally accepts an object with `sign` and `unsign` functions. This allows for implementing a custom cookie signing mechanism. See the following example:
|
|
161
|
+
|
|
162
|
+
**Example:**
|
|
163
|
+
```js
|
|
164
|
+
fastify.register(require('fastify-cookie'), {
|
|
165
|
+
secret: {
|
|
166
|
+
sign: (value) => {
|
|
167
|
+
// sign using custom logic
|
|
168
|
+
return signedValue
|
|
169
|
+
},
|
|
170
|
+
unsign: (value) => {
|
|
171
|
+
// unsign using custom logic
|
|
172
|
+
return {
|
|
173
|
+
valid: true, // the cookie has been unsigned successfully
|
|
174
|
+
renew: false, // the cookie has been unsigned with an old secret
|
|
175
|
+
value: 'unsignedValue'
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Manual cookie unsigning
|
|
183
|
+
|
|
184
|
+
The method `unsignCookie(value)` is added to the `fastify` instance and the `reply` object
|
|
185
|
+
via the Fastify `decorate` & `decorateReply` APIs. Using it on a signed cookie will call the
|
|
186
|
+
the provided signer's (or the default signer if no custom implementation is provided) `unsign` method on the cookie.
|
|
187
|
+
|
|
188
|
+
**Example:**
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
fastify.register(require('fastify-cookie'), { secret: 'my-secret' })
|
|
192
|
+
|
|
193
|
+
fastify.get('/', (req, rep) => {
|
|
194
|
+
if (fastify.unsign(req.cookie.foo).valid === false) {
|
|
195
|
+
rep.send('cookie is invalid')
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
rep.send('cookie is valid')
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
[MIT License](http://jsumners.mit-license.org/)
|
package/cookie.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Adapted from https://github.com/jshttp/cookie
|
|
3
|
+
*
|
|
4
|
+
* (The MIT License)
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
|
|
7
|
+
* Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
10
|
+
* a copy of this software and associated documentation files (the
|
|
11
|
+
* 'Software'), to deal in the Software without restriction, including
|
|
12
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
13
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
14
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
15
|
+
* the following conditions:
|
|
16
|
+
*
|
|
17
|
+
* The above copyright notice and this permission notice shall be
|
|
18
|
+
* included in all copies or substantial portions of the Software.
|
|
19
|
+
*
|
|
20
|
+
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
22
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
23
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
24
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
25
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
26
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
'use strict'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Module exports.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
exports.parse = parse
|
|
37
|
+
exports.serialize = serialize
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Module variables.
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
const decode = decodeURIComponent
|
|
45
|
+
const encode = encodeURIComponent
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
49
|
+
*
|
|
50
|
+
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
51
|
+
* field-vchar = VCHAR / obs-text
|
|
52
|
+
* obs-text = %x80-FF
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/ // eslint-disable-line
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Parse a cookie header.
|
|
59
|
+
*
|
|
60
|
+
* Parse the given cookie header string into an object
|
|
61
|
+
* The object has the various cookies as keys(names) => values
|
|
62
|
+
*
|
|
63
|
+
* @param {string} str
|
|
64
|
+
* @param {object} [options]
|
|
65
|
+
* @return {object}
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
function parse (str, options) {
|
|
70
|
+
if (typeof str !== 'string') {
|
|
71
|
+
throw new TypeError('argument str must be a string')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const result = {}
|
|
75
|
+
const dec = (options && options.decode) || decode
|
|
76
|
+
|
|
77
|
+
let pos = 0
|
|
78
|
+
let terminatorPos = 0
|
|
79
|
+
let eqIdx = 0
|
|
80
|
+
|
|
81
|
+
while (true) {
|
|
82
|
+
if (terminatorPos === str.length) {
|
|
83
|
+
break
|
|
84
|
+
}
|
|
85
|
+
terminatorPos = str.indexOf(';', pos)
|
|
86
|
+
terminatorPos = (terminatorPos === -1) ? str.length : terminatorPos
|
|
87
|
+
eqIdx = str.indexOf('=', pos)
|
|
88
|
+
|
|
89
|
+
// skip things that don't look like key=value
|
|
90
|
+
if (eqIdx === -1 || eqIdx > terminatorPos) {
|
|
91
|
+
pos = terminatorPos + 1
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const key = str.substring(pos, eqIdx++).trim()
|
|
96
|
+
|
|
97
|
+
// only assign once
|
|
98
|
+
if (undefined === result[key]) {
|
|
99
|
+
const val = (str.charCodeAt(eqIdx) === 0x22)
|
|
100
|
+
? str.substring(eqIdx + 1, terminatorPos - 1).trim()
|
|
101
|
+
: str.substring(eqIdx, terminatorPos).trim()
|
|
102
|
+
|
|
103
|
+
result[key] = (dec !== decode || val.indexOf('%') !== -1)
|
|
104
|
+
? tryDecode(val, dec)
|
|
105
|
+
: val
|
|
106
|
+
}
|
|
107
|
+
pos = terminatorPos + 1
|
|
108
|
+
}
|
|
109
|
+
return result
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Serialize data into a cookie header.
|
|
114
|
+
*
|
|
115
|
+
* Serialize the a name value pair into a cookie string suitable for
|
|
116
|
+
* http headers. An optional options object specified cookie parameters.
|
|
117
|
+
*
|
|
118
|
+
* serialize('foo', 'bar', { httpOnly: true })
|
|
119
|
+
* => "foo=bar; httpOnly"
|
|
120
|
+
*
|
|
121
|
+
* @param {string} name
|
|
122
|
+
* @param {string} val
|
|
123
|
+
* @param {object} [options]
|
|
124
|
+
* @return {string}
|
|
125
|
+
* @public
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
function serialize (name, val, options) {
|
|
129
|
+
const opt = options || {}
|
|
130
|
+
const enc = opt.encode || encode
|
|
131
|
+
if (typeof enc !== 'function') {
|
|
132
|
+
throw new TypeError('option encode is invalid')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (!fieldContentRegExp.test(name)) {
|
|
136
|
+
throw new TypeError('argument name is invalid')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const value = enc(val)
|
|
140
|
+
if (value && !fieldContentRegExp.test(value)) {
|
|
141
|
+
throw new TypeError('argument val is invalid')
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let str = name + '=' + value
|
|
145
|
+
if (opt.maxAge != null) {
|
|
146
|
+
const maxAge = opt.maxAge - 0
|
|
147
|
+
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
148
|
+
throw new TypeError('option maxAge is invalid')
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
str += '; Max-Age=' + Math.floor(maxAge)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (opt.domain) {
|
|
155
|
+
if (!fieldContentRegExp.test(opt.domain)) {
|
|
156
|
+
throw new TypeError('option domain is invalid')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
str += '; Domain=' + opt.domain
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (opt.path) {
|
|
163
|
+
if (!fieldContentRegExp.test(opt.path)) {
|
|
164
|
+
throw new TypeError('option path is invalid')
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
str += '; Path=' + opt.path
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (opt.expires) {
|
|
171
|
+
if (typeof opt.expires.toUTCString !== 'function') {
|
|
172
|
+
throw new TypeError('option expires is invalid')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
str += '; Expires=' + opt.expires.toUTCString()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (opt.httpOnly) {
|
|
179
|
+
str += '; HttpOnly'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (opt.secure) {
|
|
183
|
+
str += '; Secure'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (opt.sameSite) {
|
|
187
|
+
const sameSite = typeof opt.sameSite === 'string'
|
|
188
|
+
? opt.sameSite.toLowerCase()
|
|
189
|
+
: opt.sameSite
|
|
190
|
+
switch (sameSite) {
|
|
191
|
+
case true:
|
|
192
|
+
str += '; SameSite=Strict'
|
|
193
|
+
break
|
|
194
|
+
case 'lax':
|
|
195
|
+
str += '; SameSite=Lax'
|
|
196
|
+
break
|
|
197
|
+
case 'strict':
|
|
198
|
+
str += '; SameSite=Strict'
|
|
199
|
+
break
|
|
200
|
+
case 'none':
|
|
201
|
+
str += '; SameSite=None'
|
|
202
|
+
break
|
|
203
|
+
default:
|
|
204
|
+
throw new TypeError('option sameSite is invalid')
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return str
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Try decoding a string using a decoding function.
|
|
213
|
+
*
|
|
214
|
+
* @param {string} str
|
|
215
|
+
* @param {function} decode
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
function tryDecode (str, decode) {
|
|
220
|
+
try {
|
|
221
|
+
return decode(str)
|
|
222
|
+
} catch (e) {
|
|
223
|
+
return str
|
|
224
|
+
}
|
|
225
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fastify/cookie",
|
|
3
|
+
"version": "6.0.0",
|
|
4
|
+
"description": "Plugin for fastify to add support for cookies",
|
|
5
|
+
"main": "plugin.js",
|
|
6
|
+
"types": "plugin.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"lint": "standard | snazzy",
|
|
9
|
+
"lint:ci": "standard",
|
|
10
|
+
"lint:fix": "standard --fix",
|
|
11
|
+
"test": "npm run unit && npm run typescript",
|
|
12
|
+
"typescript": "tsd",
|
|
13
|
+
"unit": "tap -J \"test/*.test.js\"",
|
|
14
|
+
"unit:report": "npm run unit -- --coverage-report=html",
|
|
15
|
+
"unit:verbose": "npm run unit -- -Rspec"
|
|
16
|
+
},
|
|
17
|
+
"precommit": [
|
|
18
|
+
"lint",
|
|
19
|
+
"test"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@github.com/fastify/fastify-cookie.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"fastify",
|
|
27
|
+
"cookie"
|
|
28
|
+
],
|
|
29
|
+
"author": "James Sumners <james.sumners@gmail.com>",
|
|
30
|
+
"contributors": [
|
|
31
|
+
{
|
|
32
|
+
"name": "Salman Mitha",
|
|
33
|
+
"email": "SalmanMitha@gmail.com"
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/fastify/fastify-cookie/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/fastify/fastify-cookie#readme",
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^17.0.16",
|
|
43
|
+
"fastify": "^3.27.1",
|
|
44
|
+
"pre-commit": "^1.2.2",
|
|
45
|
+
"sinon": "^13.0.1",
|
|
46
|
+
"snazzy": "^9.0.0",
|
|
47
|
+
"standard": "^17.0.0",
|
|
48
|
+
"tap": "^16.0.0",
|
|
49
|
+
"tsd": "^0.20.0",
|
|
50
|
+
"typescript": "^4.5.5"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"cookie-signature": "^1.1.0",
|
|
54
|
+
"fastify-plugin": "^3.0.1"
|
|
55
|
+
},
|
|
56
|
+
"tsd": {
|
|
57
|
+
"directory": "test"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|