| 1234567891011121314151617181920212223242526272829303132333435 |
- // Internal pipe transport for the in-process current-message registration tool.
- // Stdin is not authentication. Only the gateway handler validates provenance.
- // Same-UID arbitrary code execution is outside this integration's isolation scope.
- import fs from 'node:fs';
- import path from 'node:path';
- import crypto from 'node:crypto';
- import {fileURLToPath} from 'node:url';
- import {readAgentProfileByKey,registerChannelParticipant} from './ksf-identity.mjs';
- const fail=code=>{throw Object.assign(Error(code),{code});};
- export function registerBridgeIdentity(envelope,options={}){
- if(!envelope||typeof envelope!=='object'||Array.isArray(envelope)||
- Object.keys(envelope).sort().join(',')!=='agentProfileKey,identity,version'||
- envelope.version!=='ksf.current-employee-registration.v1')fail('INVALID_BRIDGE_REQUEST');
- const agentProfile=readAgentProfileByKey(envelope.agentProfileKey,options.profileDir);
- if(agentProfile.role!=='workshop')fail('AGENT_ROLE_MISMATCH');
- const result=registerChannelParticipant(envelope.identity,{...options,agentProfile});
- return{ok:true,version:envelope.version,invocationId:crypto.randomUUID(),
- changed:result.changed,profile:result.profile,agentProfileKey:agentProfile.key,
- needsNameBinding:result.needsNameBinding,displayNameAuthenticated:false,
- accountAuthenticated:false,networkRequests:0,databaseWrites:0};
- }
- if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){
- try{
- if(process.argv.length!==2)fail('BRIDGE_NO_ARGUMENTS_REQUIRED');
- const buffer=Buffer.alloc(16385);let length=0;
- while(length<buffer.length){const n=fs.readSync(0,buffer,length,buffer.length-length,null);if(!n)break;length+=n;}
- if(length>16384)fail('BRIDGE_REQUEST_TOO_LARGE');
- console.log(JSON.stringify(registerBridgeIdentity(JSON.parse(buffer.subarray(0,length).toString('utf8')))));
- }catch(e){
- const code=/^[A-Z][A-Z0-9_]+$/.test(e.code||'')?e.code:'IDENTITY_REGISTRATION_FAILED';
- console.error(JSON.stringify({ok:false,error:code}));process.exitCode=1;
- }
- }
|