@remoteshell/device 0.0.0-stage → 0.1.0-beta.10
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/LICENSE +26 -0
- package/NOTICE +20 -0
- package/bin/dev-agent-client.mjs +228 -0
- package/bin/dev-agent-runner.mjs +60 -0
- package/bin/dev-api-client.mjs +31 -0
- package/bin/dev-auto-worker.mjs +25 -0
- package/bin/dev-browser-open.mjs +12 -0
- package/bin/dev-dogfood-read.mjs +3 -0
- package/bin/dev-heartbeat-service.mjs +77 -0
- package/bin/dev-pairing-client.mjs +63 -0
- package/bin/dev-secrets.mjs +33 -0
- package/bin/dev-setup-recovery.mjs +34 -0
- package/bin/dev-signer-pin.mjs +22 -0
- package/bin/device.mjs +384 -0
- package/bin/direct-npx-runtime.mjs +150 -0
- package/bin/enrollment-client.mjs +105 -0
- package/bin/install-acquire.mjs +89 -0
- package/bin/install-integrity.mjs +74 -0
- package/bin/install-plan.mjs +43 -0
- package/bin/lifecycle.mjs +172 -0
- package/bin/native-path-contract.mjs +23 -0
- package/bin/remoteshell-device-install.cmd +17 -0
- package/bin/remoteshell-device-install.ps1 +17 -0
- package/bin/remoteshell-device-install.sh +15 -0
- package/bin/remoteshell-device-preflight.cmd +10 -0
- package/bin/remoteshell-device-preflight.ps1 +10 -0
- package/bin/remoteshell-easy-diagnose.ps1 +32 -0
- package/bin/remoteshell-easy-install.ps1 +292 -0
- package/bin/remoteshell-easy-install.sh +71 -0
- package/bin/remoteshell-easy-uninstall.ps1 +128 -0
- package/bin/windows-acl-wmi-client.mjs +50 -0
- package/bin/windows-acl-wmi.js +43 -0
- package/bin/windows-credential-boundary.mjs +113 -0
- package/bin/windows-direct-autostart-runner.mjs +50 -0
- package/bin/windows-direct-autostart.mjs +110 -0
- package/bin/windows-install-layout.mjs +28 -0
- package/bin/windows-native-installer.mjs +91 -0
- package/bin/windows-private-store.mjs +176 -0
- package/bin/windows-user-lifecycle.mjs +34 -0
- package/npm-shrinkwrap.json +8591 -0
- package/package.json +27 -4
- package/shared/lib/bounded-search-contract.mjs +5 -0
- package/shared/lib/bounded-search-policy.mjs +24 -0
- package/shared/lib/config.mjs +14 -0
- package/shared/lib/contracts.mjs +26 -0
- package/shared/lib/dc-generic-policy.mjs +102 -0
- package/shared/lib/dc-relay-contract.mjs +42 -0
- package/shared/lib/dc-tool-catalog.mjs +67 -0
- package/shared/lib/dev-dc-relay-contract.mjs +20 -0
- package/shared/lib/dev-paged-output.mjs +32 -0
- package/shared/lib/dev-root-exclusions.mjs +56 -0
- package/shared/lib/errors.mjs +9 -0
- package/shared/lib/events.mjs +5 -0
- package/shared/lib/execution-policy.mjs +41 -0
- package/shared/lib/isolated-read-boundary.mjs +50 -0
- package/shared/lib/isolated-read-policy.mjs +29 -0
- package/shared/lib/lifecycle-events.mjs +13 -0
- package/shared/lib/runtime-timing.mjs +9 -0
- package/shared/lib/synthetic-auxiliary-policy.mjs +21 -0
- package/shared/lib/synthetic-write-policy.mjs +52 -0
- package/shared/lib/tool-registry.mjs +58 -0
- package/src/config/l2-dev-oci-credential.mjs +39 -0
- package/src/config/p2-private-credential.mjs +15 -0
- package/src/connection/dev-dogfood-processes.mjs +25 -0
- package/src/connection/dev-dogfood-read.mjs +64 -0
- package/src/connection/dev-dogfood-task.mjs +113 -0
- package/src/connection/dev-dogfood-write.mjs +62 -0
- package/src/connection/dev-file-task.mjs +119 -0
- package/src/connection/dev-r4-file-task.mjs +56 -0
- package/src/connection/dev-read-tools.mjs +134 -0
- package/src/connection/dev-root-scopes.mjs +82 -0
- package/src/core/c1-dc-transport.mjs +26 -0
- package/src/core/c1-startup.mjs +22 -0
- package/src/core/c1-supervisor.mjs +111 -0
- package/src/core/c1-worker.mjs +88 -0
- package/src/core/fixture.txt +2 -0
- package/src/dc/dc-access-readiness.mjs +31 -0
- package/src/dc/dev-dc-relay.mjs +47 -0
- package/src/device-agent/main.mjs +1 -0
- package/src/gateway-keepalive.mjs +73 -0
- package/src/main.mjs +436 -0
- package/src/policy/bounded-search-adapter.mjs +115 -0
- package/src/policy/isolated-read-adapter.mjs +50 -0
- package/src/policy/isolated-read-mount.mjs +27 -0
- package/src/policy/synthetic-auxiliary-adapter.mjs +75 -0
- package/src/policy/synthetic-file-adapter.mjs +26 -0
- package/src/policy/synthetic-file-transaction.mjs +87 -0
- package/src/policy/synthetic-write-adapter.mjs +151 -0
- package/src/policy/synthetic-write-mount.mjs +23 -0
- package/src/policy/verified-bounded-search.mjs +12 -0
- package/src/policy/verified-isolated-read.mjs +15 -0
- package/src/policy/verified-synthetic-write.mjs +14 -0
- package/README.md +0 -3
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {openSync,readSync,closeSync,lstatSync} from 'node:fs';
|
|
2
|
+
import {relative} from 'node:path';
|
|
3
|
+
import {resolveReadTarget,validateReadArguments,READ_ONLY_CAPS} from '../../shared/lib/isolated-read-boundary.mjs';
|
|
4
|
+
|
|
5
|
+
// Only usable by an isolated test Agent with an OS-enforced read-only mount.
|
|
6
|
+
// A JS pre-check cannot protect Desktop Commander's separate pathname open against races.
|
|
7
|
+
export function makeIsolatedReadAdapter({root,readonlyMountVerified=false,client}={}){
|
|
8
|
+
if(!readonlyMountVerified||!client||typeof client.callTool!=='function')throw Error('ISOLATED_READ_DISABLED');
|
|
9
|
+
const max=READ_ONLY_CAPS.maxOutputBytes;
|
|
10
|
+
return async function execute(name,args){
|
|
11
|
+
const {path}=validateReadArguments(name,args);
|
|
12
|
+
if(name==='read_multiple_files'){
|
|
13
|
+
const paths=args.paths.map(value=>resolveReadTarget(root,value,{type:'file'}));
|
|
14
|
+
if(paths.some(target=>target.size>READ_ONLY_CAPS.maxFileBytes)||paths.reduce((sum,target)=>sum+target.size,0)>max)throw Error('READ_SCOPE_DENIED');
|
|
15
|
+
for(const target of paths){const fd=openSync(target.path,'r');try{const st=lstatSync(target.path);if(st.size!==target.size||st.size>READ_ONLY_CAPS.maxFileBytes)throw Error('READ_SCOPE_DENIED');const buf=Buffer.alloc(st.size),n=readSync(fd,buf,0,st.size,0);if(n!==st.size||buf.includes(0))throw Error('READ_SCOPE_DENIED');new TextDecoder('utf-8',{fatal:true}).decode(buf);}finally{closeSync(fd);}}
|
|
16
|
+
const result=await client.callTool({name,arguments:{paths:paths.map(target=>target.path)}});
|
|
17
|
+
if(result.isError)throw Error('UPSTREAM_TOOL_ERROR');
|
|
18
|
+
const parts=result.content;if(!Array.isArray(parts)||parts.length>4||parts.some(item=>item.type!=='text'||typeof item.text!=='string'))throw Error('READ_SCOPE_DENIED');
|
|
19
|
+
let value=parts.map(item=>item.text).join('\n');if(Buffer.byteLength(value,'utf8')>max)throw Error('READ_SCOPE_DENIED');
|
|
20
|
+
for(const target of paths)value=value.split(target.path).join(relative(root,target.path));
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
const type=name==='list_directory'?'directory':name==='read_file'?'file':'metadata';
|
|
24
|
+
const target=resolveReadTarget(root,path,{type});
|
|
25
|
+
if(name==='read_file'){
|
|
26
|
+
if(target.size>READ_ONLY_CAPS.maxFileBytes)throw Error('READ_SCOPE_DENIED');
|
|
27
|
+
// Reopening through the DC pathname requires the root be immutable/read-only at OS level.
|
|
28
|
+
const fd=openSync(target.path,'r');
|
|
29
|
+
try{
|
|
30
|
+
const st=lstatSync(target.path);
|
|
31
|
+
if(st.size!==target.size||st.size>READ_ONLY_CAPS.maxFileBytes)throw Error('READ_SCOPE_DENIED');
|
|
32
|
+
const buf=Buffer.alloc(st.size),bytes=readSync(fd,buf,0,st.size,0);
|
|
33
|
+
if(bytes!==st.size||buf.includes(0)||new TextDecoder('utf-8',{fatal:true}).decode(buf).includes('\ufffd'))throw Error('READ_SCOPE_DENIED');
|
|
34
|
+
}finally{closeSync(fd);}
|
|
35
|
+
}
|
|
36
|
+
const upstream=name==='list_directory'?{path:target.path,depth:1}:{path:target.path};
|
|
37
|
+
const result=await client.callTool({name,arguments:upstream});
|
|
38
|
+
if(result.isError)throw Error('UPSTREAM_TOOL_ERROR');
|
|
39
|
+
const parts=result.content;
|
|
40
|
+
if(!Array.isArray(parts)||parts.length>4||parts.some(item=>item.type!=='text'||typeof item.text!=='string'))throw Error('READ_SCOPE_DENIED');
|
|
41
|
+
const value=parts.map(item=>item.text).join('\n');
|
|
42
|
+
if(Buffer.byteLength(value,'utf8')>max)throw Error('READ_SCOPE_DENIED');
|
|
43
|
+
if(name==='list_directory'){
|
|
44
|
+
const lines=value?value.split('\n'):[];
|
|
45
|
+
if(lines.length>READ_ONLY_CAPS.maxEntries||lines.some(line=>!/^\[(?:FILE|DIR)\] [^\r\n]+$/.test(line)))throw Error('READ_SCOPE_DENIED');
|
|
46
|
+
for(const line of lines){const entry=line.replace(/^\[(?:FILE|DIR)\] /,'');if(entry.includes('/')||entry.includes('\\\\')||entry.startsWith('.')||/(?:secret|credential|token|password|private)/i.test(entry))throw Error('READ_SCOPE_DENIED');}
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {readFileSync,statSync} from 'node:fs';
|
|
2
|
+
import {validateReadRoot} from '../../shared/lib/isolated-read-boundary.mjs';
|
|
3
|
+
|
|
4
|
+
const deny=()=>{throw new Error('ISOLATED_READ_DISABLED');};
|
|
5
|
+
const decodeMount=s=>s.replace(/\\040/g,' ').replace(/\\011/g,'\t').replace(/\\012/g,'\n').replace(/\\134/g,'\\');
|
|
6
|
+
export function assertReadOnlyMount(root,{mountInfoPath='/proc/self/mountinfo',uid=process.geteuid?.()}={}){
|
|
7
|
+
if(process.platform!=='linux'||!Number.isInteger(uid))deny();
|
|
8
|
+
const canonical=validateReadRoot(root);
|
|
9
|
+
const stat=statSync(canonical);
|
|
10
|
+
if(stat.uid!==uid || (stat.mode&0o077)!==0)deny();
|
|
11
|
+
let content;
|
|
12
|
+
try{content=readFileSync(mountInfoPath,'utf8');}catch{deny();}
|
|
13
|
+
const found=content.split('\n').filter(Boolean).some(line=>{
|
|
14
|
+
const halves=line.split(' - ');
|
|
15
|
+
if(halves.length!==2)return false;
|
|
16
|
+
const fields=halves[0].split(' ');
|
|
17
|
+
const mounted=decodeMount(fields[4]||'');
|
|
18
|
+
// Require an exact mountpoint, not merely a read-only parent directory.
|
|
19
|
+
if(mounted!==canonical)return false;
|
|
20
|
+
const vfs=fields[5]?.split(',')||[];
|
|
21
|
+
const fs=halves[1].split(' ');
|
|
22
|
+
const superblock=fs[2]?.split(',')||[];
|
|
23
|
+
return vfs.includes('ro')&&superblock.includes('ro');
|
|
24
|
+
});
|
|
25
|
+
if(!found)deny();
|
|
26
|
+
return Object.freeze({root:canonical,uid,readOnly:true});
|
|
27
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {validateAuxiliaryArguments} from '../../shared/lib/synthetic-auxiliary-policy.mjs';
|
|
2
|
+
const deny=()=>{throw Object.assign(Error('SYNTHETIC_AUXILIARY_DENIED'),{code:'SYNTHETIC_AUXILIARY_DENIED'});};
|
|
3
|
+
const rollbackDenied=()=>{throw Object.assign(Error('SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'),{code:'SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'});};
|
|
4
|
+
const textOf=result=>{
|
|
5
|
+
if(result?.isError||!Array.isArray(result?.content)||result.content.length>8||result.content.some(x=>x.type!=='text'||typeof x.text!=='string'))deny();
|
|
6
|
+
const text=result.content.map(x=>x.text).join('\n');if(Buffer.byteLength(text)>262144)deny();return text;
|
|
7
|
+
};
|
|
8
|
+
const call=async(client,name,args)=>{
|
|
9
|
+
const result=await client.callTool({name,arguments:args},undefined,{signal:AbortSignal.timeout(5000)});textOf(result);return result;
|
|
10
|
+
};
|
|
11
|
+
async function config(client){
|
|
12
|
+
const result=await call(client,'get_config',{});let value=result.structuredContent?.config;
|
|
13
|
+
if(!value){const text=textOf(result);if(!text.startsWith('Current configuration:\n'))deny();try{value=JSON.parse(text.slice('Current configuration:\n'.length));}catch{deny();}}
|
|
14
|
+
const out={};for(const key of ['fileReadLineLimit','fileWriteLineLimit']){
|
|
15
|
+
if(!Number.isSafeInteger(value?.[key])||value[key]<1||value[key]>100000)deny();out[key]=value[key];
|
|
16
|
+
}
|
|
17
|
+
if(typeof value.telemetryEnabled==='boolean')out.telemetryEnabled=value.telemetryEnabled;
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Production is restricted to a private DC process/HOME, bound to one
|
|
22
|
+
// owner + connection + device for its lifetime by the C1 worker.
|
|
23
|
+
export async function prepareSyntheticAuxiliaryTransaction(client,tool,{feedbackAuthorized=false}={}){
|
|
24
|
+
const name=tool.name,args=validateAuxiliaryArguments(name,tool.arguments);
|
|
25
|
+
const previous=name==='set_config_value'?await config(client):null;
|
|
26
|
+
let changed=false;
|
|
27
|
+
return {
|
|
28
|
+
async execute(){
|
|
29
|
+
if(name==='get_config')return JSON.stringify({scope:'synthetic_dc_instance',config:await config(client)});
|
|
30
|
+
if(name==='set_config_value'){
|
|
31
|
+
changed=true;await call(client,name,args);await this.verify();return JSON.stringify({scope:'synthetic_dc_instance',updated:true,key:args.key,value:args.value});
|
|
32
|
+
}
|
|
33
|
+
if(name==='get_usage_stats'){
|
|
34
|
+
const text=textOf(await call(client,name,{}));
|
|
35
|
+
const match=/Total calls:\s*(\d+)\s*\((\d+) successful,\s*(\d+) failed\)/.exec(text);if(!match)deny();
|
|
36
|
+
const [totalCalls,successfulCalls,failedCalls]=match.slice(1).map(Number);
|
|
37
|
+
if(![totalCalls,successfulCalls,failedCalls].every(Number.isSafeInteger)||successfulCalls+failedCalls!==totalCalls)deny();
|
|
38
|
+
return JSON.stringify({scope:'synthetic_dc_instance',totalCalls,successfulCalls,failedCalls});
|
|
39
|
+
}
|
|
40
|
+
if(name==='get_recent_tool_calls'){
|
|
41
|
+
const text=textOf(await call(client,name,args)),start=text.indexOf('\n\n');let entries;
|
|
42
|
+
try{entries=JSON.parse(text.slice(start+2));}catch{deny();}
|
|
43
|
+
if(start<0||!Array.isArray(entries)||entries.length>args.maxResults)deny();
|
|
44
|
+
const calls=entries.map(entry=>{
|
|
45
|
+
if(!/^[a-z][a-z0-9_]{0,63}$/.test(entry?.toolName??'')||!Number.isFinite(entry.duration)||entry.duration<0)deny();
|
|
46
|
+
return {toolName:entry.toolName,durationMs:Math.min(Math.round(entry.duration),3600000)};
|
|
47
|
+
});
|
|
48
|
+
return JSON.stringify({scope:'synthetic_dc_instance',calls});
|
|
49
|
+
}
|
|
50
|
+
// The product worker never grants this flag. Do not open an external
|
|
51
|
+
// feedback form or submit telemetry while publication remains on HOLD.
|
|
52
|
+
if(name==='give_feedback_to_desktop_commander'){
|
|
53
|
+
if(feedbackAuthorized!==true)deny();
|
|
54
|
+
await call(client,name,{});return JSON.stringify({feedbackInvoked:true});
|
|
55
|
+
}
|
|
56
|
+
deny();
|
|
57
|
+
},
|
|
58
|
+
async verify(){if(name==='set_config_value'){if((await config(client))[args.key]!==args.value)deny();changed=true;}},
|
|
59
|
+
async rollback(){
|
|
60
|
+
if(!changed)return;
|
|
61
|
+
try{await call(client,'set_config_value',{key:args.key,value:previous[args.key]});if((await config(client))[args.key]!==previous[args.key])deny();changed=false;}
|
|
62
|
+
catch{rollbackDenied();}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function makeSyntheticAuxiliaryAdapter({root,client,verifySandbox,authorized,onAudit}){
|
|
68
|
+
return async(name,args,context)=>{
|
|
69
|
+
const uuid=x=>typeof x==='string'&&/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(x);
|
|
70
|
+
if(!context||!['owner_id','connection_id','device_id','consent_id','requestId'].every(k=>uuid(context[k]))||context.permission!==name||context.consent!==true||context.revoked===true||await authorized(context,name)!==true||verifySandbox(root)!==true)deny();
|
|
71
|
+
const plan=await prepareSyntheticAuxiliaryTransaction(client,{name,arguments:args});
|
|
72
|
+
try{const result=await plan.execute();if(Buffer.byteLength(result)>4096)deny();await onAudit({request_id:context.requestId,tool:name,decision:'allow',outcome:'verified'});return result;}
|
|
73
|
+
catch(error){await plan.rollback();throw error;}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {openSync,closeSync,fstatSync,lstatSync,unlinkSync,constants} from 'node:fs';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
import {validateSyntheticWriteTool,SYNTHETIC_WRITE_LIMITS as limits} from '../../shared/lib/synthetic-write-policy.mjs';
|
|
4
|
+
import {prepareSyntheticFileTransaction} from './synthetic-file-transaction.mjs';
|
|
5
|
+
|
|
6
|
+
const deny=()=>{throw Object.assign(Error('SYNTHETIC_WRITE_DENIED'),{code:'SYNTHETIC_WRITE_DENIED'});};
|
|
7
|
+
const uuid=value=>typeof value==='string'&&/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
|
8
|
+
export function makeSyntheticFileAdapter({root,client,verifySandbox,authorized,onAudit}){
|
|
9
|
+
let requests=0;
|
|
10
|
+
return async (name,args,context)=>{
|
|
11
|
+
const tool=validateSyntheticWriteTool({name,arguments:args},{enabled:true});
|
|
12
|
+
if(!['create_directory','move_file','write_pdf'].includes(name)||++requests>limits.maxRequests||!context||!['owner_id','connection_id','device_id','consent_id'].every(key=>uuid(context[key]))||!/^[-A-Za-z0-9_]{8,64}$/.test(context.requestId??'')||context.consent!==true||context.revoked===true||context.permission!==name||await authorized(context,name)!==true||verifySandbox(root)!==true)deny();
|
|
13
|
+
const lock=join(root,'.rs-c1-writer.lock');let fd,owned,plan;
|
|
14
|
+
try{
|
|
15
|
+
fd=openSync(lock,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600);owned=fstatSync(fd);
|
|
16
|
+
plan=prepareSyntheticFileTransaction(root,tool);
|
|
17
|
+
const result=await client.callTool({name,arguments:plan.arguments},undefined,{signal:AbortSignal.timeout(limits.timeoutMs)});
|
|
18
|
+
if(result?.isError||!Array.isArray(result?.content)||result.content.length>8||result.content.some(item=>item.type!=='text'||typeof item.text!=='string')||Buffer.byteLength(result.content.map(item=>item.text).join('\n'))>limits.maxOutputBytes)deny();
|
|
19
|
+
const output=plan.verify();await onAudit({request_id:context.requestId,tool:name,decision:'allow',outcome:'verified'});return output;
|
|
20
|
+
}catch(error){
|
|
21
|
+
plan?.rollback();if(error?.code==='SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED')throw error;deny();
|
|
22
|
+
}finally{
|
|
23
|
+
if(fd!==undefined){try{const st=lstatSync(lock);if(st.dev!==owned.dev||st.ino!==owned.ino||st.isSymbolicLink())deny();unlinkSync(lock);}finally{closeSync(fd);}}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {lstatSync,realpathSync,readdirSync,readFileSync,renameSync,unlinkSync,rmdirSync} from 'node:fs';
|
|
2
|
+
import {join,dirname,relative,isAbsolute} from 'node:path';
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
|
+
import {validateSyntheticWriteTool,SYNTHETIC_WRITE_LIMITS as limits} from '../../shared/lib/synthetic-write-policy.mjs';
|
|
5
|
+
|
|
6
|
+
const deny=()=>{throw Object.assign(Error('SYNTHETIC_FILE_DENIED'),{code:'SYNTHETIC_FILE_DENIED'});};
|
|
7
|
+
const rollbackDenied=()=>{throw Object.assign(Error('SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'),{code:'SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'});};
|
|
8
|
+
const hash=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
9
|
+
const exists=path=>{try{return lstatSync(path);}catch(e){if(e.code!=='ENOENT')throw e;return null;}};
|
|
10
|
+
const same=(path,expected)=>{const st=exists(path);if(!st||st.isSymbolicLink()||st.dev!==expected.dev||st.ino!==expected.ino||st.nlink!==expected.nlink)deny();return st;};
|
|
11
|
+
|
|
12
|
+
export function inspectSyntheticFileTree(root){
|
|
13
|
+
const canonical=realpathSync(root),st=lstatSync(root);if(canonical!==root||!st.isDirectory()||st.isSymbolicLink()||(st.mode&0o077)!==0)deny();
|
|
14
|
+
let files=0,bytes=0,entries=0;
|
|
15
|
+
// File names are checked separately to avoid inventing a move destination.
|
|
16
|
+
const scan=(dir,depth)=>{
|
|
17
|
+
if(depth>8)deny();
|
|
18
|
+
for(const name of readdirSync(dir)){
|
|
19
|
+
if(dir===root&&name==='.rs-c1-writer.lock')continue;
|
|
20
|
+
const path=join(dir,name),st=lstatSync(path);
|
|
21
|
+
if(dir===root&&name==='.dc-home'&&st.isDirectory()&&!st.isSymbolicLink()&&(st.mode&0o077)===0&&realpathSync(path)===path)continue;
|
|
22
|
+
if(++entries>64||st.isSymbolicLink()||realpathSync(path)!==path||st.uid!==process.geteuid())deny();
|
|
23
|
+
if(st.isDirectory()){validateSyntheticWriteTool({name:'create_directory',arguments:{path:relative(root,path)}},{enabled:true});scan(path,depth+1);}
|
|
24
|
+
else if(st.isFile()&&st.nlink===1){
|
|
25
|
+
const rel=relative(root,path),ext=rel.endsWith('.pdf')?'pdf':'txt';
|
|
26
|
+
validateSyntheticWriteTool({name:'move_file',arguments:{source:rel,destination:rel==='probe.'+ext?'other.'+ext:'probe.'+ext}},{enabled:true});
|
|
27
|
+
if(++files>limits.maxFiles||st.size>limits.fileBytes||(bytes+=st.size)>limits.maxRootBytes)deny();
|
|
28
|
+
const data=readFileSync(path);
|
|
29
|
+
if(ext==='pdf'){if(data.subarray(0,5).toString()!=='%PDF-')deny();}
|
|
30
|
+
else if(data.includes(0)||/[\x01-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(new TextDecoder('utf-8',{fatal:true}).decode(data)))deny();
|
|
31
|
+
}else deny();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
scan(root,0);return {files,bytes,entries};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The production caller owns an attested private tmpfs and a single-writer lock.
|
|
38
|
+
// This plan is also held by the worker until the Gateway's finalize handshake.
|
|
39
|
+
export function prepareSyntheticFileTransaction(root,tool){
|
|
40
|
+
const {name,arguments:args}=validateSyntheticWriteTool(tool,{enabled:true});
|
|
41
|
+
if(!['create_directory','move_file','write_pdf'].includes(name))deny();
|
|
42
|
+
const inventory=inspectSyntheticFileTree(root);
|
|
43
|
+
const target=rel=>{
|
|
44
|
+
const path=join(root,rel),delta=relative(root,path);
|
|
45
|
+
if(!delta||delta.startsWith('..')||isAbsolute(delta))deny();
|
|
46
|
+
let current=root;
|
|
47
|
+
for(const part of relative(root,dirname(path)).split('/').filter(Boolean)){
|
|
48
|
+
current=join(current,part);const st=lstatSync(current);
|
|
49
|
+
if(!st.isDirectory()||st.isSymbolicLink()||realpathSync(current)!==current)deny();
|
|
50
|
+
}
|
|
51
|
+
return path;
|
|
52
|
+
};
|
|
53
|
+
const path=target(name==='move_file'?args.destination:args.path);
|
|
54
|
+
if(exists(path)||inventory.entries>=64||name==='write_pdf'&&inventory.files>=limits.maxFiles)deny();
|
|
55
|
+
const source=name==='move_file'?target(args.source):null;
|
|
56
|
+
const before=source?lstatSync(source):null;
|
|
57
|
+
if(before&&(!before.isFile()||before.isSymbolicLink()||before.nlink!==1||before.size>limits.fileBytes))deny();
|
|
58
|
+
const digest=source?hash(readFileSync(source)):null;
|
|
59
|
+
let after=null;
|
|
60
|
+
return {
|
|
61
|
+
arguments:name==='move_file'?{source,destination:path}:name==='write_pdf'?{path,content:args.content}:{path},
|
|
62
|
+
verify(){
|
|
63
|
+
const st=lstatSync(path);
|
|
64
|
+
if(st.isSymbolicLink()||st.uid!==process.geteuid()||realpathSync(path)!==path)deny();
|
|
65
|
+
if(name==='create_directory'){if(!st.isDirectory()||readdirSync(path).length)deny();}
|
|
66
|
+
else if(!st.isFile()||st.nlink!==1||st.size>limits.fileBytes)deny();
|
|
67
|
+
if(name==='move_file'&&(exists(source)||st.dev!==before.dev||st.ino!==before.ino||hash(readFileSync(path))!==digest))deny();
|
|
68
|
+
inspectSyntheticFileTree(root);after=st;
|
|
69
|
+
return JSON.stringify(name==='create_directory'?{created:true,type:'directory'}:name==='move_file'?{moved:true,sha256:digest}:{created:true,type:'pdf',bytes:st.size,sha256:hash(readFileSync(path))});
|
|
70
|
+
},
|
|
71
|
+
rollback(){
|
|
72
|
+
try{
|
|
73
|
+
const st=exists(path);
|
|
74
|
+
if(name==='move_file'){
|
|
75
|
+
if(st){same(path,before);if(exists(source)||hash(readFileSync(path))!==digest)deny();renameSync(path,source);}
|
|
76
|
+
same(source,before);if(hash(readFileSync(source))!==digest)deny();
|
|
77
|
+
}else if(st){
|
|
78
|
+
if(after)same(path,after);
|
|
79
|
+
if(st.isSymbolicLink()||st.uid!==process.geteuid()||realpathSync(path)!==path)deny();
|
|
80
|
+
if(name==='create_directory'){if(!st.isDirectory()||readdirSync(path).length)deny();rmdirSync(path);}
|
|
81
|
+
else {if(!st.isFile()||st.nlink!==1)deny();unlinkSync(path);}
|
|
82
|
+
}
|
|
83
|
+
if(exists(path))deny();
|
|
84
|
+
}catch{rollbackDenied();}
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {makeSyntheticAuxiliaryAdapter} from './synthetic-auxiliary-adapter.mjs';
|
|
2
|
+
import {isSyntheticAuxiliaryName} from '../../shared/lib/synthetic-auxiliary-policy.mjs';
|
|
3
|
+
import {makeSyntheticFileAdapter} from './synthetic-file-adapter.mjs';
|
|
4
|
+
import {inspectSyntheticFileTree} from './synthetic-file-transaction.mjs';
|
|
5
|
+
import {validateSyntheticWriteTool,SYNTHETIC_WRITE_LIMITS} from '../../shared/lib/synthetic-write-policy.mjs';
|
|
6
|
+
import {createHash,randomUUID} from 'node:crypto';
|
|
7
|
+
import {constants} from 'node:fs';
|
|
8
|
+
import {openSync,closeSync,fstatSync,fsyncSync,lstatSync,realpathSync,readFileSync,writeFileSync,renameSync,unlinkSync,readdirSync} from 'node:fs';
|
|
9
|
+
import {resolve,join,relative,isAbsolute,dirname,sep} from 'node:path';
|
|
10
|
+
|
|
11
|
+
const deny=()=>{throw Object.assign(new Error('SYNTHETIC_WRITE_DENIED'),{code:'SYNTHETIC_WRITE_DENIED'});};
|
|
12
|
+
const rollbackDeny=()=>{throw Object.assign(new Error('SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'),{code:'SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED'});};
|
|
13
|
+
export const C1_LIMITS=SYNTHETIC_WRITE_LIMITS;
|
|
14
|
+
const LOCK='.rs-c1-writer.lock';
|
|
15
|
+
const sensitive=/(?:^|[_-])(?:secret|credential|token|password|private|key)(?:[_-]|$)|^id_[a-z0-9]+$/i;
|
|
16
|
+
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
17
|
+
const uuid=value=>typeof value==='string'&&/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
|
18
|
+
|
|
19
|
+
function safeName(value){
|
|
20
|
+
if(typeof value!=='string'||Buffer.byteLength(value)>C1_LIMITS.maxPathBytes||!/^([A-Za-z0-9_-]+\/)*[A-Za-z0-9_-]+\.txt$/.test(value))deny();
|
|
21
|
+
const parts=value.split('/');if(parts.some(x=>x==='.'||x==='..'||x.startsWith('.')||sensitive.test(x.replace(/\.txt$/i,''))))deny();
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function safeRoot(root,verifySandbox){
|
|
25
|
+
if(typeof root!=='string'||!isAbsolute(root)||typeof verifySandbox!=='function')deny();
|
|
26
|
+
let current,stat;try{current=realpathSync(root);stat=lstatSync(current);}catch{deny();}
|
|
27
|
+
if(!stat.isDirectory()||stat.isSymbolicLink()||(stat.mode&0o077)!==0||verifySandbox(current)!==true)deny();
|
|
28
|
+
return current;
|
|
29
|
+
}
|
|
30
|
+
function resolveTarget(root,rel,{existing}){
|
|
31
|
+
const parts=rel.split('/'),file=parts.pop();let dir=root;
|
|
32
|
+
for(const part of parts){
|
|
33
|
+
dir=join(dir,part);let st;try{st=lstatSync(dir);}catch{deny();}
|
|
34
|
+
if(!st.isDirectory()||st.isSymbolicLink()||realpathSync(dir)!==dir)deny();
|
|
35
|
+
}
|
|
36
|
+
const abs=resolve(dir,file),delta=relative(root,abs);
|
|
37
|
+
if(delta==='..'||delta.startsWith('..'+sep)||isAbsolute(delta))deny();
|
|
38
|
+
let st;try{st=lstatSync(abs);}catch(e){if(e?.code!=='ENOENT')deny();}
|
|
39
|
+
if(existing&&(!st||!st.isFile()||st.isSymbolicLink()||st.nlink!==1||realpathSync(abs)!==abs))deny();
|
|
40
|
+
if(!existing&&st)deny();
|
|
41
|
+
return abs;
|
|
42
|
+
}
|
|
43
|
+
function decodeText(bytes){
|
|
44
|
+
let value;try{value=new TextDecoder('utf-8',{fatal:true}).decode(bytes);}catch{deny();}
|
|
45
|
+
if(value.includes('\0')||/[\x01-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(value))deny();
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function text(value){
|
|
49
|
+
if(typeof value!=='string'||Buffer.byteLength(value)>C1_LIMITS.fileBytes||/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(value))deny();
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function snapshot(abs){
|
|
53
|
+
let fd;try{fd=openSync(abs,constants.O_RDONLY|constants.O_NOFOLLOW|constants.O_CLOEXEC);}catch{deny();}
|
|
54
|
+
try{
|
|
55
|
+
const opened=fstatSync(fd),named=lstatSync(abs);
|
|
56
|
+
if(!opened.isFile()||opened.uid!==process.geteuid?.()||opened.nlink!==1||named.isSymbolicLink()||named.dev!==opened.dev||named.ino!==opened.ino||realpathSync(abs)!==abs||opened.size>C1_LIMITS.fileBytes)deny();
|
|
57
|
+
const bytes=readFileSync(fd);if(bytes.length!==opened.size)deny();
|
|
58
|
+
return {dev:opened.dev,ino:opened.ino,bytes,content:decodeText(bytes),mode:opened.mode&0o777};
|
|
59
|
+
}finally{closeSync(fd);}
|
|
60
|
+
}
|
|
61
|
+
function sameInode(abs,expected){
|
|
62
|
+
let st;try{st=lstatSync(abs);}catch{deny();}
|
|
63
|
+
if(!st.isFile()||st.isSymbolicLink()||st.nlink!==1||st.dev!==expected.dev||st.ino!==expected.ino||realpathSync(abs)!==abs)deny();
|
|
64
|
+
return st;
|
|
65
|
+
}
|
|
66
|
+
function syncDirectory(path){let fd;try{fd=openSync(path,constants.O_RDONLY|constants.O_DIRECTORY);fsyncSync(fd);}finally{if(fd!==undefined)closeSync(fd);}}
|
|
67
|
+
function restore(abs,root,rel,original){
|
|
68
|
+
let temp,fd;
|
|
69
|
+
try{
|
|
70
|
+
const parent=dirname(abs);resolveTarget(root,rel,{existing:true});
|
|
71
|
+
temp=join(parent,`.rs-rollback-${randomUUID()}`);
|
|
72
|
+
fd=openSync(temp,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600);
|
|
73
|
+
writeFileSync(fd,original.bytes);fsyncSync(fd);closeSync(fd);fd=undefined;
|
|
74
|
+
resolveTarget(root,rel,{existing:true});
|
|
75
|
+
renameSync(temp,abs);temp=undefined;syncDirectory(parent);
|
|
76
|
+
const restored=snapshot(abs);
|
|
77
|
+
if(!restored.bytes.equals(original.bytes)||sha(restored.bytes)!==sha(original.bytes))rollbackDeny();
|
|
78
|
+
}catch{rollbackDeny();}
|
|
79
|
+
finally{if(fd!==undefined)closeSync(fd);if(temp)try{unlinkSync(temp);}catch{}}
|
|
80
|
+
}
|
|
81
|
+
function removeCreated(abs,root,rel,owned){
|
|
82
|
+
try{
|
|
83
|
+
const stat=sameInode(abs,owned);resolveTarget(root,rel,{existing:true});
|
|
84
|
+
if(stat.dev!==owned.dev||stat.ino!==owned.ino)rollbackDeny();
|
|
85
|
+
unlinkSync(abs);syncDirectory(dirname(abs));
|
|
86
|
+
try{lstatSync(abs);rollbackDeny();}catch(e){if(e?.code!=='ENOENT')throw e;}
|
|
87
|
+
}catch{rollbackDeny();}
|
|
88
|
+
}
|
|
89
|
+
const inspectRoot=inspectSyntheticFileTree;
|
|
90
|
+
const exactKeys=(obj,keys)=>!!obj&&Object.getPrototypeOf(obj)===Object.prototype&&Object.keys(obj).every(k=>keys.includes(k));
|
|
91
|
+
function checkResult(result){
|
|
92
|
+
if(result?.isError===true||!Array.isArray(result?.content)||result.content.length>8||result.content.some(item=>item?.type!=='text'||typeof item.text!=='string'))deny();
|
|
93
|
+
const output=result.content.map(item=>item.text).join('\n');if(Buffer.byteLength(output)>C1_LIMITS.maxOutputBytes)deny();
|
|
94
|
+
}
|
|
95
|
+
function takeLock(root){
|
|
96
|
+
let fd;try{fd=openSync(join(root,LOCK),constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600);}catch{deny();}
|
|
97
|
+
const st=fstatSync(fd);if(!st.isFile()||st.nlink!==1){closeSync(fd);deny();}
|
|
98
|
+
return ()=>{try{const named=lstatSync(join(root,LOCK));if(named.dev!==st.dev||named.ino!==st.ino||named.isSymbolicLink())rollbackDeny();unlinkSync(join(root,LOCK));syncDirectory(root);}finally{closeSync(fd);}};
|
|
99
|
+
}
|
|
100
|
+
const nativeCall=(client,name,args)=>client.callTool({name,arguments:args},undefined,{signal:AbortSignal.timeout(C1_LIMITS.timeoutMs)});
|
|
101
|
+
|
|
102
|
+
// This is a low-level adapter. Production code must use the verified wrapper,
|
|
103
|
+
// which checks a kernel-reported exact tmpfs mount; never trust a caller boolean.
|
|
104
|
+
export function makeSyntheticWriteAdapter({enabled=false,root,client,verifySandbox,authorized=()=>false,onAudit}={}){
|
|
105
|
+
if(enabled!==true||!client||typeof client.callTool!=='function'||typeof authorized!=='function'||typeof onAudit!=='function')deny();
|
|
106
|
+
const canonical=safeRoot(root,verifySandbox);let requests=0;
|
|
107
|
+
const runAuxiliary=makeSyntheticAuxiliaryAdapter({root:canonical,client,verifySandbox,authorized,onAudit});
|
|
108
|
+
const runFiles=makeSyntheticFileAdapter({root:canonical,client,verifySandbox,authorized,onAudit});
|
|
109
|
+
async function execute(name,args,context){
|
|
110
|
+
if(isSyntheticAuxiliaryName(name))return runAuxiliary(name,args,context);
|
|
111
|
+
if(['create_directory','move_file','write_pdf'].includes(name))return runFiles(name,args,context);
|
|
112
|
+
try{args=validateSyntheticWriteTool({name,arguments:args},{enabled:true}).arguments;}catch{deny();}
|
|
113
|
+
if(name!=='write_file'&&name!=='edit_block'||!exactKeys(args,name==='write_file'?['path','content']:['path','old_string','new_string','expectedHash']))deny();
|
|
114
|
+
if(++requests>C1_LIMITS.maxRequests||!uuid(context?.owner_id)||!uuid(context?.connection_id)||!uuid(context?.device_id)||!uuid(context?.consent_id)||typeof context?.requestId!=='string'||!/^[-A-Za-z0-9_]{8,64}$/.test(context.requestId)||context?.consent!==true||context?.revoked===true||context?.permission!==name||Buffer.byteLength(JSON.stringify(args))>C1_LIMITS.requestBytes)deny();
|
|
115
|
+
if(await authorized(context,name)!==true)deny();
|
|
116
|
+
if(safeRoot(root,verifySandbox)!==canonical)deny();
|
|
117
|
+
const rel=safeName(args.path),release=takeLock(canonical);let abs,owned,original,created=false;
|
|
118
|
+
try{
|
|
119
|
+
const inventory=inspectRoot(canonical),existing=name==='edit_block';
|
|
120
|
+
abs=resolveTarget(canonical,rel,{existing});
|
|
121
|
+
if(name==='write_file'){
|
|
122
|
+
const desired=text(args.content),desiredBytes=Buffer.from(desired,'utf8');
|
|
123
|
+
if(inventory.files>=C1_LIMITS.maxFiles||inventory.bytes+desiredBytes.length>C1_LIMITS.maxRootBytes)deny();
|
|
124
|
+
// Reserve with O_EXCL before the native pathname-based tool call. This
|
|
125
|
+
// closes create races between our absence check and write_file's open.
|
|
126
|
+
let fd;try{fd=openSync(abs,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,0o600);created=true;owned=fstatSync(fd);writeFileSync(fd,desiredBytes);fsyncSync(fd);}catch{deny();}finally{if(fd!==undefined)closeSync(fd);}
|
|
127
|
+
sameInode(abs,owned);const result=await nativeCall(client,name,{path:abs,content:desired,mode:'rewrite',origin:'llm'});checkResult(result);
|
|
128
|
+
const after=snapshot(abs);if(after.dev!==owned.dev||after.ino!==owned.ino||!after.bytes.equals(desiredBytes))deny();
|
|
129
|
+
try{await onAudit({request_id:context.requestId,tool:name,decision:'allow',outcome:'verified'});}catch{deny();}
|
|
130
|
+
return JSON.stringify({created:true,bytes:desiredBytes.length,sha256:sha(desiredBytes)});
|
|
131
|
+
}
|
|
132
|
+
const old=text(args.old_string),replacement=text(args.new_string);
|
|
133
|
+
if(!old.length||typeof args.expectedHash!=='string'||!/^([a-f0-9]{64})$/.test(args.expectedHash))deny();
|
|
134
|
+
original=snapshot(abs);if(sha(original.bytes)!==args.expectedHash||original.content.split(old).length!==2)deny();
|
|
135
|
+
const expected=original.content.replace(old,replacement),expectedBytes=Buffer.from(expected,'utf8');
|
|
136
|
+
if(expectedBytes.length>C1_LIMITS.fileBytes||inventory.bytes-original.bytes.length+expectedBytes.length>C1_LIMITS.maxRootBytes)deny();
|
|
137
|
+
const result=await nativeCall(client,'edit_block',{file_path:abs,old_string:old,new_string:replacement,expected_replacements:1,origin:'llm'});checkResult(result);
|
|
138
|
+
const after=snapshot(abs);if(after.dev!==original.dev||after.ino!==original.ino||!after.bytes.equals(expectedBytes))deny();
|
|
139
|
+
try{await onAudit({request_id:context.requestId,tool:name,decision:'allow',outcome:'verified'});}catch{deny();}
|
|
140
|
+
return JSON.stringify({edited:true,sha256:sha(expectedBytes),previous_sha256:sha(original.bytes),bytes:expectedBytes.length});
|
|
141
|
+
}catch(error){
|
|
142
|
+
try{
|
|
143
|
+
if(created&&abs&&owned)removeCreated(abs,canonical,rel,owned);
|
|
144
|
+
else if(name==='edit_block'&&abs&&original)restore(abs,canonical,rel,original);
|
|
145
|
+
}catch{rollbackDeny();}
|
|
146
|
+
if(error?.code==='SYNTHETIC_WRITE_ROLLBACK_UNVERIFIED')throw error;
|
|
147
|
+
deny();
|
|
148
|
+
}finally{release();}
|
|
149
|
+
}
|
|
150
|
+
return execute;
|
|
151
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {lstatSync,realpathSync,readFileSync} from 'node:fs';
|
|
2
|
+
import {isAbsolute,resolve} from 'node:path';
|
|
3
|
+
|
|
4
|
+
const deny=()=>{throw Object.assign(new Error('SYNTHETIC_WRITE_DISABLED'),{code:'SYNTHETIC_WRITE_DISABLED'});};
|
|
5
|
+
const decodeMount=value=>value.replace(/\\040/g,' ').replace(/\\011/g,'\t').replace(/\\012/g,'\n').replace(/\\134/g,'\\');
|
|
6
|
+
|
|
7
|
+
// The write root must be its own writable tmpfs mount. A chmod'd directory or
|
|
8
|
+
// caller-provided boolean is not evidence of filesystem isolation.
|
|
9
|
+
export function assertSyntheticWritableMount(root,{mountInfoPath='/proc/self/mountinfo',uid=process.geteuid?.()}={}){
|
|
10
|
+
if(process.platform!=='linux'||!Number.isInteger(uid)||typeof root!=='string'||!isAbsolute(root)||root==='/'||root.length>1024)deny();
|
|
11
|
+
let stat,canonical,content;
|
|
12
|
+
try{stat=lstatSync(root);canonical=realpathSync(root);content=readFileSync(mountInfoPath,'utf8');}catch{deny();}
|
|
13
|
+
if(!stat.isDirectory()||stat.isSymbolicLink()||canonical!==resolve(root)||stat.uid!==uid||(stat.mode&0o077)!==0)deny();
|
|
14
|
+
const found=content.split('\n').filter(Boolean).some(line=>{
|
|
15
|
+
const halves=line.split(' - ');if(halves.length!==2)return false;
|
|
16
|
+
const fields=halves[0].split(' '),mountpoint=decodeMount(fields[4]||'');
|
|
17
|
+
if(mountpoint!==canonical)return false;
|
|
18
|
+
const vfs=new Set((fields[5]||'').split(',')),fs=halves[1].split(' '),superblock=new Set((fs[2]||'').split(','));
|
|
19
|
+
return fs[0]==='tmpfs'&&vfs.has('rw')&&superblock.has('rw')&&['nosuid','nodev','noexec'].every(flag=>vfs.has(flag));
|
|
20
|
+
});
|
|
21
|
+
if(!found)deny();
|
|
22
|
+
return Object.freeze({root:canonical,uid,writable:true,filesystem:'tmpfs'});
|
|
23
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {assertReadOnlyMount} from './isolated-read-mount.mjs';
|
|
2
|
+
import {makeBoundedSearchAdapter} from './bounded-search-adapter.mjs';
|
|
3
|
+
|
|
4
|
+
export function makeVerifiedBoundedSearchAdapter({root,rootScopeId,client}={}){
|
|
5
|
+
if(typeof root!=='string'||typeof rootScopeId!=='string'||!client||typeof client.callTool!=='function')throw Error('BOUNDED_SEARCH_DISABLED');
|
|
6
|
+
const initial=assertReadOnlyMount(root);
|
|
7
|
+
const run=makeBoundedSearchAdapter({root:initial.root,rootScopeId,client,verifyRoot:expected=>{
|
|
8
|
+
const current=assertReadOnlyMount(expected);
|
|
9
|
+
if(current.root!==initial.root||current.uid!==initial.uid||!current.readOnly)throw Error('BOUNDED_SEARCH_DISABLED');
|
|
10
|
+
}});
|
|
11
|
+
return run;
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {assertReadOnlyMount} from './isolated-read-mount.mjs';
|
|
2
|
+
import {makeIsolatedReadAdapter} from './isolated-read-adapter.mjs';
|
|
3
|
+
|
|
4
|
+
// The Agent must use this entrypoint, not the low-level mock-friendly adapter.
|
|
5
|
+
// Recheck Linux mount isolation on each request; never trust caller booleans.
|
|
6
|
+
export function makeVerifiedIsolatedReadAdapter({root,client}={}){
|
|
7
|
+
if(typeof root!=='string'||!client||typeof client.callTool!=='function')throw Error('ISOLATED_READ_DISABLED');
|
|
8
|
+
const initial=assertReadOnlyMount(root);
|
|
9
|
+
const run=makeIsolatedReadAdapter({root:initial.root,readonlyMountVerified:true,client});
|
|
10
|
+
return async (name,args)=>{
|
|
11
|
+
const current=assertReadOnlyMount(initial.root);
|
|
12
|
+
if(current.root!==initial.root||current.uid!==initial.uid||!current.readOnly)throw Error('ISOLATED_READ_DISABLED');
|
|
13
|
+
return run(name,args);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {assertSyntheticWritableMount} from './synthetic-write-mount.mjs';
|
|
2
|
+
import {makeSyntheticWriteAdapter} from './synthetic-write-adapter.mjs';
|
|
3
|
+
|
|
4
|
+
// Production entrypoint is opt-in and re-attests the exact mount on every call.
|
|
5
|
+
// The low-level adapter remains useful for unit tests but is not a security proof.
|
|
6
|
+
export function makeVerifiedSyntheticWriteAdapter({enabled=false,root,client,authorized,onAudit}={}){
|
|
7
|
+
if(enabled!==true||typeof root!=='string'||!client||typeof client.callTool!=='function')throw Error('SYNTHETIC_WRITE_DISABLED');
|
|
8
|
+
const initial=assertSyntheticWritableMount(root);
|
|
9
|
+
return makeSyntheticWriteAdapter({enabled:true,root:initial.root,client,authorized,onAudit,verifySandbox:expected=>{
|
|
10
|
+
const current=assertSyntheticWritableMount(expected);
|
|
11
|
+
if(current.root!==initial.root||current.uid!==initial.uid||current.filesystem!=='tmpfs')throw Error('SYNTHETIC_WRITE_DISABLED');
|
|
12
|
+
return true;
|
|
13
|
+
}});
|
|
14
|
+
}
|
package/README.md
DELETED