Mintplex-Labs/anything-llm · warning · Error

search pattern must not start with '-'

Error message

search pattern must not start with '-'

What it means

Defense-in-depth security guard inside searchWithRipgrep. Ripgrep patterns that start with '-' could be misinterpreted as flags (e.g. '--pre=/bin/sh' would execute a command). Even though a '--' separator is pushed before the pattern, this check rejects any leading-dash pattern before the process is spawned.

Source

Thrown at server/utils/agents/aibitat/plugins/filesystem/search-files.js:347

  // Build ripgrep arguments
  const args = [
    "--json", // JSON output for structured parsing
    "--line-number", // Include line numbers
    "--no-ignore", // Search all files, even those in .gitignore
    "--max-count",
    String(maxResults),
  ];

  if (!caseSensitive) args.push("--ignore-case");
  if (filePattern) args.push("--glob", filePattern);
  for (const exclude of excludePatterns) args.push("--glob", `!${exclude}`);

  // Security: prevent argument injection attacks where a malicious pattern like
  // "--pre=/bin/sh" could cause ripgrep to execute arbitrary commands.
  // The "--" separator tells ripgrep to treat everything after it as positional
  // arguments, not options. The startsWith("-") check is defense-in-depth.
  if (typeof pattern === "string" && pattern.startsWith("-")) {
    throw new Error("search pattern must not start with '-'");
  }
  args.push("--", pattern, searchPath);
  const result = spawnSync(rgPath, args, {
    encoding: "utf-8",
    maxBuffer: 10 * 1024 * 1024, // 10MB
  });

  // Exit code 1 means no matches (not an error)
  if (result.status > 1) {
    throw new Error(
      result.stderr || `ripgrep exited with code ${result.status}`
    );
  }

  const results = [];
  if (!result.stdout) return results;
  const matches = safeJsonParse(result.stdout, []).filter(
    (m) => m.type === "match" && m.data

View on GitHub (pinned to 526360e320)

Solutions

  1. Strip or escape a leading dash before calling the tool: if the pattern starts with '-', prefix it with a backslash or remove the dash.
  2. If the user genuinely wants to search for a literal leading dash, pass an escaped regex like '\\-' or use a pattern that does not start with the dash.
  3. Validate user/agent input upstream and reject or rewrite flag-like patterns before they reach the tool.

Example fix

// before
if (typeof pattern === "string" && pattern.startsWith("-")) {
  throw new Error("search pattern must not start with '-' ");
}

// caller-side fix — sanitize before calling the agent tool
const safePattern = pattern.startsWith("-") ? `\\${pattern}` : pattern;
// then pass safePattern instead of pattern
Defensive patterns

Strategy: validation

Validate before calling

/** Strip or reject leading dashes before passing to the search tool. */
function sanitizeSearchPattern(pattern) {
  if (typeof pattern !== "string") return pattern;
  // Remove leading dashes or escape them
  return pattern.replace(/^-+/, (m) => "\\".repeat(m.length) + m);
}
const safePattern = sanitizeSearchPattern(userPattern);

Type guard

/** @param {string} p */
function isSafePattern(p) {
  return typeof p === "string" && p.length > 0 && !p.startsWith("-");
}

Try / catch

try {
  const results = searchWithRipgrep({ searchPath, pattern: safePattern, ... });
} catch (e) {
  if (e.message === "search pattern must not start with '-'") {
    // Re-sanitize and retry, or inform the caller
    const escaped = pattern.replace(/^-/, "\\-");
    return searchWithRipgrep({ searchPath, pattern: escaped, ... });
  }
  throw e;
}

Prevention

When it happens

Trigger: An agent or caller passes a content-search pattern that begins with '-', such as '-foo', '--bar', or a regex like '-[a-z]+'. Most commonly happens when the LLM generates a malformed query or when a user's literal search term starts with a hyphen.

Common situations: LLM agent hallucinates a flag-like search term; a frontend input is passed straight to the tool without sanitization; testing with a pattern meant for grep -v syntax; the agent tries to search for a negative lookahead regex.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/c53d248558bdf645. Report an issue: GitHub.