فهرست منبع

fix: replace Math.random with uuid4 for a crypto secure client ID generation

Ivelin Ivanov 5 سال پیش
والد
کامیت
61cb1517fd
5فایلهای تغییر یافته به همراه12 افزوده شده و 7 حذف شده
  1. 5 0
      package-lock.json
  2. 1 0
      package.json
  3. 3 4
      src/models/realm.js
  4. 2 2
      test/models/realm.js
  5. 1 1
      test/services/checkBrokenConnections/index.js

+ 5 - 0
package-lock.json

@@ -2835,6 +2835,11 @@
       "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
       "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
     },
+    "uuid4": {
+      "version": "1.1.4",
+      "resolved": "https://registry.npmjs.org/uuid4/-/uuid4-1.1.4.tgz",
+      "integrity": "sha512-Gr1q2k40LpF8CokcnQFjPDsdslzJbTCTBG5xQIEflUov431gFkY5KduiGIeKYAamkQnNn4IfdHJbLnl9Bib8TQ=="
+    },
     "v8-compile-cache": {
       "version": "2.1.0",
       "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.1.0.tgz",

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
     "cors": "~2.8.4",
     "express": "^4.17.1",
     "optimist": "~0.6.1",
+    "uuid4": "^1.1.4",
     "ws": "^7.1.2"
   },
   "devDependencies": {

+ 3 - 4
src/models/realm.js

@@ -1,3 +1,4 @@
+const uuidv4 = require('uuid/v4');
 const MessageQueue = require('./messageQueue');
 
 class Realm {
@@ -43,12 +44,10 @@ class Realm {
   }
 
   generateClientId () {
-    const randomId = () => (Math.random().toString(36) + '0000000000000000000').substr(2, 16);
-
-    let clientId = randomId();
+    let clientId = uuidv4();
 
     while (this.getClientById(clientId)) {
-      clientId = randomId();
+      clientId = uuidv4();
     }
 
     return clientId;

+ 2 - 2
test/models/realm.js

@@ -4,9 +4,9 @@ const Client = require('../../src/models/client');
 
 describe('Realm', () => {
   describe('#generateClientId', () => {
-    it('should generate a 16-character ID', () => {
+    it('should generate a 36-character UUID', () => {
       const realm = new Realm();
-      expect(realm.generateClientId().length).to.eq(16);
+      expect(realm.generateClientId().length).to.eq(36);
     });
   });
 

+ 1 - 1
test/services/checkBrokenConnections/index.js

@@ -17,7 +17,7 @@ describe('checkBrokenConnections service', () => {
             expect(realm.getClientById('id')).to.be.undefined;
             checkBrokenConnections.stop();
             done();
-        }, checkBrokenConnections.CHECK_INTERVAL * 2 + 3);
+        }, checkBrokenConnections.CHECK_INTERVAL * 2 + 30);
     });
 
     it('should remove client after 1 ping', (done) => {