Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,28 @@ describe('ExtensionServerClient', () => {
expect(client.connection).toBeUndefined()
expect(mockSocketServer.clients.length).toBe(0)
})

test('uses crypto.randomUUID for id generation when available', () => {
const uuid = '12345678-1234-1234-1234-123456789012'
vi.stubGlobal('crypto', {
randomUUID: () => uuid,
})

const client = new ExtensionServerClient()
expect(client.id).toBe(uuid)

vi.unstubAllGlobals()
})

test('falls back to Math.random for id generation when crypto.randomUUID is not available', () => {
vi.stubGlobal('crypto', undefined)

const client = new ExtensionServerClient()
expect(client.id).toBeDefined()
expect(client.id.length).toBeGreaterThan(0)

vi.unstubAllGlobals()
})
})

describe('on()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class ExtensionServerClient implements ExtensionServer.Client {
private uiExtensionsByUuid: Record<string, ExtensionServer.UIExtension> = {}

constructor(options: DeepPartial<ExtensionServer.Options> = {}) {
this.id = (Math.random() + 1).toString(36).substring(7)
// We use a CSPRNG for ID generation where available to prevent predictability.
this.id =
typeof globalThis.crypto?.randomUUID === 'function'
? globalThis.crypto.randomUUID()
: (Math.random() + 1).toString(36).substring(7)
this.options = getValidatedOptions({
...options,
connection: {
Expand Down
Loading