@nekzus/liop 2.0.1-beta.1 → 2.1.0-alpha.1
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/README.md +39 -11
- package/dist/bin/agent.js +1 -1
- package/dist/bridge.d.ts +1 -1
- package/dist/bridge.js +1 -1
- package/dist/{chunk-W2QGWRTT.js → chunk-7L5ODML2.js} +3 -3
- package/dist/{chunk-W2QGWRTT.js.map → chunk-7L5ODML2.js.map} +1 -1
- package/dist/{chunk-YZVCAJJO.js → chunk-GI2LSJYZ.js} +3 -3
- package/dist/{chunk-YZVCAJJO.js.map → chunk-GI2LSJYZ.js.map} +1 -1
- package/dist/{chunk-VGXNGTIC.js → chunk-I46YEWND.js} +7 -7
- package/dist/chunk-I46YEWND.js.map +1 -0
- package/dist/{chunk-L5A64CNT.js → chunk-KQ5BDO2M.js} +3 -3
- package/dist/chunk-KQ5BDO2M.js.map +1 -0
- package/dist/chunk-PWCXZWSE.js +2 -0
- package/dist/chunk-PWCXZWSE.js.map +1 -0
- package/dist/{chunk-N6FGTZ6A.js → chunk-T3L6OCM3.js} +3 -3
- package/dist/chunk-T3L6OCM3.js.map +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/gateway.d.ts +2 -2
- package/dist/gateway.js +1 -1
- package/dist/{index-CL8m1L1d.d.ts → index-BcuTJtQX.d.ts} +2 -0
- package/dist/{index-B_Vbrb_I.d.ts → index-Brfvxmdt.d.ts} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/{verifier-DTCD9imJ.d.ts → verifier-COnid_dg.d.ts} +1 -1
- package/dist/verifier-XU2DB56Z.js +2 -0
- package/dist/{verifier-Z26UC7M4.js.map → verifier-XU2DB56Z.js.map} +1 -1
- package/dist/workers/zk-verifier.d.ts +2 -0
- package/dist/workers/zk-verifier.js +1 -1
- package/dist/workers/zk-verifier.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-L5A64CNT.js.map +0 -1
- package/dist/chunk-N6FGTZ6A.js.map +0 -1
- package/dist/chunk-SW53FNSN.js +0 -2
- package/dist/chunk-SW53FNSN.js.map +0 -1
- package/dist/chunk-VGXNGTIC.js.map +0 -1
- package/dist/verifier-Z26UC7M4.js +0 -2
|
@@ -14,7 +14,7 @@ Defaulting to 2020, but this will stop working in the future.`)),t.ecmaVersion=1
|
|
|
14
14
|
${t}
|
|
15
15
|
}`;s=bt(c,{ecmaVersion:2022,sourceType:"script",locations:!0});}catch{return null}let n=new Set,a=new Set;this.identifyRecordBoundVars(s,n),this.propagateTaint(s,n,a);let u=this.checkReturnStatements(s,n,a);if(u)return u;if(i!==void 0&&i>0&&i<r){let c=this.detectCorrelatedAggregations(s);if(c)return c.reason=c.reason.replace("50 records",`${r} records`),c}if(i!==void 0&&i>0&&i<r){let c=this.detectMinMaxExtraction(s);if(c)return c.reason=c.reason.replace("50 records",`${r} records`),c}return null}extractQueriedFields(t){let i;try{i=bt(`function w(env) {
|
|
16
16
|
${t}
|
|
17
|
-
}`,{ecmaVersion:2022,sourceType:"script"});}catch{return []}let r=new Set;this.identifyRecordBoundVars(i,r);let s=new Set;return ae(i,{MemberExpression:a=>{let u=this.getPropertyName(a);if(!(!u||u==="length")&&(a.object.type==="Identifier"&&r.has(a.object.name)&&s.add(u),a.object.type==="MemberExpression")){let c=a.object;c.computed&&this.isEnvRecordsAccess(c.object)&&s.add(u);}}}),Array.from(s)}detectCorrelatedAggregations(t){let i=new Map;ae(t,{CallExpression:s=>{if(s.callee.type!=="MemberExpression")return;let n=s.callee,a=this.getPropertyName(n);if(!a||!e.REDUCE_METHODS.has(a)||!this.isEnvRecordsChain(n.object))return;let u=s.arguments[0];if(!u||u.type!=="ArrowFunctionExpression"&&u.type!=="FunctionExpression")return;let c=u,l=c.params.length>1?c.params[1]:c.params[0];if(!l||l.type!=="Identifier")return;let p=l.name,x=this.extractFieldsFromBody(c.body,p);for(let d of x){let k=i.get(d)??0;i.set(d,k+1);}}});for(let[s,n]of i)if(n>=2)return {reason:`Correlation guard: ${n} aggregations detected on field '${s}'. Multiple correlated aggregations on the same field can enable differencing attacks. Use a single aggregation per numeric field, or increase dataset size above 50 records.`};return null}isEnvRecordsChain(t){if(this.isEnvRecordsAccess(t))return true;if(t.type==="CallExpression"){let i=t;if(i.callee.type==="MemberExpression"){let r=i.callee,s=this.getPropertyName(r);if(s&&(s==="slice"||s==="filter"||s==="toSorted"))return this.isEnvRecordsChain(r.object)}}return false}extractFieldsFromBody(t,i){let r=[];return ae(t,{MemberExpression:n=>{if(n.object.type==="Identifier"&&n.object.name===i){let a=this.getPropertyName(n);a&&a!=="length"&&r.push(a);}}}),r}detectMinMaxExtraction(t){let i=null;return ae(t,{CallExpression:s=>{if(!i&&s.callee.type==="MemberExpression"){let n=s.callee;if(n.object.type==="Identifier"&&n.object.name==="Math"){let a=this.getPropertyName(n);(a==="min"||a==="max")&&s.arguments.some(u=>u.type==="SpreadElement"&&this.isRecordsMapCall(u.argument))&&(i={reason:`Min/Max gate: Math.${a}() on individual records blocked for small datasets (n < 50). Use avg/stddev/count for privacy-safe aggregations.`});}}},MemberExpression:s=>{if(!i&&s.computed&&s.object.type==="CallExpression"){let n=s.object;if(n.callee.type==="MemberExpression"){let a=this.getPropertyName(n.callee);if(a==="sort"||a==="toSorted"){let u=n.callee.object;this.isEnvRecordsChain(u)&&(i={reason:"Min/Max gate: .sort()[index] on individual records blocked for small datasets (n < 50). Use avg/stddev/count for privacy-safe aggregations."});}}}}}),i}isRecordsMapCall(t){if(t.type!=="CallExpression")return false;let i=t;if(i.callee.type!=="MemberExpression")return false;let r=i.callee;return this.getPropertyName(r)==="map"&&this.isEnvRecordsChain(r.object)}identifyRecordBoundVars(t,i){ae(t,{CallExpression:n=>{if(n.callee.type!=="MemberExpression")return;let a=n.callee,u=this.getPropertyName(a);if(!u||!this.isEnvRecordsAccess(a.object))return;let c=n.arguments[0];if(c&&(c.type==="ArrowFunctionExpression"||c.type==="FunctionExpression")){let l=c;if(e.ARRAY_CALLBACK_METHODS.has(u)&&l.params.length>0){let p=l.params[0];p.type==="Identifier"&&i.add(p.name);}if(e.REDUCE_METHODS.has(u)&&l.params.length>1){let p=l.params[1];p.type==="Identifier"&&i.add(p.name);}}},ForOfStatement:n=>{if(this.isEnvRecordsAccess(n.right)&&n.left.type==="VariableDeclaration")for(let a of n.left.declarations)a.id.type==="Identifier"&&i.add(a.id.name);}}),ae(t,{VariableDeclarator:n=>{if(!(!n.init||n.id.type!=="Identifier")&&n.init.type==="MemberExpression"&&n.init.computed){let a=n.init;this.isEnvRecordsAccess(a.object)&&i.add(n.id.name);}}});}propagateTaint(t,i,r){for(let s=0;s<3;s++){let n=r.size;if(ae(t,{VariableDeclarator:u=>{!u.init||u.id.type!=="Identifier"||this.isExpressionTainted(u.init,i,r)&&r.add(u.id.name);},AssignmentExpression:u=>{u.left.type==="Identifier"&&this.isExpressionTainted(u.right,i,r)&&r.add(u.left.name);},FunctionDeclaration:u=>{u.id&&u.id.type==="Identifier"&&this.doesCallbackProduceTaint(u,null,i,r)&&r.add(u.id.name);},CallExpression:u=>{if(u.callee.type!=="MemberExpression")return;let c=u.callee;this.getPropertyName(c)==="push"&&c.object.type==="Identifier"&&u.arguments.some(p=>this.isExpressionTainted(p,i,r))&&r.add(c.object.name);}}),r.size===n)break}}checkReturnStatements(t,i,r){let s=null;return ae(t,{ReturnStatement:a=>{if(!s&&a.argument&&this.isExpressionTainted(a.argument,i,r)){let u=a.loc?.start.line?a.loc.start.line-1:void 0,c=this.describeTaintSource(a.argument,i,r);s={reason:`PII side-channel detected: output contains values derived from restricted fields. ${c?`Operation: ${c}. `:""}Use only non-PII fields (e.g., numeric/date columns) for aggregations.`,line:u,operation:c};}}}),s}isExpressionTainted(t,i,r){switch(t.type){case "Identifier":return r.has(t.name);case "MemberExpression":return this.isMemberExprTainted(t,i,r);case "CallExpression":return this.isCallExprTainted(t,i,r);case "YieldExpression":{let s=t;return s.argument?this.isExpressionTainted(s.argument,i,r):false}case "FunctionExpression":case "ArrowFunctionExpression":{let s=t;return this.doesCallbackProduceTaint(s,null,i,r)}case "BinaryExpression":case "LogicalExpression":{let s=t;return this.isExpressionTainted(s.left,i,r)||this.isExpressionTainted(s.right,i,r)}case "UnaryExpression":{let s=t;return this.isExpressionTainted(s.argument,i,r)}case "ConditionalExpression":{let s=t;return this.isExpressionTainted(s.test,i,r)||this.isExpressionTainted(s.consequent,i,r)||this.isExpressionTainted(s.alternate,i,r)}case "ObjectExpression":return t.properties.some(n=>n.type==="Property"&&this.isExpressionTainted(n.value,i,r));case "ArrayExpression":return t.elements.some(n=>n!==null&&this.isExpressionTainted(n,i,r));case "TemplateLiteral":return t.expressions.some(n=>this.isExpressionTainted(n,i,r));case "SpreadElement":{let s=t;return this.isExpressionTainted(s.argument,i,r)}default:return false}}isMemberExprTainted(t,i,r){let s=this.getPropertyName(t);if(t.object.type==="Identifier"&&i.has(t.object.name)&&s&&this.piiFields.has(s.toLowerCase()))return true;if(t.object.type==="MemberExpression"&&s&&this.piiFields.has(s.toLowerCase())){let n=t.object;if(n.computed&&this.isEnvRecordsAccess(n.object))return true}if(this.isExpressionTainted(t.object,i,r))return true;if(t.computed&&t.object.type==="Identifier"&&i.has(t.object.name)&&t.property.type==="Literal"){let n=t.property.value;if(typeof n=="string"&&this.piiFields.has(n.toLowerCase()))return true}return false}isCallExprTainted(t,i,r){if(t.callee.type==="MemberExpression"){let s=t.callee,n=this.getPropertyName(s);if(n&&e.TAINT_PROPAGATING_METHODS.has(n)&&this.isExpressionTainted(s.object,i,r))return true;if(this.isEnvRecordsAccess(s.object)&&t.arguments[0]){let a=t.arguments[0];if(a.type==="ArrowFunctionExpression"||a.type==="FunctionExpression")return this.doesCallbackProduceTaint(a,n,i,r)}if(this.isExpressionTainted(s.object,i,r)||t.arguments.some(a=>this.isExpressionTainted(a,i,r)))return true}if(t.callee.type==="MemberExpression"){let s=t.callee;this.getPropertyName(s)==="push"&&s.object.type==="Identifier"&&t.arguments.some(a=>this.isExpressionTainted(a,i,r))&&r.add(s.object.name);}if(t.callee.type==="Identifier"){let s=t.callee.name;if(r.has(s))return true;if(!new Set(["Math","Number","parseInt","parseFloat","isNaN","isFinite"]).has(s))return t.arguments.some(a=>this.isExpressionTainted(a,i,r))}return false}doesCallbackProduceTaint(t,i,r,s){let n=new Set(r),a=new Set(s);if(t.params.length>0){let p=i!==null&&e.REDUCE_METHODS.has(i)?1:0;t.params.length>p&&t.params[p].type==="Identifier"&&n.add(t.params[p].name);}if(t.type==="ArrowFunctionExpression"&&t.body.type!=="BlockStatement")return this.isExpressionTainted(t.body,n,a);let u=false,c={ReturnStatement:l=>{l.argument&&this.isExpressionTainted(l.argument,n,a)&&(u=true);},YieldExpression:l=>{let p=l;p.argument&&this.isExpressionTainted(p.argument,n,a)&&(u=true);}};return ae(t.body,c),u}getPropertyName(t){if(!t.computed&&t.property.type==="Identifier")return t.property.name;if(t.computed&&t.property.type==="Literal"){let i=t.property.value;if(typeof i=="string")return i}return null}isEnvRecordsAccess(t){if(t.type==="MemberExpression"){let i=t;if(this.getPropertyName(i)==="records"&&i.object.type==="Identifier"&&i.object.name==="env")return true}return t.type==="Identifier"&&t.name==="records"}describeTaintSource(t,i,r){if(t.type==="Identifier"){let s=t.name;if(r.has(s))return `variable '${s}' is PII-derived`}if(t.type==="ObjectExpression"){let s=t;for(let n of s.properties)if(n.type==="Property"&&this.isExpressionTainted(n.value,i,r))return `property '${n.key.type==="Identifier"?n.key.name:"unknown"}' contains PII-derived value`}if(t.type==="CallExpression"){let s=t;if(s.callee.type==="MemberExpression"){let n=this.getPropertyName(s.callee);if(n)return `result of .${n}() on PII data`}}}};var Gr={aspirin:"Medication",lisinopril:"Medication",metformin:"Medication",amlodipine:"Medication",atorvastatin:"Medication",omeprazole:"Medication",losartan:"Medication",simvastatin:"Medication",levothyroxine:"Medication",ibuprofen:"Medication",acetaminophen:"Medication",amoxicillin:"Medication",ciprofloxacin:"Medication",prednisone:"Medication",warfarin:"Medication",insulin:"Medication",hydrochlorothiazide:"Medication",gabapentin:"Medication",albuterol:"Medication",pantoprazole:"Medication",hypertension:"Condition",diabetes:"Condition",bronchitis:"Condition",pneumonia:"Condition",asthma:"Condition"},qe=4,qr=/^[\d\s.,:;!?()[\]{}<>@#$%^&*+=|\\/"'`~_-]+$/,Je=class e{static nlp=null;async getNlp(){if(!e.nlp){let t=await import('compromise/three');e.nlp=t.default||t,e.nlp.addWords(Gr);}return e.nlp}async scan(t){if(t.length<qe||qr.test(t))return {detected:false,entities:[]};let r=(await this.getNlp())(t),s=[],n=r.people().out("array");for(let c of n){let l=c.trim();l.length>=qe&&s.push({type:"person",text:l});}let a=r.places().out("array");for(let c of a){let l=c.trim();l.length>=qe&&s.push({type:"place",text:l});}let u=r.organizations().out("array");for(let c of u){let l=c.trim();l.length>=qe&&s.push({type:"organization",text:l});}return {detected:s.length>0,entities:s}}async scanDeep(t,i=new WeakSet){if(t==null)return {detected:false,entities:[]};if(typeof t=="string")return this.scan(t);if(typeof t=="object"){if(i.has(t))return {detected:false,entities:[]};i.add(t);let r=Array.isArray(t)?t:Object.values(t),s=[];for(let n of r){let a=await this.scanDeep(n,i);if(a.detected&&(s.push(...a.entities),a.entities.some(u=>u.type==="person")))return {detected:true,entities:s}}return {detected:s.length>0,entities:s}}return {detected:false,entities:[]}}};var Jr={maxDecimalPlaces:4,clampNonNegative:true};function Ye(e,t){let i={...Jr,...t},r=new WeakSet;function s(n){if(n==null)return n;if(typeof n=="number"){if(!Number.isFinite(n))return n;let a=n;i.clampNonNegative&&a<0&&(a=0);let u=10**i.maxDecimalPlaces;return a=Math.round(a*u)/u,a}if(typeof n=="string"||typeof n=="boolean")return n;if(typeof n=="object"){if(r.has(n))return n;if(r.add(n),Array.isArray(n))return n.map(u=>s(u));let a={};for(let[u,c]of Object.entries(n))a[u]=s(c);return a}return n}return s(e)}function Yr(e){let t=e.replace(/\D/g,"");if(t.length<13||t.length>19)return false;let i=0,r=false;for(let s=t.length-1;s>=0;s--){let n=parseInt(t.charAt(s),10);r&&(n*=2,n>9&&(n-=9)),i+=n,r=!r;}return i%10===0}function Qr(e){let t=e.replace(/\s+/g,"").toUpperCase();if(!/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/.test(t))return false;let i=t.substring(4)+t.substring(0,4),r="";for(let s=0;s<i.length;s++){let n=i.charCodeAt(s);if(n>=65&&n<=90)r+=(n-55).toString();else if(n>=48&&n<=57)r+=i.charAt(s);else return false}try{return BigInt(r)%97n===1n}catch{return false}}var D={EMAIL:{name:"EMAIL",pattern:/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b/gi,validator:e=>!e.endsWith("@example.com")&&!e.endsWith("@test.com")},CREDIT_CARD:{name:"CREDIT_CARD",pattern:/\b(?:\d[ -]*?){13,16}\b/g,validator:Yr},IP_ADDRESS:{name:"IP_ADDRESS",pattern:/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,validator:e=>["127.0.0.1","0.0.0.0","255.255.255.255"].includes(e)?false:e.split(".").map(Number).every(r=>r>=0&&r<=255)},PHONE:{name:"PHONE",pattern:/(?:(?:\+?\d{1,3}[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4})\b/g,validator:e=>{let t=e.replace(/\D/g,"");return !(t.length<7||t.length>15||/^(\d)\1+$/.test(t)||t==="1234567890")}},SSN:{name:"SSN",pattern:/\b\d{3}[- ]?\d{2}[- ]?\d{4}\b/g,validator:e=>{let t=e.replace(/\D/g,"");if(t.length!==9)return false;let i=parseInt(t.substring(0,3),10);return !(i===0||i===666||i>=900||parseInt(t.substring(3,5),10)===0||parseInt(t.substring(5,9),10)===0||/^(\d)\1+$/.test(t)||t==="123456789")}},IBAN:{name:"IBAN",pattern:/\b[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}\b/gi,validator:Qr},PASSPORT_MRZ:{name:"PASSPORT_MRZ",pattern:/\bP[A-Z<][A-Z<]{3}[A-Z0-9<]{39}(?:\b|\s|$)/g}},qi={GLOBAL_STRICT:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.PASSPORT_MRZ,D.IBAN],US_COMPLIANT:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.SSN,D.PASSPORT_MRZ],EU_GDPR:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.IBAN,D.PASSPORT_MRZ]},Qe=class e{patterns;forbiddenKeysSet;nerScanner;static KEY_SAFELIST=new Set(["grid","video","android","identity","provide","override","validate","hidden","widget","guidelines","beside","guideline","outside","inside","collide","decide","divide","aside","ride","side","wide","hide","tide","pride","bride","slide","guide","stride","oxide","dioxide","suicide","homicide","pesticide","valid","invalid","void","avoid","diagnosis","medication","namespace","namesake","rename","filename","hostname","typename","unnamed","renamed","phonetic","phoneme","microphone","headphone","telephone","saxophone","smartphone","streetview","addressable","addressing","cityscape","electricity","capacity","velocity","opacity","timestamp","timezone","image_id","computation_result","zk_receipt","testid","toolid","sessionid","peerid","nodeid","requestid","correlationid","traceid","spanid"]);shortTokenBoundaryPatterns;longForbiddenTokens;constructor(t=[],i=[],r){this.patterns=t,this.forbiddenKeysSet=new Set(i.map(s=>s.toLowerCase())),this.nerScanner=r??null,this.shortTokenBoundaryPatterns=new Map,this.longForbiddenTokens=[];for(let s of this.forbiddenKeysSet)s.length<4?this.shortTokenBoundaryPatterns.set(s,new RegExp((()=>{let n=s.split("").map(u=>`[${u.toLowerCase()}${u.toUpperCase()}]`).join(""),a=`[a-z]${s.charAt(0).toUpperCase()}${s.slice(1)}`;return `(?:^|[_-])${n}(?:$|[_-])|${a}(?:$|[A-Z_-])|^${n}$`})())):this.longForbiddenTokens.push(s);}async scan(t,i=new WeakSet){if(t==null)return null;if(typeof t=="string"){let r=t.trim();if(r.startsWith("{")&&r.endsWith("}")||r.startsWith("[")&&r.endsWith("]"))try{let n=JSON.parse(r),a=await this.scan(n,i);if(a)return a}catch{}let s=this.checkString(t);if(s)return s;if(this.nerScanner){let n=await this.nerScanner.scan(t);if(n.detected){let a=n.entities.find(u=>u.type==="person");if(a)return `PII Entity Detected: person name "${a.text}"`}}return null}if(typeof t=="object"){if(i.has(t))return null;if(i.add(t),Array.isArray(t))for(let r of t){let s=await this.scan(r,i);if(s)return s}else for(let[r,s]of Object.entries(t)){if(this.forbiddenKeysSet.has(r.toLowerCase()))return `Forbidden Key: ${r}`;let n=this.checkKeyFuzzy(r);if(n)return n;let a=await this.scan(s,i);if(a)return a}}return null}checkKeyFuzzy(t){let i=t.toLowerCase();if(e.KEY_SAFELIST.has(i))return null;for(let[r,s]of this.shortTokenBoundaryPatterns)if(s.test(t))return `Forbidden Key (fuzzy): ${t} matches boundary pattern "${r}"`;for(let r of this.longForbiddenTokens)if(i.includes(r))return `Forbidden Key (fuzzy): ${t} contains restricted token "${r}"`;return null}checkString(t){for(let i of this.patterns)if(typeof i=="string"){if(t.toLowerCase().includes(i.toLowerCase()))return i}else if(i instanceof RegExp){if(i.global&&(i.lastIndex=0),i.test(t))return i.source}else if(typeof i=="object"&&i!==null){let r=i;if(typeof r.pattern=="string"){if(t.toLowerCase().includes(r.pattern.toLowerCase())&&(!r.validator||r.validator(r.pattern)))return r.name}else if(r.pattern instanceof RegExp){r.pattern.global&&(r.pattern.lastIndex=0);let s=r.pattern.exec(t);for(;s!==null;){let n=s[0];if(!r.validator||r.validator(n))return r.name;if(!r.pattern.global)break;s=r.pattern.exec(t);}}}return null}};var Ji=Se.dirname(fileURLToPath(import.meta.url)),Yi=class e{constructor(t,i){this.serverInfo=t;this.config=i;let r=this.config?.security?.enableNerScanning?new Je:null;this.piiScanner=new Qe(this.config?.security?.piiPatterns??qi.GLOBAL_STRICT,this.config?.security?.forbiddenKeys??["id","name","fullName","firstName","lastName","address","street","city","postalCode","zipCode","phone","email","ssn","accountHolder","accountNumber","account_number","password","token","secret","privateKey"],r);let s=this.config?.security?.rateLimit;this.toolCallWindowMs=s?.windowMs??Number.parseInt(process.env.LIOP_RATE_LIMIT_WINDOW_MS??"60000",10),this.toolCallMaxPerWindow=s?.maxPerWindow??Number.parseInt(process.env.LIOP_RATE_LIMIT_MAX??"15",10),this.globalCallMaxPerWindow=s?.globalMaxPerWindow??Number.parseInt(process.env.LIOP_RATE_LIMIT_GLOBAL_MAX??"40",10);let n=this.config?.security?.forbiddenKeys??["id","name","fullName","firstName","lastName","address","street","city","postalCode","zipCode","phone","email","ssn","accountHolder","accountNumber","account_number","password","token","secret","privateKey"],a=this.config?.security?.sensitiveKeys??[];this.taintAnalyzer=new Ge(n,a);let u=import.meta.url.endsWith(".ts"),c=u?".ts":".js",l=[];if(u)try{let _=createRequire(import.meta.url).resolve("tsx/package.json");l=["--import",pathToFileURL(Se.join(Se.dirname(_),"dist","loader.mjs")).href];}catch{l=["--import","tsx"];}let p=process.env.NODE_ENV==="test"||process.env.VITEST;this.config?.capabilities&&!this.serverInfo.capabilities&&(this.serverInfo.capabilities=this.config.capabilities);let x=[Se.resolve(Ji,`./workers/logic-execution${c}`),Se.resolve(Ji,`../workers/logic-execution${c}`)],d=x.find(g=>j.existsSync(g))||x[1];this.workerPool=new Piscina({filename:d,minThreads:this.config?.workerPool?.minThreads??(p?0:2),maxThreads:this.config?.workerPool?.maxThreads??(p?1:8),idleTimeout:this.config?.workerPool?.idleTimeout??(p?500:5e3),maxQueue:"auto",taskQueue:new FixedQueue,execArgv:l,resourceLimits:{maxOldGenerationSizeMb:this.config?.workerPool?.maxHeapMb??Number.parseInt(process.env.LIOP_WORKER_MAX_HEAP_MB??"64",10)}});let k=this.config?.workerPool?.minThreads??(p?0:2);if(this.workerPool&&k>0)for(let g=0;g<k;g++)this.workerPool.run({isWarmup:true}).catch(_=>{a$1.debug(`[LiopServer] Worker pool warm-up ping failed: ${_.message}`);});if(this.config?.auth?.role==="nexus"){let g=this.config.auth.issuer||"http://localhost:3000",_=this.config.auth.audience||le.audience,w=this.config.auth.clients||[{client_id:process.env.LIOP_OAUTH_CLIENT_ID||"liop-mesh-agent",client_secret:process.env.LIOP_OAUTH_CLIENT_SECRET||"dev-secret-change-me",grant_types:["client_credentials"],scope:"liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query"}],{provider:N,jwks:Z}=ai({issuer:g,clients:w});this.oauthProvider=N,this.jwtValidator=new Pe(g,_,Z);}else if(this.config?.auth?.role==="node"){let g=this.config.auth.nexusUrl||process.env.LIOP_NEXUS_URL||"http://localhost:3000",_=this.config.auth.audience||le.audience,w=g.endsWith("/oidc")?g:`${g}/oidc`,N=new URL(`${w}/jwks`);this.jwtValidator=new Pe(g,_,N);}this.resource("LIOP Envelope Specification","liop://protocol/envelope-spec","Complete Logic-on-Origin envelope format, execution rules, and security constraints","text/plain",()=>Promise.resolve(this.buildEnvelopeSpec())),this.config?.auth?.revocationPath&&this.loadRevocationList();}logicCache=new Map;connectionStats=new Map;CACHE_TTL_MS=1440*60*1e3;THROTTLE_THRESHOLD=5;THROTTLE_COOLDOWN_MS=60*1e3;toolCallWindows=new Map;toolCallMaxPerWindow;toolCallWindowMs;globalCallWindow=[];globalCallMaxPerWindow;fieldQueryBudget=new Map;taintAnalyzer;tools=new Map;resources=new Map;prompts=new Map;activeSchema=null;sandboxRecords=[];piiScanner;workerPool;meshNode=null;rpcServer=null;boundPort=null;jwtValidator;oauthProvider;sessions=new Map;revokedTokenHashes=new Set;lastRevocationLoadTime=0;static LIOP_COMPACT_REGEX=/@LIOP\{(?<target>[^,}]+)(?:,(?<name>[^}]*))?\}\n(?<logic>[\s\S]*?)\n@END/m;extractLogic(t){let i=t.match(e.LIOP_COMPACT_REGEX);return i?.groups?.logic?i.groups.logic.trim():null}parseUnknownJson(t){if(typeof t!="string")return t;let i=t.trim();if(i.startsWith("{")&&i.endsWith("}")||i.startsWith("[")&&i.endsWith("]"))try{return JSON.parse(i)}catch{return t}return t}runPreflightPolicy(t,i,r,s="local-client"){if(r){let c=i.replace(/\s+/g," ");if(r.enforceAggregationFirst&&[/return\s+env\.records(?!\s*\.\s*(?:reduce|length|filter|every|some|find)\b)/i,/return\s*\{[\s\S]*\b(accounts|patients|rows|records)\s*:\s*env\.records(?!\s*\.\s*(?:reduce|length|filter)\b)/i].some(p=>p.test(c)))return "Preflight policy rejected: potential row-level export pattern detected.";if(r.preflightDenyPatterns?.some(l=>l.test(c)))return "Preflight policy rejected: custom deny pattern matched."}let n=50;typeof r?.enforceAggregationFirst=="object"&&(n=r.enforceAggregationFirst.minMaxBlockThreshold??50);let a=this.taintAnalyzer.analyze(i,this.sandboxRecords.length,n);if(a)return `Preflight policy rejected: ${a.reason}`;let u=this.taintAnalyzer.extractQueriedFields(i);if(u.length>0){let c=this.fieldQueryBudget.get(s);c||(c=new Map,this.fieldQueryBudget.set(s,c));let l=c.get(t);l||(l=new Map,c.set(t,l));for(let p of u){let x=this.taintAnalyzer.classifyField(p,r?.sensitiveKeys),d=25,k="public";if(r?.queryBudgetPerField!==void 0?(d=r.queryBudgetPerField,k="override"):x==="forbidden"?(d=3,k="forbidden"):x==="sensitive"&&(d=8,k="sensitive"),(l.get(p)??0)>=d)return `Preflight policy rejected: Query budget exceeded for field '${p}' (max ${d} per session for ${k} fields). Rotate PQC session to reset budget.`}for(let p of u){let x=l.get(p)??0;l.set(p,x+1);}}return null}validateOutputPolicy(t,i,r){if(!r)return null;let s=this.parseUnknownJson(i);if(s&&typeof s=="object"&&s.isError===true)return null;if(r.outputSchema){let a=(()=>{if(!(r.outputSchema instanceof b$2.ZodObject))return r.outputSchema;let u=r.outputSchema;return u._def.catchall instanceof b$2.ZodNever?u.strict():u})().safeParse(s);if(!a.success)return `[LIOP] Output schema violation for ${t}: ${a.error.issues.map(u=>`${u.path.join(".")||"<root>"} ${u.message}`).join("; ")}. HINT: Your output must conform to the declared schema. Use 'env.records' to access the dataset and return only allowed fields.`}return r.enforceAggregationFirst&&this.violatesAggregationFirstPolicy(this.unwrapForAggregationPolicyScan(s),r.enforceAggregationFirst,this.sandboxRecords.length)?process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1"?"Aggregation-First Policy Violation: row-level export or K-Anonymity violation blocked. HINT: Use .reduce() to produce a flat {key:value} object. Do NOT use .map() to create arrays of objects. Ensure dataset size > 10 for detailed results.":"Aggregation-First Policy Violation: Output blocked due to privacy constraints.":null}unwrapForAggregationPolicyScan(t){if(typeof t=="string"){let n=t.trim();if(n.startsWith("{")&&n.endsWith("}")||n.startsWith("[")&&n.endsWith("]"))try{return this.unwrapForAggregationPolicyScan(JSON.parse(n))}catch{return t}return t}if(!t||typeof t!="object")return t;let i=t;if(i.computation_result!==void 0)return this.unwrapForAggregationPolicyScan(i.computation_result);if(!Array.isArray(i.content)||i.content.length===0)return t;let r=[];for(let n of i.content)if(n&&typeof n=="object"&&"text"in n){let a=n.text;typeof a=="string"&&r.push(a);}if(r.length===0)return t;let s=r.length===1?r[0]:r.join(`
|
|
17
|
+
}`,{ecmaVersion:2022,sourceType:"script"});}catch{return []}let r=new Set;this.identifyRecordBoundVars(i,r);let s=new Set;return ae(i,{MemberExpression:a=>{let u=this.getPropertyName(a);if(!(!u||u==="length")&&(a.object.type==="Identifier"&&r.has(a.object.name)&&s.add(u),a.object.type==="MemberExpression")){let c=a.object;c.computed&&this.isEnvRecordsAccess(c.object)&&s.add(u);}}}),Array.from(s)}detectCorrelatedAggregations(t){let i=new Map;ae(t,{CallExpression:s=>{if(s.callee.type!=="MemberExpression")return;let n=s.callee,a=this.getPropertyName(n);if(!a||!e.REDUCE_METHODS.has(a)||!this.isEnvRecordsChain(n.object))return;let u=s.arguments[0];if(!u||u.type!=="ArrowFunctionExpression"&&u.type!=="FunctionExpression")return;let c=u,l=c.params.length>1?c.params[1]:c.params[0];if(!l||l.type!=="Identifier")return;let p=l.name,x=this.extractFieldsFromBody(c.body,p);for(let d of x){let k=i.get(d)??0;i.set(d,k+1);}}});for(let[s,n]of i)if(n>=2)return {reason:`Correlation guard: ${n} aggregations detected on field '${s}'. Multiple correlated aggregations on the same field can enable differencing attacks. Use a single aggregation per numeric field, or increase dataset size above 50 records.`};return null}isEnvRecordsChain(t){if(this.isEnvRecordsAccess(t))return true;if(t.type==="CallExpression"){let i=t;if(i.callee.type==="MemberExpression"){let r=i.callee,s=this.getPropertyName(r);if(s&&(s==="slice"||s==="filter"||s==="toSorted"))return this.isEnvRecordsChain(r.object)}}return false}extractFieldsFromBody(t,i){let r=[];return ae(t,{MemberExpression:n=>{if(n.object.type==="Identifier"&&n.object.name===i){let a=this.getPropertyName(n);a&&a!=="length"&&r.push(a);}}}),r}detectMinMaxExtraction(t){let i=null;return ae(t,{CallExpression:s=>{if(!i&&s.callee.type==="MemberExpression"){let n=s.callee;if(n.object.type==="Identifier"&&n.object.name==="Math"){let a=this.getPropertyName(n);(a==="min"||a==="max")&&s.arguments.some(u=>u.type==="SpreadElement"&&this.isRecordsMapCall(u.argument))&&(i={reason:`Min/Max gate: Math.${a}() on individual records blocked for small datasets (n < 50). Use avg/stddev/count for privacy-safe aggregations.`});}}},MemberExpression:s=>{if(!i&&s.computed&&s.object.type==="CallExpression"){let n=s.object;if(n.callee.type==="MemberExpression"){let a=this.getPropertyName(n.callee);if(a==="sort"||a==="toSorted"){let u=n.callee.object;this.isEnvRecordsChain(u)&&(i={reason:"Min/Max gate: .sort()[index] on individual records blocked for small datasets (n < 50). Use avg/stddev/count for privacy-safe aggregations."});}}}}}),i}isRecordsMapCall(t){if(t.type!=="CallExpression")return false;let i=t;if(i.callee.type!=="MemberExpression")return false;let r=i.callee;return this.getPropertyName(r)==="map"&&this.isEnvRecordsChain(r.object)}identifyRecordBoundVars(t,i){ae(t,{CallExpression:n=>{if(n.callee.type!=="MemberExpression")return;let a=n.callee,u=this.getPropertyName(a);if(!u||!this.isEnvRecordsAccess(a.object))return;let c=n.arguments[0];if(c&&(c.type==="ArrowFunctionExpression"||c.type==="FunctionExpression")){let l=c;if(e.ARRAY_CALLBACK_METHODS.has(u)&&l.params.length>0){let p=l.params[0];p.type==="Identifier"&&i.add(p.name);}if(e.REDUCE_METHODS.has(u)&&l.params.length>1){let p=l.params[1];p.type==="Identifier"&&i.add(p.name);}}},ForOfStatement:n=>{if(this.isEnvRecordsAccess(n.right)&&n.left.type==="VariableDeclaration")for(let a of n.left.declarations)a.id.type==="Identifier"&&i.add(a.id.name);}}),ae(t,{VariableDeclarator:n=>{if(!(!n.init||n.id.type!=="Identifier")&&n.init.type==="MemberExpression"&&n.init.computed){let a=n.init;this.isEnvRecordsAccess(a.object)&&i.add(n.id.name);}}});}propagateTaint(t,i,r){for(let s=0;s<3;s++){let n=r.size;if(ae(t,{VariableDeclarator:u=>{!u.init||u.id.type!=="Identifier"||this.isExpressionTainted(u.init,i,r)&&r.add(u.id.name);},AssignmentExpression:u=>{u.left.type==="Identifier"&&this.isExpressionTainted(u.right,i,r)&&r.add(u.left.name);},FunctionDeclaration:u=>{u.id&&u.id.type==="Identifier"&&this.doesCallbackProduceTaint(u,null,i,r)&&r.add(u.id.name);},CallExpression:u=>{if(u.callee.type!=="MemberExpression")return;let c=u.callee;this.getPropertyName(c)==="push"&&c.object.type==="Identifier"&&u.arguments.some(p=>this.isExpressionTainted(p,i,r))&&r.add(c.object.name);}}),r.size===n)break}}checkReturnStatements(t,i,r){let s=null;return ae(t,{ReturnStatement:a=>{if(!s&&a.argument&&this.isExpressionTainted(a.argument,i,r)){let u=a.loc?.start.line?a.loc.start.line-1:void 0,c=this.describeTaintSource(a.argument,i,r);s={reason:`PII side-channel detected: output contains values derived from restricted fields. ${c?`Operation: ${c}. `:""}Use only non-PII fields (e.g., numeric/date columns) for aggregations.`,line:u,operation:c};}}}),s}isExpressionTainted(t,i,r){switch(t.type){case "Identifier":return r.has(t.name);case "MemberExpression":return this.isMemberExprTainted(t,i,r);case "CallExpression":return this.isCallExprTainted(t,i,r);case "YieldExpression":{let s=t;return s.argument?this.isExpressionTainted(s.argument,i,r):false}case "FunctionExpression":case "ArrowFunctionExpression":{let s=t;return this.doesCallbackProduceTaint(s,null,i,r)}case "BinaryExpression":case "LogicalExpression":{let s=t;return this.isExpressionTainted(s.left,i,r)||this.isExpressionTainted(s.right,i,r)}case "UnaryExpression":{let s=t;return this.isExpressionTainted(s.argument,i,r)}case "ConditionalExpression":{let s=t;return this.isExpressionTainted(s.test,i,r)||this.isExpressionTainted(s.consequent,i,r)||this.isExpressionTainted(s.alternate,i,r)}case "ObjectExpression":return t.properties.some(n=>n.type==="Property"&&this.isExpressionTainted(n.value,i,r));case "ArrayExpression":return t.elements.some(n=>n!==null&&this.isExpressionTainted(n,i,r));case "TemplateLiteral":return t.expressions.some(n=>this.isExpressionTainted(n,i,r));case "SpreadElement":{let s=t;return this.isExpressionTainted(s.argument,i,r)}default:return false}}isMemberExprTainted(t,i,r){let s=this.getPropertyName(t);if(t.object.type==="Identifier"&&i.has(t.object.name)&&s&&this.piiFields.has(s.toLowerCase()))return true;if(t.object.type==="MemberExpression"&&s&&this.piiFields.has(s.toLowerCase())){let n=t.object;if(n.computed&&this.isEnvRecordsAccess(n.object))return true}if(this.isExpressionTainted(t.object,i,r))return true;if(t.computed&&t.object.type==="Identifier"&&i.has(t.object.name)&&t.property.type==="Literal"){let n=t.property.value;if(typeof n=="string"&&this.piiFields.has(n.toLowerCase()))return true}return false}isCallExprTainted(t,i,r){if(t.callee.type==="MemberExpression"){let s=t.callee,n=this.getPropertyName(s);if(n&&e.TAINT_PROPAGATING_METHODS.has(n)&&this.isExpressionTainted(s.object,i,r))return true;if(this.isEnvRecordsAccess(s.object)&&t.arguments[0]){let a=t.arguments[0];if(a.type==="ArrowFunctionExpression"||a.type==="FunctionExpression")return this.doesCallbackProduceTaint(a,n,i,r)}if(this.isExpressionTainted(s.object,i,r)||t.arguments.some(a=>this.isExpressionTainted(a,i,r)))return true}if(t.callee.type==="MemberExpression"){let s=t.callee;this.getPropertyName(s)==="push"&&s.object.type==="Identifier"&&t.arguments.some(a=>this.isExpressionTainted(a,i,r))&&r.add(s.object.name);}if(t.callee.type==="Identifier"){let s=t.callee.name;if(r.has(s))return true;if(!new Set(["Math","Number","parseInt","parseFloat","isNaN","isFinite"]).has(s))return t.arguments.some(a=>this.isExpressionTainted(a,i,r))}return false}doesCallbackProduceTaint(t,i,r,s){let n=new Set(r),a=new Set(s);if(t.params.length>0){let p=i!==null&&e.REDUCE_METHODS.has(i)?1:0;t.params.length>p&&t.params[p].type==="Identifier"&&n.add(t.params[p].name);}if(t.type==="ArrowFunctionExpression"&&t.body.type!=="BlockStatement")return this.isExpressionTainted(t.body,n,a);let u=false,c={ReturnStatement:l=>{l.argument&&this.isExpressionTainted(l.argument,n,a)&&(u=true);},YieldExpression:l=>{let p=l;p.argument&&this.isExpressionTainted(p.argument,n,a)&&(u=true);}};return ae(t.body,c),u}getPropertyName(t){if(!t.computed&&t.property.type==="Identifier")return t.property.name;if(t.computed&&t.property.type==="Literal"){let i=t.property.value;if(typeof i=="string")return i}return null}isEnvRecordsAccess(t){if(t.type==="MemberExpression"){let i=t;if(this.getPropertyName(i)==="records"&&i.object.type==="Identifier"&&i.object.name==="env")return true}return t.type==="Identifier"&&t.name==="records"}describeTaintSource(t,i,r){if(t.type==="Identifier"){let s=t.name;if(r.has(s))return `variable '${s}' is PII-derived`}if(t.type==="ObjectExpression"){let s=t;for(let n of s.properties)if(n.type==="Property"&&this.isExpressionTainted(n.value,i,r))return `property '${n.key.type==="Identifier"?n.key.name:"unknown"}' contains PII-derived value`}if(t.type==="CallExpression"){let s=t;if(s.callee.type==="MemberExpression"){let n=this.getPropertyName(s.callee);if(n)return `result of .${n}() on PII data`}}}};var Gr={aspirin:"Medication",lisinopril:"Medication",metformin:"Medication",amlodipine:"Medication",atorvastatin:"Medication",omeprazole:"Medication",losartan:"Medication",simvastatin:"Medication",levothyroxine:"Medication",ibuprofen:"Medication",acetaminophen:"Medication",amoxicillin:"Medication",ciprofloxacin:"Medication",prednisone:"Medication",warfarin:"Medication",insulin:"Medication",hydrochlorothiazide:"Medication",gabapentin:"Medication",albuterol:"Medication",pantoprazole:"Medication",hypertension:"Condition",diabetes:"Condition",bronchitis:"Condition",pneumonia:"Condition",asthma:"Condition"},qe=4,qr=/^[\d\s.,:;!?()[\]{}<>@#$%^&*+=|\\/"'`~_-]+$/,Je=class e{static nlp=null;async getNlp(){if(!e.nlp){let t=await import('compromise/three');e.nlp=t.default||t,e.nlp.addWords(Gr);}return e.nlp}async scan(t){if(t.length<qe||qr.test(t))return {detected:false,entities:[]};let r=(await this.getNlp())(t),s=[],n=r.people().out("array");for(let c of n){let l=c.trim();l.length>=qe&&s.push({type:"person",text:l});}let a=r.places().out("array");for(let c of a){let l=c.trim();l.length>=qe&&s.push({type:"place",text:l});}let u=r.organizations().out("array");for(let c of u){let l=c.trim();l.length>=qe&&s.push({type:"organization",text:l});}return {detected:s.length>0,entities:s}}async scanDeep(t,i=new WeakSet){if(t==null)return {detected:false,entities:[]};if(typeof t=="string")return this.scan(t);if(typeof t=="object"){if(i.has(t))return {detected:false,entities:[]};i.add(t);let r=Array.isArray(t)?t:Object.values(t),s=[];for(let n of r){let a=await this.scanDeep(n,i);if(a.detected&&(s.push(...a.entities),a.entities.some(u=>u.type==="person")))return {detected:true,entities:s}}return {detected:s.length>0,entities:s}}return {detected:false,entities:[]}}};var Jr={maxDecimalPlaces:4,clampNonNegative:true};function Ye(e,t){let i={...Jr,...t},r=new WeakSet;function s(n){if(n==null)return n;if(typeof n=="number"){if(!Number.isFinite(n))return n;let a=n;i.clampNonNegative&&a<0&&(a=0);let u=10**i.maxDecimalPlaces;return a=Math.round(a*u)/u,a}if(typeof n=="string"||typeof n=="boolean")return n;if(typeof n=="object"){if(r.has(n))return n;if(r.add(n),Array.isArray(n))return n.map(u=>s(u));let a={};for(let[u,c]of Object.entries(n))a[u]=s(c);return a}return n}return s(e)}function Yr(e){let t=e.replace(/\D/g,"");if(t.length<13||t.length>19)return false;let i=0,r=false;for(let s=t.length-1;s>=0;s--){let n=parseInt(t.charAt(s),10);r&&(n*=2,n>9&&(n-=9)),i+=n,r=!r;}return i%10===0}function Qr(e){let t=e.replace(/\s+/g,"").toUpperCase();if(!/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/.test(t))return false;let i=t.substring(4)+t.substring(0,4),r="";for(let s=0;s<i.length;s++){let n=i.charCodeAt(s);if(n>=65&&n<=90)r+=(n-55).toString();else if(n>=48&&n<=57)r+=i.charAt(s);else return false}try{return BigInt(r)%97n===1n}catch{return false}}var D={EMAIL:{name:"EMAIL",pattern:/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b/gi,validator:e=>!e.endsWith("@example.com")&&!e.endsWith("@test.com")},CREDIT_CARD:{name:"CREDIT_CARD",pattern:/\b(?:\d[ -]*?){13,16}\b/g,validator:Yr},IP_ADDRESS:{name:"IP_ADDRESS",pattern:/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,validator:e=>["127.0.0.1","0.0.0.0","255.255.255.255"].includes(e)?false:e.split(".").map(Number).every(r=>r>=0&&r<=255)},PHONE:{name:"PHONE",pattern:/(?:(?:\+?\d{1,3}[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4})\b/g,validator:e=>{let t=e.replace(/\D/g,"");return !(t.length<7||t.length>15||/^(\d)\1+$/.test(t)||t==="1234567890")}},SSN:{name:"SSN",pattern:/\b\d{3}[- ]?\d{2}[- ]?\d{4}\b/g,validator:e=>{let t=e.replace(/\D/g,"");if(t.length!==9)return false;let i=parseInt(t.substring(0,3),10);return !(i===0||i===666||i>=900||parseInt(t.substring(3,5),10)===0||parseInt(t.substring(5,9),10)===0||/^(\d)\1+$/.test(t)||t==="123456789")}},IBAN:{name:"IBAN",pattern:/\b[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}\b/gi,validator:Qr},PASSPORT_MRZ:{name:"PASSPORT_MRZ",pattern:/\bP[A-Z<][A-Z<]{3}[A-Z0-9<]{39}(?:\b|\s|$)/g}},qi={GLOBAL_STRICT:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.PASSPORT_MRZ,D.IBAN],US_COMPLIANT:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.SSN,D.PASSPORT_MRZ],EU_GDPR:[D.EMAIL,D.CREDIT_CARD,D.IP_ADDRESS,D.PHONE,D.IBAN,D.PASSPORT_MRZ]},Qe=class e{patterns;forbiddenKeysSet;nerScanner;static KEY_SAFELIST=new Set(["grid","video","android","identity","provide","override","validate","hidden","widget","guidelines","beside","guideline","outside","inside","collide","decide","divide","aside","ride","side","wide","hide","tide","pride","bride","slide","guide","stride","oxide","dioxide","suicide","homicide","pesticide","valid","invalid","void","avoid","diagnosis","medication","namespace","namesake","rename","filename","hostname","typename","unnamed","renamed","phonetic","phoneme","microphone","headphone","telephone","saxophone","smartphone","streetview","addressable","addressing","cityscape","electricity","capacity","velocity","opacity","timestamp","timezone","image_id","computation_result","zk_receipt","testid","toolid","sessionid","peerid","nodeid","requestid","correlationid","traceid","spanid"]);shortTokenBoundaryPatterns;longForbiddenTokens;constructor(t=[],i=[],r){this.patterns=t,this.forbiddenKeysSet=new Set(i.map(s=>s.toLowerCase())),this.nerScanner=r??null,this.shortTokenBoundaryPatterns=new Map,this.longForbiddenTokens=[];for(let s of this.forbiddenKeysSet)s.length<4?this.shortTokenBoundaryPatterns.set(s,new RegExp((()=>{let n=s.split("").map(u=>`[${u.toLowerCase()}${u.toUpperCase()}]`).join(""),a=`[a-z]${s.charAt(0).toUpperCase()}${s.slice(1)}`;return `(?:^|[_-])${n}(?:$|[_-])|${a}(?:$|[A-Z_-])|^${n}$`})())):this.longForbiddenTokens.push(s);}async scan(t,i=new WeakSet){if(t==null)return null;if(typeof t=="string"){let r=t.trim();if(r.startsWith("{")&&r.endsWith("}")||r.startsWith("[")&&r.endsWith("]"))try{let n=JSON.parse(r),a=await this.scan(n,i);if(a)return a}catch{}let s=this.checkString(t);if(s)return s;if(this.nerScanner){let n=await this.nerScanner.scan(t);if(n.detected){let a=n.entities.find(u=>u.type==="person");if(a)return `PII Entity Detected: person name "${a.text}"`}}return null}if(typeof t=="object"){if(i.has(t))return null;if(i.add(t),Array.isArray(t))for(let r of t){let s=await this.scan(r,i);if(s)return s}else for(let[r,s]of Object.entries(t)){if(this.forbiddenKeysSet.has(r.toLowerCase()))return `Forbidden Key: ${r}`;let n=this.checkKeyFuzzy(r);if(n)return n;let a=await this.scan(s,i);if(a)return a}}return null}checkKeyFuzzy(t){let i=t.toLowerCase();if(e.KEY_SAFELIST.has(i))return null;for(let[r,s]of this.shortTokenBoundaryPatterns)if(s.test(t))return `Forbidden Key (fuzzy): ${t} matches boundary pattern "${r}"`;for(let r of this.longForbiddenTokens)if(i.includes(r))return `Forbidden Key (fuzzy): ${t} contains restricted token "${r}"`;return null}checkString(t){for(let i of this.patterns)if(typeof i=="string"){if(t.toLowerCase().includes(i.toLowerCase()))return i}else if(i instanceof RegExp){if(i.global&&(i.lastIndex=0),i.test(t))return i.source}else if(typeof i=="object"&&i!==null){let r=i;if(typeof r.pattern=="string"){if(t.toLowerCase().includes(r.pattern.toLowerCase())&&(!r.validator||r.validator(r.pattern)))return r.name}else if(r.pattern instanceof RegExp){r.pattern.global&&(r.pattern.lastIndex=0);let s=r.pattern.exec(t);for(;s!==null;){let n=s[0];if(!r.validator||r.validator(n))return r.name;if(!r.pattern.global)break;s=r.pattern.exec(t);}}}return null}};var Ji=Se.dirname(fileURLToPath(import.meta.url)),Yi=class e{constructor(t,i){this.serverInfo=t;this.config=i;let r=this.config?.security?.enableNerScanning?new Je:null;this.piiScanner=new Qe(this.config?.security?.piiPatterns??qi.GLOBAL_STRICT,this.config?.security?.forbiddenKeys??["id","name","fullName","firstName","lastName","address","street","city","postalCode","zipCode","phone","email","ssn","accountHolder","accountNumber","account_number","password","token","secret","privateKey"],r);let s=this.config?.security?.rateLimit;this.toolCallWindowMs=s?.windowMs??Number.parseInt(process.env.LIOP_RATE_LIMIT_WINDOW_MS??"60000",10),this.toolCallMaxPerWindow=s?.maxPerWindow??Number.parseInt(process.env.LIOP_RATE_LIMIT_MAX??"15",10),this.globalCallMaxPerWindow=s?.globalMaxPerWindow??Number.parseInt(process.env.LIOP_RATE_LIMIT_GLOBAL_MAX??"40",10);let n=this.config?.security?.forbiddenKeys??["id","name","fullName","firstName","lastName","address","street","city","postalCode","zipCode","phone","email","ssn","accountHolder","accountNumber","account_number","password","token","secret","privateKey"],a=this.config?.security?.sensitiveKeys??[];this.taintAnalyzer=new Ge(n,a);let u=import.meta.url.endsWith(".ts"),c=u?".ts":".js",l=[];if(u)try{let _=createRequire(import.meta.url).resolve("tsx/package.json");l=["--import",pathToFileURL(Se.join(Se.dirname(_),"dist","loader.mjs")).href];}catch{l=["--import","tsx"];}let p=process.env.NODE_ENV==="test"||process.env.VITEST;this.config?.capabilities&&!this.serverInfo.capabilities&&(this.serverInfo.capabilities=this.config.capabilities);let x=[Se.resolve(Ji,`./workers/logic-execution${c}`),Se.resolve(Ji,`../workers/logic-execution${c}`)],d=x.find(g=>j.existsSync(g))||x[1];this.workerPool=new Piscina({filename:d,minThreads:this.config?.workerPool?.minThreads??(p?0:2),maxThreads:this.config?.workerPool?.maxThreads??(p?1:8),idleTimeout:this.config?.workerPool?.idleTimeout??(p?500:5e3),maxQueue:this.config?.workerPool?.maxQueue??"auto",taskQueue:new FixedQueue,execArgv:l,resourceLimits:{maxOldGenerationSizeMb:this.config?.workerPool?.maxHeapMb??Number.parseInt(process.env.LIOP_WORKER_MAX_HEAP_MB??"64",10)}});let k=this.config?.workerPool?.minThreads??(p?0:2);if(this.workerPool&&k>0)for(let g=0;g<k;g++)this.workerPool.run({isWarmup:true}).catch(_=>{a$1.debug(`[LiopServer] Worker pool warm-up ping failed: ${_.message}`);});if(this.config?.auth?.role==="nexus"){let g=this.config.auth.issuer||"http://localhost:3000",_=this.config.auth.audience||le.audience,w=this.config.auth.clients||[{client_id:process.env.LIOP_OAUTH_CLIENT_ID||"liop-mesh-agent",client_secret:process.env.LIOP_OAUTH_CLIENT_SECRET||"dev-secret-change-me",grant_types:["client_credentials"],scope:"liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query"}],{provider:N,jwks:Z}=ai({issuer:g,clients:w});this.oauthProvider=N,this.jwtValidator=new Pe(g,_,Z);}else if(this.config?.auth?.role==="node"){let g=this.config.auth.nexusUrl||process.env.LIOP_NEXUS_URL||"http://localhost:3000",_=this.config.auth.audience||le.audience,w=g.endsWith("/oidc")?g:`${g}/oidc`,N=new URL(`${w}/jwks`);this.jwtValidator=new Pe(g,_,N);}this.resource("LIOP Envelope Specification","liop://protocol/envelope-spec","Complete Logic-on-Origin envelope format, execution rules, and security constraints","text/plain",()=>Promise.resolve(this.buildEnvelopeSpec())),this.config?.auth?.revocationPath&&this.loadRevocationList();}logicCache=new Map;connectionStats=new Map;CACHE_TTL_MS=1440*60*1e3;THROTTLE_THRESHOLD=5;THROTTLE_COOLDOWN_MS=60*1e3;toolCallWindows=new Map;toolCallMaxPerWindow;toolCallWindowMs;globalCallWindow=[];globalCallMaxPerWindow;fieldQueryBudget=new Map;taintAnalyzer;tools=new Map;resources=new Map;prompts=new Map;activeSchema=null;sandboxRecords=[];piiScanner;workerPool;meshNode=null;rpcServer=null;boundPort=null;jwtValidator;oauthProvider;sessions=new Map;revokedTokenHashes=new Set;lastRevocationLoadTime=0;static LIOP_COMPACT_REGEX=/@LIOP\{(?<target>[^,}]+)(?:,(?<name>[^}]*))?\}\n(?<logic>[\s\S]*?)\n@END/m;extractLogic(t){let i=t.match(e.LIOP_COMPACT_REGEX);return i?.groups?.logic?i.groups.logic.trim():null}parseUnknownJson(t){if(typeof t!="string")return t;let i=t.trim();if(i.startsWith("{")&&i.endsWith("}")||i.startsWith("[")&&i.endsWith("]"))try{return JSON.parse(i)}catch{return t}return t}runPreflightPolicy(t,i,r,s="local-client"){if(r){let c=i.replace(/\s+/g," ");if(r.enforceAggregationFirst&&[/return\s+env\.records(?!\s*\.\s*(?:reduce|length|filter|every|some|find)\b)/i,/return\s*\{[\s\S]*\b(accounts|patients|rows|records)\s*:\s*env\.records(?!\s*\.\s*(?:reduce|length|filter)\b)/i].some(p=>p.test(c)))return "Preflight policy rejected: potential row-level export pattern detected.";if(r.preflightDenyPatterns?.some(l=>l.test(c)))return "Preflight policy rejected: custom deny pattern matched."}let n=50;typeof r?.enforceAggregationFirst=="object"&&(n=r.enforceAggregationFirst.minMaxBlockThreshold??50);let a=this.taintAnalyzer.analyze(i,this.sandboxRecords.length,n);if(a)return `Preflight policy rejected: ${a.reason}`;let u=this.taintAnalyzer.extractQueriedFields(i);if(u.length>0){let c=this.fieldQueryBudget.get(s);c||(c=new Map,this.fieldQueryBudget.set(s,c));let l=c.get(t);l||(l=new Map,c.set(t,l));for(let p of u){let x=this.taintAnalyzer.classifyField(p,r?.sensitiveKeys),d=25,k="public";if(r?.queryBudgetPerField!==void 0?(d=r.queryBudgetPerField,k="override"):x==="forbidden"?(d=3,k="forbidden"):x==="sensitive"&&(d=8,k="sensitive"),(l.get(p)??0)>=d)return `Preflight policy rejected: Query budget exceeded for field '${p}' (max ${d} per session for ${k} fields). Rotate PQC session to reset budget.`}for(let p of u){let x=l.get(p)??0;l.set(p,x+1);}}return null}validateOutputPolicy(t,i,r){if(!r)return null;let s=this.parseUnknownJson(i);if(s&&typeof s=="object"&&s.isError===true)return null;if(r.outputSchema){let a=(()=>{if(!(r.outputSchema instanceof b$2.ZodObject))return r.outputSchema;let u=r.outputSchema;return u._def.catchall instanceof b$2.ZodNever?u.strict():u})().safeParse(s);if(!a.success)return `[LIOP] Output schema violation for ${t}: ${a.error.issues.map(u=>`${u.path.join(".")||"<root>"} ${u.message}`).join("; ")}. HINT: Your output must conform to the declared schema. Use 'env.records' to access the dataset and return only allowed fields.`}return r.enforceAggregationFirst&&this.violatesAggregationFirstPolicy(this.unwrapForAggregationPolicyScan(s),r.enforceAggregationFirst,this.sandboxRecords.length)?process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1"?"Aggregation-First Policy Violation: row-level export or K-Anonymity violation blocked. HINT: Use .reduce() to produce a flat {key:value} object. Do NOT use .map() to create arrays of objects. Ensure dataset size > 10 for detailed results.":"Aggregation-First Policy Violation: Output blocked due to privacy constraints.":null}unwrapForAggregationPolicyScan(t){if(typeof t=="string"){let n=t.trim();if(n.startsWith("{")&&n.endsWith("}")||n.startsWith("[")&&n.endsWith("]"))try{return this.unwrapForAggregationPolicyScan(JSON.parse(n))}catch{return t}return t}if(!t||typeof t!="object")return t;let i=t;if(i.computation_result!==void 0)return this.unwrapForAggregationPolicyScan(i.computation_result);if(!Array.isArray(i.content)||i.content.length===0)return t;let r=[];for(let n of i.content)if(n&&typeof n=="object"&&"text"in n){let a=n.text;typeof a=="string"&&r.push(a);}if(r.length===0)return t;let s=r.length===1?r[0]:r.join(`
|
|
18
18
|
`);return this.unwrapForAggregationPolicyScan(s)}violatesAggregationFirstPolicy(t,i,r){if(!i)return false;let s=typeof i=="object"&&typeof i.maxOutputRows=="number"?i.maxOutputRows:10,n=typeof i=="object"&&typeof i.allowPrimitiveArrays=="boolean"?i.allowPrimitiveArrays:true;if(typeof t=="string"){let a=t.trim();if(a.startsWith("{")&&a.endsWith("}")||a.startsWith("[")&&a.endsWith("]"))try{return this.violatesAggregationFirstPolicy(JSON.parse(a),i,r)}catch{return false}return false}if(Array.isArray(t))return t.length>0&&t.every(a=>typeof a=="object"&&a!==null)?t.length>s?true:t.some(a=>this.violatesAggregationFirstPolicy(a,i,r)):t.length>0&&t.every(a=>typeof a!="object"||a===null)?!n:t.some(a=>this.violatesAggregationFirstPolicy(a,i,r));if(t&&typeof t=="object"){let a=Object.keys(t);return r!==void 0&&r>0&&r<10&&(a.length>3||Object.values(t).some(c=>Array.isArray(c)||typeof c=="object"&&c!==null))||a.length>s?true:Object.values(t).some(u=>this.violatesAggregationFirstPolicy(u,i,r))}return false}buildEnvelopeSpec(){let t=["LIOP v1 Envelope Specification","================================","","FORMAT:","","Compact Envelope:"," @LIOP{wasi_v1,TaskName}"," <JavaScript code>"," @END","","RUNTIME ENVIRONMENT:","- env.records: Array of data objects from the origin","- Must use 'return' to output results","- Zero-Trust WASI Sandbox (Node.js Worker Pool)","- Return aggregated objects, NOT raw row-level arrays","","SANDBOX RUNTIME RESTRICTIONS & WORKAROUNDS:","- Date is poisoned: The 'Date' class/constructor is undefined (Date.now(), Date.parse(), etc. will throw)."," Workaround: Use lexicographical string comparison on ISO 8601 date strings (e.g., record.date >= '2024-01-01').","- Poisoned globals: eval, Function, setTimeout, setInterval, Buffer, ArrayBuffer, and TypedArrays are undefined.","- Frozen prototypes: Any modifications to Object.prototype, Array.prototype, etc., are blocked.","","SECURITY CONSTRAINTS:","- PII Egress Shield blocks raw identifiers in output","- Aggregation-First policy: prefer counts, averages, summaries","- AST Guardian: static analysis before execution","","DIFFERENTIAL PRIVACY (DP) MECHANISM (Laplace Mechanism):","- Default field noise scale is derived from node global sensitivity.","- COUNT / LENGTH Optimization: To obtain EXACT counts without noise (sensitivity=1),"," the return keys MUST contain 'count', 'length', 'size', 'num', 'positive', 'negative',"," or start with 'total_' or 'num_' (e.g. 'total_tx', 'credits_count').","- AVERAGE Optimization: Keys containing 'avg', 'mean' or 'average' scale noise"," down automatically by dividing sensitivity by dataset size (sensitivity / n).","- SUM / OTHER queries: Receive full Laplace noise based on global node sensitivity"," (e.g., Sensitivity=100,000 in Bank to protect balances)."];return this.config?.security?.forbiddenKeys?.length&&t.push(`- Restricted fields: ${this.config.security.forbiddenKeys.join(", ")}`),t.push("","TAINT TRACKING (Phase 108):","- AST-level analysis blocks PII-derived scalars (charCodeAt, charAt, etc.)","- Operations on restricted fields are tracked through variable assignments","- Boolean inference (field.charCodeAt(0) < N ? 1 : 0) is blocked","- Allowed: aggregations on non-PII fields (balance, amount, date)","","K-ANONYMITY THRESHOLDS:","- Small Datasets (< 10 records): Maximum of 3 scalar output fields. Nesting or arrays in output are strictly forbidden.","- Large Datasets (>= 10 records): Maximum of 10 output fields.","","RATE LIMITS (OWASP A01):","- Per-tool: 15 calls/min (configurable via LIOP_RATE_LIMIT_MAX)","- Global: 40 calls/min across all tools (LIOP_RATE_LIMIT_GLOBAL_MAX)","","OPTIONAL PARAMETERS:","- __liop_bypass_ast_cache: boolean (force AST re-evaluation)","","AUTHENTICATION (tokenSlug Convention):","- Restricted nodes declare authRequired: true in their manifest.","- Token resolution: LIOP_TOKEN_<tokenSlug> (deterministic) > LIOP_TOKEN_<PeerID> > LIOP_TOKEN_<ProviderName>","- Format: tokenSlug must match SCREAMING_SNAKE_CASE /^[A-Z][A-Z0-9_]*$/ (e.g., BANK, VAULT, HFT_ORACLE)."),t.join(`
|
|
19
19
|
`)}extractSchemaFieldSummary(t,i=0){if(i>3)return "{...}";let r=t.type,s=t.properties,n=t.items;return s?`{${Object.entries(s).map(([u,c])=>{let l=c.type;if(l==="array"&&c.items){let p=this.extractSchemaFieldSummary(c.items,i+1);return `${u}(array of ${p})`}if(l==="object"&&c.properties){let p=this.extractSchemaFieldSummary(c,i+1);return `${u}(${p})`}return `${u}(${l||"unknown"})`}).join(", ")}}`:r==="array"&&n?`Array of ${this.extractSchemaFieldSummary(n,i+1)}`:r||Object.keys(t).join(", ")}async connect(t={}){return this.connectToMesh(t)}tool(t,i,r,s,n){if(this.tools.has(t))throw new Error(`Tool already registered: ${t}`);let a=b$2.object(r),u=st(a),c=i,l=s;if(r.payload&&r.payload instanceof b$2.ZodString){let x=this.config?.security?.forbiddenKeys||[];if(c+=`
|
|
20
20
|
|
|
@@ -50,5 +50,5 @@ ${JSON.stringify(this.activeSchema,null,2)}`:""}
|
|
|
50
50
|
Protocol Adherence is mandatory for successful execution.`}}]}));}resource(t,i,r,s,n){if(this.resources.has(i))throw new Error(`Resource URI already registered: ${i}`);this.resources.set(i,{name:t,uri:i,description:r,mimeType:s,content:n});}buildExecutionGuidelines(){return ["LIOP Sandbox Execution Guidelines","=================================","","1. DATE POISONING & FILTERING WORKAROUND:"," The global 'Date' class is set to undefined inside the sandbox. Calling 'new Date()', 'Date.now()', or 'Date.parse()' will throw a ReferenceError."," - Workaround: Perform chronological filtering using lexicographical string comparisons on ISO 8601 strings."," Example: const filtered = env.records.filter(r => r.date >= '2024-01-01' && r.date <= '2024-12-31');","","2. K-ANONYMITY CONSTRAINTS:"," - Datasets with LESS than 10 records: The returned object must contain at most 3 scalar fields, and must NOT contain any arrays or nested objects."," - Datasets with 10 or MORE records: The returned object can contain up to 10 fields.","","3. DIFFERENTIAL PRIVACY SUFFIXES:"," To avoid Laplacian noise adding random perturbations to your counts or averages, you must name your object keys using specific terms:"," - Counts (Exact, no noise): Key names must contain 'count', 'length', 'size', 'num', 'positive', 'negative', or start with 'total_' or 'num_'."," - Averages (Reduced noise): Key names must contain 'avg', 'mean', or 'average'."," - Sums/Other: Will receive full Laplace noise.","","4. GENERAL RESTRICTIONS:"," - Do not use 'eval', 'Function', 'setTimeout', 'setInterval', 'Buffer', 'ArrayBuffer', or TypedArrays."," - Do not attempt to modify prototypes (Object.prototype, Array.prototype)."].join(`
|
|
51
51
|
`)}dataDictionary(t,i="Global Medical Data Dictionary",r="liop://schema/global",s="Exposes the internal database schema for Zero-Shot Autonomy planning"){t.$comment="LIOP DIRECTIVES: 1. Date is undefined (Date.now(), new Date() throw). Workaround: use lexicographical string comparison on ISO 8601 string dates (e.g. record.date >= '2024-01-01'). 2. Small datasets (<10 records) limit outputs to max 3 scalar keys with NO nesting. 3. DP counts must contain count/length/size/num/total_ prefix/suffix.",this.activeSchema=t;let n=this.extractSchemaFieldSummary(t);for(let[a,u]of this.tools.entries())u.schema.shape.payload&&u.schema.shape.payload instanceof b$2.ZodString&&u.tool.description&&!u.tool.description.includes("Data structure:")&&(u.tool.description+=`
|
|
52
52
|
Data structure: ${n}. Full schema: resource ${r}. Guidelines: resource liop://schema/guidelines`,this.tools.set(a,u));this.resources.has("liop://schema/guidelines")||this.resource("LIOP Execution Guidelines","liop://schema/guidelines","Directives for generating compliant JavaScript code for the LIOP Sandbox runtime","text/plain",()=>Promise.resolve(this.buildExecutionGuidelines())),this.resource(i,r,s,"application/json",JSON.stringify(t,null,2));}clearAstCache(){this.logicCache.clear(),a$1.info("[LIOP-SDK] AST Security Cache cleared by Admin.");}checkToolCallRateLimit(t){let i=Date.now(),r=this.toolCallWindowMs,s=this.toolCallMaxPerWindow,a=(this.toolCallWindows.get(t)||[]).filter(u=>i-u<r);if(a.length>=s){let u=Math.ceil((a[0]+r-i)/1e3);return {content:[{type:"text",text:`LIOP_RATE_LIMITED: Too many calls to ${t}. Max ${s} per ${r/1e3}s window. Retry after ${u}s.`}],isError:true}}return a.push(i),this.toolCallWindows.set(t,a),null}checkGlobalRateLimit(){let t=Date.now(),i=this.toolCallWindowMs,r=this.globalCallMaxPerWindow;if(this.globalCallWindow=this.globalCallWindow.filter(s=>t-s<i),this.globalCallWindow.length>=r){let s=Math.ceil((this.globalCallWindow[0]+i-t)/1e3);return {content:[{type:"text",text:`LIOP_RATE_LIMITED: Global call limit exceeded. Max ${r} total calls per ${i/1e3}s window. Retry after ${s}s.`}],isError:true}}return this.globalCallWindow.push(t),null}async callTool(t,i="local-client"){let r=this.tools.get(t.name);if(!r)throw new Error(`Tool not found: ${t.name}`);let s=this.checkGlobalRateLimit();if(s)return s;let n=this.checkToolCallRateLimit(t.name);if(n)return n;try{let a=r.schema.parse(t.arguments||{});if(t.arguments?.__liop_bypass_ast_cache===!0&&(a.__liop_bypass_ast_cache=!0),a&&typeof a.payload=="string"){let c=a.payload,l=this.extractLogic(c);if(l){let p=this.runPreflightPolicy(t.name,l,r.policy,i);return p?{content:[{type:"text",text:p}],isError:!0}:(a.payload=l,await this.executeInWorkerPool(a,l,t.name))}}return await r.handler(a,{})}catch(a){let u=a;return u instanceof b$2.ZodError?{content:[{type:"text",text:`Validation Error: ${u.message}`}],isError:true}:{content:[{type:"text",text:`Internal Execution Error: ${u.message}`}],isError:true}}}listTools(){return Array.from(this.tools.values()).map(t=>t.tool)}listPrompts(){return Array.from(this.prompts.values()).map(t=>t.prompt)}async getPrompt(t){let i=this.prompts.get(t.name);if(!i)throw new Error(`Prompt not found: ${t.name}`);return await i.handler(t)}listResources(){return Array.from(this.resources.values())}async readResource(t){let i=this.resources.get(t);if(!i)throw new Error(`Resource not found: ${t}`);let r="No description provided";return typeof i.content=="function"?r=await i.content():typeof i.content=="string"?r=i.content:i.description&&(r=i.description),{contents:[{uri:i.uri,mimeType:i.mimeType||"text/plain",text:r}]}}getServerInfo(){return this.serverInfo}getMeshNode(){return this.meshNode}setSandboxData(t){this.sandboxRecords=t;}getBoundPort(){return this.boundPort}async connectToMesh(t={}){let i=process.env.LIOP_GRPC_PORT?Number.parseInt(process.env.LIOP_GRPC_PORT,10):void 0,r=t.port??i??50051,s=/^[A-Z][A-Z0-9_]*$/;if(this.config?.tokenSlug&&!s.test(this.config.tokenSlug))throw new Error(`Invalid tokenSlug "${this.config.tokenSlug}". Must match SCREAMING_SNAKE_CASE: /^[A-Z][A-Z0-9_]*$/ (e.g., "BANK", "VAULT", "HFT_ORACLE").`);this.meshNode=new a$2(t.meshConfig),await this.meshNode.start();let n=this.meshNode;this.meshNode.registerManifestHandler(()=>{let a=this.listTools().map(c=>({name:c.name,description:c.description,inputSchema:c.inputSchema})),u=Array.from(this.resources.values()).map(c=>({name:c.name,uri:c.uri,description:c.description,mimeType:c.mimeType,text:typeof c.content=="string"?c.content:c.description}));return {peerId:n.getPeerId(),grpcPort:r,tools:a,resources:u,serverInfo:this.serverInfo,authRequired:this.jwtValidator!==void 0,tokenSlug:this.config?.tokenSlug,taxonomy:this.config?.taxonomy?{domain:this.config.taxonomy.domain||"Unknown Domain",clearanceTier:this.config.taxonomy.clearanceTier??0,executionTypes:this.config.taxonomy.executionTypes||[]}:void 0}});for(let a of this.listTools())await this.meshNode.announceCapability(a.name).catch(a$1.info);await this.meshNode.announceManifest().catch(a$1.info),this.rpcServer=new Me,this.rpcServer.addService({negotiateIntent:(a,u)=>{let c=a.request;if(a$1.info(`[LIOP-RPC] Negotiating intent for capability: ${c.capability_hash}`),this.jwtValidator){let l=a.metadata.get("authorization")[0];if(!l){u({code:H.status.UNAUTHENTICATED,details:"Missing Authorization header in gRPC metadata"});return}let p=l.startsWith("Bearer ")?l.slice(7):l,x=de.createHash("sha256").update(p).digest("hex").toLowerCase();if(this.loadRevocationList(),this.revokedTokenHashes.has(x)){u({code:H.status.UNAUTHENTICATED,details:"Token has been revoked by the resource owner"});return}let d=this.config?.auth?.localTestToken,k=/^[a-zA-Z0-9_-]+-local-test-token$/;if(d){if(p===d){a$1.info(`[LIOP-RPC] Bypass authentication for matching localTestToken: ${d}`),import('./kyber-NONMBQNH.js').then(async({Kyber768Wrapper:g})=>{let{publicKey:_,secretKey:w}=await g.generateKeyPair(),N=de.randomUUID();this.sessions.set(N,{capability_hash:c.capability_hash,kyber_sk:w,agent_did:c.agent_did,tokenHash:x}),u(null,{accepted:true,session_token:N,error_message:"",kyber_public_key:_});});return}u({code:H.status.PERMISSION_DENIED,details:p&&k.test(p)?"Pre-shared local test token is invalid for this resource domain (segregation violation).":"Access Denied: This restricted node requires its specific static access token."});return}this.jwtValidator.validate(p).then(g=>{let _=a$3("tools/call",g);if(!_.allowed){u({code:H.status.PERMISSION_DENIED,details:_.reason||"Access Denied"});return}import('./kyber-NONMBQNH.js').then(async({Kyber768Wrapper:w})=>{let{publicKey:N,secretKey:Z}=await w.generateKeyPair(),B=de.randomUUID();this.sessions.set(B,{capability_hash:c.capability_hash,kyber_sk:Z,agent_did:c.agent_did,tokenHash:x}),u(null,{accepted:true,session_token:B,error_message:"",kyber_public_key:N});});}).catch(g=>{u({code:H.status.UNAUTHENTICATED,details:`Invalid JWT token: ${g.message}`});});}else import('./kyber-NONMBQNH.js').then(async({Kyber768Wrapper:l})=>{let{publicKey:p,secretKey:x}=await l.generateKeyPair(),d=de.randomUUID();this.sessions.set(d,{capability_hash:c.capability_hash,kyber_sk:x,agent_did:c.agent_did}),u(null,{accepted:true,session_token:d,error_message:"",kyber_public_key:p});});},executeLogic:async a=>{let u=a.request;a$1.info(`[LIOP-RPC] Executing Logic-on-Origin for session: ${u.session_token}`);let c=async()=>{let l=this.sessions.get(u.session_token);if(!l){a.emit("error",{code:H.status.UNAUTHENTICATED,details:"Invalid session token"});return}if(l.tokenHash&&(this.loadRevocationList(),this.revokedTokenHashes.has(l.tokenHash))){a.emit("error",{code:H.status.UNAUTHENTICATED,details:"Token has been revoked by the resource owner"});return}let p=l.capability_hash,d=(p?this.tools.get(p):void 0)?.policy;try{let g=(await createMlKem768()).decap(new Uint8Array(u.pqc_ciphertext),l.kyber_sk),_=Buffer.from(g),w=Buffer.from(u.wasm_binary),N=w.subarray(-16),Z=w.subarray(0,-16),B=de.createDecipheriv("aes-256-gcm",_,Buffer.from(u.aes_nonce||new Uint8Array(12)));B.setAuthTag(N);let K=B.update(Z);K=Buffer.concat([K,B.final()]);let me=K.toString("utf-8"),M=this.extractLogic(me)||me.trim(),X=this.runPreflightPolicy(p||"unknown_tool",M,d,u.session_token);if(X){a$1.info(`[LIOP-RPC] Preflight blocked: ${X}`);let ee={semantic_evidence:X,cryptographic_proof:Buffer.from(""),zk_receipt:Buffer.from(""),is_error:!0};a.write(ee,()=>{a.end();});return}}catch(k){let g=k instanceof Error?k.message:String(k);a$1.error(`[LIOP-RPC] Preflight decryption failed: ${g}`),a.emit("error",{code:H.status.INTERNAL,details:`Preflight logic analysis failed: ${g}`});return}try{let k=d?{epsilon:d.dpEpsilon??1,sensitivity:d.dpSensitivity??1,smallDatasetThreshold:d.dpSmallDatasetThreshold??50}:void 0,g=await this.workerPool.run({ciphertext:u.pqc_ciphertext,secretKeyObj:Array.from(l.kyber_sk),wasmBinary:u.wasm_binary,inputs:u.inputs,aesNonce:u.aes_nonce,records:this.sandboxRecords,sessionToken:u.session_token,isEncrypted:!0,dpConfig:k}),_=Ye(g.output),w,N=_;try{w=typeof _=="string"?_:JSON.stringify(_);let M=JSON.parse(w);if(M.__liop_proxy_tool){a$1.info(`[LIOP-RPC] Executing Proxied Tool: ${M.__liop_proxy_tool}`);let X=l.agent_did||"unknown-client",ee=await this.callTool({name:M.__liop_proxy_tool,arguments:M.__liop_proxy_args||{}},X);if(ee.isError){a$1.info(`[LIOP-RPC] Proxy tool execution failed: ${ee.content[0].text}`);let Qi={semantic_evidence:ee.content[0].text||"Unknown error",cryptographic_proof:Buffer.from(""),zk_receipt:Buffer.from(""),is_error:!0};a.write(Qi,()=>{a.end();});return}let _t=Ye(ee);w=JSON.stringify(_t),N=this.unwrapForAggregationPolicyScan(_t);}}catch{w=String(_);}let Z=this.validateOutputPolicy(p||"unknown_tool",N,d);if(Z){a$1.info(`[LIOP-RPC] Output policy blocked for ${p||"unknown_tool"}: ${Z}`);let ee={semantic_evidence:process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1"?Z:"[LIOP] Egress Security Violation. Output blocked due to policy enforcement.",cryptographic_proof:Buffer.from(""),zk_receipt:Buffer.from(""),is_error:!0};a.write(ee,()=>{a.end();});return}let B={semantic_evidence:w,cryptographic_proof:Buffer.from(g.image_id||"","hex"),zk_receipt:g.zk_receipt?Buffer.from(g.zk_receipt,"base64"):Buffer.from(""),is_error:!1},K=await this.piiScanner.scan(N),me=this.violatesAggregationFirstPolicy(this.unwrapForAggregationPolicyScan(N),d?.enforceAggregationFirst,this.sandboxRecords?.length);if(K||me){let M=K||"Aggregation-First Policy Violation";a$1.info(`[LIOP-RPC] Secure egress blocked in gRPC stream: ${M}`),B.semantic_evidence="[LIOP] Egress Security Violation. Output blocked due to policy enforcement.",B.is_error=!0;}a.write(B,()=>{a.end();});}catch(k){let g=k,_=process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test",w=g.message||String(k);a$1.error(`[LIOP-RPC] Execution Error: ${w}`);let Z={semantic_evidence:_?`Execution Error: ${w}`:"[LIOP] Execution Failed. The injected logic violated runtime constraints or encountered a fatal error.",cryptographic_proof:Buffer.from(""),zk_receipt:Buffer.from(""),is_error:true};try{a.write(Z,()=>{a.end();});}catch{a.end();}}};if(this.jwtValidator){let l=a.metadata.get("authorization")[0];if(!l){a.emit("error",{code:H.status.UNAUTHENTICATED,details:"Missing Authorization header in gRPC metadata"});return}let p=l.startsWith("Bearer ")?l.slice(7):l,x=de.createHash("sha256").update(p).digest("hex").toLowerCase();if(this.loadRevocationList(),this.revokedTokenHashes.has(x)){a.emit("error",{code:H.status.UNAUTHENTICATED,details:"Token has been revoked by the resource owner"});return}let d=this.config?.auth?.localTestToken,k=/^[a-zA-Z0-9_-]+-local-test-token$/;if(d){if(p===d){a$1.info(`[LIOP-RPC] Bypass authentication in executeLogic for matching localTestToken: ${d}`),await c();return}a.emit("error",{code:H.status.PERMISSION_DENIED,details:p&&k.test(p)?"Pre-shared local test token is invalid for this resource domain (segregation violation).":"Access Denied: This restricted node requires its specific static access token."});return}try{let g=await this.jwtValidator.validate(p),_=a$3("tools/call",g);if(!_.allowed){a.emit("error",{code:H.status.PERMISSION_DENIED,details:_.reason||"Access Denied"});return}await c();}catch(g){a.emit("error",{code:H.status.UNAUTHENTICATED,details:`Invalid JWT token: ${g instanceof Error?g.message:String(g)}`});}}else await c();}}),this.boundPort=await this.rpcServer.listen(r),a$1.info(`[LIOP-SDK] Node successfully announced to Mesh. PeerID: ${this.meshNode.getPeerId()}`);}async executeInWorkerPool(t,i,r){try{let s=r?this.tools.get(r)?.policy:void 0,n=s?{epsilon:s.dpEpsilon??1,sensitivity:s.dpSensitivity??1,smallDatasetThreshold:s.dpSmallDatasetThreshold??50}:void 0,a=await this.workerPool.run({ciphertext:new Uint8Array(0),secretKeyObj:Array.from(new Uint8Array(0)),kyberPublicKey:new Uint8Array(0),wasmBinary:Buffer.from(i),inputs:{},records:this.sandboxRecords,sessionToken:"local-dev-token",isEncrypted:!1,dpConfig:n}),u=a.output,c=Ye(u),p=[{type:"text",text:JSON.stringify({computation_result:c,image_id:a.image_id,zk_receipt:a.zk_receipt,status:"Worker Pool Execution Success"})}],x=r?this.tools.get(r)?.policy:void 0,d=this.validateOutputPolicy(r||"unknown_tool",c,x);if(d)return a$1.info(`[LIOP-SDK] Output policy blocked for ${r||"unknown_tool"}: ${d}`),{content:[{type:"text",text:process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1"?d:"[LIOP] Egress Security Violation. Output blocked due to policy enforcement. Ensure your logic uses strictly aggregated, non-PII patterns."}],isError:!0};let k=await this.piiScanner.scan(c),g=this.violatesAggregationFirstPolicy(c,x?.enforceAggregationFirst,this.sandboxRecords?.length);if(k||g){let _=k||"Aggregation-First Policy Violation: Output blocked due to dynamic flat-key policy enforcement.";return a$1.info(`[LIOP-SDK] Secure egress blocked in local execution: ${_}`),{content:[{type:"text",text:process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1"?`[LIOP] Egress Security Violation: ${_}`:"[LIOP] Egress Security Violation. Output blocked due to policy enforcement. Ensure your logic uses strictly aggregated, non-PII patterns."}],isError:!0}}return {content:p}}catch(s){let n=s,a=process.env.NODE_ENV==="development"||process.env.NODE_ENV==="test"||process.env.LIOP_SEC_VERBOSE==="1",u=n.message||String(s);return a$1.error(`[LIOP-SDK] WorkerPool Execution Fault: ${u}`),{content:[{type:"text",text:u.includes("worker_thread_exited")||u.includes("ERR_WORKER_OUT_OF_MEMORY")||u.includes("terminated")||u.includes("heap limit")?"[LIOP] Execution terminated: memory limit exceeded (64MB heap). Reduce data processing volume.":a?`WorkerPoolError: ${u}`:"[LIOP] Execution Failed. The injected logic violated runtime constraints or encountered a fatal error."}],isError:true}}}async close(){this.workerPool&&await this.workerPool.close({force:true}),this.rpcServer&&await this.rpcServer.stop(),this.meshNode&&await this.meshNode.stop();}loadRevocationList(){let t=this.config?.auth?.revocationPath;if(t)try{if(j.existsSync(t)){let i=j.statSync(t);if(i.mtimeMs<=this.lastRevocationLoadTime)return;let r=j.readFileSync(t,"utf-8"),s=JSON.parse(r);if(Array.isArray(s)){this.revokedTokenHashes.clear();for(let n of s)typeof n=="string"&&this.revokedTokenHashes.add(n.trim().toLowerCase());this.lastRevocationLoadTime=i.mtimeMs,a$1.info(`[LiopServer] Loaded ${this.revokedTokenHashes.size} revoked token hashes from ${t}`);}}else {let i=Se.dirname(t);j.existsSync(i)||j.mkdirSync(i,{recursive:!0}),j.writeFileSync(t,JSON.stringify([],null,2),"utf-8"),this.lastRevocationLoadTime=Date.now(),a$1.info(`[LiopServer] Created empty local revocation list at ${t}`);}}catch(i){a$1.error(`[LiopServer] Failed to load revocation list: ${i instanceof Error?i.message:String(i)}`);}}revokeToken(t){if(!t)return;let i=de.createHash("sha256").update(t).digest("hex").toLowerCase();this.revokeTokenHash(i);}revokeTokenHash(t){if(!t)return;let i=t.toLowerCase();this.loadRevocationList(),this.revokedTokenHashes.add(i);let r=this.config?.auth?.revocationPath;if(r)try{let s=Se.dirname(r);j.existsSync(s)||j.mkdirSync(s,{recursive:!0});let n=Array.from(this.revokedTokenHashes);j.writeFileSync(r,JSON.stringify(n,null,2),"utf-8");let a=j.statSync(r);this.lastRevocationLoadTime=a.mtimeMs,a$1.info(`[LiopServer] Persisted revocation for hash ${i} to ${r}`);}catch(s){a$1.error(`[LiopServer] Failed to persist revocation: ${s instanceof Error?s.message:String(s)}`);}}};
|
|
53
|
-
export{Me as a,le as b,Pe as c,ai as d,Je as e,Ye as f,D as g,qi as h,Qe as i,Yi as j};//# sourceMappingURL=chunk-
|
|
54
|
-
//# sourceMappingURL=chunk-
|
|
53
|
+
export{Me as a,le as b,Pe as c,ai as d,Je as e,Ye as f,D as g,qi as h,Qe as i,Yi as j};//# sourceMappingURL=chunk-KQ5BDO2M.js.map
|
|
54
|
+
//# sourceMappingURL=chunk-KQ5BDO2M.js.map
|