Skip to content

Add randomUUID to CryptoProvider for edge runtime compatibility#1404

Merged
nicknisi merged 1 commit intomainfrom
nicknisi/v7-randomUUID-fix
Dec 2, 2025
Merged

Add randomUUID to CryptoProvider for edge runtime compatibility#1404
nicknisi merged 1 commit intomainfrom
nicknisi/v7-randomUUID-fix

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented Dec 1, 2025

Summary

Fixes #1403

  • Adds randomUUID() abstract method to CryptoProvider interface
  • Implements in NodeCryptoProvider using Node's crypto.randomUUID()
  • Implements in SubtleCryptoProvider using randomBytes(16) with UUID v4 bit manipulation
  • Updates AuditLogs to use getCryptoProvider().randomUUID() instead of direct Node crypto import

This ensures the audit-logs idempotency key generation works across all supported runtimes including edge environments (Convex, Cloudflare Workers) that cannot import the Node "crypto" package.

@nicknisi nicknisi requested a review from a team as a code owner December 1, 2025 23:14
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 1, 2025

Greptile Overview

Greptile Summary

This PR fixes edge runtime compatibility by abstracting UUID generation through the CryptoProvider interface. The direct Node crypto import in audit-logs was breaking edge environments like Convex and Cloudflare Workers.

Key Changes:

  • Added randomUUID() abstract method to CryptoProvider interface
  • NodeCryptoProvider uses native crypto.randomUUID()
  • SubtleCryptoProvider implements UUID v4 manually using random bytes with proper bit manipulation
  • AuditLogs.createEvent() now uses getCryptoProvider().randomUUID() instead of direct import
  • Comprehensive tests validate UUID v4 format and uniqueness for both implementations

The implementation correctly follows UUID v4 specification with proper version (0x40) and variant (0x80) bits set.

Confidence Score: 5/5

  • This PR is safe to merge with no identified issues
  • The changes are well-implemented with proper abstraction, correct UUID v4 bit manipulation, comprehensive test coverage, and directly address the reported issue without introducing new risks
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
src/common/crypto/crypto-provider.ts 5/5 Added abstract randomUUID() method to interface with clear documentation
src/common/crypto/subtle-crypto-provider.ts 5/5 Implemented randomUUID() with proper UUID v4 format using bit manipulation on random bytes
src/audit-logs/audit-logs.ts 5/5 Removed direct crypto import and now uses getCryptoProvider().randomUUID() for edge runtime compatibility

Sequence Diagram

sequenceDiagram
    participant Client
    participant AuditLogs
    participant WorkOS
    participant CryptoProvider
    participant NodeCrypto as NodeCryptoProvider
    participant SubtleCrypto as SubtleCryptoProvider
    
    Client->>AuditLogs: createEvent(org, event, options)
    
    alt idempotencyKey not provided
        AuditLogs->>WorkOS: getCryptoProvider()
        WorkOS-->>AuditLogs: CryptoProvider instance
        
        alt Node.js runtime
            AuditLogs->>NodeCrypto: randomUUID()
            NodeCrypto->>NodeCrypto: crypto.randomUUID()
            NodeCrypto-->>AuditLogs: UUID v4 string
        else Edge runtime (Convex, Cloudflare)
            AuditLogs->>SubtleCrypto: randomUUID()
            SubtleCrypto->>SubtleCrypto: randomBytes(16)
            SubtleCrypto->>SubtleCrypto: Apply UUID v4 bit masks
            SubtleCrypto-->>AuditLogs: UUID v4 string
        end
        
        AuditLogs->>AuditLogs: Prefix UUID with workos-node
    end
    
    AuditLogs->>WorkOS: post('/audit_logs/events', data, options)
    WorkOS-->>Client: Response
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@nicknisi nicknisi force-pushed the nicknisi/v7-randomUUID-fix branch from 43a7286 to 43f415e Compare December 1, 2025 23:20
Copy link
Member

@nholden nholden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

Fixes #1403. The audit-logs module was importing randomUUID directly
from Node's crypto module, breaking edge-like runtimes (Convex,
Cloudflare Workers) that cannot import the "crypto" package.

This adds randomUUID() to the CryptoProvider interface with
implementations in both NodeCryptoProvider and SubtleCryptoProvider,
ensuring the audit-logs idempotency key generation works across all
supported runtimes.
@nicknisi nicknisi force-pushed the nicknisi/v7-randomUUID-fix branch from 43f415e to c4ae1dc Compare December 2, 2025 00:34
@nicknisi nicknisi merged commit 0658b5e into main Dec 2, 2025
5 checks passed
@nicknisi nicknisi deleted the nicknisi/v7-randomUUID-fix branch December 2, 2025 00:35
@nicknisi nicknisi mentioned this pull request Dec 2, 2025
nicknisi added a commit that referenced this pull request Dec 2, 2025
## Description

- #1404 

## Documentation

Does this require changes to the WorkOS Docs? E.g. the [API
Reference](https://workos.com/docs/reference) or code snippets need
updates.

```
[ ] Yes
```

If yes, link a related docs PR and add a docs maintainer as a reviewer.
Their approval is required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Node crypto import breaks edge-like runtimes

2 participants