@lethevimlet/sshift 1.8.3 → 1.9.1

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.
@@ -0,0 +1,155 @@
1
+ const crypto = require('crypto');
2
+
3
+ const SENSITIVE_FIELDS = [
4
+ 'host',
5
+ 'port',
6
+ 'username',
7
+ 'password',
8
+ 'privateKey',
9
+ 'passphrase',
10
+ 'url'
11
+ ];
12
+
13
+ let bookmarkEncryptionKey = null;
14
+
15
+ class BookmarkEncryptionLockedError extends Error {
16
+ constructor(message = 'Encrypted bookmarks are locked') {
17
+ super(message);
18
+ this.name = 'BookmarkEncryptionLockedError';
19
+ this.code = 'BOOKMARKS_LOCKED';
20
+ }
21
+ }
22
+
23
+ function deriveKey(password, salt) {
24
+ return crypto.scryptSync(password, Buffer.from(salt, 'base64'), 32);
25
+ }
26
+
27
+ function encryptConnectionData(data, key, bookmarkId) {
28
+ const iv = crypto.randomBytes(12);
29
+ const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
30
+ cipher.setAAD(Buffer.from(String(bookmarkId)));
31
+ const ciphertext = Buffer.concat([
32
+ cipher.update(JSON.stringify(data), 'utf8'),
33
+ cipher.final()
34
+ ]);
35
+ return {
36
+ version: 1,
37
+ iv: iv.toString('base64'),
38
+ tag: cipher.getAuthTag().toString('base64'),
39
+ ciphertext: ciphertext.toString('base64')
40
+ };
41
+ }
42
+
43
+ function decryptConnectionData(payload, key, bookmarkId) {
44
+ if (!payload || payload.version !== 1) {
45
+ throw new Error('Unsupported encrypted bookmark format');
46
+ }
47
+ const decipher = crypto.createDecipheriv(
48
+ 'aes-256-gcm',
49
+ key,
50
+ Buffer.from(payload.iv, 'base64')
51
+ );
52
+ decipher.setAAD(Buffer.from(String(bookmarkId)));
53
+ decipher.setAuthTag(Buffer.from(payload.tag, 'base64'));
54
+ const plaintext = Buffer.concat([
55
+ decipher.update(Buffer.from(payload.ciphertext, 'base64')),
56
+ decipher.final()
57
+ ]);
58
+ return JSON.parse(plaintext.toString('utf8'));
59
+ }
60
+
61
+ function encryptBookmark(bookmark, key) {
62
+ const stored = { ...bookmark };
63
+ const connectionData = {};
64
+ for (const field of SENSITIVE_FIELDS) {
65
+ if (Object.prototype.hasOwnProperty.call(stored, field)) {
66
+ connectionData[field] = stored[field];
67
+ delete stored[field];
68
+ }
69
+ }
70
+ delete stored.encryptedConnection;
71
+ stored.encryptedConnection = encryptConnectionData(connectionData, key, stored.id);
72
+ return stored;
73
+ }
74
+
75
+ function decryptBookmark(bookmark, key) {
76
+ if (!bookmark.encryptedConnection) return { ...bookmark };
77
+ const stored = { ...bookmark };
78
+ const connectionData = decryptConnectionData(stored.encryptedConnection, key, stored.id);
79
+ delete stored.encryptedConnection;
80
+ return { ...stored, ...connectionData };
81
+ }
82
+
83
+ function encryptBookmarks(bookmarks, key) {
84
+ return (bookmarks || []).map(bookmark => encryptBookmark(bookmark, key));
85
+ }
86
+
87
+ function decryptBookmarks(bookmarks, key) {
88
+ return (bookmarks || []).map(bookmark => decryptBookmark(bookmark, key));
89
+ }
90
+
91
+ function prepareEncryptedBookmarks(bookmarks, password, salt = crypto.randomBytes(16).toString('base64')) {
92
+ const key = deriveKey(password, salt);
93
+ return { salt, key, bookmarks: encryptBookmarks(bookmarks, key) };
94
+ }
95
+
96
+ function decryptBookmarksWithPassword(config, password) {
97
+ if (!config.bookmarkEncryptionSalt) {
98
+ throw new Error('Bookmark encryption salt is missing');
99
+ }
100
+ const key = deriveKey(password, config.bookmarkEncryptionSalt);
101
+ return { key, bookmarks: decryptBookmarks(config.bookmarks, key) };
102
+ }
103
+
104
+ function unlockBookmarkEncryption(config, password) {
105
+ if (!config.bookmarkEncryptionEnabled) {
106
+ clearBookmarkEncryptionKey();
107
+ return;
108
+ }
109
+ if (!config.bookmarkEncryptionSalt) {
110
+ throw new Error('Bookmark encryption salt is missing');
111
+ }
112
+ // Password verification happens before this call. Keep login available
113
+ // even if one bookmark payload is corrupt; decryption errors remain scoped
114
+ // to bookmark access instead of locking the user out of the whole app.
115
+ setBookmarkEncryptionKey(deriveKey(password, config.bookmarkEncryptionSalt));
116
+ }
117
+
118
+ function getDecryptedBookmarks(config) {
119
+ if (!config.bookmarkEncryptionEnabled) return config.bookmarks || [];
120
+ if (!bookmarkEncryptionKey) throw new BookmarkEncryptionLockedError();
121
+ return decryptBookmarks(config.bookmarks, bookmarkEncryptionKey);
122
+ }
123
+
124
+ function storeBookmarks(config, bookmarks) {
125
+ if (!config.bookmarkEncryptionEnabled) {
126
+ config.bookmarks = bookmarks;
127
+ return;
128
+ }
129
+ if (!bookmarkEncryptionKey) throw new BookmarkEncryptionLockedError();
130
+ config.bookmarks = encryptBookmarks(bookmarks, bookmarkEncryptionKey);
131
+ }
132
+
133
+ function setBookmarkEncryptionKey(key) {
134
+ if (bookmarkEncryptionKey) bookmarkEncryptionKey.fill(0);
135
+ bookmarkEncryptionKey = key ? Buffer.from(key) : null;
136
+ }
137
+
138
+ function clearBookmarkEncryptionKey() {
139
+ if (bookmarkEncryptionKey) bookmarkEncryptionKey.fill(0);
140
+ bookmarkEncryptionKey = null;
141
+ }
142
+
143
+ module.exports = {
144
+ BookmarkEncryptionLockedError,
145
+ deriveKey,
146
+ encryptBookmarks,
147
+ decryptBookmarks,
148
+ prepareEncryptedBookmarks,
149
+ decryptBookmarksWithPassword,
150
+ unlockBookmarkEncryption,
151
+ getDecryptedBookmarks,
152
+ storeBookmarks,
153
+ setBookmarkEncryptionKey,
154
+ clearBookmarkEncryptionKey
155
+ };
@@ -47,6 +47,8 @@ const defaultConfig = {
47
47
  bookmarks: [],
48
48
  folders: [],
49
49
  passwordHash: null,
50
+ bookmarkEncryptionEnabled: false,
51
+ bookmarkEncryptionSalt: null,
50
52
  scrollback: 10000,
51
53
  webglRenderer: true,
52
54
  imageAddonEnabled: true,
@@ -142,12 +144,24 @@ function loadConfig() {
142
144
  * @returns {boolean} Success status
143
145
  */
144
146
  function saveConfig(config) {
147
+ let tempPath = null;
145
148
  try {
146
149
  const configPath = getConfigPath();
147
- fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
150
+ tempPath = `${configPath}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
151
+ const fd = fs.openSync(tempPath, 'w', 0o600);
152
+ try {
153
+ fs.writeFileSync(fd, JSON.stringify(config, null, 2));
154
+ fs.fsyncSync(fd);
155
+ } finally {
156
+ fs.closeSync(fd);
157
+ }
158
+ fs.renameSync(tempPath, configPath);
148
159
  return true;
149
160
  } catch (err) {
150
161
  console.error('Error saving config:', err);
162
+ if (tempPath) {
163
+ try { fs.unlinkSync(tempPath); } catch (_) {}
164
+ }
151
165
  return false;
152
166
  }
153
167
  }
@@ -379,7 +393,9 @@ function getLegacyDataDir() {
379
393
  }
380
394
 
381
395
  function hashPassword(password) {
382
- return crypto.createHash('sha3-256').update(password).digest('hex');
396
+ const salt = crypto.randomBytes(16);
397
+ const hash = crypto.scryptSync(password, salt, 32);
398
+ return `scrypt$${salt.toString('base64')}$${hash.toString('base64')}`;
383
399
  }
384
400
 
385
401
  function isPasswordSet() {
@@ -390,7 +406,45 @@ function isPasswordSet() {
390
406
  function verifyPassword(password) {
391
407
  const config = loadConfig();
392
408
  if (!config.passwordHash) return true;
393
- return hashPassword(password) === config.passwordHash;
409
+ if (config.passwordHash.startsWith('scrypt$')) {
410
+ const parts = config.passwordHash.split('$');
411
+ if (parts.length !== 3) return false;
412
+ try {
413
+ const expected = Buffer.from(parts[2], 'base64');
414
+ const actual = crypto.scryptSync(password, Buffer.from(parts[1], 'base64'), expected.length);
415
+ return expected.length === actual.length && crypto.timingSafeEqual(expected, actual);
416
+ } catch (_) {
417
+ return false;
418
+ }
419
+ }
420
+ // Legacy password hashes used unsalted SHA3-256. They remain valid until
421
+ // the password is changed, at which point setPassword writes scrypt.
422
+ const legacyHash = crypto.createHash('sha3-256').update(password).digest('hex');
423
+ const expected = Buffer.from(config.passwordHash, 'hex');
424
+ const actual = Buffer.from(legacyHash, 'hex');
425
+ return expected.length === actual.length && crypto.timingSafeEqual(expected, actual);
426
+ }
427
+
428
+ function verifyPasswordAsync(password) {
429
+ const config = loadConfig();
430
+ if (!config.passwordHash) return Promise.resolve(true);
431
+ if (!config.passwordHash.startsWith('scrypt$')) {
432
+ return Promise.resolve(verifyPassword(password));
433
+ }
434
+ const parts = config.passwordHash.split('$');
435
+ if (parts.length !== 3) return Promise.resolve(false);
436
+ try {
437
+ const expected = Buffer.from(parts[2], 'base64');
438
+ const salt = Buffer.from(parts[1], 'base64');
439
+ return new Promise(resolve => {
440
+ crypto.scrypt(password, salt, expected.length, (err, actual) => {
441
+ if (err || expected.length !== actual.length) return resolve(false);
442
+ resolve(crypto.timingSafeEqual(expected, actual));
443
+ });
444
+ });
445
+ } catch (_) {
446
+ return Promise.resolve(false);
447
+ }
394
448
  }
395
449
 
396
450
  function setPassword(password) {
@@ -552,6 +606,7 @@ module.exports = {
552
606
  hashPassword,
553
607
  isPasswordSet,
554
608
  verifyPassword,
609
+ verifyPasswordAsync,
555
610
  setPassword,
556
611
  removePassword,
557
612
  getSpeechAiPublicConfig,
@@ -562,4 +617,4 @@ module.exports = {
562
617
  loadSpeechAiProfile,
563
618
  deleteSpeechAiProfile,
564
619
  USER_INSTALL_DIR
565
- };
620
+ };
@@ -13,6 +13,9 @@ let tabOrder = [];
13
13
  // Active tab per panel: Map<panelId, sessionId>
14
14
  const activeTabsByPanel = new Map();
15
15
 
16
+ // Most recently focused tab on the client that last saved tab state.
17
+ let activeSessionId = null;
18
+
16
19
  // Current layout for sync
17
20
  let currentLayout = 'single';
18
21
 
@@ -104,6 +107,7 @@ function removeTab(sessionId) {
104
107
  activeTabsByPanel.delete(panelId);
105
108
  }
106
109
  }
110
+ if (activeSessionId === sessionId) activeSessionId = null;
107
111
  }
108
112
 
109
113
  /**
@@ -236,6 +240,14 @@ function getActiveTabsByPanel() {
236
240
  return activeTabsByPanel;
237
241
  }
238
242
 
243
+ function setActiveSession(sessionId) {
244
+ activeSessionId = sessionId && openTabs.has(sessionId) ? sessionId : null;
245
+ }
246
+
247
+ function getActiveSession() {
248
+ return activeSessionId;
249
+ }
250
+
239
251
  /**
240
252
  * Get current theme
241
253
  * @returns {string} Current theme
@@ -298,9 +310,11 @@ module.exports = {
298
310
  setActiveTab,
299
311
  getActiveTab,
300
312
  getActiveTabsByPanel,
313
+ setActiveSession,
314
+ getActiveSession,
301
315
  getCurrentTheme,
302
316
  setCurrentTheme,
303
317
  getCurrentAccent,
304
318
  setCurrentAccent,
305
319
  forEachTab
306
- };
320
+ };
@@ -1013,6 +1013,20 @@
1013
1013
  </button>
1014
1014
  </div>
1015
1015
  </div>
1016
+ <div class="setting-item">
1017
+ <label class="setting-label">
1018
+ <i class="fas fa-key"></i> Bookmark Connection Encryption
1019
+ </label>
1020
+ <div class="setting-description">
1021
+ Encrypt bookmark hosts, usernames, passwords, private keys, passphrases, ports, and URLs using your app password. Decrypted values are kept only in memory. If you lose the password, this connection data cannot be recovered.
1022
+ </div>
1023
+ <div class="setting-control">
1024
+ <label class="toggle-switch">
1025
+ <input type="checkbox" id="bookmarkEncryptionToggle" disabled>
1026
+ <span class="toggle-slider"></span>
1027
+ </label>
1028
+ </div>
1029
+ </div>
1016
1030
  </div>
1017
1031
 
1018
1032
  <div class="settings-category" id="settings-category-system" style="display:none;">
@@ -1251,8 +1265,18 @@
1251
1265
  <label class="setting-label" style="margin-bottom: 8px;">Confirm Password</label>
1252
1266
  <input type="password" id="passwordModalConfirmInput" class="setting-input" placeholder="Confirm password" autocomplete="off" style="width:100%;">
1253
1267
  </div>
1268
+ <div id="passwordModalEncryptionGroup" style="display: none; margin-top: 16px;">
1269
+ <label class="setting-label" style="display:flex; align-items:center; gap:10px;">
1270
+ <input type="checkbox" id="passwordModalEncryptionToggle">
1271
+ Encrypt bookmark connection data
1272
+ </label>
1273
+ <div class="setting-description" style="margin-top:8px;">
1274
+ If you lose this password, encrypted connection data cannot be recovered.
1275
+ </div>
1276
+ </div>
1254
1277
  </div>
1255
1278
  </div>
1279
+ <p id="passwordModalMessage" class="setting-description" style="display:none; margin-top:8px;"></p>
1256
1280
  <p class="password-modal-error" id="passwordModalError" style="display: none; color: var(--accent-danger); font-size: 13px; margin-top: 8px;"></p>
1257
1281
  </div>
1258
1282
  <div class="modal-footer">
@@ -1642,4 +1666,4 @@
1642
1666
  }
1643
1667
  </script>
1644
1668
  </body>
1645
- </html>
1669
+ </html>