Add fallback if crypto.randomUUID() is not available#4450
Conversation
📝 WalkthroughWalkthroughA new ChangesuseRandomId composable and upload UID migration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ❌ 1❌ Failed checks (1 warning)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
resources/js/composables/useRandomId.ts (1)
3-11: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick winConsider adding
crypto.getRandomValues()as an intermediate fallback.The current implementation falls back directly from
crypto.randomUUID()toMath.random(). However,crypto.getRandomValues()is more widely supported thancrypto.randomUUID()(available since Chrome 11 vs. Chrome 92) and provides cryptographically secure randomness, whileMath.random()is neither secure nor guaranteed to have sufficient entropy for collision avoidance.A three-tier approach would improve ID uniqueness and quality:
crypto.randomUUID()(best, newest)crypto.getRandomValues()(good, widely supported)Math.random()(fallback for very old environments)🔐 Proposed three-tier fallback implementation
export function useRandomId(): () => string { function generateId(): string { if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") { return crypto.randomUUID(); } + + // Fallback to crypto.getRandomValues if available (more secure than Math.random) + if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") { + const bytes = new Uint8Array(16); + crypto.getRandomValues(bytes); + // Set version (4) and variant bits per RFC 4122 + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; + } + // Final fallback to Math.random for very old environments return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === "x" ? r : (r & 0x3) | 0x8; return v.toString(16); }); } return generateId; }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 073c0572-8d68-4fa8-91f0-8ce93fe9dc4d
📒 Files selected for processing (5)
resources/js/components/modals/CameraCapture.vueresources/js/components/modals/UploadPanel.vueresources/js/composables/album/folderDrop.tsresources/js/composables/album/uploadEvents.tsresources/js/composables/useRandomId.ts
This pull request refactors how unique IDs are generated for uploadable files throughout the application. Instead of directly using
crypto.randomUUID(), a newuseRandomIdcomposable is introduced to provide a consistent and fallback-safe method for generating random IDs. All relevant file upload logic is updated to use this new approach.ID Generation Consistency and Fallback:
useRandomIdcomposable inresources/js/composables/useRandomId.tsthat generates a UUID usingcrypto.randomUUID()if available, or falls back to a manual UUID implementation if not.Refactoring Upload Logic to Use
useRandomId:CameraCapture.vue,UploadPanel.vue,album/folderDrop.ts, andalbum/uploadEvents.tsto generate fileuids usinggenerateId()from the new composable instead ofcrypto.randomUUID(). This ensures consistent and reliable ID generation across all upload entry points. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]Summary by CodeRabbit