@remoteshell/device 0.1.0-beta.1 → 0.1.0-beta.11

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 (90) hide show
  1. package/LICENSE +26 -0
  2. package/NOTICE +20 -0
  3. package/bin/dev-agent-client.mjs +228 -0
  4. package/bin/dev-agent-runner.mjs +60 -0
  5. package/bin/dev-api-client.mjs +31 -0
  6. package/bin/dev-auto-worker.mjs +25 -0
  7. package/bin/dev-browser-open.mjs +12 -0
  8. package/bin/dev-dogfood-read.mjs +3 -0
  9. package/bin/dev-heartbeat-service.mjs +77 -0
  10. package/bin/dev-pairing-client.mjs +63 -0
  11. package/bin/dev-secrets.mjs +33 -0
  12. package/bin/dev-setup-recovery.mjs +34 -0
  13. package/bin/dev-signer-pin.mjs +22 -0
  14. package/bin/device.mjs +254 -9
  15. package/bin/direct-npx-runtime.mjs +156 -0
  16. package/bin/install-acquire.mjs +89 -0
  17. package/bin/install-integrity.mjs +74 -0
  18. package/bin/install-plan.mjs +43 -0
  19. package/bin/native-path-contract.mjs +23 -0
  20. package/bin/remoteshell-device-install.cmd +17 -0
  21. package/bin/remoteshell-device-install.ps1 +17 -0
  22. package/bin/remoteshell-device-install.sh +15 -0
  23. package/bin/remoteshell-device-preflight.cmd +10 -0
  24. package/bin/remoteshell-device-preflight.ps1 +10 -0
  25. package/bin/remoteshell-easy-diagnose.ps1 +32 -0
  26. package/bin/remoteshell-easy-install.ps1 +292 -0
  27. package/bin/remoteshell-easy-install.sh +71 -0
  28. package/bin/remoteshell-easy-uninstall.ps1 +128 -0
  29. package/bin/windows-acl-wmi-client.mjs +50 -0
  30. package/bin/windows-acl-wmi.js +43 -0
  31. package/bin/windows-credential-boundary.mjs +113 -0
  32. package/bin/windows-direct-autostart-runner.mjs +50 -0
  33. package/bin/windows-direct-autostart.mjs +110 -0
  34. package/bin/windows-install-layout.mjs +28 -0
  35. package/bin/windows-native-installer.mjs +91 -0
  36. package/bin/windows-private-store.mjs +176 -0
  37. package/bin/windows-user-lifecycle.mjs +34 -0
  38. package/npm-shrinkwrap.json +8591 -0
  39. package/package.json +14 -5
  40. package/shared/lib/bounded-search-contract.mjs +5 -0
  41. package/shared/lib/dc-generic-policy.mjs +102 -0
  42. package/shared/lib/dc-relay-contract.mjs +42 -0
  43. package/shared/lib/dc-tool-catalog.mjs +67 -0
  44. package/shared/lib/dev-dc-relay-contract.mjs +20 -0
  45. package/shared/lib/dev-paged-output.mjs +32 -0
  46. package/shared/lib/dev-root-exclusions.mjs +56 -0
  47. package/{src/common → shared/lib}/synthetic-write-policy.mjs +0 -1
  48. package/src/config/l2-dev-oci-credential.mjs +39 -0
  49. package/src/config/p2-private-credential.mjs +15 -0
  50. package/src/connection/dev-dogfood-processes.mjs +25 -0
  51. package/src/connection/dev-dogfood-read.mjs +64 -0
  52. package/src/connection/dev-dogfood-task.mjs +113 -0
  53. package/src/connection/dev-dogfood-write.mjs +62 -0
  54. package/src/connection/dev-file-task.mjs +119 -0
  55. package/src/connection/dev-r4-file-task.mjs +56 -0
  56. package/src/connection/dev-read-tools.mjs +134 -0
  57. package/src/connection/dev-root-scopes.mjs +82 -0
  58. package/src/{device-agent → core}/c1-supervisor.mjs +9 -7
  59. package/src/{device-agent → core}/c1-worker.mjs +7 -7
  60. package/src/dc/dc-access-readiness.mjs +31 -0
  61. package/src/dc/dev-dc-relay.mjs +47 -0
  62. package/src/device-agent/main.mjs +1 -230
  63. package/src/gateway-keepalive.mjs +73 -0
  64. package/src/main.mjs +436 -0
  65. package/src/{common → policy}/bounded-search-adapter.mjs +2 -3
  66. package/src/{common → policy}/isolated-read-adapter.mjs +1 -1
  67. package/src/{common → policy}/isolated-read-mount.mjs +1 -1
  68. package/src/{common → policy}/synthetic-auxiliary-adapter.mjs +1 -1
  69. package/src/{common → policy}/synthetic-file-adapter.mjs +1 -1
  70. package/src/{common → policy}/synthetic-file-transaction.mjs +1 -1
  71. package/src/{common → policy}/synthetic-write-adapter.mjs +2 -3
  72. /package/{src/common → shared/lib}/bounded-search-policy.mjs +0 -0
  73. /package/{src/common → shared/lib}/config.mjs +0 -0
  74. /package/{src/common → shared/lib}/contracts.mjs +0 -0
  75. /package/{src/common → shared/lib}/errors.mjs +0 -0
  76. /package/{src/common → shared/lib}/events.mjs +0 -0
  77. /package/{src/common → shared/lib}/execution-policy.mjs +0 -0
  78. /package/{src/common → shared/lib}/isolated-read-boundary.mjs +0 -0
  79. /package/{src/common → shared/lib}/isolated-read-policy.mjs +0 -0
  80. /package/{src/common → shared/lib}/lifecycle-events.mjs +0 -0
  81. /package/{src/common → shared/lib}/runtime-timing.mjs +0 -0
  82. /package/{src/common → shared/lib}/synthetic-auxiliary-policy.mjs +0 -0
  83. /package/{src/common → shared/lib}/tool-registry.mjs +0 -0
  84. /package/src/{device-agent → core}/c1-dc-transport.mjs +0 -0
  85. /package/src/{device-agent → core}/c1-startup.mjs +0 -0
  86. /package/src/{device-agent → core}/fixture.txt +0 -0
  87. /package/src/{common → policy}/synthetic-write-mount.mjs +0 -0
  88. /package/src/{common → policy}/verified-bounded-search.mjs +0 -0
  89. /package/src/{common → policy}/verified-isolated-read.mjs +0 -0
  90. /package/src/{common → policy}/verified-synthetic-write.mjs +0 -0
@@ -0,0 +1,176 @@
1
+ // Windows native owner-paired DEV credential storage.
2
+ // No existing RDC/RS credential is read, copied, imported or overwritten.
3
+ import {spawnSync} from 'node:child_process';
4
+ import {existsSync,lstatSync,mkdirSync,readFileSync,realpathSync,renameSync,rmSync,writeFileSync} from 'node:fs';
5
+ import {homedir} from 'node:os';
6
+ import {randomBytes} from 'node:crypto';
7
+ import {win32} from 'node:path';
8
+ import {canonicalWindowsLocalFile,isWindowsChildPath} from './native-path-contract.mjs';
9
+ import {inspectWindowsCredentialAcl,inspectWindowsPrivateDirectoryAcl} from './windows-credential-boundary.mjs';
10
+
11
+ const deny=()=>{throw Error('DEVICE_WINDOWS_PRIVATE_STORE_DENIED');};
12
+ const verifiedUpdate=Symbol('windows-private-preverified-update');
13
+ const sidPattern=/^S-1-\d+(?:-\d+){1,15}$/;
14
+ function nativeTool(name){
15
+ const root=process.env.SystemRoot||'C:\\Windows';
16
+ canonicalWindowsLocalFile(root+'\\System32');
17
+ return win32.join(root,'System32',name);
18
+ }
19
+ function runWin(name,args,{run=spawnSync,maxBuffer=8192}={}){
20
+ const res=run(nativeTool(name),args,{shell:false,windowsHide:true,
21
+ encoding:'utf8',timeout:20000,maxBuffer,stdio:'pipe'});
22
+ if(res.error||res.signal||res.status!==0)deny();
23
+ return String(res.stdout||'');
24
+ }
25
+ function currentSid({run=spawnSync}={}){
26
+ const stdout=runWin('whoami.exe',['/user','/fo','csv','/nh'],{run});
27
+ const sid=stdout.match(/S-1-\d+(?:-\d+){1,15}/)?.[0];
28
+ if(!sidPattern.test(sid||''))deny();
29
+ return sid;
30
+ }
31
+ function checkStore(store,{home=homedir()}={}){
32
+ if(process.platform!=='win32')deny();
33
+ canonicalWindowsLocalFile(home);
34
+ isWindowsChildPath(store,home);
35
+ if(!store.toLowerCase().endsWith('\\dev-credentials'))deny();
36
+ const parent=win32.dirname(store);
37
+ canonicalWindowsLocalFile(parent);
38
+ isWindowsChildPath(parent,home);
39
+ for(const item of [home,parent]){
40
+ const st=lstatSync(item);
41
+ if(!st.isDirectory()||st.isSymbolicLink()||
42
+ realpathSync(item).toLowerCase()!==item.toLowerCase())deny();
43
+ }
44
+ return home;
45
+ }
46
+ export function parseWindowsLogonSids(text){
47
+ if(typeof text!=='string'||text.length>8192)deny();
48
+ const numeric=text.match(/S-1-5-5-\d+-\d+/g)||[];
49
+ // icacls sometimes resolves S-1-5-5-X-Y as LogonSessionId_X_Y.
50
+ const named=[...text.matchAll(/LogonSessionId_(\d+)_(\d+)/gi)]
51
+ .map(match=>'S-1-5-5-'+match[1]+'-'+match[2]);
52
+ return [...new Set([...numeric,...named])];
53
+ }
54
+ function sealAcl(path,{directory=false,run=spawnSync}={}){
55
+ const sid=currentSid({run});
56
+ const suffix=directory?':(OI)(CI)F':':F';
57
+ runWin('icacls.exe',[path,'/inheritance:r','/grant:r','*'+sid+suffix,
58
+ '/grant:r','*S-1-5-18'+suffix],{run});
59
+ // Task Scheduler interactive tokens can leave an explicit per-logon SID on
60
+ // freshly written files. /grant:r replaces only grants for named SIDs; it
61
+ // does not remove this extra entry, which the independent ACL verifier denies.
62
+ // Remove only session SIDs, never broaden the user/SYSTEM allowlist.
63
+ const aclText=runWin('icacls.exe',[path],{run});
64
+ // icacls may resolve the same SID to NT AUTHORITY\\LogonSessionId_N_N.
65
+ // Match both localized display forms without granting any new identity.
66
+ const logonSids=parseWindowsLogonSids(aclText);
67
+ for(const logonSid of logonSids){
68
+ runWin('icacls.exe',[path,'/remove:g','*'+logonSid],{run});
69
+ }
70
+ // Windows service tokens may create new files owned by Administrators even
71
+ // when the creator is LocalSystem. Ensure the owner matches the running SID
72
+ // before the independent NTFS owner/ACL verification.
73
+ if(sid==='S-1-5-18')runWin('icacls.exe',[path,'/setowner','*S-1-5-18'],{run});
74
+ }
75
+ export function ensureWindowsPrivateStore(store,{home=homedir(),run=spawnSync}={}){
76
+ if(process.platform!=='win32')deny();
77
+ try{
78
+ canonicalWindowsLocalFile(home);
79
+ isWindowsChildPath(store,home);
80
+ const base=win32.dirname(store);
81
+ canonicalWindowsLocalFile(base);
82
+ isWindowsChildPath(base,home);
83
+ if(!store.toLowerCase().endsWith('\\dev-credentials'))deny();
84
+ const existed=existsSync(store);
85
+ if(!existed){
86
+ if(!existsSync(base))mkdirSync(base,{recursive:true});
87
+ const parent=lstatSync(base);
88
+ if(!parent.isDirectory()||parent.isSymbolicLink()||
89
+ realpathSync(base).toLowerCase()!==base.toLowerCase())deny();
90
+ mkdirSync(store);
91
+ sealAcl(store,{directory:true,run});
92
+ }
93
+ checkStore(store,{home});
94
+ inspectWindowsPrivateDirectoryAcl({directory:store,home});
95
+ return Object.freeze({ready:true,created:!existed,aclVerified:true});
96
+ }catch{deny();}
97
+ }
98
+ function verifyFile(file,{home=homedir()}={}){
99
+ const store=win32.dirname(file);
100
+ checkStore(store,{home});
101
+ // The native file snapshot also checks its protected parent directory.
102
+ // Avoid a duplicate PowerShell invocation in every credential operation.
103
+ inspectWindowsCredentialAcl({file,home});
104
+ }
105
+ // Seal a newly created exclusive lock using the same owner/SYSTEM ACL proof
106
+ // as credentials. Used by #327 S2 for Windows logoff/reboot recovery.
107
+ export function sealWindowsPrivateFile(file,{home=homedir()}={}){
108
+ try{
109
+ const store=win32.dirname(file);
110
+ checkStore(store,{home});
111
+ const stat=lstatSync(file);
112
+ if(!stat.isFile()||stat.isSymbolicLink()||stat.nlink!==1)deny();
113
+ sealAcl(file);
114
+ verifyFile(file,{home});
115
+ return true;
116
+ }catch{deny();}
117
+ }
118
+ export function readWindowsPrivate(file,{home=homedir()}={}){
119
+ try{
120
+ verifyFile(file,{home});
121
+ return readFileSync(file,'utf8');
122
+ }catch{deny();}
123
+ }
124
+ // A one-use update capability keeps the old credential ACL verification and
125
+ // the new credential ACL verification, without re-launching PowerShell twice
126
+ // for an unchanged old file. It never skips the OS owner/DACL checks.
127
+ export function readWindowsPrivateForUpdate(file,{home=homedir()}={}){
128
+ try{
129
+ verifyFile(file,{home});
130
+ const initial=readFileSync(file,'utf8');
131
+ const stat=lstatSync(file);
132
+ if(!stat.isFile()||stat.isSymbolicLink()||stat.nlink!==1)deny();
133
+ let consumed=false;
134
+ return Object.freeze({content:initial,replace(value){
135
+ if(consumed)deny();
136
+ consumed=true;
137
+ try{
138
+ const current=lstatSync(file);
139
+ if(!current.isFile()||current.isSymbolicLink()||current.nlink!==1||
140
+ current.size!==stat.size||current.mtimeMs!==stat.mtimeMs||
141
+ current.ctimeMs!==stat.ctimeMs||readFileSync(file,'utf8')!==initial)deny();
142
+ return atomicWindowsPrivate(file,value,{replace:true,home,
143
+ [verifiedUpdate]:true});
144
+ }catch{deny();}
145
+ }});
146
+ }catch{deny();}
147
+ }
148
+ export function atomicWindowsPrivate(file,value,{replace=false,home=homedir(),run=spawnSync,...proof}={}){
149
+ let temp;
150
+ try{
151
+ if(typeof value!=='string'||Buffer.byteLength(value)>8192||Buffer.byteLength(value)<1)deny();
152
+ const store=win32.dirname(file);
153
+ canonicalWindowsLocalFile(file);
154
+ isWindowsChildPath(file,store);
155
+ // For replacement, existing file verification already checks the parent
156
+ // directory and file ACLs. New files still require a protected store.
157
+ if(replace){
158
+ if(proof[verifiedUpdate]!==true)verifyFile(file,{home});
159
+ else checkStore(store,{home});
160
+ }else {ensureWindowsPrivateStore(store,{home,run});if(existsSync(file))deny();}
161
+ temp=file+'.tmp-'+randomBytes(12).toString('hex');
162
+ writeFileSync(temp,value,{flag:'wx'});
163
+ // The protected parent directory and successful SID-specific icacls seal
164
+ // protect the temporary file. After atomic rename, verify the final file
165
+ // and parent with a fresh independent native ACL snapshot.
166
+ sealAcl(temp,{run});
167
+ if(!replace&&existsSync(file))deny();
168
+ renameSync(temp,file);
169
+ // Windows file replacement can preserve the destination security descriptor.
170
+ // Seal the final file as well, then independently verify its owner/DACL.
171
+ sealAcl(file,{run});
172
+ verifyFile(file,{home});
173
+ return Object.freeze({written:true,aclVerified:true});
174
+ }catch{deny();
175
+ }finally{if(temp&&existsSync(temp))rmSync(temp,{force:true});}
176
+ }
@@ -0,0 +1,34 @@
1
+ // #310 Windows native per-user agent lifecycle. Explicit opt-in only.
2
+ // No admin service, shell invocation, token copy, or implicit enrolment.
3
+ import {spawnSync} from 'node:child_process';
4
+ import {win32} from 'node:path';
5
+ import {canonicalWindowsLocalFile} from './native-path-contract.mjs';
6
+
7
+ const deny=()=>{throw Error('DEVICE_WINDOWS_LIFECYCLE_DENIED');};
8
+ const TASK='RemoteShell Device Agent (User)';
9
+ const safePath=value=>{try{canonicalWindowsLocalFile(value);return true;}catch{return false;}};
10
+ const quote=value=>'"'+value.replaceAll('"','""')+'"';
11
+ export function windowsUserPlan({action,nodeBin,agentScript,os='win32'}={}){
12
+ if(os!=='win32'||!['install','start','stop','status','uninstall'].includes(action))deny();
13
+ if(action==='install'&&(!safePath(nodeBin)||!safePath(agentScript)))deny();
14
+ // Task Scheduler only supports a single command string in /TR. Paths are
15
+ // validated and quoted, with no cmd.exe or PowerShell in the execution path.
16
+ const base=['/TN',TASK];
17
+ if(action==='install')return Object.freeze({program:'schtasks.exe',
18
+ args:['/Create','/SC','ONLOGON','/RL','LIMITED',...base,
19
+ '/TR',quote(win32.normalize(nodeBin))+' '+quote(win32.normalize(agentScript))+' dev-heartbeat'],
20
+ task:TASK,autostart:true,credentialsPreserved:true,approvalRequired:true});
21
+ if(action==='start')return Object.freeze({program:'schtasks.exe',args:['/Run',...base],task:TASK,credentialsPreserved:true});
22
+ if(action==='stop')return Object.freeze({program:'schtasks.exe',args:['/End',...base],task:TASK,credentialsPreserved:true});
23
+ if(action==='status')return Object.freeze({program:'schtasks.exe',args:['/Query',...base,'/FO','LIST'],task:TASK,credentialsPreserved:true});
24
+ return Object.freeze({program:'schtasks.exe',args:['/Delete',...base,'/F'],task:TASK,credentialsPreserved:true});
25
+ }
26
+ export function runWindowsUserPlan(options,{run=spawnSync,confirmed=false}={}){
27
+ if(!confirmed)deny();
28
+ const plan=windowsUserPlan(options);
29
+ const result=run(plan.program,plan.args,{shell:false,windowsHide:true,encoding:'utf8',
30
+ timeout:10000,maxBuffer:16384,stdio:'pipe'});
31
+ if(result?.error||result?.signal||result?.status!==0)deny();
32
+ return Object.freeze({action:options.action,task:plan.task,commandSucceeded:true,
33
+ connected:false,dcReady:false,credentialsPreserved:true});
34
+ }