Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion script/devices.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, test} from 'bun:test';
import {validateNode, validateRegistry, renderSSH, assertIdentity,preflightSSH,installSSH,identityCommand,localInstance} from './devices';
import {validateNode, validateRegistry, renderSSH, assertIdentity,preflightSSH,installSSH,identityCommand,localInstance,includeDirective,managedIncludeState,replaceManagedInclude} from './devices';
const node = {id:'wsl',label:'Ubuntu',os:'linux',instanceId:'306ab9474cff458ca563e7807ded1da5',host:'100.118.123.62',port:2222,user:'basic',hostKey:'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB7vsV/ZP4V/Z1+MyVUCRAomZRpDsMWUm6A2rzOFv7ce',session:'default'};
test('rejects SSH config and command injection before effects',()=>{
for(const patch of [{host:'ok\nProxyCommand evil'},{user:'$(id)'},{session:'a;id'},{id:'../x'},{hostKey:'ssh-ed25519 nope'},{port:0},{os:'other'},{privateKey:'secret'}]) expect(()=>validateNode({...node,...patch})).toThrow();
Expand Down Expand Up @@ -37,6 +37,15 @@ import {mkdtempSync,writeFileSync,mkdirSync,readFileSync,rmSync,existsSync,symli
import {tmpdir} from 'node:os';
import {join} from 'node:path';
const unixTest=process.platform==='win32'?test.skip:test;
test('Windows Include uses ~/.ssh and migrates only the previous first-line managed Include',()=>{
const home=join(tmpdir(),'devices-win-home'),dir=join(home,'.ssh');
expect(includeDirective(dir,true)).toBe('Include ~/.ssh/devices.conf\n');
const legacy=`Include "${join(dir,'devices.conf').replaceAll('\\','/')}"\r\nHost keep\r\n`;
expect(managedIncludeState(legacy,home,true)).toBe('legacy');
expect(replaceManagedInclude(legacy,home,true)).toBe('Include ~/.ssh/devices.conf\nHost keep\r\n');
expect(managedIncludeState(`Host keep\n${legacy}`,home,true)).toBe('none');
expect(managedIncludeState('Include ~/.ssh/devices.conf\r\nHost keep\r\n',home,true)).toBe('current');
});
unixTest('real CLI sync is idempotent, isolates foreign profiles and rejects wrong replacement identity',()=>{
const dir=mkdtempSync(join(tmpdir(),'devices-test-')),home=join(dir,'home'),bin=join(dir,'bin');
mkdirSync(home);mkdirSync(bin);
Expand Down
62 changes: 50 additions & 12 deletions script/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ export function validateRegistry(values:unknown[]):Device[] {
return nodes.sort((a,b)=>a.id.localeCompare(b.id));
}
function quotePath(p:string){if(/[\r\n"%]/.test(p))throw Error('unsupported path');return '"'+p.replaceAll('\\','/')+'"';}
function quotePathOrNull(p:string){return /[\r\n"%]/.test(p)?null:'"'+p.replaceAll('\\','/')+'"';}
// Windows OpenSSH resolves Include files without a leading '/' as relative to
// ~/.ssh, so a 'C:/...' absolute path never matches. '~' expands correctly.
export function includeDirective(dir:string,windows=platform()==='win32'){return `Include ${windows?'~/.ssh/devices.conf':quotePath(join(dir,'devices.conf'))}\n`;}
function firstLine(text:string){const i=text.indexOf('\n');return (i===-1?text:text.slice(0,i)).replace(/\r$/,'');}
function legacyInclude(dir:string){const quoted=quotePathOrNull(join(dir,'devices.conf'));return quoted?`Include ${quoted}`:null;}
export type ManagedIncludeState='current'|'legacy'|'none';
export function managedIncludeState(text:string,home:string,windows=platform()==='win32'):ManagedIncludeState{
const dir=join(home,'.ssh'),first=firstLine(text),current=includeDirective(dir,windows);
if(first===current.slice(0,-1))return 'current';
if(windows){const old=legacyInclude(dir);if(old!==null&&first===old)return 'legacy';}
return 'none';
}
export function replaceManagedInclude(text:string,home:string,windows=platform()==='win32'){
const state=managedIncludeState(text,home,windows),include=includeDirective(join(home,'.ssh'),windows);
if(state==='current')return text;
if(state==='legacy'){const i=text.indexOf('\n');return i===-1?include:include+text.slice(i+1);}
return include+text;
}
// The rex-* aliases mirror device-* but permit forwarding: the rex CLI reaches a
// remote server over ssh -L streamlocal forwarding, which ClearAllForwardings
// would otherwise strip. Host key verification still resolves through the same
Expand Down Expand Up @@ -149,8 +168,8 @@ function generated(path:string,text:string){
}
export function preflightSSH(home:string){
const dir=join(home,'.ssh');
const config=join(dir,'config'),include=`Include ${quotePath(join(dir,'devices.conf'))}\n`;
let linkedConfig=false;
const config=join(dir,'config'),backup=config+'.before-devices';
let linkedConfig=false,old:string|null=null,state:ManagedIncludeState='none';
for(const path of [dir,...['config','config.before-devices','devices.conf','devices_known_hosts'].map(p=>join(dir,p))]){
try{
if(lstatSync(path).isSymbolicLink()){
Expand All @@ -163,35 +182,54 @@ export function preflightSSH(home:string){
}
for(const name of ['devices.conf','devices_known_hosts']){const p=join(dir,name);if(existsSync(p)&&!readGenerated(p).startsWith(marker))throw Error(`foreign ${name}`);}
if(existsSync(config)){
const old=readFileSync(config,'utf8');
if(linkedConfig&&!old.startsWith(include))throw Error('SSH config symlink must already contain the managed Include; refusing to modify its target');
if(!old.startsWith(include)&&existsSync(config+'.before-devices'))throw Error('backup path already exists; preserve and reconcile explicitly');
if(!old.startsWith(include)&&old.includes('devices.conf'))throw Error('managed Include already exists elsewhere');
if(platform()==='win32')run(['ssh','-G','-F',config,'device-preflight']);
old=readFileSync(config,'utf8');state=managedIncludeState(old,home);
if(linkedConfig&&state!=='current')throw Error('SSH config symlink must already contain the managed Include; refusing to modify its target');
if(state==='none'&&existsSync(backup))throw Error('backup path already exists; preserve and reconcile explicitly');
if(state==='none'&&old.includes('devices.conf'))throw Error('managed Include already exists elsewhere');
const firstNewline=old.indexOf('\n');
if(state==='legacy'&&firstNewline!==-1&&old.slice(firstNewline+1).includes('devices.conf'))throw Error('managed Include already exists elsewhere');
if(platform()==='win32'){
if(state==='legacy'&&old!==null){
// Validate the post-migration text without touching the user's config.
const tmp=join(dir,`.config.preflight-${process.pid}-${randomUUID()}`);
let fd:number|undefined;
try{
fd=openSync(tmp,'wx',0o600);writeFileSync(fd,replaceManagedInclude(old,home));closeSync(fd);fd=undefined;
run(['ssh','-G','-F',tmp,'device-preflight']);
}finally{if(fd!==undefined)closeSync(fd);try{rmSync(tmp);}catch(e){if(errnoCode(e)!=='ENOENT')throw e;}}
}else run(['ssh','-G','-F',config,'device-preflight']);
}
}
}
export function installSSH(nodes:Device[],home:string){
preflightSSH(home);
const dir=join(home,'.ssh');mkdirSync(dir,{recursive:true,mode:0o700});
const config=join(dir,'config'),include=`Include ${quotePath(join(dir,'devices.conf'))}\n`;
const existed=existsSync(config),old=existed?readFileSync(config,'utf8'):'';
const config=join(dir,'config'),backup=config+'.before-devices',include=includeDirective(dir);
const existed=existsSync(config),old=existed?readFileSync(config,'utf8'):'',state=existed?managedIncludeState(old,home):'none';
// Exclusive backup must succeed before any generated or user SSH file changes.
if(existed&&!old.startsWith(include))copyFileSync(config,config+'.before-devices',constants.COPYFILE_EXCL);
// A legacy managed Include was already backed up by the sync that wrote it;
// create the backup only when it is genuinely absent.
if(existed&&state==='none')copyFileSync(config,backup,constants.COPYFILE_EXCL);
else if(existed&&state==='legacy'&&!existsSync(backup))copyFileSync(config,backup,constants.COPYFILE_EXCL);
generated(join(dir,'devices_known_hosts'),marker+nodes.map(n=>`device-${n.id} ${n.hostKey}\n`).join(''));
generated(join(dir,'devices.conf'),renderSSH(nodes,home));
if(!old.startsWith(include)){
if(state==='none'){
if(existed){
// Keep the existing inode and ACL. Never apply generated-file ACLs to user config.
writeFileSync(config,include+old);
}else atomic(config,include);
}else if(state==='legacy'){
// Replace only the first, tool-owned Include line; user settings keep their
// inode, ACL and bytes after that line.
writeFileSync(config,replaceManagedInclude(old,home));
}
}
export function assertSyncedTarget(n:Device,home:string){
const config=join(home,'.ssh/devices.conf'),known=join(home,'.ssh/devices_known_hosts');
const expected=renderSSH([n],home).slice(marker.length).replace(/\nHost \*\n$/, '');
if(!existsSync(config)||!readGenerated(config).includes(expected)||!existsSync(known)||!readGenerated(known).split(/\r?\n/).includes(`device-${n.id} ${n.hostKey}`))throw Error(`${n.id}: SSH definition changed or missing; run devices sync explicitly`);
const userConfig=join(home,'.ssh/config');
if(!existsSync(userConfig)||!readFileSync(userConfig,'utf8').startsWith(`Include ${quotePath(config)}\n`))throw Error('managed SSH Include missing; run devices sync explicitly');
if(!existsSync(userConfig)||managedIncludeState(readFileSync(userConfig,'utf8'),home)!=='current')throw Error('managed SSH Include missing; run devices sync explicitly');
}
function profiles():Profile[]{
const raw:unknown=JSON.parse(run(['herdr','machine','list','--json']));
Expand Down
Loading