Skip to content

fix(web): escape prompt editor typeahead query before building RegExp - #38416

Open
manan-tech wants to merge 1 commit into
langgenius:mainfrom
manan-tech:fix/prompt-editor-escape-regex-query
Open

fix(web): escape prompt editor typeahead query before building RegExp#38416
manan-tech wants to merge 1 commit into
langgenius:mainfrom
manan-tech:fix/prompt-editor-escape-regex-query

Conversation

@manan-tech

Copy link
Copy Markdown
Contributor

Fixes #38384

Summary

The {-triggered component picker in the prompt editor builds a regular expression directly from the typeahead query string:

const regex = new RegExp(queryString, "i")

The query string is arbitrary prompt text that follows a {. A common prompt such as:

- [Open manual]({source_url})

makes the query string source_url}), which is invalid regex syntax (Unmatched ")"). new RegExp throws a SyntaxError during render, and the whole prompt editor crashes with an unexpected component rendering error. Other characters such as [ (unterminated character class) or a trailing \ fail the same way.

Fix

Escape the query string before constructing the RegExp, using the same inline escapeRegExp idiom already used elsewhere in the codebase (goto-anything, agent-v2, console-openapi-url):

const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
...
const regex = new RegExp(escapeRegExp(queryString), "i")

This is applied at both call sites (useVariableOptions and useExternalToolOptions). As a side benefit, the typeahead now matches the query literally instead of interpreting user-typed text as a regex pattern, which is the expected behavior when filtering variable / tool names.

Testing

Added regression tests to the existing component-picker-block/__tests__/hooks.spec.tsx:

  • Crash-guard cases (source_url}), a[b, trailing \) for both useVariableOptions and useExternalToolOptions — verified these fail on the unescaped code (exact SyntaxError) and pass with the fix.

  • A literal-match assertion confirming a.b matches only a.b, not axb.

  • pnpm test on the spec: 67/67 pass

  • pnpm eslint on changed files: 0 errors

  • pnpm type-check: passes

The `{`-triggered component picker builds `new RegExp(queryString, 'i')`
from arbitrary prompt text. Text such as `[Open manual]({source_url})`
makes the query string `source_url})`, which is invalid regex syntax and
throws during render, crashing the prompt editor.

Escape the query string before constructing the RegExp so user-entered
text is matched literally instead of being interpreted as a pattern.
Applies to both `useVariableOptions` and `useExternalToolOptions`.

Fixes langgenius#38384
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 4, 2026
@github-actions github-actions Bot added the web This relates to changes on the web. label Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files. web This relates to changes on the web.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt editor crashes when brace-triggered variable search contains regex special characters

1 participant