|
| 1 | +import "reflect-metadata"; |
| 2 | +import express from "express"; |
| 3 | +import { AddressInfo } from "node:net"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { RecoveryController } from "./RecoveryController"; |
| 6 | +import { VerificationController } from "./VerificationController"; |
| 7 | + |
| 8 | +describe("Session ID validation in controllers", () => { |
| 9 | + const previousEnv = { |
| 10 | + PROVISIONER_SHARED_SECRET: process.env.PROVISIONER_SHARED_SECRET, |
| 11 | + DIDIT_API_KEY: process.env.DIDIT_API_KEY, |
| 12 | + PUBLIC_EVAULT_SERVER_URI: process.env.PUBLIC_EVAULT_SERVER_URI, |
| 13 | + }; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + process.env.PROVISIONER_SHARED_SECRET = "test-shared-secret"; |
| 17 | + process.env.DIDIT_API_KEY = "test-api-key"; |
| 18 | + process.env.PUBLIC_EVAULT_SERVER_URI = "https://evault.example.com"; |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + process.env.PROVISIONER_SHARED_SECRET = previousEnv.PROVISIONER_SHARED_SECRET; |
| 23 | + process.env.DIDIT_API_KEY = previousEnv.DIDIT_API_KEY; |
| 24 | + process.env.PUBLIC_EVAULT_SERVER_URI = previousEnv.PUBLIC_EVAULT_SERVER_URI; |
| 25 | + }); |
| 26 | + |
| 27 | + it("rejects invalid diditSessionId in /recovery/face-search", async () => { |
| 28 | + const app = express(); |
| 29 | + app.use(express.json()); |
| 30 | + |
| 31 | + const verificationServiceStub = { |
| 32 | + create: async () => ({}), |
| 33 | + findByIdAndUpdate: async () => null, |
| 34 | + findOne: async () => null, |
| 35 | + } as any; |
| 36 | + |
| 37 | + new RecoveryController(verificationServiceStub).registerRoutes(app); |
| 38 | + |
| 39 | + const server = app.listen(0); |
| 40 | + const baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; |
| 41 | + |
| 42 | + try { |
| 43 | + const response = await fetch(`${baseUrl}/recovery/face-search`, { |
| 44 | + method: "POST", |
| 45 | + headers: { "Content-Type": "application/json" }, |
| 46 | + body: JSON.stringify({ diditSessionId: "../etc/passwd" }), |
| 47 | + }); |
| 48 | + const body = await response.json(); |
| 49 | + |
| 50 | + expect(response.status).toBe(400); |
| 51 | + expect(body.error).toContain("valid UUID"); |
| 52 | + } finally { |
| 53 | + await new Promise<void>((resolve, reject) => { |
| 54 | + server.close((error) => { |
| 55 | + if (error) reject(error); |
| 56 | + else resolve(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it("rejects invalid sessionId in /verification/decision/:sessionId", async () => { |
| 63 | + const app = express(); |
| 64 | + app.use(express.json()); |
| 65 | + |
| 66 | + const verificationServiceStub = { |
| 67 | + findById: async () => null, |
| 68 | + findOne: async () => null, |
| 69 | + create: async () => ({}), |
| 70 | + findByIdAndUpdate: async () => null, |
| 71 | + } as any; |
| 72 | + |
| 73 | + new VerificationController(verificationServiceStub).registerRoutes(app); |
| 74 | + |
| 75 | + const server = app.listen(0); |
| 76 | + const baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; |
| 77 | + |
| 78 | + try { |
| 79 | + const response = await fetch( |
| 80 | + `${baseUrl}/verification/decision/not-a-uuid`, |
| 81 | + { |
| 82 | + method: "GET", |
| 83 | + headers: { |
| 84 | + "x-shared-secret": "test-shared-secret", |
| 85 | + }, |
| 86 | + }, |
| 87 | + ); |
| 88 | + const body = await response.json(); |
| 89 | + |
| 90 | + expect(response.status).toBe(400); |
| 91 | + expect(body.error).toContain("valid UUID"); |
| 92 | + } finally { |
| 93 | + await new Promise<void>((resolve, reject) => { |
| 94 | + server.close((error) => { |
| 95 | + if (error) reject(error); |
| 96 | + else resolve(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + } |
| 100 | + }); |
| 101 | +}); |
0 commit comments