From 6eb790e88ff2e4f2e8a0fe4ceb77826146015785 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 2 May 2026 08:20:20 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20Replace=20insecure=20Math.random()=20with=20Web=20Crypto=20A?= =?UTF-8?q?PI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Defined `generateSecureCode` in `lib/utils.ts` using `crypto.getRandomValues()` - Replaced weak pseudo-random coupon code generation in `app/api/bargain/route.ts` - Replaced weak pseudo-random store credit code generation in `lib/actions/admin.ts` - Replaced insecure combo group ID generation in `lib/cart-context.tsx` - Created sentinel journal entry regarding the vulnerability and mitigation Co-authored-by: f4teless <60130665+f4teless@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ app/api/bargain/route.ts | 8 ++------ lib/actions/admin.ts | 7 ++----- lib/cart-context.tsx | 3 ++- lib/utils.ts | 15 +++++++++++++++ 5 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 .jules/sentinel.md diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..690831d --- /dev/null +++ b/.jules/sentinel.md @@ -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. diff --git a/app/api/bargain/route.ts b/app/api/bargain/route.ts index cd182da..940c89b 100644 --- a/app/api/bargain/route.ts +++ b/app/api/bargain/route.ts @@ -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; @@ -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 { diff --git a/lib/actions/admin.ts b/lib/actions/admin.ts index 54c3848..9a1c424 100644 --- a/lib/actions/admin.ts +++ b/lib/actions/admin.ts @@ -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 @@ -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); diff --git a/lib/cart-context.tsx b/lib/cart-context.tsx index 47e4f67..36fbc83 100644 --- a/lib/cart-context.tsx +++ b/lib/cart-context.tsx @@ -1,6 +1,7 @@ "use client" import { createContext, useContext, useState, useEffect, ReactNode } from "react" +import { generateSecureCode } from "@/lib/utils" export interface CartItem { id: string @@ -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) => ({ diff --git a/lib/utils.ts b/lib/utils.ts index bd0c391..33d0911 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -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; +}