-
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 canceled.
|
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 7 days ago
To fix the problem, we will introduce rate-limiting middleware using a well-known library such as fastify-rate-limit
. This middleware will limit the number of requests that can be sent to the /hsm/verify-pin
endpoint within a specified time window. The rate limiting will be applied directly to this endpoint to ensure it is protected without affecting the rest of the application.
Steps:
- Install the
fastify-rate-limit
package. - Import the middleware into the file.
- Register the rate-limiting plugin in the Fastify instance.
- Configure rate limiting specifically for the
/hsm/verify-pin
route.
-
Copy modified lines R28-R31 -
Copy modified lines R335-R337
@@ -25,6 +25,10 @@ | ||
|
||
export function createApp(port: number) { | ||
const app = fastify() | ||
app.register(require('fastify-rate-limit'), { | ||
max: 100, // maximum number of requests | ||
timeWindow: '1 minute' // time window for rate limiting | ||
}) | ||
|
||
app.post( | ||
'/hsm/ase-customer/generate-zmk', | ||
@@ -328,7 +332,9 @@ | ||
}) | ||
|
||
// Add PIN verification endpoint | ||
app.post('/hsm/verify-pin', async function handler(ffReq, ffReply) { | ||
app.post('/hsm/verify-pin', { | ||
config: { rateLimit: { max: 10, timeWindow: '1 minute' } } // limit to 10 requests per minute | ||
}, async function handler(ffReq, ffReply) { | ||
const requestBody = JSON.parse(JSON.stringify(ffReq.body)) | ||
const { pinBlock, pan, format, expectedPin, pinEncryptionKey } = requestBody | ||
|
-
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": "^5.9.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.0.0", |
Package | Version | Security advisories |
fastify-rate-limit (npm) | 5.9.0 | None |
Changes proposed in this pull request
Context
Checklist
fixes #number
user-docs
label (if necessary)