Skip to content

Conversation

koekiebox
Copy link
Collaborator

Changes proposed in this pull request

Context

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Make sure that all checks pass
  • Bruno collection updated (if necessary)
  • Documentation issue created with user-docs label (if necessary)
  • OpenAPI specs updated (if necessary)

@koekiebox koekiebox self-assigned this Jul 14, 2025
Copy link

netlify bot commented Jul 14, 2025

Deploy Preview for brilliant-pasca-3e80ec ready!

Name Link
🔨 Latest commit 0269a21
🔍 Latest deploy log https://app.netlify.com/projects/brilliant-pasca-3e80ec/deploys/68d196bd6ec111000832d63e
😎 Deploy Preview https://deploy-preview-3562--brilliant-pasca-3e80ec.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the type: tests Testing related label Jul 14, 2025
@koekiebox koekiebox changed the title Jason/car 9 feat(car): pin relation hsm calls Jul 14, 2025
Comment on lines 331 to 366
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

This route handler performs
authorization
, but is not rate-limited.

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

Suggested changeset 2
test/hsm-emulator/src/app.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/test/hsm-emulator/src/app.ts b/test/hsm-emulator/src/app.ts
--- a/test/hsm-emulator/src/app.ts
+++ b/test/hsm-emulator/src/app.ts
@@ -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,
EOF
@@ -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,
test/hsm-emulator/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/test/hsm-emulator/package.json b/test/hsm-emulator/package.json
--- a/test/hsm-emulator/package.json
+++ b/test/hsm-emulator/package.json
@@ -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",
EOF
@@ -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",
This fix introduces these dependencies
Package Version Security advisories
@fastify/rate-limit (npm) 10.3.0 None
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link

🚀 Performance Test Results

Test Configuration:

  • VUs: 4
  • Duration: 1m0s

Test Metrics:

  • Requests/s: 42.73
  • Iterations/s: 14.27
  • Failed Requests: 0.00% (0 of 2569)
📜 Logs

> [email protected] run-tests:testenv /home/runner/work/rafiki/rafiki/test/performance
> ./scripts/run-tests.sh -e test "-k" "-q" "--vus" "4" "--duration" "1m"

Cloud Nine GraphQL API is up: http://localhost:3101/graphql
Cloud Nine Wallet Address is up: http://localhost:3100/
Happy Life Bank Address is up: http://localhost:4100/
cloud-nine-wallet-test-backend already set
cloud-nine-wallet-test-auth already set
happy-life-bank-test-backend already set
happy-life-bank-test-auth already set
     data_received..................: 928 kB 15 kB/s
     data_sent......................: 2.0 MB 33 kB/s
     http_req_blocked...............: avg=6.91µs   min=2.13µs   med=5.33µs   max=635.39µs p(90)=6.49µs   p(95)=7.12µs  
     http_req_connecting............: avg=599ns    min=0s       med=0s       max=534.82µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=92.97ms  min=7.7ms    med=75.23ms  max=557.31ms p(90)=163.18ms p(95)=183.7ms 
       { expected_response:true }...: avg=92.97ms  min=7.7ms    med=75.23ms  max=557.31ms p(90)=163.18ms p(95)=183.7ms 
     http_req_failed................: 0.00%  ✓ 0         ✗ 2569
     http_req_receiving.............: avg=85µs     min=23.94µs  med=75.78µs  max=1.51ms   p(90)=113.75µs p(95)=139.59µs
     http_req_sending...............: avg=36.32µs  min=10.79µs  med=27.81µs  max=1.21ms   p(90)=43.58µs  p(95)=62.12µs 
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=92.84ms  min=7.58ms   med=75.11ms  max=557.25ms p(90)=163.06ms p(95)=183.61ms
     http_reqs......................: 2569   42.725614/s
     iteration_duration.............: avg=280.01ms min=160.07ms med=267.46ms max=1.07s    p(90)=341.03ms p(95)=390.24ms
     iterations.....................: 858    14.26959/s
     vus............................: 4      min=4       max=4 
     vus_max........................: 4      min=4       max=4 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant