Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-05-02 - Insecure Randomness in Code Generation
**Vulnerability:** Weak pseudo-random number generator `Math.random()` was used for generating sensitive items like coupon codes and store credits.
**Learning:** `Math.random()` is not cryptographically secure and its outputs can be predicted, allowing an attacker to potentially guess valid coupon codes or store credit codes.
**Prevention:** Always use `generateSecureCode()` from `@/lib/utils` which leverages the Web Crypto API (`crypto.getRandomValues()`) for cryptographically secure randomness.
8 changes: 2 additions & 6 deletions app/api/bargain/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { db, user, coupons, bargainSessions, products, combos } from "@/lib/db";
import { and, eq, inArray } from "drizzle-orm";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
import { generateSecureCode } from "@/lib/utils";

export const maxDuration = 30;
const MAX_NEGOTIATION_ROUNDS = 10;
Expand All @@ -20,12 +21,7 @@ type BargainCartItem = {

// Generate unique coupon code
function generateCouponCode(): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let code = "BRG-";
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
return code;
return generateSecureCode("BRG-", 6);
}

function calculateCartRuleCap(cartTotal: number, isFirstTimeUser: boolean): number {
Expand Down
7 changes: 2 additions & 5 deletions lib/actions/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { products, productVariants, coupons, orders, orderItems } from "@/lib/db
import { requireAdmin } from "@/lib/auth-server";
import { eq, desc, sql, and, gte } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { generateSecureCode } from "@/lib/utils";

// ============================================
// PRODUCT ACTIONS
Expand Down Expand Up @@ -567,11 +568,7 @@ export async function issueStoreCredit(data: IssueStoreCreditInput) {
const creditAmount = Math.round(data.refundAmount * bonusMultiplier);

// Generate unique store credit code
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let code = "CREDIT-";
for (let i = 0; i < 8; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
const code = generateSecureCode("CREDIT-", 8);

// Set validity (30-60 days)
const validityDays = Math.min(Math.max(data.validityDays || 30, 30), 60);
Expand Down
3 changes: 2 additions & 1 deletion lib/cart-context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client"

import { createContext, useContext, useState, useEffect, ReactNode } from "react"
import { generateSecureCode } from "@/lib/utils"

export interface CartItem {
id: string
Expand Down Expand Up @@ -80,7 +81,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
}

const addCombo: CartContextType["addCombo"] = (combo) => {
const comboGroupId = `combo-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
const comboGroupId = generateSecureCode(`combo-${Date.now()}-`, 6)
setItems((prev) => [
...prev,
...combo.items.map((item) => ({
Expand Down
15 changes: 15 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@ import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

/**
* Generates a cryptographically secure random string with the given prefix and length.
* Uses the Web Crypto API to ensure sufficient entropy for tokens and secrets.
*/
export function generateSecureCode(prefix: string, length: number): string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let result = prefix;
const randomValues = new Uint32Array(length);
crypto.getRandomValues(randomValues);
for (let i = 0; i < length; i++) {
result += chars[randomValues[i] % chars.length];
}
return result;
}