Hash username in MQTT client ID generation

Replaces the plain username in the MQTT client ID with a SHA-1 hash for improved privacy and to avoid exposing usernames in client identifiers.
This commit is contained in:
Pascal Bourque
2025-11-09 10:57:45 -05:00
parent 5fce04543a
commit 2d49a4ddb9

View File

@@ -19,6 +19,7 @@ import {
CognitoUserSession CognitoUserSession
} from 'amazon-cognito-identity-js'; } from 'amazon-cognito-identity-js';
import { iot, mqtt } from 'aws-iot-device-sdk-v2'; import { iot, mqtt } from 'aws-iot-device-sdk-v2';
import { hash } from 'crypto';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration.js'; import duration from 'dayjs/plugin/duration.js';
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
@@ -713,7 +714,9 @@ export class MysaApiClient {
// Per-process stable client id. Random suffix avoids collisions with other running processes. // Per-process stable client id. Random suffix avoids collisions with other running processes.
if (!this._mqttClientId) { if (!this._mqttClientId) {
this._mqttClientId = `mysa-js-sdk-${this.session?.username ?? 'anon'}-${process.pid}-${getRandomClientId()}`; const username = this.session?.username ?? 'anon';
const usernameHash = hash('sha1', username);
this._mqttClientId = `mysa-js-sdk-${usernameHash}-${process.pid}-${getRandomClientId()}`;
} }
const builder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets() const builder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets()