Skip to content

Commit 34a65f0

Browse files
committed
Improve crypto routine in password-like context
1 parent 9449fdb commit 34a65f0

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎src/components/Calendar/client/index.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,28 @@ const buildOutlookCalendarUrl = (eventData: CalendarEventData): string => {
135135
return `https://outlook.office.com/calendar/0/deeplink/compose?${params.toString()}`
136136
}
137137

138+
const createCalendarUid = (now: Date): string => {
139+
// Use Web Crypto instead of Math.random() to avoid insecure-randomness findings.
140+
const cryptoApi = globalThis.crypto
141+
142+
if (cryptoApi && 'randomUUID' in cryptoApi && typeof cryptoApi.randomUUID === 'function') {
143+
return cryptoApi.randomUUID()
144+
}
145+
146+
if (cryptoApi && 'getRandomValues' in cryptoApi && typeof cryptoApi.getRandomValues === 'function') {
147+
const bytes = new Uint8Array(16)
148+
cryptoApi.getRandomValues(bytes)
149+
return Array.from(bytes, (value) => value.toString(16).padStart(2, '0')).join('')
150+
}
151+
152+
// Extremely old/non-browser environment fallback: unique-enough without randomness.
153+
return `${now.getTime()}`
154+
}
155+
138156
const buildIcsText = (eventData: CalendarEventData): string => {
139157
const now = new Date()
140158
const dtStamp = formatUtcDateTimeForCalendar(now)
141-
const uid = `${now.getTime()}-${Math.random().toString(16).slice(2)}`
159+
const uid = createCalendarUid(now)
142160

143161
const start = parseIsoUtcDate(eventData.startDate, eventData.startTime)
144162
const end = ((): Date => {

0 commit comments

Comments
 (0)