Class: Antigravity::Guards::FileProtection

Inherits:
Object
  • Object
show all
Defined in:
lib/antigravity/guards.rb

Overview

Configurable opt-in file protection guard

Constant Summary collapse

DEFAULT_FILES =
[
  /\.env(\..*)?$/i,
  /Gemfile(\.lock)?$/i,
  /config\/secrets\.yml$/i,
  /config\/database\.yml$/i
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files: nil, add: [], remove: []) ⇒ FileProtection

Returns a new instance of FileProtection.



153
154
155
156
157
158
159
# File 'lib/antigravity/guards.rb', line 153

def initialize(files: nil, add: [], remove: [])
  initial_patterns = files ? Array(files) : DEFAULT_FILES
  patterns = initial_patterns.map { |f| f.is_a?(Regexp) ? f : /#{Regexp.escape(f.to_s)}$/i }
  patterns += Array(add).map { |f| f.is_a?(Regexp) ? f : /#{Regexp.escape(f.to_s)}$/i }
  patterns -= Array(remove).map { |f| f.is_a?(Regexp) ? f : /#{Regexp.escape(f.to_s)}$/i }
  @protected_files = patterns.freeze
end

Instance Attribute Details

#protected_filesObject (readonly)

Returns the value of attribute protected_files.



151
152
153
# File 'lib/antigravity/guards.rb', line 151

def protected_files
  @protected_files
end

Instance Method Details

#call(_tool_name, params) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/antigravity/guards.rb', line 161

def call(_tool_name, params)
  path = extract_path(params)
  return :allow unless path

  if protected_path?(path)
    { status: :deny, reason: "FileProtection Guard: Modifications to '#{path}' are restricted." }
  else
    :allow
  end
end

#to_procObject



172
173
174
# File 'lib/antigravity/guards.rb', line 172

def to_proc
  method(:call).to_proc
end