Conversation
# Conflicts: # pnpm-lock.yaml
# Conflicts: # pnpm-lock.yaml
# Conflicts: # bruno/collections/Rafiki/environments/Local Playground.bru
❌ Deploy Preview for brilliant-pasca-3e80ec failed. Why did it fail? →
|
| app.post('/hsm/verify-pin', async function handler(ffReq, ffReply) { | ||
| const requestBody = JSON.parse(JSON.stringify(ffReq.body)) | ||
| const { pinBlock, pan, format, expectedPin, pinEncryptionKey } = requestBody | ||
|
|
||
| try { | ||
| // Validate input | ||
| if (!pinBlock || !pan || !format || !expectedPin) { | ||
| throw new Error('Missing required parameters') | ||
| } | ||
|
|
||
| if (format !== 'ISO-0' && format !== 'ISO-1') { | ||
| throw new Error('Format must be ISO-0 or ISO-1') | ||
| } | ||
|
|
||
| // Verify the PIN | ||
| const isValid = verifyPin( | ||
| pinBlock, | ||
| pan, | ||
| format, | ||
| expectedPin, | ||
| pinEncryptionKey | ||
| ) | ||
|
|
||
| logger.info(`PIN verification result: ${isValid ? 'Valid' : 'Invalid'}`) | ||
|
|
||
| ffReply.code(200).send({ | ||
| isValid, | ||
| format | ||
| }) | ||
| } catch (error) { | ||
| logger.error(`PIN verification error: ${error.message}`) | ||
| ffReply.code(400).send({ | ||
| error: error.message | ||
| }) | ||
| } | ||
| }) |
Check failure
Code scanning / CodeQL
Missing rate limiting High test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 8 months ago
To fix this problem, a rate-limiting middleware should be applied to the /hsm/verify-pin endpoint. Since this is a Fastify application rather than Express, the correct approach is to use the @fastify/rate-limit plugin, which works similarly to the rate limiter shown in the CodeQL example (which used express-rate-limit). This involves:
- Adding an import (or require) for
@fastify/rate-limit. - Registering the plugin on the Fastify app instance (
app), either globally or with route-specific overrides. - For minimal impact, apply a sensible rate limit to the
/hsm/verify-pinroute only, so that other routes remain unaffected.
The best practice is to register the rate-limit plugin once with global options, but then supply more restrictive per-route overrides if desired. Here we'll register the plugin at the start of the createApp function and apply it specifically to the /hsm/verify-pin route via the route's config property.
What needs to be changed:
- Import
@fastify/rate-limit - Register the rate limit plugin
- Add rate limit options SPECIFICALLY for the verify-pin route
| @@ -1,5 +1,6 @@ | ||
| import fastify from 'fastify' | ||
| import logger from './logger' | ||
| import rateLimit from '@fastify/rate-limit' | ||
| import { | ||
| AES_AUSTRIA_CARD_LMK_HEX, | ||
| AES_CUSTOMER_ASE_LMK_HEX, | ||
| @@ -23,6 +24,12 @@ | ||
| export function createApp(port: number) { | ||
| const app = fastify() | ||
|
|
||
| // Register @fastify/rate-limit plugin | ||
| // It's safe to register it globally, but we can override per-route configs below | ||
| app.register(rateLimit, { | ||
| global: false, // We'll enable only per-route | ||
| }) | ||
|
|
||
| app.post( | ||
| '/hsm/ase-customer/generate-zmk', | ||
| async function handler(ffReq, ffReply) { | ||
| @@ -309,7 +316,14 @@ | ||
| }) | ||
|
|
||
| // Add PIN verification endpoint | ||
| app.post('/hsm/verify-pin', async function handler(ffReq, ffReply) { | ||
| app.post('/hsm/verify-pin', { | ||
| config: { | ||
| rateLimit: { | ||
| max: 5, | ||
| timeWindow: '1 minute' | ||
| } | ||
| } | ||
| }, async function handler(ffReq, ffReply) { | ||
| const requestBody = JSON.parse(JSON.stringify(ffReq.body)) | ||
| const { | ||
| pinBlock, |
| @@ -14,7 +14,8 @@ | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "fastify": "^5.2.1", | ||
| "pino": "^9.6.0" | ||
| "pino": "^9.6.0", | ||
| "@fastify/rate-limit": "^10.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^20.0.0", |
| Package | Version | Security advisories |
| @fastify/rate-limit (npm) | 10.3.0 | None |
# Conflicts: # pnpm-lock.yaml
🚀 Performance Test ResultsTest Configuration:
Test Metrics:
📜 Logs |
# Conflicts: # pnpm-lock.yaml
|
@koekiebox closing this, as it seems like it is not relevant given the ongoing card & POS work :) |
Changes proposed in this pull request
Context
Checklist
fixes #numberuser-docslabel (if necessary)