-
Notifications
You must be signed in to change notification settings - Fork 96
feat(car): pin relation hsm calls #3562
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: main
Are you sure you want to change the base?
Conversation
# Conflicts: # pnpm-lock.yaml
# Conflicts: # pnpm-lock.yaml
# Conflicts: # bruno/collections/Rafiki/environments/Local Playground.bru
✅ Deploy Preview for brilliant-pasca-3e80ec ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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
authorization
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 days 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-pin
route 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
-
Copy modified line R3 -
Copy modified lines R27-R32 -
Copy modified lines R319-R326
@@ -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, |
-
Copy modified lines R17-R18
@@ -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
|
Changes proposed in this pull request
Context
Checklist
fixes #number
user-docs
label (if necessary)