Skip to content

Commit

Permalink
Merge next into master (#156)
Browse files Browse the repository at this point in the history
* update for Fastify v5 (#154)

* Fix next branch tests (#157)

* Disable tsd

* patch accordingly

---------

Co-authored-by: Aras Abbasi <[email protected]>

* Update package.json

Signed-off-by: James Sumners <[email protected]>

* Disable tsd

* update fastify-error

* update deps

* update fastify deps

---------

Signed-off-by: James Sumners <[email protected]>
Co-authored-by: Gürgün Dayıoğlu <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>
  • Loading branch information
3 people authored Jul 5, 2024
1 parent e943ad4 commit bf2d032
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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": [
Expand Down
110 changes: 73 additions & 37 deletions test/user-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,77 +177,113 @@ 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'))
})

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'))
})

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'))
})

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'))
})

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)
})
}))
})

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)
})
}))
})
7 changes: 6 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }))
Expand Down

0 comments on commit bf2d032

Please sign in to comment.