Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge next into master #156

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "^9.3.1",
"@fastify/pre-commit": "^2.1.0",
"@fastify/secure-session": "^7.5.0",
"@fastify/session": "^10.7.0",
"@types/node": "^20.12.7",
"fastify": "^4.26.2",
jsumners marked this conversation as resolved.
Show resolved Hide resolved
"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
Loading