@ai-workbench/connect 0.7.55-rc.4 → 0.7.55-rc.5

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.
@@ -99,7 +99,9 @@ function Write-AiwbFile([string]$Path, [string]$Text, [string]$Sddl) {
99
99
  }
100
100
 
101
101
  # entry.ps1(ForceCommand):cd 到 AW_EMP_CWD(失败回 $HOME);shell 选法同 DSH pwsh-local(PowerShell 7 安装位置 → PATH 中 pwsh.exe → Windows PowerShell 5.1);
102
- # 有 SSH_ORIGINAL_COMMAND 则先固定 UTF-8 输出(同 DSH pwsh-local 的前缀)再以 -EncodedCommand 执行并透传退出码,无则交互;stdin 原样继承。
102
+ # 有 SSH_ORIGINAL_COMMAND 则先关进度流、固定 UTF-8 输出(DSH pwsh-local 的前缀)再以 -EncodedCommand 执行并透传退出码,无则交互;stdin 原样继承。
103
+ # 关进度流:stderr 被管道重定向时,Windows PowerShell 5.1 把进度记录(如模块自动加载的“正在准备首次使用模块”)序列化成 CLIXML 写到 stderr,
104
+ # 混入主机 agent 读到的输出;在内层命令前设 $ProgressPreference='SilentlyContinue'(about_Preference_Variables)即不再产生(实机:外层设无效、内层设即无)。
103
105
  function Get-AiwbEntry {
104
106
  return @'
105
107
  $ErrorActionPreference = 'Continue'
@@ -112,7 +114,7 @@ $candidates += "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
112
114
  $shell = 'pwsh'
113
115
  foreach ($c in $candidates) { if ([IO.File]::Exists($c)) { $shell = $c; break } }
114
116
  if ($env:SSH_ORIGINAL_COMMAND) {
115
- $pre = '[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [System.Text.UTF8Encoding]::new($false); '
117
+ $pre = '$ProgressPreference = ''SilentlyContinue''; [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); $OutputEncoding = [System.Text.UTF8Encoding]::new($false); '
116
118
  $enc = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($pre + $env:SSH_ORIGINAL_COMMAND))
117
119
  & $shell -NoLogo -NoProfile -NonInteractive -EncodedCommand $enc
118
120
  exit $LASTEXITCODE
@@ -31,9 +31,17 @@ export declare function windowsSetSegment(text: string, user: string, programDat
31
31
  export declare function windowsSegmentOk(text: string | undefined, user: string, programData: string): boolean;
32
32
  /** 内联进 -EncodedCommand 的脚本正文:去掉整行注释、空行与行首缩进(只为缩短命令行,语句不变)。 */
33
33
  export declare function inlineScript(script: string): string;
34
+ /**
35
+ * -EncodedCommand 的内层正文:校验过的三个常量 + 一小段引导,引导把 gzip 后 base64 内联的脚本(UTF-8)在内存里解压,
36
+ * 以 scriptblock 在同一作用域执行(脚本末尾 exit 即进程退出码)。只为缩短命令行(CreateProcess/ShellExecuteEx 上限 32767 字符),
37
+ * 脚本语句不变、仍整段内联、不落盘。依据:.NET IO.Compression.GZipStream(RFC 1952,Node zlib.gzipSync 同格式)。
38
+ */
39
+ export declare function windowsAdminBody(script: string, user: string, sid: string, key: string): string;
40
+ /** Windows PowerShell 命令行(CreateProcess)上限 32767 字符;断言与回报按其一半留余量。 */
41
+ export declare const WINDOWS_COMMAND_LINE_MAX = 32767;
34
42
  /**
35
43
  * Windows UAC 提权(设计 C):外层 powershell -Command 以 ShellExecuteEx “runas” 动词(ProcessStartInfo Verb=runas、UseShellExecute、隐藏窗口,
36
- * 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;脚本整段以 UTF-16LE base64 内联(-EncodedCommand),
44
+ * 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;内层正文见 windowsAdminBody(以 UTF-16LE base64 经 -EncodedCommand 内联),
37
45
  * 参数为已校验的常量,不落到用户可写位置。用户在 UAC 点“否”时抛 Win32Exception 1223(ERROR_CANCELLED),外层以其 NativeErrorCode 退出。
38
46
  * 不用 Start-Process 本身:Windows PowerShell 5.1 的 Start-Process 把 Win32Exception 换成只带本地化文字的 InvalidOperationException,丢了错误码(实机核过)。
39
47
  */
@@ -3,6 +3,7 @@ import { execFile } from 'node:child_process';
3
3
  import { stat } from 'node:fs/promises';
4
4
  import { connect } from 'node:net';
5
5
  import { win32 } from 'node:path';
6
+ import { gzipSync } from 'node:zlib';
6
7
  import { shellQuote } from "./ssh.js";
7
8
  import { windowsSecurity } from "./windows-security.js";
8
9
  import { NETWORK_DEFAULTS } from "./defaults.js";
@@ -75,9 +76,20 @@ export function windowsSegmentOk(text, user, programData) { return text !== unde
75
76
  /** 内联进 -EncodedCommand 的脚本正文:去掉整行注释、空行与行首缩进(只为缩短命令行,语句不变)。 */
76
77
  export function inlineScript(script) { return script.split(/\r?\n/).map(line => line.trim()).filter(line => line && !line.startsWith('#')).join('\n'); }
77
78
  const psLiteral = (value) => "'" + value.replaceAll(/['\u2018\u2019\u201A\u201B]/g, match => match + match) + "'";
79
+ /**
80
+ * -EncodedCommand 的内层正文:校验过的三个常量 + 一小段引导,引导把 gzip 后 base64 内联的脚本(UTF-8)在内存里解压,
81
+ * 以 scriptblock 在同一作用域执行(脚本末尾 exit 即进程退出码)。只为缩短命令行(CreateProcess/ShellExecuteEx 上限 32767 字符),
82
+ * 脚本语句不变、仍整段内联、不落盘。依据:.NET IO.Compression.GZipStream(RFC 1952,Node zlib.gzipSync 同格式)。
83
+ */
84
+ export function windowsAdminBody(script, user, sid, key) {
85
+ const packed = gzipSync(Buffer.from(inlineScript(script), 'utf8'), { level: 9 }).toString('base64');
86
+ return `$AiwbUser=${psLiteral(user)};$AiwbSid=${psLiteral(sid)};$AiwbKey=${psLiteral(key)};$z=New-Object IO.Compression.GZipStream((New-Object IO.MemoryStream(,[Convert]::FromBase64String('${packed}'))),[IO.Compression.CompressionMode]::Decompress);$t=(New-Object IO.StreamReader($z,[Text.Encoding]::UTF8)).ReadToEnd();. ([scriptblock]::Create($t))`;
87
+ }
88
+ /** Windows PowerShell 命令行(CreateProcess)上限 32767 字符;断言与回报按其一半留余量。 */
89
+ export const WINDOWS_COMMAND_LINE_MAX = 32767;
78
90
  /**
79
91
  * Windows UAC 提权(设计 C):外层 powershell -Command 以 ShellExecuteEx “runas” 动词(ProcessStartInfo Verb=runas、UseShellExecute、隐藏窗口,
80
- * 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;脚本整段以 UTF-16LE base64 内联(-EncodedCommand),
92
+ * 即 Start-Process -Verb RunAs 的同一底层调用)启动管理员 powershell 并等待,透传其退出码;内层正文见 windowsAdminBody(以 UTF-16LE base64 经 -EncodedCommand 内联),
81
93
  * 参数为已校验的常量,不落到用户可写位置。用户在 UAC 点“否”时抛 Win32Exception 1223(ERROR_CANCELLED),外层以其 NativeErrorCode 退出。
82
94
  * 不用 Start-Process 本身:Windows PowerShell 5.1 的 Start-Process 把 Win32Exception 换成只带本地化文字的 InvalidOperationException,丢了错误码(实机核过)。
83
95
  */
@@ -87,8 +99,7 @@ export function windowsAdminCommand(script, user, sid, key, env = process.env) {
87
99
  const normalized = employeeKey(key);
88
100
  if (normalized === undefined || normalized !== key)
89
101
  throw Error('AW_REMOTE_ACCESS_KEY_INVALID');
90
- const body = `$AiwbUser=${psLiteral(user)};$AiwbSid=${psLiteral(sid)};$AiwbKey=${psLiteral(key)}\n` + inlineScript(script);
91
- const encoded = Buffer.from(body, 'utf16le').toString('base64');
102
+ const encoded = Buffer.from(windowsAdminBody(script, user, sid, key), 'utf16le').toString('base64');
92
103
  const outer = `try{$s=New-Object Diagnostics.ProcessStartInfo;$s.FileName=(Join-Path $PSHOME 'powershell.exe');$s.Arguments='-NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand ${encoded}';$s.Verb='runas';$s.UseShellExecute=$true;$s.WindowStyle='Hidden';$p=[Diagnostics.Process]::Start($s);$p.WaitForExit();exit $p.ExitCode}catch{$e=$_.Exception;while($e){if($e -is [ComponentModel.Win32Exception]){exit $e.NativeErrorCode};$e=$e.InnerException};exit 1}`;
93
104
  return { command: `${env.SYSTEMROOT || env.windir || 'C:\\Windows'}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`, args: ['-NoProfile', '-NonInteractive', '-Command', outer] };
94
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-workbench/connect",
3
- "version": "0.7.55-rc.4",
3
+ "version": "0.7.55-rc.5",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.13.1",