Skip to content

Commit 54261d8

Browse files
committed
Use crypto randomUUID for generating file IDs
Simpler. More secure. Already used in RPC.
1 parent ac9bc4e commit 54261d8

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed

src-main/windows/editor.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fsPromises = require('fs/promises');
22
const path = require('path');
33
const nodeURL = require('url');
44
const zlib = require('zlib');
5+
const nodeCrypto = require('crypto');
56
const {app, dialog} = require('electron');
67
const ProjectRunningWindow = require('./project-running-window');
78
const AddonsWindow = require('./addons');
@@ -202,38 +203,12 @@ const isChildPath = (parent, child) => {
202203
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
203204
};
204205

205-
/** @type {Set<string>} */
206-
const allFileIDs = new Set();
207-
208206
/**
209207
* @returns {string} A unique string.
210208
*/
211209
const generateFileId = () => {
212-
let result;
213-
let tries = 0;
214-
215-
do {
216-
tries++;
217-
if (tries > 50) {
218-
// Should never happen...
219-
throw new Error('Failed to generate file ID');
220-
}
221-
222-
result = 'desktop_file_id{';
223-
224-
// >200 bits of randomness; impractical to brute force.
225-
// Math.random() is not cryptographically secure, but even if someone can reverse it, they would
226-
// still only be able to access files that were already opened, so impact is not that big.
227-
const soup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
228-
for (let i = 0; i < 40; i++) {
229-
result += soup[Math.floor(Math.random() * soup.length)];
230-
}
231-
232-
result += '}';
233-
} while (allFileIDs.has(result));
234-
235-
allFileIDs.add(result);
236-
return result;
210+
// Note that we can't use the randomUUID from web crypto as we need to support Electron 22.
211+
return `desktop_file_id{${nodeCrypto.randomUUID()}}`;
237212
};
238213

239214
class EditorWindow extends ProjectRunningWindow {

0 commit comments

Comments
 (0)