100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI: replace Math.random() with crypto randomUUID (#2648)
Martin Peeters16514ea9a009
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.tsindex 380365ee2..c6490ec35 100644--- a/ui/component/core/src/util.ts+++ b/ui/component/core/src/util.ts@@ -1064,3 +1064,17 @@ export function blobToBase64(blob:Blob) {};});}++/**+ * Generates a 36 character UUID using the crypto API. https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID+ * As a fallback, in case an insecure browser context is used, we use `getRandomValues` and change it into UUIDv4.+ */+export function generateUniqueUUID(): string {+ if (typeof crypto.randomUUID === "function") {+ return crypto.randomUUID();+ }+ // Fallback for insecure contexts+ return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>+ (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)+ );+}diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-tree.ts b/ui/component/or-dashboard-builder/src/or-dashboard-tree.tsindex a16156936..e2decc039 100644--- a/ui/component/or-dashboard-builder/src/or-dashboard-tree.ts+++ b/ui/component/or-dashboard-builder/src/or-dashboard-tree.ts@@ -4,7 +4,7 @@ import {InputType} from '@openremote/or-mwc-components/or-mwc-input';import '@openremote/or-icon';import {style} from './style';import {Dashboard} from '@openremote/model';-import manager from '@openremote/core';+import manager, {Util} from '@openremote/core';import {ListItem} from '@openremote/or-mwc-components/or-mwc-list';import '@openremote/or-mwc-components/or-mwc-menu';import {showOkCancelDialog} from '@openremote/or-mwc-components/or-mwc-dialog';@@ -158,7 +158,7 @@ export class OrDashboardTree extends LitElement {const newDashboard = JSON.parse(JSON.stringify(dashboard)) as Dashboard;newDashboard.displayName = (newDashboard.displayName + " copy");if(newDashboard.template) {- newDashboard.template.id = Math.random().toString(36).slice(2, (String(newDashboard.template.id).length + 2)); // generate a new ID+ newDashboard.template.id = Util.generateUniqueUUID();}DashboardService.create(newDashboard, undefined, this.realm).then(d => {if (!this.dashboards) {diff --git a/ui/component/or-dashboard-builder/src/service/dashboard-service.ts b/ui/component/or-dashboard-builder/src/service/dashboard-service.tsindex 2936d1047..2bf282028 100644--- a/ui/component/or-dashboard-builder/src/service/dashboard-service.ts+++ b/ui/component/or-dashboard-builder/src/service/dashboard-service.ts@@ -1,5 +1,5 @@import {Dashboard, DashboardRefreshInterval, DashboardScalingPreset, DashboardScreenPreset} from "@openremote/model";-import manager from "@openremote/core";+import manager, {Util} from "@openremote/core";import {i18next} from "@openremote/or-translate";export enum DashboardSizeOption {@@ -9,24 +9,23 @@ export enum DashboardSizeOption {export class DashboardService {public static async create(dashboard?: Dashboard, size: DashboardSizeOption = DashboardSizeOption.DESKTOP, realm: string = manager.displayRealm, post = true): Promise<Dashboard> {- const randomId = () => (Math.random() + 1).toString(36).substring(2);if(!dashboard) {dashboard = {realm: realm,displayName: this.getDefaultDisplayName(size),template: {- id: randomId(),+ id: Util.generateUniqueUUID(),columns: this.getDefaultColumns(size),maxScreenWidth: this.getDefaultMaxScreenWidth(size),refreshInterval: DashboardRefreshInterval.OFF,- screenPresets: this.getDefaultScreenPresets(size),+ screenPresets: this.getDefaultScreenPresets(size)}} as Dashboard;} else {dashboard.id = undefined;if(dashboard.template) {- dashboard.template.id = randomId();- dashboard.template.widgets?.forEach(w => w.id = randomId());+ dashboard.template.id = Util.generateUniqueUUID();+ dashboard.template.widgets?.forEach(w => w.id = Util.generateUniqueUUID());}}diff --git a/ui/component/or-dashboard-builder/src/service/widget-service.ts b/ui/component/or-dashboard-builder/src/service/widget-service.tsindex 21e9596af..28b7b0ae6 100644--- a/ui/component/or-dashboard-builder/src/service/widget-service.ts+++ b/ui/component/or-dashboard-builder/src/service/widget-service.ts@@ -15,9 +15,9 @@ export class WidgetService {}public static async placeNew(widgetTypeId: string, x: number, y: number): Promise<DashboardWidget> {- const randomId = (Math.random() + 1).toString(36).substring(2);+ const randomId = Util.generateUniqueUUID();const manifest = this.getManifest(widgetTypeId);- const widget = {+ return {id: randomId,displayName: manifest.displayName,gridItem: {@@ -37,7 +37,6 @@ export class WidgetService {widgetConfig: manifest.getDefaultConfig(),widgetTypeId: widgetTypeId} as DashboardWidget;- return widget;}// Method used to correct the OrWidgetConfig specificationdiff --git a/ui/component/or-tree-menu/src/index.ts b/ui/component/or-tree-menu/src/index.tsindex 977bd7eb0..e4354dab9 100644--- a/ui/component/or-tree-menu/src/index.ts+++ b/ui/component/or-tree-menu/src/index.ts@@ -718,7 +718,7 @@ export class OrTreeMenu extends LitElement {* @param node - The TreeNode object to cache* @param randomId - Optionally you can supply an ID to use for caching*/- protected _setTreeNodeId(node: TreeNode, randomId = Math.random().toString(36).substring(2, 11)): string {+ protected _setTreeNodeId(node: TreeNode, randomId = Util.generateUniqueUUID()): string {if(this._treeNodeCache.get(node)) {return this._treeNodeCache.get(node)!;}