@lethevimlet/sshift 2.0.0 → 2.0.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.
package/README.md CHANGED
@@ -180,6 +180,18 @@ SSHIFT uses a priority-based configuration system. Config files are searched in
180
180
  3. `config.json` `bind` setting
181
181
  4. Default: `0.0.0.0`
182
182
 
183
+ ### Secrets from a secret manager
184
+
185
+ Bookmark passwords, private keys, passphrases and the speech/LLM keys can be
186
+ `"$NAME"` references instead of values: resolved on the server right before
187
+ connecting, never stored in `config.json` and never sent to the browser. Start
188
+ sshift with those variables, e.g. through the
189
+ [sm secret manager](https://github.com/lethevimlet/secret-manager)
190
+ (`sm -c 'exec node …/sshift'` with only the needed secrets), or list your own
191
+ variables in `SSHIFT_SECRET_VARS`. Without either, every value is used literally
192
+ as before. See
193
+ [Configuration → Secrets](docs/configuration.md#secrets-from-a-secret-manager-name-references).
194
+
183
195
  See [Configuration](docs/configuration.md) for details.
184
196
 
185
197
  ## 🔒 HTTPS on Local Network (PWA / "Not Secure" Warnings)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -28,6 +28,7 @@ const {
28
28
  loadSpeechAiProfile,
29
29
  deleteSpeechAiProfile
30
30
  } = require('../../utils/config');
31
+ const { resolveSecret } = require('../../utils/secrets');
31
32
 
32
33
  // Use the global fetch (Node 18+) for proxying. Fall back to undici/https for
33
34
  // ancient environments — but Node >=20 is the project floor, so global fetch
@@ -289,7 +290,7 @@ async function handleSttProxy(req, res) {
289
290
  const upstream = await fetch(url, {
290
291
  method: 'POST',
291
292
  headers: {
292
- 'Authorization': `Bearer ${cfg.sttAuthKey}`,
293
+ 'Authorization': `Bearer ${resolveSecret(cfg.sttAuthKey)}`,
293
294
  'Content-Type': `multipart/form-data; boundary=${boundary}`
294
295
  },
295
296
  body,
@@ -323,7 +324,7 @@ async function handleWandProxy(req, res) {
323
324
  const upstream = await fetch(cfg.llmEndpoint, {
324
325
  method: 'POST',
325
326
  headers: {
326
- 'Authorization': `Bearer ${cfg.llmAuthKey}`,
327
+ 'Authorization': `Bearer ${resolveSecret(cfg.llmAuthKey)}`,
327
328
  'Content-Type': 'application/json'
328
329
  },
329
330
  body: JSON.stringify({
@@ -12,6 +12,10 @@ if (typeof util.isDate !== 'function') {
12
12
  util.isDate = (d) => d instanceof Date;
13
13
  }
14
14
 
15
+ // Secrets injected by the sm secret manager: move them out of process.env into
16
+ // memory before anything else runs, so no child process inherits them.
17
+ require('./utils/secrets').captureSecretsFromEnv();
18
+
15
19
  // Load environment variables from .env files
16
20
  require('./utils/env-loader');
17
21
 
@@ -1,5 +1,6 @@
1
1
  const { Client } = require('ssh2');
2
2
  const { convertKeyIfNeeded } = require('../utils/key-converter');
3
+ const { resolveConnectionSecrets } = require('../utils/secrets');
3
4
  const { removeTab } = require('../utils/tab-manager');
4
5
 
5
6
  class SFTPManager {
@@ -10,6 +11,8 @@ class SFTPManager {
10
11
  }
11
12
 
12
13
  async connect(socket, options) {
14
+ // "$NAME" credentials (sm secret manager) are resolved here, server-side only.
15
+ options = resolveConnectionSecrets(options);
13
16
  // Pre-process private key (auto-convert PPK to OpenSSH format)
14
17
  if (options.privateKey && options.privateKey.length > 0) {
15
18
  try {
@@ -9,6 +9,7 @@ const SYNC_SCROLLBACK_TIERS = [2000, 500, 100, 0];
9
9
  const { Unicode11Addon } = require('@xterm/addon-unicode11');
10
10
  const pluginManager = require('../plugins/plugin-manager');
11
11
  const { convertKeyIfNeeded } = require('../utils/key-converter');
12
+ const { resolveConnectionSecrets } = require('../utils/secrets');
12
13
  const { removeTab } = require('../utils/tab-manager');
13
14
 
14
15
  class SSHManager {
@@ -27,6 +28,8 @@ class SSHManager {
27
28
  }
28
29
 
29
30
  async connect(socket, options) {
31
+ // "$NAME" credentials (sm secret manager) are resolved here, server-side only.
32
+ options = resolveConnectionSecrets(options);
30
33
  // Pre-process private key (auto-convert PPK to OpenSSH format)
31
34
  if (options.privateKey && options.privateKey.length > 0) {
32
35
  try {
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Secrets from the sm secret manager (https://github.com/lethevimlet/secret-manager).
3
+ *
4
+ * Bookmark credentials (password, privateKey, passphrase) and the speech/LLM
5
+ * auth keys may be references of the form "$NAME" instead of the value. They
6
+ * are resolved only on the server, right before an SSH/SFTP connection or an
7
+ * upstream request, so the value is never stored in config.json and never sent
8
+ * to the browser (which only ever sees "$NAME").
9
+ *
10
+ * Only names that were declared as secrets are references: the variables sm
11
+ * lists in __SM_SECRET_VARS, plus an optional space-separated SSHIFT_SECRET_VARS
12
+ * for setups without sm. Every other value, including a real password that
13
+ * happens to start with "$" (e.g. "$ecretPass1" or "$HOME"), is used literally,
14
+ * exactly as before. Without sm nothing changes.
15
+ *
16
+ * Run sshift through sm with only the needed secrets, e.g. in the systemd unit
17
+ * Environment=SM_AUTO_ENV=0
18
+ * Environment=LEVISTATION_PASSWORD={{secret:LEVISTATION_PASSWORD}}
19
+ * ExecStart=/usr/local/bin/sm -c 'exec node …/sshift'
20
+ * At startup captureSecretsFromEnv() moves them out of process.env into memory,
21
+ * so nothing sshift starts (key converter, updater, plugins) inherits them.
22
+ */
23
+
24
+ const SECRET_REF = /^\$([A-Za-z_][A-Za-z0-9_]*)$/;
25
+ const captured = new Map();
26
+
27
+ /**
28
+ * Move every variable sm reports in __SM_SECRET_VARS from the environment into
29
+ * memory, and drop sm's own markers. Returns the number of captured secrets.
30
+ */
31
+ function captureSecretsFromEnv(env = process.env) {
32
+ const names = `${env.__SM_SECRET_VARS || ''} ${env.SSHIFT_SECRET_VARS || ''}`.split(/\s+/).filter(Boolean);
33
+ for (const name of names) {
34
+ if (env[name] !== undefined) {
35
+ captured.set(name, env[name]);
36
+ delete env[name];
37
+ }
38
+ }
39
+ delete env.__SM_SECRET_VARS;
40
+ delete env.SSHIFT_SECRET_VARS;
41
+ delete env.SM_ACTIVE;
42
+ if (env.BASH_ENV && /sm-agents\.bash$/.test(env.BASH_ENV)) {
43
+ if (env.__SM_USER_BASH_ENV) env.BASH_ENV = env.__SM_USER_BASH_ENV;
44
+ else delete env.BASH_ENV;
45
+ }
46
+ delete env.__SM_USER_BASH_ENV;
47
+ return names.length;
48
+ }
49
+
50
+ /** True only for "$NAME" where NAME was declared as a secret at startup. */
51
+ function isSecretRef(value) {
52
+ const m = typeof value === 'string' && SECRET_REF.exec(value);
53
+ return Boolean(m) && captured.has(m[1]);
54
+ }
55
+
56
+ /**
57
+ * "$NAME" of a declared secret -> its value. Anything else, including "$WORD"
58
+ * values that aren't declared secrets, is returned unchanged.
59
+ */
60
+ function resolveSecret(value) {
61
+ return isSecretRef(value) ? captured.get(SECRET_REF.exec(value)[1]) : value;
62
+ }
63
+
64
+ /**
65
+ * Like resolveSecret, but a reference that can't be resolved is an error with
66
+ * a clear message instead of an empty credential.
67
+ */
68
+ function resolveSecretStrict(value, what = 'secret') {
69
+ const resolved = resolveSecret(value);
70
+ if (isSecretRef(value) && !resolved) {
71
+ throw new Error(`${what} ${value} is empty: check the secret ${value.slice(1)} in sm`);
72
+ }
73
+ return resolved;
74
+ }
75
+
76
+ /** Resolve the credential fields of SSH/SFTP connection options. */
77
+ function resolveConnectionSecrets(options) {
78
+ const out = { ...options };
79
+ for (const field of ['password', 'privateKey', 'passphrase']) {
80
+ if (isSecretRef(out[field])) out[field] = resolveSecretStrict(out[field], field);
81
+ }
82
+ return out;
83
+ }
84
+
85
+ module.exports = { SECRET_REF, captureSecretsFromEnv, isSecretRef, resolveSecret, resolveSecretStrict, resolveConnectionSecrets, _captured: captured };