@matatbread/matbot-tool-types 0.4.21 → 0.4.25

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "matbotRuntime": [
4
4
  "node"
5
5
  ],
6
- "version": "0.4.21",
6
+ "version": "0.4.25",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
@@ -37,13 +37,13 @@
37
37
  "typescript": "^6.0.3"
38
38
  },
39
39
  "peerDependencies": {
40
- "@matatbread/matbot-plugin-api": "^0.4.21",
41
- "@matatbread/matbot-core": "^0.4.21"
40
+ "@matatbread/matbot-plugin-api": "^0.4.25",
41
+ "@matatbread/matbot-core": "^0.4.25"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^6.0.3",
45
- "@matatbread/matbot-plugin-api": "^0.4.21",
46
- "@matatbread/matbot-core": "^0.4.21"
45
+ "@matatbread/matbot-plugin-api": "^0.4.25",
46
+ "@matatbread/matbot-core": "^0.4.25"
47
47
  },
48
48
  "scripts": {
49
49
  "typecheck": "tsc --noEmit"
package/src/checker.ts CHANGED
@@ -114,7 +114,7 @@ const MAX_FULL = 8;
114
114
  // A structural rule's label, by its private code. CAST-GATE is the default because it was the first and
115
115
  // is what the contract gate (90004) has always reported as; a rule about something other than types
116
116
  // needs its own name, or the label says the wrong thing about the finding under it.
117
- const SYN_LABELS: Record<number, string> = { 90005: 'ENV-GATE', 90006: 'IMPORT-GATE' };
117
+ const SYN_LABELS: Record<number, string> = { 90005: 'ENV-GATE', 90006: 'IMPORT-GATE', 90007: 'ESCAPE-GATE' };
118
118
 
119
119
  // One rule, one name, in every renderer: a per-rule label read off the record rather than spelled at
120
120
  // each render site, so a summary can never call a finding something its own entry did not.
@@ -411,8 +411,8 @@ try {
411
411
  //
412
412
  // WHICH names are withheld comes from the runner's withheldGlobals, because it is the only thing that
413
413
  // knows and a copy here could not be kept in step: a name shadowed but not rejected typechecks and
414
- // fails at the first call, which is the one failure this gate exists to prevent. 'eval' and 'Function'
415
- // are the worst to miss, being lib.es globals that typecheck in every configuration.
414
+ // fails at the first call, which is the one failure this gate exists to prevent. 'eval' is the worst
415
+ // to miss, being a lib.es global that typechecks in every configuration.
416
416
  //
417
417
  // The ADVICE stays here, because it is about repairing a snippet and that is this file's job, not the
418
418
  // runner's. A withheld name with no entry below still fires, with a generic line — the list is the
@@ -427,7 +427,6 @@ try {
427
427
  globalThis: "'globalThis' is not defined in a function body — the runner withholds it, so a host global cannot be reached by name. Everything a body is given is already in scope: 'tool', 'toolInContext' and 'context'.",
428
428
  global: "'global' is not defined in a function body — the runner withholds it, and it is node's older alias for 'globalThis', which is withheld too. Everything a body is given is already in scope: 'tool', 'toolInContext' and 'context'.",
429
429
  eval: "'eval' is not defined in a function body — the runner withholds it. There is nothing to evaluate at run time: write the code, or call another tool with 'await tool.<name>(…)'.",
430
- Function: "'Function' is not defined in a function body — the runner withholds the constructor, so 'new Function(…)' cannot build code at run time. Write the code directly. (As a TYPE annotation it is fine; this is about the value.)",
431
430
  };
432
431
  // A withheld global named in a TYPE is harmless: types are erased before the body is compiled, so
433
432
  // 'args: Buffer' never reaches the runner. Only the VALUE is rejected. Needed because the second half of
@@ -542,6 +541,103 @@ try {
542
541
  ts.forEachChild(node, function (c) { envVisit(c, sf); });
543
542
  };
544
543
 
544
+ // ── escape gate: what reaches past the runner without naming anything it withholds ──
545
+ // Shadowing works on NAMES, and these are not names: every function's '.constructor' is a compiler for
546
+ // the global scope, where the real 'process' and every ungated builtin are; 'getBuiltinModule' loads a
547
+ // builtin with no import to gate; and a shared object a body changes — a built-in, its prototype, or
548
+ // something it was handed — is changed for the rest of matbot. The runner refuses the first two at run
549
+ // time however they are reached, and cannot refuse the third, so this is where a plainly-spelled one is
550
+ // stopped and the author told why. A computed spelling gets past it; that is deliberate tampering,
551
+ // which no checker is the answer to. Only with a runner: without one nothing is refused, and saying so
552
+ // would be untrue.
553
+ const escapeGate = envGate && workerData.withheldGlobals !== undefined;
554
+ const ESCAPE = {
555
+ ctor: "a function's 'constructor' compiles a string as code in matbot's own scope — outside the runner, around every withheld global and permission gate — so a function body may not reach it, and it is refused at run time too. Write the code directly, import a module with 'await import(…)' (the user is asked), or call a tool.",
556
+ Function: "'Function' compiles a string as code in matbot's own scope — outside the runner, around every withheld global and permission gate — so a function body may not call it, and it is refused at run time too. Write the code directly. (As a TYPE annotation it is fine; this is about the value.)",
557
+ builtin: "'getBuiltinModule' loads a node builtin without the import gate, so a function body may not call it, and it is refused at run time too. Import the module with 'await import('node:…')', which asks the user.",
558
+ proto: "'__proto__' reaches a prototype shared with the rest of matbot, which a function body may neither reach nor change. For an object with a prototype of your own, use 'Object.create(proto)' or a class.",
559
+ mutate: function (what) {
560
+ return "this changes '" + what + "', which this function body did not create. Built-ins, their prototypes and what a body is handed are shared with the rest of matbot, so changing them changes matbot. Work on a copy ('const mine = { ...x }') or a wrapper of your own instead.";
561
+ },
562
+ };
563
+ const unwrap = function (e) {
564
+ while (e && (ts.isParenthesizedExpression(e) || ts.isNonNullExpression(e) || ts.isAsExpression(e)
565
+ || (ts.isTypeAssertionExpression && ts.isTypeAssertionExpression(e)) || ts.isSatisfiesExpression(e))) e = e.expression;
566
+ return e;
567
+ };
568
+ const memberName = function (e) {
569
+ if (ts.isPropertyAccessExpression(e)) return e.name.text;
570
+ if (ts.isElementAccessExpression(e) && ts.isStringLiteralLike(e.argumentExpression)) return e.argumentExpression.text;
571
+ return undefined;
572
+ };
573
+ const isGetPrototypeOf = function (e) {
574
+ return ts.isCallExpression(e) && ts.isPropertyAccessExpression(e.expression) && e.expression.name.text === 'getPrototypeOf';
575
+ };
576
+ // Whether an object a body is about to change is one it did not create: reached through a prototype,
577
+ // or rooted at a binding declared outside the snippet (a built-in, or 'tool' / 'context').
578
+ const shared = function (target) {
579
+ let e = unwrap(target);
580
+ for (;;) {
581
+ if (isGetPrototypeOf(e)) return true;
582
+ if (ts.isPropertyAccessExpression(e) || ts.isElementAccessExpression(e)) {
583
+ const n = memberName(e);
584
+ if (n === 'prototype' || n === '__proto__') return true;
585
+ e = unwrap(e.expression);
586
+ continue;
587
+ }
588
+ if (!ts.isIdentifier(e)) return false;
589
+ const sym = checker.getSymbolAtLocation(e);
590
+ return !!(sym && sym.declarations && sym.declarations.length > 0) && !authorDeclared(e);
591
+ }
592
+ };
593
+ const isCallable = function (e) {
594
+ const t = checker.getTypeAtLocation(e);
595
+ return t.getCallSignatures().length > 0 || t.getConstructSignatures().length > 0;
596
+ };
597
+ const MUTATORS = { Object: ['defineProperty', 'defineProperties', 'setPrototypeOf', 'assign'],
598
+ Reflect: ['defineProperty', 'set', 'setPrototypeOf', 'deleteProperty'] };
599
+ const isAssignment = function (k) {
600
+ return k >= ts.SyntaxKind.FirstAssignment && k <= ts.SyntaxKind.LastAssignment;
601
+ };
602
+ const escapeVisit = function (node, sf) {
603
+ if (escapeGate && node.getStart(sf) >= minStart) {
604
+ const name = (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) ? memberName(node) : undefined;
605
+ if (name === 'constructor') {
606
+ const obj = unwrap(node.expression);
607
+ if (isCallable(obj) || memberName(obj) === 'constructor') synth.push(mk(sf, node, 90007, ESCAPE.ctor));
608
+ } else if (name === 'getBuiltinModule') {
609
+ synth.push(mk(sf, node, 90007, ESCAPE.builtin));
610
+ } else if (name === '__proto__') {
611
+ synth.push(mk(sf, node, 90007, ESCAPE.proto));
612
+ } else if (ts.isIdentifier(node) && node.text === 'Function'
613
+ && !(node.parent && ts.isPropertyAccessExpression(node.parent) && node.parent.name === node)
614
+ && !(node.parent && (ts.isPropertyAssignment(node.parent) || ts.isPropertySignature(node.parent)) && node.parent.name === node)
615
+ && !(node.parent && ts.isBindingElement(node.parent) && node.parent.propertyName === node)
616
+ && !inTypePosition(node) && !authorDeclared(node)) {
617
+ synth.push(mk(sf, node, 90007, ESCAPE.Function));
618
+ }
619
+ let changed;
620
+ if (ts.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) changed = node.left;
621
+ else if ((ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node))
622
+ && (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)) changed = node.operand;
623
+ else if (ts.isDeleteExpression(node)) changed = node.expression;
624
+ else if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)
625
+ && ts.isIdentifier(node.expression.expression) && node.arguments.length > 0) {
626
+ const ns = MUTATORS[node.expression.expression.text];
627
+ if (ns && ns.indexOf(node.expression.name.text) !== -1 && !authorDeclared(node.expression.expression)) changed = node.arguments[0];
628
+ }
629
+ if (changed !== undefined) {
630
+ const c = unwrap(changed);
631
+ // Only a member: assigning a name rebinds the body's own variable, which shadowing makes harmless.
632
+ const member = ts.isPropertyAccessExpression(c) || ts.isElementAccessExpression(c);
633
+ if ((member && shared(c.expression)) || (!member && ts.isCallExpression(node) && shared(c))) {
634
+ synth.push(mk(sf, changed, 90007, ESCAPE.mutate(c.getText(sf))));
635
+ }
636
+ }
637
+ }
638
+ ts.forEachChild(node, function (c) { escapeVisit(c, sf); });
639
+ };
640
+
545
641
  const skip = new Set();
546
642
  const visit = function (node, sf) {
547
643
  const isAssertion = ts.isAsExpression(node) || (ts.isTypeAssertionExpression && ts.isTypeAssertionExpression(node));
@@ -566,7 +662,7 @@ try {
566
662
  }
567
663
  ts.forEachChild(node, function (c) { visit(c, sf); });
568
664
  };
569
- for (const gf of gateFiles) { visit(gf, gf); contractVisit(gf, gf); envVisit(gf, gf); }
665
+ for (const gf of gateFiles) { visit(gf, gf); contractVisit(gf, gf); envVisit(gf, gf); escapeVisit(gf, gf); }
570
666
 
571
667
  // A bare 'node' is not the builtins namespace — it resolves to @types/node's index.d.ts, which is
572
668
  // references and ambient blocks with no export, so tsc reports "File '<matbot's own node_modules>/
package/src/index.ts CHANGED
@@ -401,6 +401,7 @@ export class ToolTypeIndexImpl implements ToolTypeIndex {
401
401
  return `${note}`
402
402
  + (withheld.length > 0
403
403
  ? `// A body is a bare async function, not a module, and not every global is in scope. Absent: ${withheld.join(', ')}, import.meta.\n`
404
+ + `// Nor can it build code from a string: 'Function' and every function's '.constructor' refuse a body.\n`
404
405
  : '');
405
406
  }
406
407