接入受限原生只读助手并修复来源物理路径核验
This commit is contained in:
parent
56467ef79f
commit
67d90d90a4
9 changed files with 61 additions and 26 deletions
16
runtime/worker.mjs
Normal file
16
runtime/worker.mjs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/env node
|
||||
// WORLD_RUNTIME: bounded root-owned read-only persona worker; no command, path, neural or memory setter.
|
||||
import fs from 'node:fs';import path from 'node:path';import {askQuestion} from './question.mjs';
|
||||
const ROOT='/etc/guanghu/public-question-worker',CONFIG=ROOT+'/config.json',STATE='/var/lib/guanghu/public-question-worker';
|
||||
const need=(x,c)=>{if(!x)throw Error(c)};let locked=false,lock=STATE+'/request.lock';
|
||||
try{
|
||||
need(process.getuid()===0&&process.argv.length===2,'PRIVATE_WORKER_IDENTITY_REQUIRED');const stat=fs.lstatSync(CONFIG);need(stat.isFile()&&!stat.isSymbolicLink()&&stat.uid===0&&(stat.mode&0o077)===0&&fs.realpathSync(ROOT)===ROOT,'WORKER_CONFIG_NOT_PROTECTED');const cfg=JSON.parse(fs.readFileSync(CONFIG));
|
||||
let raw='';for await(const c of process.stdin){raw+=c;need(Buffer.byteLength(raw)<=16000,'QUESTION_BODY_TOO_LARGE');}let i;try{i=JSON.parse(raw);}catch{throw Error('QUESTION_JSON_REQUIRED');}
|
||||
need(i&&!Array.isArray(i)&&Object.keys(i).every(k=>['persona_id','question','tool_host','session_id','request_id'].includes(k))&&typeof i.question==='string'&&i.question.trim()&&Buffer.byteLength(i.question)<=12000&&['tool_host','session_id','request_id'].every(k=>typeof i[k]==='string'&&/^[-A-Za-z0-9._:]{1,160}$/.test(i[k])),'QUESTION_COORDINATES_INVALID');const route=cfg.personas.find(x=>x.persona_id===i.persona_id);need(route&&cfg.allowed_personas.includes(i.persona_id),'PERSONA_OUTSIDE_WORKER_SCOPE');
|
||||
fs.mkdirSync(STATE,{recursive:true,mode:0o700});need(fs.lstatSync(STATE).uid===0&&!fs.lstatSync(STATE).isSymbolicLink(),'WORKER_STATE_NOT_PROTECTED');
|
||||
if(fs.existsSync(lock)){let pid;try{pid=Number(fs.readFileSync(lock,'utf8'));}catch{}let alive=true;try{if(!pid)throw Error();process.kill(pid,0);}catch{alive=false;}if(!alive)fs.unlinkSync(lock);}
|
||||
try{const fd=fs.openSync(lock,'wx',0o600);fs.writeSync(fd,String(process.pid));fs.closeSync(fd);locked=true;}catch{throw Error('QUESTION_CAPACITY_LIMIT');}
|
||||
const file=STATE+'/rate.json',now=Date.now();let rate=fs.existsSync(file)?JSON.parse(fs.readFileSync(file)):{start:now,count:0};if(now-rate.start>=60000)rate={start:now,count:0};need(rate.count<(cfg.requests_per_minute||12),'QUESTION_CAPACITY_LIMIT');rate.count++;const next=file+'.next';fs.writeFileSync(next,JSON.stringify(rate),{mode:0o600});fs.renameSync(next,file);
|
||||
const result=await askQuestion(route,i,cfg.fuel);console.log(JSON.stringify({...result,guard_scope:'IDENTITY_SOURCE_COORDINATES_EVIDENCE_AND_RACE_NOT_SEMANTIC_CERTAINTY'}));
|
||||
}catch(e){console.log(JSON.stringify({error:/^[A-Z_]+$/.test(e.message)?e.message:'PERSONA_WORKER_SOURCE_UNAVAILABLE',reply_withheld:true,no_neural_state_write:true,no_memory_write:true,no_execution_authority:true}));process.exitCode=2;}
|
||||
finally{if(locked&&fs.existsSync(lock)&&fs.readFileSync(lock,'utf8')===String(process.pid))fs.unlinkSync(lock);}
|
||||
Loading…
Reference in a new issue