Skip to content
Open
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
45 changes: 41 additions & 4 deletions packages/device-id/src/get-device-id.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { createHmac } from 'crypto';
import { expect } from 'chai';
import { getDeviceId } from './get-device-id';

// Replicates machineid.ProtectedID(appID) from
// https://github.com/denisbrodbeck/machineid/blob/master/helper.go
// HMAC-SHA256(key=rawMachineId, message=appID), no case normalization.
function atlasCLIDeviceId(rawMachineId: string): string {
return createHmac('sha256', rawMachineId).update('atlascli').digest('hex');
}

describe('getDeviceId', function () {
it('returns a hashed device id when machine id is available', async function () {
const mockMachineId = 'test-machine-id';
Expand All @@ -15,19 +23,18 @@ describe('getDeviceId', function () {
expect(deviceId).to.not.equal('unknown');
});

it('converts machine id to uppercase when using node-machine-id', async function () {
it('produces different hashes for lower and uppercase machine ids', async function () {
const mockMachineId = 'test-machine-id';
const getMachineId = () => Promise.resolve(mockMachineId);

const resultA = await getDeviceId({
getMachineId,
getMachineId: () => Promise.resolve(mockMachineId),
});

const resultB = await getDeviceId({
getMachineId: () => Promise.resolve(mockMachineId.toUpperCase()),
});

expect(resultA).to.equal(resultB);
expect(resultA).to.not.equal(resultB);
});

it('returns "unknown" when machine id is not found', async function () {
Expand Down Expand Up @@ -149,4 +156,34 @@ describe('getDeviceId', function () {
});
}
});

describe('Atlas CLI compatibility', function () {
// Machine IDs format (case and hyphens) matter to catch case normalization bug.
const cases = [
{
platform: 'Linux',
// /etc/machine-id is always lowercase hex without hyphens
rawMachineId: 'a8098c1af14ef2e1dc4d7f5b4e08d4c0',
},
{
platform: 'macOS',
// IOPlatformUUID from IOKit is uppercase with hyphens
rawMachineId: 'A8098C1A-F14E-F2E1-DC4D-7F5B4E08D4C0',
},
{
platform: 'Windows',
// MachineGuid from HKLM\SOFTWARE\Microsoft\Cryptography is lowercase with hyphens
rawMachineId: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
},
];

for (const { platform, rawMachineId } of cases) {
it(`matches Atlas CLI output for ${platform} machine id format`, async function () {
const result = await getDeviceId({
getMachineId: () => Promise.resolve(rawMachineId),
});
expect(result).to.equal(atlasCLIDeviceId(rawMachineId));
});
}
});
});
2 changes: 1 addition & 1 deletion packages/device-id/src/get-device-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function resolveMachineId({
onError,
}: GetDeviceIdOptions): Promise<string> {
try {
const originalId = (await getMachineId())?.toUpperCase();
const originalId = await getMachineId();

if (!originalId) {
onError?.('resolutionError', new Error('Failed to resolve machine ID'));
Expand Down
Loading