mastra-ai/mastra · error

Prompt injection detection failed: ${error instanceof Error

Error message

Prompt injection detection failed: ${error instanceof Error ? error.stack : 'Unknown error'}

What it means

PromptInjectionDetectorProcessor.processInput wraps its detection pass: TripWire throws (an injection was intentionally flagged) are re-thrown unchanged, but any other exception during injection detection is re-thrown as this generic Error including the original stack. It means the detector itself malfunctioned, not that injection was detected.

Source

Thrown at packages/core/src/processors/processors/prompt-injection-detector.ts:234

            continue;
          } else if (this.strategy === 'rewrite') {
            if (processedMessage) {
              processedMessages.push(processedMessage);
            }
            // If processedMessage is null (no rewrite available), skip the message
            continue;
          }
        }

        processedMessages.push(message);
      }

      return processedMessages;
    } catch (error) {
      if (error instanceof TripWire) {
        throw error; // Re-throw tripwire errors
      }
      throw new Error(`Prompt injection detection failed: ${error instanceof Error ? error.stack : 'Unknown error'}`);
    }
  }

  /**
   * Notify the consumer-supplied `onDetection` callback. Never throws.
   */
  private async emitDetection(input: string, detectionResult: PromptInjectionResult, flagged: boolean): Promise<void> {
    if (!this.onDetection) return;
    try {
      await this.onDetection({
        detectionResult,
        input,
        flagged,
        strategyApplied: flagged ? this.strategy : 'none',
      });
    } catch (error) {
      console.warn('[PromptInjectionDetector] onDetection callback failed:', error);
    }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Read the embedded original stack from the message for the root cause
  2. Validate detector options (thresholds, patterns) at construction
  3. Verify detection model credentials/connectivity if applicable
  4. Catch TripWire separately in callers to distinguish intentional trips from processor failures

Example fix

// before
try { await processor.processInput(messages); } catch (e) { /* indistinguishable */ }
// after
try {
  await processor.processInput(messages);
} catch (e) {
  if (e instanceof TripWire) throw e; // intentional detection
  throw e; // processor failure — inspect stack
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (typeof options.threshold !== 'number' || options.threshold < 0 || options.threshold > 1) {
  throw new TypeError('prompt-injection threshold must be a number between 0 and 1');
}

Type guard

function isTripWire(e: unknown): e is TripWire {
  return e instanceof TripWire;
}
function isInjectionProcessorFailure(e: unknown): e is Error {
  return e instanceof Error && e.message.startsWith('Prompt injection detection failed');
}

Try / catch

try {
  processed = await processor.processInput(messages);
} catch (e) {
  if (isTripWire(e)) throw e; // intentional injection trip
  if (isInjectionProcessorFailure(e)) {
    logger.error('Injection detector crashed', { stack: e.message });
    return messages; // or rethrow for fail-closed
  }
  throw e;
}

Prevention

When it happens

Trigger: Detector internals throw due to invalid options (bad patterns/thresholds), model/API errors in model-based detection, unexpected message shapes, or bugs in consumer-supplied detection configuration.

Common situations: Misconfigured detection threshold or pattern options; detection model auth/network failures; message content that is not plain text (images/attachments) hitting string-only code paths; version drift in message internals after upgrades.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/633b58a4acb9604d. Report an issue: GitHub.