Class: Blazer::Ai::PromptSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/ai/prompt_sanitizer.rb

Constant Summary collapse

SUSPICIOUS_PATTERNS =

Patterns that might indicate prompt injection attempts

[
  /ignore\s+(previous|above|all)\s+instructions/i,
  /disregard\s+(previous|above|all)/i,
  /forget\s+(everything|what|your)/i,
  /new\s+instructions?:/i,
  /system\s*:/i,
  /assistant\s*:/i,
  /\bDROP\b/i,
  /\bDELETE\b/i,
  /\bTRUNCATE\b/i,
  /<script/i,
  /javascript:/i
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(max_length: nil) ⇒ PromptSanitizer

Returns a new instance of PromptSanitizer.



19
20
21
# File 'lib/blazer/ai/prompt_sanitizer.rb', line 19

def initialize(max_length: nil)
  @max_length = max_length || Blazer::Ai.configuration.max_prompt_length
end

Instance Method Details

#sanitize(prompt) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/blazer/ai/prompt_sanitizer.rb', line 23

def sanitize(prompt)
  return "" if prompt.blank?

  sanitized = prompt.to_s.strip

  # Truncate to max length
  sanitized = sanitized[0...@max_length]

  # Remove potential injection patterns (replace with empty string)
  SUSPICIOUS_PATTERNS.each do |pattern|
    sanitized = sanitized.gsub(pattern, "")
  end

  # Remove angle brackets that might affect prompt parsing
  sanitized = sanitized.gsub(/[<>]/, "")

  sanitized.strip
end

#suspicious?(prompt) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/blazer/ai/prompt_sanitizer.rb', line 42

def suspicious?(prompt)
  return false if prompt.blank?
  SUSPICIOUS_PATTERNS.any? { |p| prompt.match?(p) }
end