@fastify/cookie 11.0.0 → 11.0.2
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/.github/workflows/ci.yml +1 -1
- package/README.md +31 -28
- package/benchmark/cookie-multi.js +1 -1
- package/benchmark/cookie.js +1 -1
- package/eslint.config.js +6 -0
- package/package.json +30 -9
- package/plugin.js +2 -2
- package/signer.js +1 -1
- package/test/cookie.test.js +504 -536
- package/test/signer.test.js +51 -64
- package/types/plugin.d.ts +21 -26
- package/types/plugin.test-d.ts +120 -120
- package/.taprc +0 -2
package/test/signer.test.js
CHANGED
|
@@ -1,138 +1,125 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { beforeEach, describe, test } = require('node:test')
|
|
4
4
|
const sinon = require('sinon')
|
|
5
5
|
const crypto = require('node:crypto')
|
|
6
|
-
const { Signer, sign, unsign } = require('../signer')
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
t.plan(5)
|
|
7
|
+
const { Signer, sign, unsign } = require('../signer')
|
|
10
8
|
|
|
9
|
+
describe('default', () => {
|
|
11
10
|
const secret = 'my-secret'
|
|
12
11
|
const signer = Signer(secret)
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
test('signer.sign should throw if there is no value provided', (t) => {
|
|
15
14
|
t.plan(1)
|
|
16
|
-
|
|
17
|
-
t.throws(() => signer.sign(undefined), 'Cookie value must be provided as a string.')
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
t.test('signer.sign', (t) => {
|
|
21
|
-
t.plan(2)
|
|
22
|
-
|
|
23
|
-
const input = 'some-value'
|
|
24
|
-
const result = signer.sign(input)
|
|
25
|
-
|
|
26
|
-
t.equal(result, sign(input, secret))
|
|
27
|
-
t.throws(() => sign(undefined), 'Cookie value must be provided as a string.')
|
|
15
|
+
t.assert.throws(() => signer.sign(undefined), err => err.message === 'Cookie value must be provided as a string.')
|
|
28
16
|
})
|
|
29
17
|
|
|
30
|
-
|
|
18
|
+
test('sign', (t) => {
|
|
31
19
|
t.plan(5)
|
|
32
20
|
|
|
33
21
|
const input = 'some-value'
|
|
34
22
|
const result = signer.sign(input)
|
|
35
23
|
|
|
36
|
-
t.
|
|
37
|
-
t.
|
|
38
|
-
t.
|
|
39
|
-
t.
|
|
24
|
+
t.assert.strictEqual(result, sign(input, secret))
|
|
25
|
+
t.assert.strictEqual(result, sign(input, [secret]))
|
|
26
|
+
t.assert.strictEqual(result, sign(input, Buffer.from(secret)))
|
|
27
|
+
t.assert.strictEqual(result, sign(input, [Buffer.from(secret)]))
|
|
40
28
|
|
|
41
|
-
t.throws(() => sign(undefined), '
|
|
29
|
+
t.assert.throws(() => sign(undefined), err => err.message === 'Secret key must be a string or Buffer.')
|
|
42
30
|
})
|
|
43
31
|
|
|
44
|
-
|
|
32
|
+
test('signer.unsign', (t) => {
|
|
45
33
|
t.plan(4)
|
|
46
34
|
|
|
47
35
|
const input = signer.sign('some-value', secret)
|
|
48
36
|
const result = signer.unsign(input)
|
|
49
37
|
|
|
50
|
-
t.
|
|
51
|
-
t.
|
|
52
|
-
t.
|
|
53
|
-
t.throws(() => signer.unsign(undefined), 'Signed cookie string must be provided.')
|
|
38
|
+
t.assert.strictEqual(result.valid, true)
|
|
39
|
+
t.assert.strictEqual(result.renew, false)
|
|
40
|
+
t.assert.strictEqual(result.value, 'some-value')
|
|
41
|
+
t.assert.throws(() => signer.unsign(undefined), err => err.message === 'Signed cookie string must be provided.')
|
|
54
42
|
})
|
|
55
43
|
|
|
56
|
-
|
|
44
|
+
test('unsign', (t) => {
|
|
57
45
|
t.plan(6)
|
|
58
46
|
|
|
59
47
|
const input = sign('some-value', secret)
|
|
60
48
|
const result = unsign(input, secret)
|
|
61
49
|
|
|
62
|
-
t.
|
|
63
|
-
t.
|
|
64
|
-
t.
|
|
65
|
-
t.
|
|
66
|
-
t.throws(() => unsign(undefined), 'Secret key must be a string or Buffer.')
|
|
67
|
-
t.throws(() => unsign(undefined, secret), 'Signed cookie string must be provided.')
|
|
50
|
+
t.assert.strictEqual(result.valid, true)
|
|
51
|
+
t.assert.strictEqual(result.renew, false)
|
|
52
|
+
t.assert.strictEqual(result.value, 'some-value')
|
|
53
|
+
t.assert.deepStrictEqual(result, unsign(input, [secret]))
|
|
54
|
+
t.assert.throws(() => unsign(undefined), err => err.message === 'Secret key must be a string or Buffer.')
|
|
55
|
+
t.assert.throws(() => unsign(undefined, secret), err => err.message === 'Signed cookie string must be provided.')
|
|
68
56
|
})
|
|
69
57
|
})
|
|
70
58
|
|
|
71
|
-
|
|
72
|
-
t.plan(3)
|
|
59
|
+
describe('key rotation', () => {
|
|
73
60
|
const secret1 = 'my-secret-1'
|
|
74
61
|
const secret2 = 'my-secret-2'
|
|
75
62
|
const secret3 = 'my-secret-3'
|
|
76
63
|
const signer = Signer([secret1, secret2, secret3])
|
|
77
64
|
const signSpy = sinon.spy(crypto, 'createHmac')
|
|
78
65
|
|
|
79
|
-
|
|
66
|
+
beforeEach(() => {
|
|
80
67
|
signSpy.resetHistory()
|
|
81
68
|
})
|
|
82
69
|
|
|
83
|
-
|
|
70
|
+
test('signer.sign always signs using first key', (t) => {
|
|
84
71
|
t.plan(1)
|
|
85
72
|
|
|
86
73
|
const input = 'some-value'
|
|
87
74
|
const result = signer.sign(input)
|
|
88
75
|
|
|
89
|
-
t.
|
|
76
|
+
t.assert.strictEqual(result, sign(input, secret1))
|
|
90
77
|
})
|
|
91
78
|
|
|
92
|
-
|
|
79
|
+
test('signer.unsign tries to decode using all keys till it finds', (t) => {
|
|
93
80
|
t.plan(4)
|
|
94
81
|
|
|
95
82
|
const input = sign('some-value', secret2)
|
|
96
83
|
signSpy.resetHistory()
|
|
97
84
|
const result = signer.unsign(input)
|
|
98
85
|
|
|
99
|
-
t.
|
|
100
|
-
t.
|
|
101
|
-
t.
|
|
102
|
-
t.
|
|
86
|
+
t.assert.strictEqual(result.valid, true)
|
|
87
|
+
t.assert.strictEqual(result.renew, true)
|
|
88
|
+
t.assert.strictEqual(result.value, 'some-value')
|
|
89
|
+
t.assert.strictEqual(signSpy.callCount, 2) // should have returned early when the right key was found
|
|
103
90
|
})
|
|
104
91
|
|
|
105
|
-
|
|
92
|
+
test('signer.unsign failure response', (t) => {
|
|
106
93
|
t.plan(4)
|
|
107
94
|
|
|
108
95
|
const input = sign('some-value', 'invalid-secret')
|
|
109
96
|
signSpy.resetHistory()
|
|
110
97
|
const result = signer.unsign(input)
|
|
111
98
|
|
|
112
|
-
t.
|
|
113
|
-
t.
|
|
114
|
-
t.
|
|
115
|
-
t.
|
|
99
|
+
t.assert.strictEqual(result.valid, false)
|
|
100
|
+
t.assert.strictEqual(result.renew, false)
|
|
101
|
+
t.assert.strictEqual(result.value, null)
|
|
102
|
+
t.assert.strictEqual(signSpy.callCount, 3) // should have tried all 3
|
|
116
103
|
})
|
|
117
104
|
})
|
|
118
105
|
|
|
119
|
-
|
|
120
|
-
t
|
|
121
|
-
|
|
122
|
-
t.test('Signer needs a string or Buffer as secret', (t) => {
|
|
106
|
+
describe('Signer', () => {
|
|
107
|
+
test('Signer needs a string or Buffer as secret', (t) => {
|
|
123
108
|
t.plan(6)
|
|
124
|
-
|
|
125
|
-
t.throws(() => Signer(
|
|
126
|
-
t.
|
|
127
|
-
t.doesNotThrow(() => Signer(
|
|
128
|
-
t.doesNotThrow(() => Signer(
|
|
129
|
-
t.doesNotThrow(() => Signer(
|
|
109
|
+
|
|
110
|
+
t.assert.throws(() => Signer(1), err => err.message === 'Secret key must be a string or Buffer.')
|
|
111
|
+
t.assert.throws(() => Signer(undefined), err => err.message === 'Secret key must be a string or Buffer.')
|
|
112
|
+
t.assert.doesNotThrow(() => Signer('secret'))
|
|
113
|
+
t.assert.doesNotThrow(() => Signer(['secret']))
|
|
114
|
+
t.assert.doesNotThrow(() => Signer(Buffer.from('deadbeef76543210', 'hex')))
|
|
115
|
+
t.assert.doesNotThrow(() => Signer([Buffer.from('deadbeef76543210', 'hex')]))
|
|
130
116
|
})
|
|
131
117
|
|
|
132
|
-
|
|
118
|
+
test('Signer handles algorithm properly', (t) => {
|
|
133
119
|
t.plan(3)
|
|
134
|
-
|
|
135
|
-
t.
|
|
136
|
-
t.doesNotThrow(() => Signer('secret', '
|
|
120
|
+
|
|
121
|
+
t.assert.throws(() => Signer('secret', 'invalid'), err => err.message === 'Algorithm invalid not supported.')
|
|
122
|
+
t.assert.doesNotThrow(() => Signer('secret', 'sha512'))
|
|
123
|
+
t.assert.doesNotThrow(() => Signer('secret', 'sha256'))
|
|
137
124
|
})
|
|
138
125
|
})
|
package/types/plugin.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/// <reference types='node' />
|
|
2
2
|
|
|
3
|
-
import { FastifyPluginCallback } from
|
|
3
|
+
import { FastifyPluginCallback } from 'fastify'
|
|
4
4
|
|
|
5
|
-
declare module
|
|
5
|
+
declare module 'fastify' {
|
|
6
6
|
interface FastifyInstance extends SignerMethods {
|
|
7
7
|
/**
|
|
8
8
|
* Serialize a cookie name-value pair into a Set-Cookie header string
|
|
@@ -42,7 +42,7 @@ declare module "fastify" {
|
|
|
42
42
|
* Signs the specified cookie using the secret/signer provided.
|
|
43
43
|
* @param value cookie value
|
|
44
44
|
*/
|
|
45
|
-
|
|
45
|
+
signCookie(value: string): string;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Unsigns the specified cookie using the secret/signer provided.
|
|
@@ -55,7 +55,7 @@ declare module "fastify" {
|
|
|
55
55
|
name: string,
|
|
56
56
|
value: string,
|
|
57
57
|
options?: fastifyCookie.CookieSerializeOptions
|
|
58
|
-
) => FastifyReply
|
|
58
|
+
) => FastifyReply
|
|
59
59
|
|
|
60
60
|
interface FastifyReply {
|
|
61
61
|
/**
|
|
@@ -96,7 +96,7 @@ declare module "fastify" {
|
|
|
96
96
|
|
|
97
97
|
type FastifyCookiePlugin = FastifyPluginCallback<
|
|
98
98
|
NonNullable<fastifyCookie.FastifyCookieOptions>
|
|
99
|
-
|
|
99
|
+
>
|
|
100
100
|
|
|
101
101
|
declare namespace fastifyCookie {
|
|
102
102
|
interface SignerBase {
|
|
@@ -106,8 +106,8 @@ declare namespace fastifyCookie {
|
|
|
106
106
|
|
|
107
107
|
export class Signer implements SignerBase {
|
|
108
108
|
constructor (secrets: string | Array<string> | Buffer | Array<Buffer>, algorithm?: string)
|
|
109
|
-
sign: (value: string) => string
|
|
110
|
-
unsign: (input: string) => UnsignResult
|
|
109
|
+
sign: (value: string) => string
|
|
110
|
+
unsign: (input: string) => UnsignResult
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
export interface SerializeOptions {
|
|
@@ -143,17 +143,11 @@ declare namespace fastifyCookie {
|
|
|
143
143
|
decode?: (encodedURIComponent: string) => string;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
type HookType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler'
|
|
146
|
+
type HookType = 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization'
|
|
147
147
|
|
|
148
|
-
export
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
parseOptions?: fastifyCookie.CookieSerializeOptions;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export type Sign = (value: string, secret: string | Buffer, algorithm?: string) => string;
|
|
155
|
-
export type Unsign = (input: string, secret: string | Buffer, algorithm?: string) => UnsignResult;
|
|
156
|
-
export type SignerFactory = (secrets: string | string[] | Buffer | Buffer[], algorithm?: string) => SignerBase;
|
|
148
|
+
export type Sign = (value: string, secret: string | Buffer, algorithm?: string) => string
|
|
149
|
+
export type Unsign = (input: string, secret: string | Buffer, algorithm?: string) => UnsignResult
|
|
150
|
+
export type SignerFactory = (secrets: string | string[] | Buffer | Buffer[], algorithm?: string) => SignerBase
|
|
157
151
|
|
|
158
152
|
export type UnsignResult = {
|
|
159
153
|
valid: true;
|
|
@@ -165,9 +159,9 @@ declare namespace fastifyCookie {
|
|
|
165
159
|
value: null;
|
|
166
160
|
}
|
|
167
161
|
|
|
168
|
-
export const signerFactory: SignerFactory
|
|
169
|
-
export const sign: Sign
|
|
170
|
-
export const unsign: Unsign
|
|
162
|
+
export const signerFactory: SignerFactory
|
|
163
|
+
export const sign: Sign
|
|
164
|
+
export const unsign: Unsign
|
|
171
165
|
|
|
172
166
|
export interface FastifyCookie extends FastifyCookiePlugin {
|
|
173
167
|
parse: (cookieHeader: string, opts?: ParseOptions) => { [key: string]: string };
|
|
@@ -178,19 +172,20 @@ declare namespace fastifyCookie {
|
|
|
178
172
|
unsign: Unsign;
|
|
179
173
|
}
|
|
180
174
|
|
|
181
|
-
export const fastifyCookie: FastifyCookie
|
|
175
|
+
export const fastifyCookie: FastifyCookie
|
|
182
176
|
|
|
183
177
|
export interface FastifyCookieOptions {
|
|
184
|
-
secret?: string | string[] | Buffer | Buffer[] | SignerBase;
|
|
178
|
+
secret?: string | string[] | Buffer | Buffer[] | Signer | SignerBase;
|
|
185
179
|
algorithm?: string;
|
|
180
|
+
hook?: HookType | false;
|
|
186
181
|
parseOptions?: CookieSerializeOptions;
|
|
187
182
|
}
|
|
188
183
|
|
|
189
|
-
export { fastifyCookie as default }
|
|
184
|
+
export { fastifyCookie as default }
|
|
190
185
|
}
|
|
191
186
|
|
|
192
|
-
declare function fastifyCookie(
|
|
187
|
+
declare function fastifyCookie (
|
|
193
188
|
...params: Parameters<FastifyCookiePlugin>
|
|
194
|
-
): ReturnType<FastifyCookiePlugin
|
|
189
|
+
): ReturnType<FastifyCookiePlugin>
|
|
195
190
|
|
|
196
|
-
export = fastifyCookie
|
|
191
|
+
export = fastifyCookie
|
package/types/plugin.test-d.ts
CHANGED
|
@@ -1,67 +1,66 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
app.register(
|
|
12
|
-
app.register(
|
|
13
|
-
app.register(
|
|
14
|
-
app.register(fastifyCookieCjsImport.
|
|
15
|
-
app.register(
|
|
16
|
-
app.register(fastifyCookieStar.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
expectType<fastifyCookieStar.FastifyCookie>(
|
|
20
|
-
expectType<fastifyCookieStar.FastifyCookie>(
|
|
21
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.
|
|
22
|
-
expectType<fastifyCookieStar.FastifyCookie>(
|
|
23
|
-
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieStar.default);
|
|
1
|
+
import { expectError, expectType } from 'tsd'
|
|
2
|
+
import * as fastifyCookieStar from '..'
|
|
3
|
+
import fastifyCookieCjsImport = require('..')
|
|
4
|
+
import cookie, { fastifyCookie as fastifyCookieNamed, Signer } from '..'
|
|
5
|
+
import fastify, { FastifyInstance, FastifyReply, setCookieWrapper } from 'fastify'
|
|
6
|
+
|
|
7
|
+
const fastifyCookieCjs = require('..')
|
|
8
|
+
|
|
9
|
+
const app: FastifyInstance = fastify()
|
|
10
|
+
app.register(fastifyCookieNamed)
|
|
11
|
+
app.register(cookie)
|
|
12
|
+
app.register(fastifyCookieCjs)
|
|
13
|
+
app.register(fastifyCookieCjsImport.default)
|
|
14
|
+
app.register(fastifyCookieCjsImport.fastifyCookie)
|
|
15
|
+
app.register(fastifyCookieStar.default)
|
|
16
|
+
app.register(fastifyCookieStar.fastifyCookie)
|
|
17
|
+
|
|
18
|
+
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieNamed)
|
|
19
|
+
expectType<fastifyCookieStar.FastifyCookie>(cookie)
|
|
20
|
+
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.default)
|
|
21
|
+
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieCjsImport.fastifyCookie)
|
|
22
|
+
expectType<fastifyCookieStar.FastifyCookie>(fastifyCookieStar.default)
|
|
24
23
|
expectType<fastifyCookieStar.FastifyCookie>(
|
|
25
24
|
fastifyCookieStar.fastifyCookie
|
|
26
|
-
)
|
|
27
|
-
expectType<any>(fastifyCookieCjs)
|
|
25
|
+
)
|
|
26
|
+
expectType<any>(fastifyCookieCjs)
|
|
28
27
|
|
|
29
|
-
expectType<fastifyCookieStar.Sign>(
|
|
30
|
-
expectType<fastifyCookieStar.Unsign>(
|
|
31
|
-
expectType<fastifyCookieStar.SignerFactory >(
|
|
28
|
+
expectType<fastifyCookieStar.Sign>(cookie.sign)
|
|
29
|
+
expectType<fastifyCookieStar.Unsign>(cookie.unsign)
|
|
30
|
+
expectType<fastifyCookieStar.SignerFactory >(cookie.signerFactory)
|
|
32
31
|
|
|
33
|
-
expectType<fastifyCookieStar.Sign>(fastifyCookieNamed.sign)
|
|
34
|
-
expectType<fastifyCookieStar.Unsign>(fastifyCookieNamed.unsign)
|
|
35
|
-
expectType<fastifyCookieStar.SignerFactory >(fastifyCookieNamed.signerFactory
|
|
32
|
+
expectType<fastifyCookieStar.Sign>(fastifyCookieNamed.sign)
|
|
33
|
+
expectType<fastifyCookieStar.Unsign>(fastifyCookieNamed.unsign)
|
|
34
|
+
expectType<fastifyCookieStar.SignerFactory >(fastifyCookieNamed.signerFactory)
|
|
36
35
|
|
|
37
|
-
const server = fastify()
|
|
36
|
+
const server = fastify()
|
|
38
37
|
|
|
39
|
-
server.register(cookie)
|
|
38
|
+
server.register(cookie)
|
|
40
39
|
|
|
41
40
|
server.after((_err) => {
|
|
42
41
|
expectType< string >(
|
|
43
42
|
server.serializeCookie('sessionId', 'aYb4uTIhdBXC')
|
|
44
|
-
)
|
|
43
|
+
)
|
|
45
44
|
|
|
46
45
|
expectType<{ [key: string]: string }>(
|
|
47
46
|
// See https://github.com/fastify/fastify-cookie#manual-cookie-parsing
|
|
48
47
|
server.parseCookie('sessionId=aYb4uTIhdBXC')
|
|
49
|
-
)
|
|
48
|
+
)
|
|
50
49
|
|
|
51
50
|
server.get('/', (request, reply) => {
|
|
52
|
-
const test = request.cookies.test
|
|
53
|
-
expectType<string | undefined>(test)
|
|
51
|
+
const test = request.cookies.test
|
|
52
|
+
expectType<string | undefined>(test)
|
|
54
53
|
|
|
55
|
-
expectType<setCookieWrapper>(reply.cookie)
|
|
56
|
-
expectType<setCookieWrapper>(reply.setCookie)
|
|
54
|
+
expectType<setCookieWrapper>(reply.cookie)
|
|
55
|
+
expectType<setCookieWrapper>(reply.setCookie)
|
|
57
56
|
|
|
58
57
|
expectType<FastifyReply>(
|
|
59
58
|
reply
|
|
60
59
|
.setCookie('test', test!, { domain: 'example.com', path: '/' })
|
|
61
60
|
.clearCookie('foo')
|
|
62
61
|
.send({ hello: 'world' })
|
|
63
|
-
)
|
|
64
|
-
})
|
|
62
|
+
)
|
|
63
|
+
})
|
|
65
64
|
|
|
66
65
|
expectType<(value: string) => string>(server.signCookie)
|
|
67
66
|
expectType<(value: string) => fastifyCookieStar.UnsignResult>(server.unsignCookie)
|
|
@@ -71,100 +70,100 @@ server.after((_err) => {
|
|
|
71
70
|
expectType<(value: string) => string>(reply.signCookie)
|
|
72
71
|
expectType<(value: string) => fastifyCookieStar.UnsignResult>(request.unsignCookie)
|
|
73
72
|
expectType<(value: string) => fastifyCookieStar.UnsignResult>(reply.unsignCookie)
|
|
74
|
-
})
|
|
75
|
-
})
|
|
73
|
+
})
|
|
74
|
+
})
|
|
76
75
|
|
|
77
|
-
const serverWithHttp2 = fastify({ http2: true })
|
|
76
|
+
const serverWithHttp2 = fastify({ http2: true })
|
|
78
77
|
|
|
79
|
-
serverWithHttp2.register(cookie)
|
|
78
|
+
serverWithHttp2.register(cookie)
|
|
80
79
|
|
|
81
80
|
serverWithHttp2.after(() => {
|
|
82
81
|
serverWithHttp2.get('/', (request, reply) => {
|
|
83
|
-
const test = request.cookies.test
|
|
82
|
+
const test = request.cookies.test
|
|
84
83
|
reply
|
|
85
84
|
.setCookie('test', test!, { domain: 'example.com', path: '/' })
|
|
86
85
|
.clearCookie('foo')
|
|
87
|
-
.send({ hello: 'world' })
|
|
88
|
-
})
|
|
89
|
-
})
|
|
86
|
+
.send({ hello: 'world' })
|
|
87
|
+
})
|
|
88
|
+
})
|
|
90
89
|
|
|
91
|
-
const testSamesiteOptionsApp = fastify()
|
|
90
|
+
const testSamesiteOptionsApp = fastify()
|
|
92
91
|
|
|
93
|
-
testSamesiteOptionsApp.register(cookie)
|
|
92
|
+
testSamesiteOptionsApp.register(cookie)
|
|
94
93
|
testSamesiteOptionsApp.after(() => {
|
|
95
94
|
server.get('/test-samesite-option-true', (request, reply) => {
|
|
96
|
-
const test = request.cookies.test
|
|
97
|
-
reply.setCookie('test', test!, { sameSite: true }).send({ hello: 'world' })
|
|
98
|
-
})
|
|
95
|
+
const test = request.cookies.test
|
|
96
|
+
reply.setCookie('test', test!, { sameSite: true }).send({ hello: 'world' })
|
|
97
|
+
})
|
|
99
98
|
server.get('/test-samesite-option-false', (request, reply) => {
|
|
100
|
-
const test = request.cookies.test
|
|
101
|
-
reply.setCookie('test', test!, { sameSite: false }).send({ hello: 'world' })
|
|
102
|
-
})
|
|
99
|
+
const test = request.cookies.test
|
|
100
|
+
reply.setCookie('test', test!, { sameSite: false }).send({ hello: 'world' })
|
|
101
|
+
})
|
|
103
102
|
server.get('/test-samesite-option-lax', (request, reply) => {
|
|
104
|
-
const test = request.cookies.test
|
|
105
|
-
reply.setCookie('test', test!, { sameSite: 'lax' }).send({ hello: 'world' })
|
|
106
|
-
})
|
|
103
|
+
const test = request.cookies.test
|
|
104
|
+
reply.setCookie('test', test!, { sameSite: 'lax' }).send({ hello: 'world' })
|
|
105
|
+
})
|
|
107
106
|
server.get('/test-samesite-option-strict', (request, reply) => {
|
|
108
|
-
const test = request.cookies.test
|
|
107
|
+
const test = request.cookies.test
|
|
109
108
|
reply
|
|
110
109
|
.setCookie('test', test!, { sameSite: 'strict' })
|
|
111
|
-
.send({ hello: 'world' })
|
|
112
|
-
})
|
|
110
|
+
.send({ hello: 'world' })
|
|
111
|
+
})
|
|
113
112
|
server.get('/test-samesite-option-none', (request, reply) => {
|
|
114
|
-
const test = request.cookies.test
|
|
113
|
+
const test = request.cookies.test
|
|
115
114
|
reply
|
|
116
115
|
.setCookie('test', test!, { sameSite: 'none' })
|
|
117
|
-
.send({ hello: 'world' })
|
|
118
|
-
})
|
|
119
|
-
})
|
|
116
|
+
.send({ hello: 'world' })
|
|
117
|
+
})
|
|
118
|
+
})
|
|
120
119
|
|
|
121
|
-
const appWithImplicitHttpSigned = fastify()
|
|
120
|
+
const appWithImplicitHttpSigned = fastify()
|
|
122
121
|
|
|
123
122
|
appWithImplicitHttpSigned.register(cookie, {
|
|
124
123
|
secret: 'testsecret',
|
|
125
|
-
})
|
|
124
|
+
})
|
|
126
125
|
appWithImplicitHttpSigned.register(cookie, {
|
|
127
126
|
secret: 'testsecret',
|
|
128
127
|
algorithm: 'sha512'
|
|
129
|
-
})
|
|
128
|
+
})
|
|
130
129
|
appWithImplicitHttpSigned.after(() => {
|
|
131
130
|
server.get('/', (request, reply) => {
|
|
132
|
-
appWithImplicitHttpSigned.unsignCookie(request.cookies.test!)
|
|
133
|
-
appWithImplicitHttpSigned.unsignCookie('test')
|
|
131
|
+
appWithImplicitHttpSigned.unsignCookie(request.cookies.test!)
|
|
132
|
+
appWithImplicitHttpSigned.unsignCookie('test')
|
|
134
133
|
|
|
135
|
-
reply.unsignCookie(request.cookies.test!)
|
|
136
|
-
reply.unsignCookie('test')
|
|
134
|
+
reply.unsignCookie(request.cookies.test!)
|
|
135
|
+
reply.unsignCookie('test')
|
|
137
136
|
|
|
138
|
-
request.unsignCookie(request.cookies.anotherTest!)
|
|
139
|
-
request.unsignCookie('anotherTest')
|
|
137
|
+
request.unsignCookie(request.cookies.anotherTest!)
|
|
138
|
+
request.unsignCookie('anotherTest')
|
|
140
139
|
|
|
141
|
-
reply.send({ hello: 'world' })
|
|
142
|
-
})
|
|
143
|
-
})
|
|
140
|
+
reply.send({ hello: 'world' })
|
|
141
|
+
})
|
|
142
|
+
})
|
|
144
143
|
|
|
145
|
-
const appWithRotationSecret = fastify()
|
|
144
|
+
const appWithRotationSecret = fastify()
|
|
146
145
|
|
|
147
146
|
appWithRotationSecret.register(cookie, {
|
|
148
147
|
secret: ['testsecret'],
|
|
149
|
-
})
|
|
148
|
+
})
|
|
150
149
|
appWithRotationSecret.after(() => {
|
|
151
150
|
server.get('/', (request, reply) => {
|
|
152
|
-
reply.unsignCookie(request.cookies.test!)
|
|
153
|
-
const unsigned = reply.unsignCookie('test')
|
|
151
|
+
reply.unsignCookie(request.cookies.test!)
|
|
152
|
+
const unsigned = reply.unsignCookie('test')
|
|
154
153
|
|
|
155
|
-
expectType<boolean>(unsigned.valid)
|
|
154
|
+
expectType<boolean>(unsigned.valid)
|
|
156
155
|
if (unsigned.valid) {
|
|
157
|
-
expectType<string>(unsigned.value)
|
|
156
|
+
expectType<string>(unsigned.value)
|
|
158
157
|
} else {
|
|
159
|
-
expectType<null>(unsigned.value)
|
|
158
|
+
expectType<null>(unsigned.value)
|
|
160
159
|
}
|
|
161
|
-
expectType<boolean>(unsigned.renew)
|
|
160
|
+
expectType<boolean>(unsigned.renew)
|
|
162
161
|
|
|
163
|
-
reply.send({ hello: 'world' })
|
|
164
|
-
})
|
|
165
|
-
})
|
|
162
|
+
reply.send({ hello: 'world' })
|
|
163
|
+
})
|
|
164
|
+
})
|
|
166
165
|
|
|
167
|
-
const appWithParseOptions = fastify()
|
|
166
|
+
const appWithParseOptions = fastify()
|
|
168
167
|
|
|
169
168
|
const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
|
|
170
169
|
domain: 'example.com',
|
|
@@ -177,26 +176,26 @@ const parseOptions: fastifyCookieStar.CookieSerializeOptions = {
|
|
|
177
176
|
secure: true,
|
|
178
177
|
signed: true,
|
|
179
178
|
partitioned: false,
|
|
180
|
-
}
|
|
181
|
-
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions)
|
|
179
|
+
}
|
|
180
|
+
expectType<fastifyCookieStar.CookieSerializeOptions>(parseOptions)
|
|
182
181
|
|
|
183
182
|
appWithParseOptions.register(cookie, {
|
|
184
183
|
secret: 'testsecret',
|
|
185
184
|
parseOptions,
|
|
186
|
-
})
|
|
185
|
+
})
|
|
187
186
|
appWithParseOptions.after(() => {
|
|
188
187
|
server.get('/', (request, reply) => {
|
|
189
|
-
const unsigned = reply.unsignCookie(request.cookies.test!)
|
|
188
|
+
const unsigned = reply.unsignCookie(request.cookies.test!)
|
|
190
189
|
|
|
191
|
-
expectType<boolean>(unsigned.valid)
|
|
190
|
+
expectType<boolean>(unsigned.valid)
|
|
192
191
|
if (unsigned.valid) {
|
|
193
|
-
expectType<string>(unsigned.value)
|
|
192
|
+
expectType<string>(unsigned.value)
|
|
194
193
|
} else {
|
|
195
|
-
expectType<null>(unsigned.value)
|
|
194
|
+
expectType<null>(unsigned.value)
|
|
196
195
|
}
|
|
197
|
-
expectType<boolean>(unsigned.renew)
|
|
198
|
-
})
|
|
199
|
-
})
|
|
196
|
+
expectType<boolean>(unsigned.renew)
|
|
197
|
+
})
|
|
198
|
+
})
|
|
200
199
|
|
|
201
200
|
const appWithCustomSigner = fastify()
|
|
202
201
|
|
|
@@ -214,36 +213,37 @@ appWithCustomSigner.after(() => {
|
|
|
214
213
|
reply.unsignCookie(request.cookies.test!)
|
|
215
214
|
const unsigned = reply.unsignCookie('test')
|
|
216
215
|
|
|
217
|
-
expectType<boolean>(unsigned.valid)
|
|
216
|
+
expectType<boolean>(unsigned.valid)
|
|
218
217
|
if (unsigned.valid) {
|
|
219
|
-
expectType<string>(unsigned.value)
|
|
218
|
+
expectType<string>(unsigned.value)
|
|
220
219
|
} else {
|
|
221
|
-
expectType<null>(unsigned.value)
|
|
220
|
+
expectType<null>(unsigned.value)
|
|
222
221
|
}
|
|
223
|
-
expectType<boolean>(unsigned.renew)
|
|
222
|
+
expectType<boolean>(unsigned.renew)
|
|
224
223
|
|
|
225
224
|
reply.send({ hello: 'world' })
|
|
226
225
|
})
|
|
227
226
|
})
|
|
228
227
|
|
|
229
|
-
new fastifyCookieStar.Signer('secretString')
|
|
230
|
-
new fastifyCookieStar.Signer(['secretStringInArray'])
|
|
231
|
-
new fastifyCookieStar.Signer(Buffer.from('secretString'))
|
|
232
|
-
new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')])
|
|
228
|
+
expectType<Signer>(new fastifyCookieStar.Signer('secretString'))
|
|
229
|
+
expectType<Signer>(new fastifyCookieStar.Signer(['secretStringInArray']))
|
|
230
|
+
expectType<Signer>(new fastifyCookieStar.Signer(Buffer.from('secretString')))
|
|
231
|
+
expectType<Signer>(new fastifyCookieStar.Signer([Buffer.from('secretStringInArray')]))
|
|
232
|
+
|
|
233
233
|
const signer = new fastifyCookieStar.Signer(['secretStringInArray'], 'sha256')
|
|
234
234
|
signer.sign('Lorem Ipsum')
|
|
235
235
|
signer.unsign('Lorem Ipsum')
|
|
236
236
|
|
|
237
|
-
const appWithHook: FastifyInstance = fastify()
|
|
237
|
+
const appWithHook: FastifyInstance = fastify()
|
|
238
238
|
|
|
239
|
-
appWithHook.register(cookie, { hook: false })
|
|
240
|
-
appWithHook.register(cookie, { hook: 'onRequest' })
|
|
241
|
-
appWithHook.register(cookie, { hook: 'preHandler' })
|
|
242
|
-
appWithHook.register(cookie, { hook: 'preParsing' })
|
|
243
|
-
appWithHook.register(cookie, { hook: 'preSerialization' })
|
|
244
|
-
appWithHook.register(cookie, { hook: 'preValidation' })
|
|
245
|
-
expectError(appWithHook.register(cookie, { hook: true }))
|
|
246
|
-
expectError(appWithHook.register(cookie, { hook: 'false' }))
|
|
239
|
+
appWithHook.register(cookie, { hook: false })
|
|
240
|
+
appWithHook.register(cookie, { hook: 'onRequest' })
|
|
241
|
+
appWithHook.register(cookie, { hook: 'preHandler' })
|
|
242
|
+
appWithHook.register(cookie, { hook: 'preParsing' })
|
|
243
|
+
appWithHook.register(cookie, { hook: 'preSerialization' })
|
|
244
|
+
appWithHook.register(cookie, { hook: 'preValidation' })
|
|
245
|
+
expectError(appWithHook.register(cookie, { hook: true }))
|
|
246
|
+
expectError(appWithHook.register(cookie, { hook: 'false' }))
|
|
247
247
|
|
|
248
|
-
expectType<(cookieHeader: string, opts?: fastifyCookieStar.ParseOptions) => { [key: string]: string }>(
|
|
249
|
-
expectType<(name: string, value: string, opts?: fastifyCookieStar.SerializeOptions) => string>(
|
|
248
|
+
expectType<(cookieHeader: string, opts?: fastifyCookieStar.ParseOptions) => { [key: string]: string }>(cookie.parse)
|
|
249
|
+
expectType<(name: string, value: string, opts?: fastifyCookieStar.SerializeOptions) => string>(cookie.serialize)
|
package/.taprc
DELETED