1
- import { doubleCsrf } from "@/index"
2
- import type { DoubleCsrfConfig } from "@/types"
3
- import type { Request , Response } from "@tinyhttp/app"
4
- import { serialize as serializeCookie } from "@tinyhttp/cookie"
5
- import { sign } from "@tinyhttp/cookie-signature"
1
+ import { serialize as serializeCookie } from "@otterhttp/cookie"
2
+ import { sign } from "@otterhttp/cookie-signature"
6
3
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
7
4
import { assert , describe , expect , it } from "vitest"
8
5
9
- import { HEADER_KEY , TEST_TOKEN } from "./utils/constants"
6
+ import { COOKIE_SECRET , HEADER_KEY , TEST_TOKEN } from "./utils/constants" ;
10
7
import { getCookieFromRequest , getCookieFromResponse , switchSecret } from "./utils/helpers"
11
8
import { generateMocks , generateMocksWithToken , next } from "./utils/mock"
9
+ import type { Request , Response } from "./utils/mock-types"
10
+
11
+ import { doubleCsrf } from "@/index"
12
+ import type { DoubleCsrfConfig } from "@/types"
12
13
13
14
type CreateTestSuite = (
14
15
name : string ,
@@ -63,7 +64,7 @@ export const createTestSuite: CreateTestSuite = (name, doubleCsrfOptions) => {
63
64
it ( "should attach both a token and its hash to the response and return a token" , ( ) => {
64
65
const { mockRequest, decodedCookieValue, setCookie } = generateMocksWithTokenInternal ( )
65
66
const cookieValue = signed
66
- ? `s:${ sign ( decodedCookieValue as string , mockRequest . secret as string ) } `
67
+ ? `s:${ sign ( decodedCookieValue as string , COOKIE_SECRET ) } `
67
68
: decodedCookieValue
68
69
69
70
const expectedSetCookieValue = serializeCookie ( cookieName , cookieValue as string , {
@@ -109,7 +110,7 @@ export const createTestSuite: CreateTestSuite = (name, doubleCsrfOptions) => {
109
110
// modify the cookie to make the token/hash pair invalid
110
111
const cookieJar = signed ? mockRequest . signedCookies : mockRequest . cookies
111
112
cookieJar [ cookieName ] = signed
112
- ? `s:${ sign ( `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash` , mockRequest . secret as string ) } `
113
+ ? `s:${ sign ( `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash` , COOKIE_SECRET ) } `
113
114
: `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash`
114
115
115
116
expect ( ( ) =>
@@ -120,7 +121,7 @@ export const createTestSuite: CreateTestSuite = (name, doubleCsrfOptions) => {
120
121
) . to . throw ( invalidCsrfTokenError . message )
121
122
122
123
// just an invalid value in the cookie
123
- cookieJar [ cookieName ] = signed ? `s:${ sign ( "invalid-value" , mockRequest . secret as string ) } ` : "invalid-value"
124
+ cookieJar [ cookieName ] = signed ? `s:${ sign ( "invalid-value" , COOKIE_SECRET ) } ` : "invalid-value"
124
125
125
126
expect ( ( ) =>
126
127
generateToken ( mockRequest , mockResponse , {
@@ -146,7 +147,7 @@ export const createTestSuite: CreateTestSuite = (name, doubleCsrfOptions) => {
146
147
// modify the cookie to make the token/hash pair invalid
147
148
const cookieJar = signed ? mockRequest . signedCookies : mockRequest . cookies
148
149
cookieJar [ cookieName ] = signed
149
- ? `s:${ sign ( `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash` , mockRequest . secret as string ) } `
150
+ ? `s:${ sign ( `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash` , COOKIE_SECRET ) } `
150
151
: `${ ( decodedCookieValue as string ) . split ( "|" ) [ 0 ] } |invalid-hash`
151
152
152
153
assert . doesNotThrow ( ( ) => {
@@ -160,7 +161,7 @@ export const createTestSuite: CreateTestSuite = (name, doubleCsrfOptions) => {
160
161
assert . notEqual ( generatedToken , csrfToken )
161
162
162
163
// just an invalid value in the cookie
163
- cookieJar [ cookieName ] = signed ? `s:${ sign ( "invalid-value" , mockRequest . secret as string ) } ` : "invalid-value"
164
+ cookieJar [ cookieName ] = signed ? `s:${ sign ( "invalid-value" , COOKIE_SECRET ) } ` : "invalid-value"
164
165
165
166
assert . doesNotThrow ( ( ) => {
166
167
generatedToken = generateToken ( mockRequest , mockResponse , {
0 commit comments