Skip to content

Commit fcee9f7

Browse files
committed
fix(*): adding random function for unsecure context #15461
1 parent f37e791 commit fcee9f7

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Use the function to get a random UUID string when secure context is not guaranteed making crypto.randomUUID unavailable.
3+
* @returns A random UUID string.
4+
*/
5+
export function getUUID() {
6+
if (typeof crypto.randomUUID === "function") {
7+
return crypto.randomUUID();
8+
}
9+
// Secure fallback using crypto.getRandomValues()
10+
const bytes = new Uint8Array(16);
11+
crypto.getRandomValues(bytes);
12+
13+
// Set version (4) and variant (RFC 4122)
14+
bytes[6] = (bytes[6] & 0x0f) | 0x40; // Version 4
15+
bytes[8] = (bytes[8] & 0x3f) | 0x80; // Variant 1
16+
17+
return [...bytes]
18+
.map((b, i) =>
19+
[4, 6, 8, 10].includes(i) ? `-${b.toString(16).padStart(2, "0")}` : b.toString(16).padStart(2, "0")
20+
)
21+
.join("");
22+
}

projects/igniteui-angular/src/lib/grids/filtering/excel-style/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isTree } from '../../../data-operations/expressions-tree-util';
22
import { FilteringLogic, IFilteringExpression } from '../../../data-operations/filtering-expression.interface';
33
import { IFilteringExpressionsTree } from '../../../data-operations/filtering-expressions-tree';
4+
import { getUUID } from '../../common/random';
45

56
/**
67
* @hidden @internal
@@ -30,7 +31,7 @@ export class ExpressionUI {
3031

3132
constructor() {
3233
// Use IDs to identify expressions clearly and use to track them in template @for cycles.
33-
this.expressionId = crypto.randomUUID();
34+
this.expressionId = getUUID();
3435
}
3536
}
3637

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ import { IgxGridCellComponent } from './cell.component';
179179
import { IgxGridValidationService } from './grid/grid-validation.service';
180180
import { getCurrentResourceStrings } from '../core/i18n/resources';
181181
import { isTree, recreateTreeFromFields } from '../data-operations/expressions-tree-util';
182+
import { getUUID } from './common/random';
182183

183184
interface IMatchInfoCache {
184185
row: any;
@@ -3785,7 +3786,7 @@ export abstract class IgxGridBaseDirective implements GridType,
37853786
const primaryColumn = this._columns.find(col => col.field === this.primaryKey);
37863787
const idType = this.data.length ?
37873788
this.resolveDataTypes(this.data[0][this.primaryKey]) : primaryColumn ? primaryColumn.dataType : 'string';
3788-
return idType === 'string' ? crypto.randomUUID() : FAKE_ROW_ID--;
3789+
return idType === 'string' ? getUUID() : FAKE_ROW_ID--;
37893790
}
37903791

37913792
/**

0 commit comments

Comments
 (0)