Skip to content

Commit 12162d7

Browse files
authored
fix(*): adding random function for unsecure context #15461 (#15597)
1 parent ccbc14b commit 12162d7

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getUUID } from './random';
2+
3+
describe('Random (crypto.randomUuid()) fallback unit tests', () => {
4+
let originalRandomUuid = crypto.randomUUID;
5+
6+
beforeAll(() => {
7+
crypto.randomUUID = null; // Mock crypto.randomUUID to simulate a non-secure context
8+
});
9+
10+
it('should generate a valid UUID', () => {
11+
const uuid = getUUID();
12+
expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
13+
});
14+
15+
it('should generate unique UUIDs', () => {
16+
const uuids = new Set();
17+
for (let i = 0; i < 100; i++) {
18+
uuids.add(getUUID());
19+
}
20+
expect(uuids.size).toBe(100); // All UUIDs should be unique
21+
});
22+
23+
afterAll(() => {
24+
crypto.randomUUID = originalRandomUuid; // Restore the original function
25+
});
26+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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(): `${string}-${string}-${string}-${string}-${string}` {
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+
const a = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('');
18+
return `${a.slice(0, 8)}-${a.slice(8, 12)}-${a.slice(12, 16)}-${a.slice(16, 20)}-${a.slice(20)}`;
19+
}

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)