From bf2d032321fa16ae9f611d92bf41906cc6a2c55d Mon Sep 17 00:00:00 2001 From: James Sumners <321201+jsumners@users.noreply.github.com> Date: Fri, 5 Jul 2024 06:57:03 -0400 Subject: [PATCH] Merge `next` into `master` (#156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update for Fastify v5 (#154) * Fix next branch tests (#157) * Disable tsd * patch accordingly --------- Co-authored-by: Aras Abbasi * Update package.json Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> * Disable tsd * update fastify-error * update deps * update fastify deps --------- Signed-off-by: James Sumners <321201+jsumners@users.noreply.github.com> Co-authored-by: Gürgün Dayıoğlu Co-authored-by: Aras Abbasi --- .github/workflows/ci.yml | 2 +- package.json | 26 ++++----- test/user-info.test.js | 110 ++++++++++++++++++++++++++------------- types/index.test-d.ts | 7 ++- 4 files changed, 93 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0610c55..582175c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ on: jobs: test: - uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 + uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.1.0 with: lint: true license-check: true diff --git a/package.json b/package.json index 8c0214d..92b2852 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "types": "types/index.d.ts", "scripts": { "lint": "standard", - "test": "npm run test:unit && npm run test:typescript", + "test": "npm run test:unit", "test:unit": "tap", "test:typescript": "tsd" }, @@ -29,21 +29,21 @@ }, "homepage": "https://github.com/fastify/fastify-csrf#readme", "dependencies": { - "@fastify/csrf": "^6.0.0", - "@fastify/error": "^3.0.0", - "fastify-plugin": "^4.0.0" + "@fastify/csrf": "^7.0.0-pre.fv5.1", + "@fastify/error": "^4.0.0", + "fastify-plugin": "^5.0.0-pre.fv5.1" }, "devDependencies": { - "@fastify/cookie": "^9.0.4", - "@fastify/pre-commit": "^2.0.2", - "@fastify/secure-session": "^7.0.0", - "@fastify/session": "^10.3.0", - "@types/node": "^20.1.0", - "fastify": "^4.2.0", + "@fastify/cookie": "^10.0.0-pre.fv5.1", + "@fastify/pre-commit": "^2.1.0", + "@fastify/secure-session": "^8.0.0-pre.fv5.1", + "@fastify/session": "^11.0.0-pre.fv5.1", + "@types/node": "^20.12.7", + "fastify": "^5.0.0-alpha.3", "proxyquire": "^2.1.3", - "sinon": "^17.0.0", - "standard": "^17.0.0", - "tap": "^16.0.0", + "sinon": "^17.0.1", + "standard": "^17.1.0", + "tap": "^18.7.2", "tsd": "^0.31.0" }, "pre-commit": [ diff --git a/test/user-info.test.js b/test/user-info.test.js index 269ca80..d1f8171 100644 --- a/test/user-info.test.js +++ b/test/user-info.test.js @@ -177,10 +177,16 @@ test('Validate presence of hmac key with User-Info /1', async (t) => { const fastify = Fastify() await fastify.register(fastifyCookie) - await t.rejects(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - } + await t.rejects(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + } + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) }), Error('csrfOpts.hmacKey is required')) }) @@ -188,11 +194,17 @@ test('Validate presence of hmac key with User-Info /2', async (t) => { const fastify = Fastify() await fastify.register(fastifyCookie) - await t.rejects(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - }, - sessionPlugin: '@fastify/cookie' + await t.rejects(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + }, + sessionPlugin: '@fastify/cookie' + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) }), Error('csrfOpts.hmacKey is required')) }) @@ -200,13 +212,19 @@ test('Validate presence of hmac key with User-Info /3', async (t) => { const fastify = Fastify() await fastify.register(fastifyCookie) - await t.rejects(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - }, - csrfOpts: { - hmacKey: undefined - } + await t.rejects(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + }, + csrfOpts: { + hmacKey: undefined + } + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) }), Error('csrfOpts.hmacKey is required')) }) @@ -214,14 +232,20 @@ test('Validate presence of hmac key with User-Info /4', async (t) => { const fastify = Fastify() await fastify.register(fastifyCookie) - await t.rejects(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - }, - sessionPlugin: '@fastify/cookie', - csrfOpts: { - hmacKey: undefined - } + await t.rejects(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + }, + sessionPlugin: '@fastify/cookie', + csrfOpts: { + hmacKey: undefined + } + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) }), Error('csrfOpts.hmacKey is required')) }) @@ -229,11 +253,17 @@ test('Validate presence of hmac key with User-Info /5', async (t) => { const fastify = Fastify() await fastify.register(fastifySecureSession, { key, cookie: { path: '/', secure: false } }) - await t.resolves(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - }, - sessionPlugin: '@fastify/secure-session' + await t.resolves(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + }, + sessionPlugin: '@fastify/secure-session' + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) })) }) @@ -241,13 +271,19 @@ test('Validate presence of hmac key with User-Info /6', async (t) => { const fastify = Fastify() await fastify.register(fastifySecureSession, { key, cookie: { path: '/', secure: false } }) - await t.resolves(fastify.register(fastifyCsrf, { - getUserInfo (req) { - return req.session.get('username') - }, - sessionPlugin: '@fastify/secure-session', - csrfOpts: { - hmacKey: 'foo' - } + await t.resolves(new Promise((resolve, reject) => { + fastify.register(fastifyCsrf, { + getUserInfo (req) { + return req.session.get('username') + }, + sessionPlugin: '@fastify/secure-session', + csrfOpts: { + hmacKey: 'foo' + } + }).then(() => { + resolve() + }).catch(err => { + reject(err) + }) })) }) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 0ff755f..f5898d7 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -46,7 +46,12 @@ fastify.register(FastifyCsrfProtection, { hmacKey: '123' }, getUserInfo(req) { - return req.session.get<'username', string>('username') + const info = req.session.get('username') + if (info) { + return info + } else { + throw new Error('No user info') + } } }) expectError(fastify.register(FastifyCsrfProtection, { getUserInfo: 'invalid' }))