Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import type { RefsOf } from '../context'; |
| 2 | import { createPatternMatcher, patternMatches, type PatternMatcher } from './linear-pattern'; |
| 3 | |
| 4 | /** |
| 5 | * Longest `pattern` / `patternProperties` key this package compiles. Tool |
| 6 | * schemas come from third-party toolkit definitions; anything past this is |
| 7 | * far outside what a format constraint needs and is rejected before it can |
| 8 | * cost anything at validation time. |
| 9 | */ |
| 10 | export const MAX_PATTERN_LENGTH = 1024; |
| 11 | |
| 12 | export type InvalidPatternReason = 'syntax' | 'too-long' | 'unsupported'; |
| 13 | |
| 14 | /** |
| 15 | * Raised at conversion time when a schema `pattern` cannot be turned into a |
| 16 | * `RegExp`. Conversion fails eagerly, in line with how this package and |
| 17 | * `@composio/json-schema-to-effect-schema` treat other schema defects: a broken |
| 18 | * schema is the tool author's bug, so it surfaces once at construction rather |
| 19 | * than on every validation as if the caller's input were wrong. |
| 20 | */ |
| 21 | export class InvalidPatternError extends Error { |
| 22 | override readonly name = 'InvalidPatternError'; |
| 23 | |
| 24 | constructor( |
| 25 | readonly keyword: 'pattern' | 'patternProperties', |
| 26 | readonly pattern: string, |
| 27 | readonly path: ReadonlyArray<string | number>, |
| 28 | readonly reason: InvalidPatternReason, |
| 29 | detail: string, |
| 30 | options?: ErrorOptions |
| 31 | ) { |
| 32 | const location = path.length > 0 ? path.join('.') : '<root>'; |
| 33 | super( |
| 34 | `Invalid ${keyword} regular expression ${JSON.stringify(pattern)} at ${location}: ${detail}`, |
| 35 | options |
| 36 | ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * A schema pattern as a `RegExp` whose matching cannot be made to backtrack |
| 42 | * catastrophically. The native `source` stays intact, so JSON Schema emitted |
| 43 | * from the Zod schema still carries the original pattern for the model, while |
| 44 | * `test` and `exec` go through a {@link PatternMatcher}: RE2 where it can run |
| 45 | * the pattern, otherwise a statically vetted, length-bounded native match, or |
| 46 | * no check at all. |
| 47 | */ |
| 48 | export class SchemaPatternRegExp extends RegExp { |
| 49 | readonly matcher: PatternMatcher; |
| 50 | |
| 51 | constructor(pattern: string, matcher: PatternMatcher = createPatternMatcher(pattern)) { |
| 52 | super(pattern); |
| 53 | this.matcher = matcher; |
| 54 | } |
| 55 | |
| 56 | override test(input: string): boolean { |
| 57 | return patternMatches(this.matcher, String(input)); |
| 58 | } |
| 59 | |
| 60 | override exec(input: string): RegExpExecArray | null { |
| 61 | const string = String(input); |
| 62 | switch (this.matcher.kind) { |
| 63 | case 'linear': |
| 64 | return this.matcher.regex.exec(string) as RegExpExecArray | null; |
| 65 | case 'backtracking': |
| 66 | return patternMatches(this.matcher, string) ? this.matcher.regex.exec(string) : null; |
| 67 | case 'unenforced': |
| 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // `split` and `matchAll` build a derived regex through the species |
| 73 | // constructor, whose signature this class does not share. Only `test` and |
| 74 | // `exec`, which Zod and the guard call, are protected. |
| 75 | static override get [Symbol.species](): RegExpConstructor { |
| 76 | return RegExp; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Compile a schema pattern, rejecting anything that does not compile or that |
| 82 | * exceeds {@link MAX_PATTERN_LENGTH}. |
| 83 | * |
| 84 | * Matching is protected against catastrophic backtracking (ReDoS): tool |
| 85 | * arguments come from a model that may be steered by untrusted content, and |
| 86 | * object keys are as attacker-influenced as values. Patterns run on RE2, |
| 87 | * which is linear-time. A `pattern` RE2 cannot express (lookaround) falls back |
| 88 | * to the native engine only when it passes a conservative static check, and |
| 89 | * only on bounded input; otherwise it is left unenforced, which widens the |
| 90 | * schema no further than omitting the pattern would. A `patternProperties` |
| 91 | * key decides which schema applies to a property, so it is never silently |
| 92 | * skipped: one RE2 cannot express is rejected at conversion. |
| 93 | * |
| 94 | * No `u` flag is passed: existing tool schemas rely on identity escapes that |
| 95 | * Unicode mode refuses, and the goal here is to fail on defects, not to |
| 96 | * tighten which patterns are accepted. |
| 97 | */ |
| 98 | export const compilePattern = ( |
| 99 | keyword: 'pattern' | 'patternProperties', |
| 100 | pattern: string, |
| 101 | refs: Pick<RefsOf<unknown>, 'path'> |
| 102 | ): SchemaPatternRegExp => { |
| 103 | const path = keyword === 'pattern' ? refs.path : [...refs.path, 'patternProperties', pattern]; |
| 104 | |
| 105 | if (pattern.length > MAX_PATTERN_LENGTH) { |
| 106 | throw new InvalidPatternError( |
| 107 | keyword, |
| 108 | pattern, |
| 109 | path, |
| 110 | 'too-long', |
| 111 | `pattern exceeds ${MAX_PATTERN_LENGTH} characters` |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | new RegExp(pattern); |
| 117 | } catch (cause) { |
| 118 | const detail = cause instanceof Error ? cause.message : String(cause); |
| 119 | throw new InvalidPatternError(keyword, pattern, path, 'syntax', detail, { cause }); |
| 120 | } |
| 121 | |
| 122 | const matcher = createPatternMatcher(pattern); |
| 123 | if (keyword === 'patternProperties' && matcher.kind !== 'linear') { |
| 124 | throw new InvalidPatternError( |
| 125 | keyword, |
| 126 | pattern, |
| 127 | path, |
| 128 | 'unsupported', |
| 129 | 'patternProperties keys must not use lookaround or backreferences' |
| 130 | ); |
| 131 | } |
| 132 | return new SchemaPatternRegExp(pattern, matcher); |
| 133 | }; |