@remoteshell/device 0.1.0-beta.5 → 0.1.0-beta.7
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// #327 S1: direct public-npx runtime. Never copies or installs the package.
|
|
2
2
|
import {existsSync,mkdirSync,openSync,closeSync,readFileSync,rmSync,writeFileSync,lstatSync} from 'node:fs';
|
|
3
3
|
import {join,resolve} from 'node:path';
|
|
4
|
+
import {randomUUID} from 'node:crypto';
|
|
4
5
|
import {spawnSync} from 'node:child_process';
|
|
5
6
|
import {homedir,platform} from 'node:os';
|
|
6
7
|
import {devOfficialRegistration} from './dev-agent-client.mjs';
|
|
@@ -52,22 +53,63 @@ function privateLockDir(p){
|
|
|
52
53
|
}
|
|
53
54
|
return p.storageDir;
|
|
54
55
|
}
|
|
55
|
-
export function acquireDirectLock(p,{pid=process.pid,alive=id=>{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
export function acquireDirectLock(p,{pid=process.pid,alive=id=>{
|
|
57
|
+
try{process.kill(id,0);return true;}catch(e){return e?.code==='EPERM';}
|
|
58
|
+
}}={}){
|
|
59
|
+
const dir=privateLockDir(p),lock=join(dir,'direct-run.lock'),reap=join(dir,'direct-run.reap.lock');
|
|
60
|
+
const nonce=randomUUID();
|
|
61
|
+
const create=()=>{
|
|
62
|
+
let fd;
|
|
63
|
+
try{
|
|
64
|
+
fd=openSync(lock,'wx',0o600);
|
|
65
|
+
writeFileSync(fd,JSON.stringify({pid,created:Date.now(),nonce}));
|
|
66
|
+
closeSync(fd);fd=undefined;
|
|
67
|
+
}catch(e){if(fd!==undefined)closeSync(fd);throw e;}
|
|
68
|
+
return ()=>{
|
|
69
|
+
try{
|
|
70
|
+
const info=JSON.parse(readFileSync(lock,'utf8'));
|
|
71
|
+
if(info.pid===pid&&info.nonce===nonce)rmSync(lock);
|
|
72
|
+
}catch{}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
try{return create();}catch(e){
|
|
76
|
+
if(e.code!=='EEXIST')FAIL('DEVICE_DIRECT_LOCK_DENIED');
|
|
77
|
+
}
|
|
78
|
+
// Linux may be killed by systemd before an npm-wrapper child signals exit.
|
|
79
|
+
// Reclaim only a clearly dead, owner-private PID lock; never replace a live
|
|
80
|
+
// worker or trust a malformed/foreign lock.
|
|
81
|
+
if(p.os!=='linux')FAIL('DEVICE_DIRECT_STALE_LOCK');
|
|
82
|
+
let snapshot,prior,bytes;
|
|
83
|
+
try{
|
|
84
|
+
snapshot=lstatSync(lock);
|
|
85
|
+
if(!snapshot.isFile()||snapshot.isSymbolicLink()||snapshot.uid!==process.getuid?.()||
|
|
86
|
+
(snapshot.mode&0o777)!==0o600||snapshot.size<1||snapshot.size>256)
|
|
87
|
+
FAIL('DEVICE_DIRECT_STALE_LOCK');
|
|
88
|
+
bytes=readFileSync(lock,'utf8');prior=JSON.parse(bytes);
|
|
89
|
+
if(!Number.isSafeInteger(prior?.pid)||prior.pid<=1)
|
|
67
90
|
FAIL('DEVICE_DIRECT_STALE_LOCK');
|
|
68
|
-
|
|
91
|
+
}catch{FAIL('DEVICE_DIRECT_STALE_LOCK');}
|
|
92
|
+
if(alive(prior.pid))FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
|
|
93
|
+
let mutex;
|
|
94
|
+
try{mutex=openSync(reap,'wx',0o600);}catch{FAIL('DEVICE_DIRECT_STALE_LOCK');}
|
|
95
|
+
try{
|
|
96
|
+
const current=lstatSync(lock);
|
|
97
|
+
if(current.dev!==snapshot.dev||current.ino!==snapshot.ino||
|
|
98
|
+
!current.isFile()||readFileSync(lock,'utf8')!==bytes)
|
|
99
|
+
FAIL('DEVICE_DIRECT_STALE_LOCK');
|
|
100
|
+
if(alive(prior.pid))FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
|
|
101
|
+
rmSync(lock);
|
|
102
|
+
}catch(e){
|
|
103
|
+
if(e.message==='DEVICE_DIRECT_ALREADY_RUNNING')throw e;
|
|
104
|
+
FAIL('DEVICE_DIRECT_STALE_LOCK');
|
|
105
|
+
}finally{
|
|
106
|
+
closeSync(mutex);
|
|
107
|
+
rmSync(reap,{force:true});
|
|
108
|
+
}
|
|
109
|
+
try{return create();}catch(e){
|
|
110
|
+
if(e.code==='EEXIST')FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
|
|
111
|
+
FAIL('DEVICE_DIRECT_LOCK_DENIED');
|
|
69
112
|
}
|
|
70
|
-
FAIL('DEVICE_DIRECT_ALREADY_RUNNING');
|
|
71
113
|
}
|
|
72
114
|
export async function directRegister(p,{pair=devPairDevice,onApprove=url=>console.log(JSON.stringify({next:'approve-device-in-browser',approval_url:url,ownerApprovalRequired:true}))}={}){
|
|
73
115
|
requireNoLegacyInstall(p);
|
|
@@ -84,6 +126,8 @@ export async function directRun(p,{packageRoot,runWorker=runDevAgentWorker,guard
|
|
|
84
126
|
const mainFile=join(packageRoot,'src/device-agent/main.mjs');
|
|
85
127
|
if(!existsSync(mainFile))FAIL('DEVICE_DIRECT_PACKAGE_INVALID');
|
|
86
128
|
const release=acquireDirectLock(p);
|
|
129
|
+
// Also clean on normal process exit when the npm parent receives SIGTERM.
|
|
130
|
+
process.once('exit',release);
|
|
87
131
|
try{
|
|
88
132
|
const child=runWorker({gatewayUrl:GATEWAY,credentialFile:join(p.storageDir,'device.json'),
|
|
89
133
|
rootId:registration.deviceId,rootPath:p.home,home:p.home,mainFile});
|
|
@@ -94,5 +138,5 @@ export async function directRun(p,{packageRoot,runWorker=runDevAgentWorker,guard
|
|
|
94
138
|
child.once('exit',(code,signal)=>{cleanup();ok({exitCode:signal?1:(code??1),signal:signal||null});});
|
|
95
139
|
process.once('SIGINT',onInt);process.once('SIGTERM',onTerm);
|
|
96
140
|
});
|
|
97
|
-
}finally{release();}
|
|
141
|
+
}finally{process.removeListener('exit',release);release();}
|
|
98
142
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// #327 Windows owner/DACL query without starting nested PowerShell.
|
|
2
|
+
// Uses built-in cscript/WMI under the caller's OS token; read-only, fail closed.
|
|
3
|
+
import {spawnSync} from 'node:child_process';
|
|
4
|
+
import {win32} from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
const DENY=()=>{throw Error('DEVICE_WINDOWS_ACL_DENIED');};
|
|
7
|
+
const SID=/^S-1-\d+(?:-\d+){1,15}$/;
|
|
8
|
+
export function parseWmiAclRows(output,user,{directory=false}={}){
|
|
9
|
+
if(typeof output!=='string'||output.length>8192||!SID.test(user||''))DENY();
|
|
10
|
+
const expected=directory?['file']:['parent','file'];
|
|
11
|
+
const lines=output.trim().split(/\r?\n/),result={};
|
|
12
|
+
let index=0;
|
|
13
|
+
for(const name of expected){
|
|
14
|
+
if(lines[index++]!==`begin=${name}`)DENY();
|
|
15
|
+
const ownerLine=lines[index++];
|
|
16
|
+
if(!ownerLine?.startsWith('owner='))DENY();
|
|
17
|
+
const owner=ownerLine.slice(6);
|
|
18
|
+
if(!SID.test(owner))DENY();
|
|
19
|
+
const protectedLine=lines[index++];
|
|
20
|
+
if(protectedLine!=='protected=true'&&protectedLine!=='protected=false')DENY();
|
|
21
|
+
const rules=[];
|
|
22
|
+
while(index<lines.length&&lines[index].startsWith('ace=')){
|
|
23
|
+
const match=/^ace=(S-1-\d+(?:-\d+){1,15}),(true|false),([01]),(\d{1,12})$/.exec(lines[index++]);
|
|
24
|
+
if(!match)DENY();
|
|
25
|
+
const rights=Number(match[4]);
|
|
26
|
+
if(!Number.isSafeInteger(rights))DENY();
|
|
27
|
+
rules.push({sid:match[1],inherited:match[2]==='true',
|
|
28
|
+
access:match[3]==='0'?'Allow':'Deny',rights});
|
|
29
|
+
if(rules.length>32)DENY();
|
|
30
|
+
}
|
|
31
|
+
if(lines[index++]!==`end=${name}`||rules.length===0)DENY();
|
|
32
|
+
result[name]={user,owner,protected:protectedLine==='protected=true',rules};
|
|
33
|
+
}
|
|
34
|
+
if(index!==lines.length)DENY();
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
export function inspectWmiAcl(target,{directory=false,run=spawnSync}={}){
|
|
38
|
+
const root=process.env.SystemRoot||'C:\\Windows';
|
|
39
|
+
const call=(name,args,timeout)=>run(win32.join(root,'System32',name),args,{
|
|
40
|
+
shell:false,windowsHide:true,encoding:'utf8',timeout,maxBuffer:8192,
|
|
41
|
+
stdio:['ignore','pipe','pipe']});
|
|
42
|
+
const who=call('whoami.exe',['/user','/fo','csv','/nh'],10000);
|
|
43
|
+
if(who.error||who.signal||who.status!==0)DENY();
|
|
44
|
+
const user=String(who.stdout||'').match(/S-1-\d+(?:-\d+){1,15}/)?.[0];
|
|
45
|
+
if(!SID.test(user||''))DENY();
|
|
46
|
+
const script=fileURLToPath(new URL('./windows-acl-wmi.js',import.meta.url));
|
|
47
|
+
const probe=call('cscript.exe',['//nologo',script,target,directory?'1':'0'],25000);
|
|
48
|
+
if(probe.error||probe.signal||probe.status!==0)DENY();
|
|
49
|
+
return parseWmiAclRows(probe.stdout,user,{directory});
|
|
50
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Windows JScript Host WMI ACL snapshot. Read-only; no ACL repair.
|
|
2
|
+
// Executes under the current Windows user token via cscript.exe.
|
|
3
|
+
try {
|
|
4
|
+
var target = String(WScript.Arguments.Item(0));
|
|
5
|
+
var directory = String(WScript.Arguments.Item(1)) === '1';
|
|
6
|
+
if (target.length > 1024 || !/^[A-Za-z]:\\/.test(target) ||
|
|
7
|
+
/['"\r\n]/.test(target) || (WScript.Arguments.Length !== 2))
|
|
8
|
+
throw new Error('PATH_DENIED');
|
|
9
|
+
var svc = new ActiveXObject('WbemScripting.SWbemLocator').ConnectServer('.', 'root\\cimv2');
|
|
10
|
+
var fso = new ActiveXObject('Scripting.FileSystemObject');
|
|
11
|
+
function inspect(path, label) {
|
|
12
|
+
var q = "SELECT * FROM Win32_LogicalFileSecuritySetting WHERE Path='" +
|
|
13
|
+
path.replace(/\\/g, '\\\\') + "'";
|
|
14
|
+
var found = new Enumerator(svc.ExecQuery(q)), count = 0, record = null;
|
|
15
|
+
for (; !found.atEnd(); found.moveNext()) { record = found.item(); count++; }
|
|
16
|
+
if (count !== 1) throw new Error('MATCH_DENIED');
|
|
17
|
+
var result = record.ExecMethod_('GetSecurityDescriptor');
|
|
18
|
+
if (result.Properties_.Item('ReturnValue').Value !== 0)
|
|
19
|
+
throw new Error('ACCESS_DENIED');
|
|
20
|
+
var descriptor = result.Properties_.Item('Descriptor').Value;
|
|
21
|
+
var owner = descriptor.Properties_.Item('Owner').Value.Properties_.Item('SIDString').Value;
|
|
22
|
+
var flags = descriptor.Properties_.Item('ControlFlags').Value;
|
|
23
|
+
var list = descriptor.Properties_.Item('DACL').Value.toArray();
|
|
24
|
+
if (list.length < 1 || list.length > 32) throw new Error('ACE_COUNT_DENIED');
|
|
25
|
+
WScript.Echo('begin=' + label);
|
|
26
|
+
WScript.Echo('owner=' + owner);
|
|
27
|
+
WScript.Echo('protected=' + ((flags & 4096) !== 0));
|
|
28
|
+
for (var i = 0; i < list.length; i++) {
|
|
29
|
+
var ace = list[i];
|
|
30
|
+
var sid = ace.Properties_.Item('Trustee').Value.Properties_.Item('SIDString').Value;
|
|
31
|
+
WScript.Echo('ace=' + sid + ',' +
|
|
32
|
+
((ace.Properties_.Item('AceFlags').Value & 16) !== 0) + ',' +
|
|
33
|
+
ace.Properties_.Item('AceType').Value + ',' +
|
|
34
|
+
ace.Properties_.Item('AccessMask').Value);
|
|
35
|
+
}
|
|
36
|
+
WScript.Echo('end=' + label);
|
|
37
|
+
}
|
|
38
|
+
if (!directory) inspect(fso.GetParentFolderName(target), 'parent');
|
|
39
|
+
inspect(target, 'file');
|
|
40
|
+
} catch (error) {
|
|
41
|
+
WScript.Echo('DEVICE_WINDOWS_ACL_DENIED');
|
|
42
|
+
WScript.Quit(1);
|
|
43
|
+
}
|
|
@@ -5,6 +5,7 @@ import {spawnSync} from 'node:child_process';
|
|
|
5
5
|
import {lstatSync,realpathSync} from 'node:fs';
|
|
6
6
|
import {win32} from 'node:path';
|
|
7
7
|
import {canonicalWindowsLocalFile,isWindowsChildPath} from './native-path-contract.mjs';
|
|
8
|
+
import {inspectWmiAcl} from './windows-acl-wmi-client.mjs';
|
|
8
9
|
const DENY=()=>{throw Error('DEVICE_WINDOWS_ACL_DENIED');};
|
|
9
10
|
const SID=/^S-1-\d+(?:-\d+){1,15}$/;
|
|
10
11
|
const SYSTEM='S-1-5-18';
|
|
@@ -67,6 +68,11 @@ export function inspectWindowsCredentialAcl({
|
|
|
67
68
|
stat.nlink!==1||stat.size<1||stat.size>8192)DENY();
|
|
68
69
|
if(win32.normalize(realpath(file)).toLowerCase()!==file.toLowerCase()||
|
|
69
70
|
win32.normalize(realpath(parent)).toLowerCase()!==parent.toLowerCase())DENY();
|
|
71
|
+
if(run===spawnSync){
|
|
72
|
+
const snapshot=inspectWmiAcl(file);
|
|
73
|
+
validateWindowsAclSnapshot(snapshot.parent);
|
|
74
|
+
return validateWindowsAclSnapshot(snapshot.file);
|
|
75
|
+
}
|
|
70
76
|
const systemRoot=process.env.SystemRoot||'C:\\Windows';
|
|
71
77
|
canonicalWindowsLocalFile(systemRoot+'\\System32');
|
|
72
78
|
const powerShell=win32.join(systemRoot,'System32','WindowsPowerShell','v1.0','powershell.exe');
|
|
@@ -93,6 +99,7 @@ export function inspectWindowsPrivateDirectoryAcl({directory,home,os=process.pla
|
|
|
93
99
|
const st=lstat(directory);
|
|
94
100
|
if(!st.isDirectory()||st.isSymbolicLink()||
|
|
95
101
|
win32.normalize(realpath(directory)).toLowerCase()!==directory.toLowerCase())DENY();
|
|
102
|
+
if(run===spawnSync)return validateWindowsAclSnapshot(inspectWmiAcl(directory,{directory:true}).file);
|
|
96
103
|
const systemRoot=process.env.SystemRoot||'C:\\Windows';
|
|
97
104
|
const ps=win32.join(systemRoot,'System32','WindowsPowerShell','v1.0','powershell.exe');
|
|
98
105
|
const child=run(ps,['-NoProfile','-NonInteractive','-EncodedCommand',
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remoteshell/device",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.7",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@remoteshell/device",
|
|
9
|
-
"version": "0.1.0-beta.
|
|
9
|
+
"version": "0.1.0-beta.7",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@modelcontextprotocol/sdk": "1.32.1",
|
|
12
12
|
"@wonderwhy-er/desktop-commander": "0.3.1"
|