@scrypted/server 0.147.0 → 0.148.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/cluster/cluster-setup.js +16 -17
  2. package/dist/cluster/cluster-setup.js.map +1 -1
  3. package/dist/http-interfaces.js +16 -2
  4. package/dist/http-interfaces.js.map +1 -1
  5. package/dist/plugin/media.d.ts +5 -39
  6. package/dist/plugin/media.js +73 -109
  7. package/dist/plugin/media.js.map +1 -1
  8. package/dist/plugin/plugin-console.d.ts +2 -0
  9. package/dist/plugin/plugin-console.js +10 -0
  10. package/dist/plugin/plugin-console.js.map +1 -1
  11. package/dist/plugin/plugin-host.js +7 -3
  12. package/dist/plugin/plugin-host.js.map +1 -1
  13. package/dist/plugin/plugin-remote-worker.js.map +1 -1
  14. package/dist/plugin/plugin-remote.d.ts +15 -0
  15. package/dist/plugin/plugin-remote.js +36 -39
  16. package/dist/plugin/plugin-remote.js.map +1 -1
  17. package/dist/rpc-serializer.js +4 -0
  18. package/dist/rpc-serializer.js.map +1 -1
  19. package/dist/scrypted-cluster-main.js +20 -0
  20. package/dist/scrypted-cluster-main.js.map +1 -1
  21. package/dist/scrypted-server-main.js +31 -15
  22. package/dist/scrypted-server-main.js.map +1 -1
  23. package/dist/services/cluster-fork.js +3 -3
  24. package/dist/services/cluster-fork.js.map +1 -1
  25. package/dist/services/plugin.d.ts +1 -0
  26. package/dist/services/plugin.js +5 -0
  27. package/dist/services/plugin.js.map +1 -1
  28. package/package.json +1 -1
  29. package/python/cluster_setup.py +20 -20
  30. package/python/plugin_remote.py +0 -1
  31. package/src/cluster/cluster-setup.ts +16 -17
  32. package/src/http-interfaces.ts +16 -2
  33. package/src/plugin/media.ts +95 -120
  34. package/src/plugin/plugin-console.ts +11 -0
  35. package/src/plugin/plugin-host.ts +14 -4
  36. package/src/plugin/plugin-remote-worker.ts +1 -1
  37. package/src/plugin/plugin-remote.ts +43 -41
  38. package/src/rpc-serializer.ts +4 -0
  39. package/src/scrypted-cluster-main.ts +24 -0
  40. package/src/scrypted-server-main.ts +34 -16
  41. package/src/services/cluster-fork.ts +3 -3
  42. package/src/services/plugin.ts +6 -0
@@ -46,6 +46,18 @@ if (SCRYPTED_SERVER_LISTEN_HOSTNAMES) {
46
46
  }
47
47
  }
48
48
 
49
+ // constant time comparison for password hashes and tokens so the string comparison
50
+ // does not leak how many leading characters matched.
51
+ function timingSafeStringEqual(a: string | undefined, b: string | undefined) {
52
+ if (typeof a !== 'string' || typeof b !== 'string')
53
+ return false;
54
+ const ab = Buffer.from(a);
55
+ const bb = Buffer.from(b);
56
+ if (ab.length !== bb.length)
57
+ return false;
58
+ return crypto.timingSafeEqual(ab, bb);
59
+ }
60
+
49
61
  async function listenServerPort(env: string, port: number, server: http.Server | https.Server | net.Server) {
50
62
  server.listen(port);
51
63
  try {
@@ -187,7 +199,7 @@ async function start(mainFilename: string, options?: {
187
199
  hash.update(salted);
188
200
  const sha = hash.digest().toString('hex');
189
201
 
190
- callback(sha === user.passwordHash || password === user.token);
202
+ callback(timingSafeStringEqual(sha, user.passwordHash) || timingSafeStringEqual(password, user.token));
191
203
  });
192
204
 
193
205
  // the default http-auth will returns a WWW-Authenticate header if login fails.
@@ -273,7 +285,7 @@ async function start(mainFilename: string, options?: {
273
285
  // lack of login from cookie auth.
274
286
 
275
287
  const checkToken = (token: string) => {
276
- if (process.env.SCRYPTED_ADMIN_TOKEN === token) {
288
+ if (process.env.SCRYPTED_ADMIN_TOKEN && timingSafeStringEqual(process.env.SCRYPTED_ADMIN_TOKEN, token)) {
277
289
  let username = process.env.SCRYPTED_ADMIN_USERNAME;
278
290
  if (!username) {
279
291
  const firstAdmin = [...scrypted.usersService.users.values()].find(u => !u.aclId);
@@ -287,7 +299,7 @@ async function start(mainFilename: string, options?: {
287
299
  }
288
300
 
289
301
  for (const user of scrypted.usersService.users.values()) {
290
- if (user.token === token) {
302
+ if (timingSafeStringEqual(user.token, token)) {
291
303
  res.locals.username = user._id;
292
304
  res.locals.aclId = user.aclId;
293
305
  break;
@@ -302,7 +314,7 @@ async function start(mainFilename: string, options?: {
302
314
  hash.update(salted);
303
315
  const sha = hash.digest().toString('hex');
304
316
 
305
- if (checkHash === sha) {
317
+ if (timingSafeStringEqual(checkHash, sha)) {
306
318
  const userToken = checkValidUserToken(tokenPart);
307
319
  if (userToken) {
308
320
  res.locals.username = userToken.username;
@@ -551,8 +563,10 @@ async function start(mainFilename: string, options?: {
551
563
  return undefined;
552
564
  try {
553
565
  const userToken = UserToken.validateToken(token);
554
- if (scrypted.usersService.users.has(userToken.username))
555
- return userToken;
566
+ const user = scrypted.usersService.users.get(userToken.username);
567
+ if (!user || userToken.timestamp < user.passwordDate || userToken.aclId !== user.aclId)
568
+ return undefined;
569
+ return userToken;
556
570
  }
557
571
  catch (e) {
558
572
  // console.warn('invalid token', e.message);
@@ -594,9 +608,13 @@ async function start(mainFilename: string, options?: {
594
608
  };
595
609
  }
596
610
 
611
+ async function updateUser(user: ScryptedUser) {
612
+ await db.upsert(user);
613
+ scrypted.usersService.users.set(user._id, user);
614
+ }
615
+
597
616
  app.post('/login', async (req, res) => {
598
617
  const { username, password, change_password, maxAge: maxAgeRequested } = req.body;
599
- const timestamp = Date.now();
600
618
  const maxAge = parseInt(maxAgeRequested) || ONE_DAY_MILLISECONDS;
601
619
  const alternateAddresses = await getAlternateAddresses();
602
620
 
@@ -614,7 +632,7 @@ async function start(mainFilename: string, options?: {
614
632
  const hash = crypto.createHash('sha256');
615
633
  hash.update(salted);
616
634
  const sha = hash.digest().toString('hex');
617
- if (user.passwordHash !== sha && user.token !== password) {
635
+ if (!timingSafeStringEqual(user.passwordHash, sha) && !timingSafeStringEqual(user.token, password)) {
618
636
  res.send({
619
637
  error: 'Incorrect password.',
620
638
  hasLogin,
@@ -622,7 +640,12 @@ async function start(mainFilename: string, options?: {
622
640
  return;
623
641
  }
624
642
 
625
- const userToken = new UserToken(username, user.aclId, timestamp, maxAge);
643
+ if (change_password) {
644
+ setScryptedUserPassword(user, change_password, Date.now());
645
+ await updateUser(user);
646
+ }
647
+
648
+ const userToken = new UserToken(username, user.aclId, Date.now(), maxAge);
626
649
  const login_user_token = userToken.toString();
627
650
  res.cookie(getLoginUserToken(req), login_user_token, {
628
651
  maxAge,
@@ -631,11 +654,6 @@ async function start(mainFilename: string, options?: {
631
654
  httpOnly: true,
632
655
  });
633
656
 
634
- if (change_password) {
635
- setScryptedUserPassword(user, change_password, timestamp);
636
- await db.upsert(user);
637
- }
638
-
639
657
  res.send({
640
658
  ...createTokens(userToken),
641
659
  username,
@@ -657,7 +675,7 @@ async function start(mainFilename: string, options?: {
657
675
  const user = await scrypted.usersService.addUserInternal(username, password, undefined);
658
676
  hasLogin = true;
659
677
 
660
- const userToken = new UserToken(username, user.aclId, timestamp);
678
+ const userToken = new UserToken(username, user.aclId, Date.now());
661
679
  const login_user_token = userToken.toString();
662
680
  res.cookie(getLoginUserToken(req), login_user_token, {
663
681
  maxAge,
@@ -725,7 +743,7 @@ async function start(mainFilename: string, options?: {
725
743
  const user = await db.tryGet(ScryptedUser, username) as ScryptedUser;
726
744
  if (!user.token) {
727
745
  user.token = crypto.randomBytes(16).toString('hex');
728
- await db.upsert(user);
746
+ await updateUser(user);
729
747
  }
730
748
 
731
749
  const userToken = new UserToken(user._id, user.aclId, Date.now());
@@ -92,9 +92,9 @@ export class ClusterForkService {
92
92
  });
93
93
  forkResultPromise.then(forkResult => {
94
94
  forkResult.clusterWorkerId = worker.id;
95
- forkResult.waitKilled().catch(() => { }).finally(() => {
96
- worker.forks.delete(options);
97
- });
95
+ return forkResult.waitKilled().catch(() => { });
96
+ }).catch(() => { }).finally(() => {
97
+ worker.forks.delete(options);
98
98
  });
99
99
 
100
100
  const ret: ClusterForkResultInterface = new WrappedForkResult(worker.id, forkResultPromise);
@@ -172,6 +172,12 @@ export class PluginComponent {
172
172
  consoleServer.clear(pluginDevice.nativeId);
173
173
  }
174
174
 
175
+ async readConsole(id: string) {
176
+ const pluginDevice = this.scrypted.findPluginDeviceById(id)!;
177
+ const consoleServer = await this.scrypted.plugins[pluginDevice.pluginId]!.consoleServer;
178
+ return consoleServer.read(pluginDevice.nativeId).toString();
179
+ }
180
+
175
181
  async getRemoteServicePort(pluginId: string, name: string, ...args: any[]): Promise<[number, string]> {
176
182
  if (name === 'console') {
177
183
  const consoleServer = await this.scrypted.plugins[pluginId]!.consoleServer;