-
Notifications
You must be signed in to change notification settings - Fork 339
Allow blocking on fastify cookie #5910
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
base: master
Are you sure you want to change the base?
Changes from all commits
cc4df74
194d1ca
40618aa
ba54d6b
fc4342b
bd41db1
0aa385a
f70bf62
5e07eed
96a7246
7930de8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,14 @@ const Axios = require('axios') | |
const { assert } = require('chai') | ||
const getPort = require('get-port') | ||
const path = require('path') | ||
const semver = require('semver') | ||
const zlib = require('zlib') | ||
const fs = require('node:fs') | ||
const agent = require('../plugins/agent') | ||
const appsec = require('../../src/appsec') | ||
const Config = require('../../src/config') | ||
const { json } = require('../../src/appsec/blocked_templates') | ||
|
||
withVersions('fastify', 'fastify', version => { | ||
withVersions('fastify', 'fastify', '>=2', version => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't you removing coverage for fastify v1 by doing this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not support fastify v1, I will add this to the documentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not ? the instrumentation is there no ? |
||
describe('Suspicious request blocking - query', () => { | ||
let server, requestBody, axios | ||
|
||
|
@@ -234,11 +233,6 @@ withVersions('fastify', 'fastify', version => { | |
}) | ||
|
||
it('should return 403 for dangerous payloads', async () => { | ||
// Skip Fastify v1 - different behavior where schema validation takes precedence | ||
if (semver.lt(semver.coerce(version), '2.0.0')) { | ||
return | ||
} | ||
|
||
try { | ||
await axios.post('/schema-validated', { key: 'testattack' }) | ||
|
||
|
@@ -250,11 +244,6 @@ withVersions('fastify', 'fastify', version => { | |
}) | ||
|
||
it('should return 403 for valid schema with attack content', async () => { | ||
// Skip Fastify v1 - different behavior where schema validation takes precedence | ||
if (semver.lt(semver.coerce(version), '2.0.0')) { | ||
return | ||
} | ||
|
||
try { | ||
await axios.post('/schema-validated', { validField: 'testattack' }) | ||
|
||
|
@@ -267,11 +256,6 @@ withVersions('fastify', 'fastify', version => { | |
}) | ||
|
||
describe('Suspicious request blocking - path parameters', () => { | ||
// Skip Fastify v1 - preValidation hook is not supported | ||
if (semver.lt(semver.coerce(version), '2.0.0')) { | ||
return | ||
} | ||
|
||
let server, preHandlerHookSpy, preValidationHookSpy, axios | ||
|
||
before(() => { | ||
|
@@ -447,6 +431,111 @@ withVersions('fastify', 'fastify', version => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('Suspicious request blocking - cookie', () => { | ||
withVersions('fastify', '@fastify/cookie', cookieVersion => { | ||
const hookConfigurations = [ | ||
'onRequest', | ||
'preParsing', | ||
'preValidation', | ||
'preHandler' | ||
] | ||
|
||
hookConfigurations.forEach((hook) => { | ||
describe(`with ${hook} hook`, () => { | ||
let server, requestCookie, axios | ||
|
||
before(function () { | ||
if (version === '3.9.2') { | ||
// Fastify 3.9.2 is incompatible with @fastify/cookie >=6 | ||
this.skip() | ||
} | ||
|
||
// Skip preParsing hook for Fastify 2.x - has compatibility issues | ||
if (hook === 'preParsing' && version.startsWith('2')) { | ||
this.skip() | ||
} | ||
|
||
return agent.load(['fastify', '@fastify/cookie', 'http'], { client: false }) | ||
}) | ||
|
||
before((done) => { | ||
const fastify = require(`../../../../versions/fastify@${version}`).get() | ||
const fastifyCookie = require(`../../../../versions/@fastify/cookie@${cookieVersion}`).get() | ||
|
||
const app = fastify() | ||
|
||
app.register(fastifyCookie, { | ||
secret: 'my-secret', | ||
hook | ||
}) | ||
|
||
// Dummy hook | ||
app.addHook('onRequest', (req, reply, done) => done()) | ||
|
||
app.post('/', (request, reply) => { | ||
requestCookie() | ||
reply.send('DONE') | ||
}) | ||
|
||
getPort().then((port) => { | ||
app.listen({ port }, () => { | ||
axios = Axios.create({ baseURL: `http://localhost:${port}` }) | ||
done() | ||
}) | ||
server = app.server | ||
}).catch(done) | ||
}) | ||
|
||
beforeEach(async () => { | ||
requestCookie = sinon.stub() | ||
appsec.enable( | ||
new Config({ | ||
appsec: { | ||
enabled: true, | ||
rules: path.join(__dirname, 'cookie-parser-rules.json') | ||
} | ||
}) | ||
) | ||
}) | ||
|
||
afterEach(() => { | ||
appsec.disable() | ||
}) | ||
|
||
after(() => { | ||
if (server) { | ||
server.close() | ||
} | ||
return agent.close({ ritmReset: false }) | ||
}) | ||
|
||
it('should not block the request without an attack', async () => { | ||
const res = await axios.post('/', {}) | ||
|
||
sinon.assert.calledOnce(requestCookie) | ||
assert.strictEqual(res.data, 'DONE') | ||
}) | ||
|
||
it('should block the request when attack is detected', async () => { | ||
try { | ||
await axios.post('/', {}, { | ||
headers: { | ||
Cookie: 'key=testattack' | ||
} | ||
}) | ||
|
||
return Promise.reject(new Error('Request should not return 200')) | ||
} catch (e) { | ||
assert.strictEqual(e.response.status, 403) | ||
assert.deepEqual(e.response.data, JSON.parse(json)) | ||
sinon.assert.notCalled(requestCookie) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Api Security - Fastify', () => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.