-
Notifications
You must be signed in to change notification settings - Fork 2
🔒 [security] Replace predictable invoice numbers with secure random identifiers #398
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?
Changes from all commits
e25fef3
46365d1
c361dbc
8f246d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
|
|
||||||
| import { prisma } from '@/lib/prisma'; | ||||||
| import { getAppUrl } from '@/lib/utils'; | ||||||
| import * as crypto from 'crypto'; | ||||||
| import { getRemainingDays, getExpiryDate, isExpiringSoon } from './state-machine'; | ||||||
| import { getEffectiveFeatureLimits, getUsageStats } from './feature-enforcer'; | ||||||
| import { getPaymentGateway } from './payment-gateway'; | ||||||
|
|
@@ -746,13 +747,14 @@ function getRetryDelay(retryCount: number): number { | |||||
| return delays[Math.min(retryCount, delays.length - 1)]; | ||||||
| } | ||||||
|
|
||||||
| async function createInvoiceForPayment( | ||||||
| export async function createInvoiceForPayment( | ||||||
| subscriptionId: string, | ||||||
| amount: number, | ||||||
| periodStart: Date, | ||||||
| periodEnd: Date | ||||||
| ): Promise<void> { | ||||||
| const invoiceNumber = `INV-${Date.now()}-${Math.random().toString(36).slice(2, 7).toUpperCase()}`; | ||||||
| const randomPart = crypto.randomBytes(4).toString('base64url').toUpperCase().slice(0, 5); | ||||||
|
||||||
| const randomPart = crypto.randomBytes(4).toString('base64url').toUpperCase().slice(0, 5); | |
| const randomPart = crypto.randomBytes(12).toString('base64url'); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { prisma } from '@/lib/prisma'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createInvoiceForPayment } from '@/lib/subscription/billing-service'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Mock prisma | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.mock('@/lib/prisma', () => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prisma: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| invoice: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| create: vi.fn().mockResolvedValue({ id: 'inv_123' }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('BillingService', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| vi.clearAllMocks(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('createInvoiceForPayment', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should generate an invoice number with the correct format', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subscriptionId = 'sub_123'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const amount = 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const periodStart = new Date(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const periodEnd = new Date(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await createInvoiceForPayment(subscriptionId, amount, periodStart, periodEnd); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(prisma.invoice.create).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const call = vi.mocked(prisma.invoice.create).mock.calls[0][0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const invoiceNumber = call.data.invoiceNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Pattern: INV-TIMESTAMP-RANDOM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // INV- (4 chars) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TIMESTAMP (digits) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // - (1 char) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // RANDOM (5 chars ALPHANUMERIC-ish) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(invoiceNumber).toMatch(/^INV-\d+-[0-9A-Z_-]{5}$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+37
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // RANDOM (5 chars ALPHANUMERIC-ish) | |
| expect(invoiceNumber).toMatch(/^INV-\d+-[0-9A-Z_-]{5}$/); | |
| // RANDOM (>= 5 chars, base64url-safe charset) | |
| expect(invoiceNumber).toMatch(/^INV-\d+-[A-Za-z0-9_-]{5,}$/); |
Copilot
AI
Mar 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test can be flaky because it relies on real randomness for uniqueness. Even if collisions are unlikely, it’s better to stub the random generator (and optionally Date.now) to return two different known values and assert the invoice numbers differ for the expected reason.
| await createInvoiceForPayment(subscriptionId, amount, periodStart, periodEnd); | |
| await createInvoiceForPayment(subscriptionId, amount, periodStart, periodEnd); | |
| expect(prisma.invoice.create).toHaveBeenCalledTimes(2); | |
| const firstCall = vi.mocked(prisma.invoice.create).mock.calls[0][0]; | |
| const secondCall = vi.mocked(prisma.invoice.create).mock.calls[1][0]; | |
| expect(firstCall.data.invoiceNumber).not.toBe(secondCall.data.invoiceNumber); | |
| const dateNowSpy = vi.spyOn(Date, 'now').mockReturnValue(1_700_000_000_000); | |
| const randomSpy = vi | |
| .spyOn(Math, 'random') | |
| .mockReturnValueOnce(0.1) | |
| .mockReturnValueOnce(0.9); | |
| try { | |
| await createInvoiceForPayment(subscriptionId, amount, periodStart, periodEnd); | |
| await createInvoiceForPayment(subscriptionId, amount, periodStart, periodEnd); | |
| } finally { | |
| dateNowSpy.mockRestore(); | |
| randomSpy.mockRestore(); | |
| } | |
| expect(prisma.invoice.create).toHaveBeenCalledTimes(2); | |
| const firstCall = vi.mocked(prisma.invoice.create).mock.calls[0][0]; | |
| const secondCall = vi.mocked(prisma.invoice.create).mock.calls[1][0]; | |
| const firstInvoiceNumber = firstCall.data.invoiceNumber as string; | |
| const secondInvoiceNumber = secondCall.data.invoiceNumber as string; | |
| const firstMatch = firstInvoiceNumber.match(/^(.+)-([0-9A-Z_-]{5})$/); | |
| const secondMatch = secondInvoiceNumber.match(/^(.+)-([0-9A-Z_-]{5})$/); | |
| expect(firstMatch).not.toBeNull(); | |
| expect(secondMatch).not.toBeNull(); | |
| if (!firstMatch || !secondMatch) return; | |
| const firstPrefix = firstMatch[1]; | |
| const firstRandomPart = firstMatch[2]; | |
| const secondPrefix = secondMatch[1]; | |
| const secondRandomPart = secondMatch[2]; | |
| // With Date.now stubbed, the prefix (INV + timestamp) should be identical, | |
| // and only the random suffix should differ between calls. | |
| expect(firstPrefix).toBe(secondPrefix); | |
| expect(firstRandomPart).not.toBe(secondRandomPart); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting createInvoiceForPayment appears to be solely for unit testing (no other production references found). This increases the module’s public surface area; consider keeping the DB-writing function private and instead exporting/testing a small pure helper that generates the invoice number (or test via the existing public entrypoint that calls it).