diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..4938cb2 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,9 @@ +## 2026-01-14 - Dependency Version Mismatch blocking Testing/Startup +**Vulnerability:** Application crashed on startup/test due to `@fastify/jwt` v10 incompatibility with `fastify` v4 (requires v5). +**Learning:** `npm install` without lockfile or with loose versions can pull incompatible major versions if plugins break semver or update requirements faster than core. +**Prevention:** Pin dependencies strictly or ensure peer dependencies are checked. Use `pnpm` workspace strictness. + +## 2026-01-14 - Rate Limiting Disabled in Production Code +**Vulnerability:** Rate limiting middleware was commented out in `app.ts`. +**Learning:** Security features should not be commented out for "development". They should be conditionally configured or enabled with looser limits. +**Prevention:** Use environment variables to control enablement or limits, never comment out code. diff --git a/backend/jest.config.js b/backend/jest.config.js new file mode 100644 index 0000000..c444c0c --- /dev/null +++ b/backend/jest.config.js @@ -0,0 +1,15 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + moduleNameMapper: { + '^@/(.*)$': '/src/$1', + '^@modules/(.*)$': '/src/modules/$1', + '^@shared/(.*)$': '/src/shared/$1', + '^@config/(.*)$': '/src/config/$1', + '^@types/(.*)$': '/src/types/$1' + }, + rootDir: '.', + testMatch: ['**/__tests__/**/*.test.ts'], + setupFilesAfterEnv: ['/jest.setup.js'], +}; diff --git a/backend/jest.setup.js b/backend/jest.setup.js new file mode 100644 index 0000000..a53b437 --- /dev/null +++ b/backend/jest.setup.js @@ -0,0 +1,20 @@ +// Global setup for Jest tests +process.env.NODE_ENV = 'test'; +process.env.DATABASE_URL = 'postgresql://test:test@localhost:5432/launchpad_test'; +process.env.JWT_SECRET = 'test-secret-min-32-chars-length-required-for-production'; +process.env.LUNES_RPC_URL = 'http://localhost:9944'; +process.env.CONTRACT_ADDRESS = '0x0000000000000000000000000000000000000000'; +process.env.PRIVATE_KEY = '0x0000000000000000000000000000000000000000000000000000000000000000'; + +const mockLogger = { + info: jest.fn(), + error: jest.fn(), + warn: jest.fn(), + debug: jest.fn(), + auth: jest.fn(), +}; + +jest.mock('./src/shared/logger', () => ({ + logger: mockLogger, + Logger: mockLogger, +})); diff --git a/backend/package-lock.json b/backend/package-lock.json index c5266d5..c8f8ce2 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@fastify/cors": "^8.4.0", "@fastify/helmet": "^11.1.1", - "@fastify/jwt": "^10.0.0", + "@fastify/jwt": "^8.0.0", "@fastify/multipart": "^8.0.0", "@fastify/rate-limit": "^9.0.1", "@fastify/swagger": "^8.12.0", @@ -1536,60 +1536,18 @@ } }, "node_modules/@fastify/jwt": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/@fastify/jwt/-/jwt-10.0.0.tgz", - "integrity": "sha512-2Qka3NiyNNcsfejMUvyzot1T4UYIzzcbkFGDdVyrl344fRZ/WkD6VFXOoXhxe2Pzf3LpJNkoSxUM4Ru4DVgkYA==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/fastify" - }, - { - "type": "opencollective", - "url": "https://opencollective.com/fastify" - } - ], + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/@fastify/jwt/-/jwt-8.0.1.tgz", + "integrity": "sha512-295bd7V6bDCnZOu8MAQgM6r7V1KILB+kdEq1q6nbHfXCnML569n7NSo3WzeLDG6IAqDl+Rhzi1vjxwaNHhRCBA==", "license": "MIT", "dependencies": { - "@fastify/error": "^4.2.0", - "@lukeed/ms": "^2.0.2", - "fast-jwt": "^6.0.2", - "fastify-plugin": "^5.0.1", + "@fastify/error": "^3.0.0", + "@lukeed/ms": "^2.0.0", + "fast-jwt": "^4.0.0", + "fastify-plugin": "^4.0.0", "steed": "^1.1.3" } }, - "node_modules/@fastify/jwt/node_modules/@fastify/error": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.2.0.tgz", - "integrity": "sha512-RSo3sVDXfHskiBZKBPRgnQTtIqpi/7zhJOEmAxCiBcM7d0uwdGdxLlsCaLzGs8v8NnxIRlfG0N51p5yFaOentQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/fastify" - }, - { - "type": "opencollective", - "url": "https://opencollective.com/fastify" - } - ], - "license": "MIT" - }, - "node_modules/@fastify/jwt/node_modules/fastify-plugin": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/fastify-plugin/-/fastify-plugin-5.1.0.tgz", - "integrity": "sha512-FAIDA8eovSt5qcDgcBvDuX/v0Cjz0ohGhENZ/wpc3y+oZCY2afZ9Baqql3g/lC+OHRnciQol4ww7tuthOb9idw==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/fastify" - }, - { - "type": "opencollective", - "url": "https://opencollective.com/fastify" - } - ], - "license": "MIT" - }, "node_modules/@fastify/merge-json-schemas": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@fastify/merge-json-schemas/-/merge-json-schemas-0.1.1.tgz", @@ -6860,27 +6818,18 @@ "license": "MIT" }, "node_modules/fast-jwt": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-6.0.2.tgz", - "integrity": "sha512-dTF4bhYnuXhZYQUaxsHKqAyA5y/L/kQc4fUu0wQ0BSA0dMfcNrcv0aqR2YnVi4f7e1OnzDVU7sDsNdzl1O5EVA==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-4.0.5.tgz", + "integrity": "sha512-QnpNdn0955GT7SlT8iMgYfhTsityUWysrQjM+Q7bGFijLp6+TNWzlbSMPvgalbrQGRg4ZaHZgMcns5fYOm5avg==", "license": "Apache-2.0", "dependencies": { - "@lukeed/ms": "^2.0.2", + "@lukeed/ms": "^2.0.1", "asn1.js": "^5.4.1", "ecdsa-sig-formatter": "^1.0.11", - "mnemonist": "^0.40.0" + "mnemonist": "^0.39.5" }, "engines": { - "node": ">=20" - } - }, - "node_modules/fast-jwt/node_modules/mnemonist": { - "version": "0.40.3", - "resolved": "https://registry.npmjs.org/mnemonist/-/mnemonist-0.40.3.tgz", - "integrity": "sha512-Vjyr90sJ23CKKH/qPAgUKicw/v6pRoamxIEDFOF8uSgFME7DqPRpHgRTejWVjkdGg5dXj0/NyxZHZ9bcjH+2uQ==", - "license": "MIT", - "dependencies": { - "obliterator": "^2.0.4" + "node": ">=16" } }, "node_modules/fast-levenshtein": { diff --git a/backend/package.json b/backend/package.json index 11cd17a..644d9a6 100644 --- a/backend/package.json +++ b/backend/package.json @@ -38,7 +38,7 @@ "dependencies": { "@fastify/cors": "^8.4.0", "@fastify/helmet": "^11.1.1", - "@fastify/jwt": "^10.0.0", + "@fastify/jwt": "^8.0.0", "@fastify/multipart": "^8.0.0", "@fastify/rate-limit": "^9.0.1", "@fastify/swagger": "^8.12.0", diff --git a/backend/src/__tests__/rate_limit.test.ts b/backend/src/__tests__/rate_limit.test.ts new file mode 100644 index 0000000..08add1c --- /dev/null +++ b/backend/src/__tests__/rate_limit.test.ts @@ -0,0 +1,50 @@ +import { app } from '../app'; + +// Mock envConfig before importing app (jest.mock is hoisted automatically) +jest.mock('../config/env.config', () => { + const actual = jest.requireActual('../config/env.config'); + return { + envConfig: { + ...actual.envConfig, + RATE_LIMIT_MAX_REQUESTS: 2, + RATE_LIMIT_WINDOW_MS: 60000, // 1 minute + NODE_ENV: 'test', + }, + }; +}); + +describe('Rate Limiting Security', () => { + beforeAll(async () => { + await app.initialize(); + }); + + afterAll(async () => { + await app.stop(); + }); + + it('should enforce rate limits', async () => { + // Request 1: Allowed + const res1 = await app.server.inject({ + method: 'GET', + url: '/health', + }); + expect(res1.statusCode).toBe(200); + + // Request 2: Allowed + const res2 = await app.server.inject({ + method: 'GET', + url: '/health', + }); + expect(res2.statusCode).toBe(200); + + // Request 3: Blocked (Limit is 2) + const res3 = await app.server.inject({ + method: 'GET', + url: '/health', + }); + + expect(res3.statusCode).toBe(429); + const body = JSON.parse(res3.body); + expect(body.error.message).toMatch(/Rate limit exceeded/i); + }); +}); diff --git a/backend/src/app.ts b/backend/src/app.ts index 22bd2dd..b8a2d94 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -38,7 +38,7 @@ class App { this.setupErrorHandling(); } - private async initialize(): Promise { + public async initialize(): Promise { await this.setupMiddlewares(); await this.setupRoutes(); } @@ -64,17 +64,11 @@ class App { }, }); - // Rate Limiting - DESABILITADO PARA DESENVOLVIMENTO - // await this.server.register(rateLimit, { - // max: envConfig.RATE_LIMIT_MAX_REQUESTS, - // timeWindow: envConfig.RATE_LIMIT_WINDOW_MS, - // errorResponseBuilder: (request, context) => ({ - // code: 429, - // error: 'Rate Limit Exceeded', - // message: `Muitas tentativas. Tente novamente em ${Math.round(context.ttl / 1000)} segundos.`, - // expiresIn: context.ttl, - // }), - // }); + // Rate Limiting + await this.server.register(rateLimit, { + max: envConfig.RATE_LIMIT_MAX_REQUESTS, + timeWindow: envConfig.RATE_LIMIT_WINDOW_MS, + }); // Swagger Documentation if (envConfig.ENABLE_SWAGGER) {