Class: Antigravity::Guards::FileProtection
- Inherits:
-
Object
- Object
- Antigravity::Guards::FileProtection
- 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
-
#protected_files ⇒ Object
readonly
Returns the value of attribute protected_files.
Instance Method Summary collapse
- #call(_tool_name, params) ⇒ Object
-
#initialize(files: nil, add: [], remove: []) ⇒ FileProtection
constructor
A new instance of FileProtection.
- #to_proc ⇒ Object
Constructor Details
#initialize(files: nil, add: [], remove: []) ⇒ FileProtection
Returns a new instance of FileProtection.
93 94 95 96 97 98 99 |
# File 'lib/antigravity/guards.rb', line 93 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_files ⇒ Object (readonly)
Returns the value of attribute protected_files.
91 92 93 |
# File 'lib/antigravity/guards.rb', line 91 def protected_files @protected_files end |
Instance Method Details
#call(_tool_name, params) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/antigravity/guards.rb', line 101 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_proc ⇒ Object
112 113 114 |
# File 'lib/antigravity/guards.rb', line 112 def to_proc method(:call).to_proc end |