security: use CSPRNG for deduplication tag generation in buildViewEvent

Replaced Math.random() with a tiered approach using crypto.randomUUID()
and crypto.getRandomValues() to ensure cryptographically secure random
identifiers for deduplication tags in Nostr events.

This fix addresses a potential collision risk and follows security best
practices for random number generation in both browser and Node.js
environments.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-16 16:33:24 +00:00
1 parent c63ecd8cd9
commit b0d507a1e0
1 file changed
+19 -3
+19 -3
View File
@@ -740,6 +740,24 @@ export function getAllNostrEventSchemas() {
return entries;
}
function generateRandomIdentifier() {
if (
typeof globalThis.crypto !== "undefined" &&
typeof globalThis.crypto.randomUUID === "function"
) {
return globalThis.crypto.randomUUID();
}
if (
typeof globalThis.crypto !== "undefined" &&
typeof globalThis.crypto.getRandomValues === "function"
) {
const bytes = new Uint8Array(16);
globalThis.crypto.getRandomValues(bytes);
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
}
return Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
}
function appendSchemaTags(tags, schema) {
if (!Array.isArray(schema?.appendTags)) {
return tags;
@@ -1834,9 +1852,7 @@ export function buildViewEvent(params) {
const resolvedDedupeTag =
dedupeTag ||
(schema?.identifierTag?.name
? Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2)
: "");
(schema?.identifierTag?.name ? generateRandomIdentifier() : "");
if (resolvedDedupeTag && schema?.identifierTag?.name) {
const identifierName = schema.identifierTag.name;