ai_co_discuss 0.4.56 → 0.4.57

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai_co_discuss",
3
- "version": "0.4.56",
3
+ "version": "0.4.57",
4
4
  "bin": {
5
5
  "codiscuss": "runner/codiscuss.mjs"
6
6
  },
@@ -5,8 +5,11 @@
5
5
  * owner's files. The owner's management panel creates the user (one elevation prompt), runs programs as it with the
6
6
  * stored password, installs the command-line programs where every user can run them, and signs them in.
7
7
  *
8
- * The password is kept in the owner's service directory, encrypted with Windows DPAPI for the owner's own user, so only
9
- * the owner can use it. Everything here is Windows only.
8
+ * The password is kept in the owner's service directory, which only the owner and administrators can read, encrypted with
9
+ * Windows DPAPI for this computer. The computer scope, not the owner's own, because an administrator signed in over SSH
10
+ * with a key has no user DPAPI key (Windows answers "Access is denied"; seen 2026-11-15 on the hosting test computer),
11
+ * and hosted computers are managed over SSH. Passwords saved earlier with the owner's scope still work where that scope
12
+ * does. Everything here is Windows only.
10
13
  */
11
14
  import { spawnSync } from 'node:child_process';
12
15
  import { randomBytes } from 'node:crypto';
@@ -33,6 +36,9 @@ const JOB_ROOT = join(SHARED_ROOT, 'jobs');
33
36
  // the shared folder, so they cannot depend on it already being secure.
34
37
  const OWNER_JOB_ROOT = join(tmpdir(), 'codiscuss-owner-jobs');
35
38
  const POWERSHELL = 'powershell.exe';
39
+ // Stored passwords encrypted for this computer start with this; older ones are the owner's DPAPI (ConvertFrom-SecureString).
40
+ const MACHINE_SECRET = 'machine:';
41
+ const SECRET_ENTROPY = 'AI CoDiscuss hosted account';
36
42
  const DEFAULT_TIMEOUT_MS = 120_000;
37
43
  const ELEVATED_TIMEOUT_MS = 300_000;
38
44
 
@@ -158,9 +164,16 @@ exit $process.ExitCode`;
158
164
  } finally { rmSync(job, { recursive: true, force: true }); }
159
165
  }
160
166
 
161
- /** The owner's DPAPI encryption of a password, usable only by the owner on this computer. */
167
+ /** A password encrypted with DPAPI for this computer; what keeps it private is the owner-only file it is stored in. */
162
168
  function protect(password) {
163
- return run_powershell(`ConvertTo-SecureString ${quote(password)} -AsPlainText -Force | ConvertFrom-SecureString`);
169
+ return MACHINE_SECRET + run_powershell(`Add-Type -AssemblyName System.Security
170
+ [Convert]::ToBase64String([Security.Cryptography.ProtectedData]::Protect([Text.Encoding]::UTF8.GetBytes(${quote(password)}), [Text.Encoding]::UTF8.GetBytes(${quote(SECRET_ENTROPY)}), 'LocalMachine'))`);
171
+ }
172
+
173
+ /** A PowerShell expression giving the stored password back as a SecureString (either encryption). */
174
+ function secure_secret(secret) {
175
+ if (!secret.startsWith(MACHINE_SECRET)) return `(ConvertTo-SecureString ${quote(secret)})`;
176
+ return `(& { Add-Type -AssemblyName System.Security; ConvertTo-SecureString ([Text.Encoding]::UTF8.GetString([Security.Cryptography.ProtectedData]::Unprotect([Convert]::FromBase64String(${quote(secret.slice(MACHINE_SECRET.length))}), [Text.Encoding]::UTF8.GetBytes(${quote(SECRET_ENTROPY)}), 'LocalMachine'))) -AsPlainText -Force })`;
164
177
  }
165
178
 
166
179
  /**
@@ -173,6 +186,8 @@ export function create_os_user(data, account, account_folder, { run = run_powers
173
186
  const user = os_user_name(account);
174
187
  if (read_os_users(data)[account]) throw new Error(`账号 ${account} 已有 Windows 用户 ${user}`);
175
188
  const password = `Cd!${randomBytes(18).toString('base64url')}`;
189
+ // Encrypted before anything changes, so a failure here leaves the computer as it was.
190
+ const secret = protect(password);
176
191
  run(`$secure = ConvertTo-SecureString ${quote(password)} -AsPlainText -Force
177
192
  if (Get-LocalUser -Name ${quote(user)} -ErrorAction SilentlyContinue) { Set-LocalUser -Name ${quote(user)} -Password $secure }
178
193
  else { New-LocalUser -Name ${quote(user)} -Password $secure -PasswordNeverExpires -UserMayNotChangePassword -Description 'AI CoDiscuss hosted account' | Out-Null }
@@ -180,7 +195,7 @@ if (-not (Get-LocalGroupMember -Group 'Users' -Member ${quote(user)} -ErrorActio
180
195
  icacls ${quote(account_folder)} /grant ${quote(`${user}:(OI)(CI)M`)} /T /C /Q | Out-Null
181
196
  ${register_task_script(account, user, '$secure', owner_sid({ run }))}
182
197
  'ok'`, { elevated: true });
183
- write_os_users(data, { ...read_os_users(data), [account]: { user, secret: protect(password) } });
198
+ write_os_users(data, { ...read_os_users(data), [account]: { user, secret } });
184
199
  return { user };
185
200
  }
186
201
 
@@ -249,7 +264,7 @@ export function run_as_user(data, account, script, { files = {}, folders = {}, v
249
264
  writeFileSync(job, `\ufeff[Console]::OutputEncoding = [Text.Encoding]::UTF8\n$Job = ${quote(folder)}\n$Out = ${quote(join(folder, 'out'))}\n${script}\n`, 'utf8');
250
265
  const command = visible ? `-NoProfile -ExecutionPolicy Bypass -NoExit -File ${quote(job)}`
251
266
  : `-NoProfile -ExecutionPolicy Bypass -Command & ${quote(job)} *> ${quote(output)}`;
252
- const start = `$credential = New-Object System.Management.Automation.PSCredential(${quote(record.user)}, (ConvertTo-SecureString ${quote(record.secret)}))
267
+ const start = `$credential = New-Object System.Management.Automation.PSCredential(${quote(record.user)}, ${secure_secret(record.secret)})
253
268
  $process = Start-Process ${POWERSHELL} -Credential $credential -LoadUserProfile -WorkingDirectory ${quote(folder)} -PassThru ${visible ? '' : '-WindowStyle Hidden'} -ArgumentList ${quote(command)}
254
269
  ${wait ? `if (-not $process.WaitForExit(${timeout})) { $process.Kill(); throw '运行超时' }` : ''}
255
270
  $process.Id`;
@@ -520,7 +535,7 @@ $task.SetSecurityDescriptor($task.GetSecurityDescriptor(4) + '(A;;GRGX;;;${sid})
520
535
  /** Register the instance task of an account isolated before instances ran as tasks (one elevation prompt). */
521
536
  function register_instance_task(data, account, { run = run_powershell } = {}) {
522
537
  const record = read_os_users(data)[account];
523
- run(`$secure = ConvertTo-SecureString ${quote(record.secret)}
538
+ run(`$secure = ${secure_secret(record.secret)}
524
539
  ${register_task_script(account, record.user, '$secure', owner_sid({ run }))}
525
540
  'ok'`, { elevated: true });
526
541
  }