Files
openwork/patches/@opencode__codemode@2.2.5.patch
Jalil 04909d5fcb fix(den): stop slow regular expressions from stalling the API (#7389)
Security hardening. A regular expression matches synchronously, so no
timer, including a Workflow's own timeout, can stop one that backtracks
exponentially. Den now stops caller-supplied patterns itself.

- time-limit.ts: runWithTimeLimit runs synchronous work under Node's
  script timeout, whose watchdog thread can end a match in progress.
- JSON Schema checks for Workflow input and output and for remote MCP
  tool arguments run under it (250 ms) and report a check that took too
  long as a validation failure.
- Programs match through a RegExp subclass that allows 250 ms of
  matching per event-loop turn, so slow matches in a loop stop too. The
  program gets a RangeError it can read or catch.
- Both engines build program regular expressions through one hook:
  host-regexp.ts in @openwork/codemode and a two-line patch to
  @opencode/codemode 2.2.5. Hosts that install no constructor are
  unchanged.

Signed-off-by: Jalil <reachjalil@gmail.com>
2026-11-13 13:31:53 -08:00

28 lines
1.5 KiB
Diff

diff --git a/dist/interpreter/objects.js b/dist/interpreter/objects.js
index 702a490bb177561d76fc3e9fff2758be3489ef44..c3c643d3db8cb5701dfaaf8ed762d1c06e8fbd1e 100644
--- a/dist/interpreter/objects.js
+++ b/dist/interpreter/objects.js
@@ -260,7 +260,8 @@ export class RegExpObj extends Wrapper {
regex;
constructor(proto, pattern, flags) {
super(proto);
- this.regex = new RegExp(pattern, flags);
+ // OpenWork: a host may install a RegExp subclass, for example one that limits match time.
+ this.regex = new (globalThis.__openworkCodemodeRegExp ?? RegExp)(pattern, flags);
}
toString() {
return `/${this.regex.source}/${this.regex.flags}`;
diff --git a/dist/stdlib/regexp.js b/dist/stdlib/regexp.js
index 4caa44f962e5bcd6d84788a86b2fdd8d1c925779..a17e5a294bf37d0a176edc52982f88b1c4bb008b 100644
--- a/dist/stdlib/regexp.js
+++ b/dist/stdlib/regexp.js
@@ -19,7 +19,7 @@ export const toHostRegex = (arg, method, extraFlags = "") => {
// Anything else is a pattern string, as `new RegExp(arg)` would read it: undefined is the empty pattern.
const source = arg === undefined ? "" : coerceToString(arg);
try {
- return new RegExp(source, extraFlags);
+ return new (globalThis.__openworkCodemodeRegExp ?? RegExp)(source, extraFlags);
}
catch (error) {
throw syntaxError(`String.${method} received the string ${JSON.stringify(source)}, which is not a valid regular expression pattern (${regexFailureReason(error)}). ${escapeRegexHint}`);