console1984 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc64d037f2de5570292e0b09710b4543a68ba1af12759150cc68e7b7f4dd6e16
4
- data.tar.gz: c5929af5061393a32c4df38022535d53c62aaaabe951727798e340322fb0d950
3
+ metadata.gz: 2f1312b244e339b0196d4baa80fc989ed3871cd722f166a278f4877ca1dee4da
4
+ data.tar.gz: a66ab76f426a50cb99c4de2223d420c9d2d246b5dd0a6ec8f1712deca56244f9
5
5
  SHA512:
6
- metadata.gz: b64f422bcdd421e7a874af965c9ee615f6d873e0d2a685d68667216fd1ee91d4e8e4815d4dd9e78ef7838a75763fb898be1f328c8466a137f1782a4121531da6
7
- data.tar.gz: ce7e1f60bfdb666abe3c85a02ddaa1ac25344c994a1471ec64d07cd15f8aac5735bcd0608497e71d9813cf111cdb76f414a2373eb8dbef14b983757ba3876812
6
+ metadata.gz: d46797d8751dde38fccb25fef4e86e3a8ee42b17251d932e5cbc9ffc381bba2bd42c2a946ce8e7e867ff8e6270700dec73d8425534f52970c87bffbc682365f8
7
+ data.tar.gz: cd1a43abe5170dfb94a83ed375b8517b4263354ba30a53cb2287f3ffd240ab18984134df79801657fcb07fea1826d6fc3f978f6ae752c013ead820330d8e024a
@@ -1,12 +1,14 @@
1
- static_validations:
1
+ validations:
2
2
  forbidden_reopening:
3
3
  - ActiveRecord
4
4
  - Console1984
5
5
  - PG
6
6
  - Mysql2
7
+ - IRB
7
8
  forbidden_constant_reference:
8
9
  always:
9
10
  - Console1984
11
+ - IRB
10
12
  protected:
11
13
  - PG
12
14
  - Mysql2
@@ -16,15 +18,14 @@ static_validations:
16
18
  - Console1984
17
19
  - secret
18
20
  - credentials
21
+ - irb
19
22
  forbidden_methods:
20
- always:
21
- user:
22
- Kernel:
23
- - eval
24
- Object:
25
- - eval
26
- BasicObject:
27
- - eval
28
- - instance_eval
29
- Module:
30
- - class_eval
23
+ Kernel:
24
+ - eval
25
+ Object:
26
+ - eval
27
+ BasicObject:
28
+ - eval
29
+ - instance_eval
30
+ Module:
31
+ - class_eval
@@ -19,11 +19,15 @@ class Console1984::CommandExecutor
19
19
  run_as_system { session_logger.before_executing commands }
20
20
  validate_command commands
21
21
  execute_in_protected_mode(&block)
22
- rescue Console1984::Errors::ForbiddenCommand, FrozenError => e
22
+ rescue Console1984::Errors::ForbiddenCommandAttempted, FrozenError
23
23
  flag_suspicious(commands)
24
- rescue Console1984::Errors::SuspiciousCommand
24
+ rescue Console1984::Errors::SuspiciousCommandAttempted
25
25
  flag_suspicious(commands)
26
26
  execute_in_protected_mode(&block)
27
+ rescue Console1984::Errors::ForbiddenCommandExecuted
28
+ # We detected that a forbidden command was executed. We exit IRB right away.
29
+ flag_suspicious(commands)
30
+ Console1984.supervisor.exit_irb
27
31
  ensure
28
32
  run_as_system { session_logger.after_executing commands }
29
33
  end
@@ -65,13 +69,21 @@ class Console1984::CommandExecutor
65
69
  command_validator.validate(command)
66
70
  end
67
71
 
72
+ def from_irb?(backtrace)
73
+ executing_user_command? && backtrace.find do |line|
74
+ line_from_irb = line =~ /^[^\/]/
75
+ break if !(line =~ /console1984\/lib/ || line_from_irb)
76
+ line_from_irb
77
+ end
78
+ end
79
+
68
80
  private
69
81
  def command_validator
70
82
  @command_validator ||= build_command_validator
71
83
  end
72
84
 
73
85
  def build_command_validator
74
- Console1984::CommandValidator.from_config(Console1984.protections_config.static_validations)
86
+ Console1984::CommandValidator.from_config(Console1984.protections_config.validations)
75
87
  end
76
88
 
77
89
  def flag_suspicious(commands)
@@ -14,11 +14,11 @@ class Console1984::CommandValidator::ForbiddenConstantReferenceValidation
14
14
  @constant_names_forbidden_in_protected_mode = config[:protected] || []
15
15
  end
16
16
 
17
- # Raises a Console1984::Errors::ForbiddenCommand if a banned constant is referenced.
17
+ # Raises a Console1984::Errors::ForbiddenCommandAttempted if a banned constant is referenced.
18
18
  def validate(parsed_command)
19
19
  if contains_invalid_const_reference?(parsed_command, @forbidden_constants_names) ||
20
20
  (@shield.protected_mode? && contains_invalid_const_reference?(parsed_command, @constant_names_forbidden_in_protected_mode))
21
- raise Console1984::Errors::ForbiddenCommand
21
+ raise Console1984::Errors::ForbiddenCommandAttempted
22
22
  end
23
23
  end
24
24
 
@@ -8,11 +8,11 @@ class Console1984::CommandValidator::ForbiddenReopeningValidation
8
8
  @banned_class_or_module_names = banned_classes_or_modules.collect(&:to_s)
9
9
  end
10
10
 
11
- # Raises a Console1984::Errors::ForbiddenCommand if an banned class or module reopening
11
+ # Raises a Console1984::Errors::ForbiddenCommandAttempted if an banned class or module reopening
12
12
  # is detected.
13
13
  def validate(parsed_command)
14
14
  if contains_invalid_class_or_module_declaration?(parsed_command)
15
- raise Console1984::Errors::ForbiddenCommand
15
+ raise Console1984::Errors::ForbiddenCommandAttempted
16
16
  end
17
17
  end
18
18
 
@@ -76,7 +76,7 @@ class Console1984::CommandValidator::ParsedCommand
76
76
 
77
77
  def on_casgn(node)
78
78
  super
79
- scope_node, name, value_node = *node
79
+ _scope_node, name, value_node = *node
80
80
  @constant_assignments.push(*extract_constants(value_node))
81
81
  end
82
82
 
@@ -9,7 +9,7 @@ class Console1984::CommandValidator::SuspiciousTermsValidation
9
9
  # Raises a Console1984::Errors::SuspiciousCommand if the term is referenced.
10
10
  def validate(parsed_command)
11
11
  if contains_suspicious_term?(parsed_command)
12
- raise Console1984::Errors::SuspiciousCommand
12
+ raise Console1984::Errors::SuspiciousCommandAttempted
13
13
  end
14
14
  end
15
15
 
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # The validation itself happens as a chain of validation objects. The system will invoke
7
7
  # each validation in order. Validations will raise an error if the validation fails (typically
8
- # a Console1984::Errors::ForbiddenCommand or Console1984::Errors::SuspiciousCommands).
8
+ # a Console1984::Errors::ForbiddenCommandAttempted or Console1984::Errors::SuspiciousCommands).
9
9
  #
10
10
  # Internally, validations will receive a Console1984::CommandValidator::ParsedCommand object. This
11
11
  # exposes parsed constructs in addition to the raw strings so that validations can use those.
@@ -10,11 +10,15 @@ module Console1984
10
10
 
11
11
  # Attempt to execute a command that is not allowed. The system won't
12
12
  # execute such commands and will flag them as sensitive.
13
- class ForbiddenCommand < StandardError; end
13
+ class ForbiddenCommandAttempted < StandardError; end
14
14
 
15
15
  # A suspicious command was executed. The command will be flagged but the system
16
16
  # will let it run.
17
- class SuspiciousCommand < StandardError; end
17
+ class SuspiciousCommandAttempted < StandardError; end
18
+
19
+ # A forbidden command was executed. The system will flag the command
20
+ # and exit.
21
+ class ForbiddenCommandExecuted < StandardError; end
18
22
 
19
23
  # Attempt to incinerate a session ahead of time as determined by
20
24
  # +config.console1984.incinerate_after+.
@@ -6,7 +6,7 @@ module Console1984::Ext::ActiveRecord::ProtectedAuditableTables
6
6
  define_method method do |*args, **kwargs|
7
7
  sql = args.first
8
8
  if Console1984.command_executor.executing_user_command? && sql =~ auditable_tables_regexp
9
- raise Console1984::Errors::ForbiddenCommand, "#{sql}"
9
+ raise Console1984::Errors::ForbiddenCommandAttempted, "#{sql}"
10
10
  else
11
11
  super(*args, **kwargs)
12
12
  end
@@ -7,9 +7,26 @@ module Console1984::Ext::Core::Module
7
7
 
8
8
  def instance_eval(*)
9
9
  if Console1984.command_executor.executing_user_command?
10
- raise Console1984::Errors::ForbiddenCommand
10
+ raise Console1984::Errors::ForbiddenCommandAttempted
11
11
  else
12
12
  super
13
13
  end
14
14
  end
15
+
16
+ def method_added(method)
17
+ if Console1984.command_executor.from_irb?(caller) && banned_for_reopening?
18
+ raise Console1984::Errors::ForbiddenCommandExecuted, "Trying to add method `#{method}` to #{self.name}"
19
+ end
20
+ end
21
+
22
+ private
23
+ def banned_for_reopening?
24
+ classes_and_modules_banned_for_reopening.find do |banned_class_or_module_name|
25
+ "#{self.name}::".start_with?("#{banned_class_or_module_name}::")
26
+ end
27
+ end
28
+
29
+ def classes_and_modules_banned_for_reopening
30
+ @classes_and_modules_banned_for_reopening ||= Console1984.protections_config.validations[:forbidden_reopening]
31
+ end
15
32
  end
@@ -25,7 +25,7 @@ module Console1984::Ext::Core::Object
25
25
  # See the list +forbidden_reopening+ in +config/command_protections.yml+.
26
26
  Console1984.command_executor.validate_command("class #{arguments.first}; end")
27
27
  super
28
- rescue Console1984::Errors::ForbiddenCommand
28
+ rescue Console1984::Errors::ForbiddenCommandAttempted
29
29
  raise
30
30
  rescue StandardError
31
31
  super
@@ -39,7 +39,7 @@ module Console1984::Freezeable
39
39
  private
40
40
  def prevent_sensitive_method(method_name)
41
41
  define_method method_name do |*arguments|
42
- raise Console1984::Errors::ForbiddenCommand, "You can't invoke #{method_name} on #{self}"
42
+ raise Console1984::Errors::ForbiddenCommandAttempted, "You can't invoke #{method_name} on #{self}"
43
43
  end
44
44
  end
45
45
  end
@@ -1,7 +1,7 @@
1
1
  class Console1984::ProtectionsConfig
2
2
  include Console1984::Freezeable
3
3
 
4
- delegate :static_validations, to: :instance
4
+ delegate :validations, to: :instance
5
5
 
6
6
  attr_reader :config
7
7
 
@@ -9,7 +9,7 @@ class Console1984::ProtectionsConfig
9
9
  @config = config
10
10
  end
11
11
 
12
- %i[ static_validations forbidden_methods ].each do |method_name|
12
+ %i[ validations forbidden_methods ].each do |method_name|
13
13
  define_method method_name do
14
14
  config[method_name].symbolize_keys
15
15
  end
@@ -26,7 +26,3 @@ class Console1984::Refrigerator
26
26
  Console1984.class_loader.eager_load
27
27
  end
28
28
  end
29
-
30
- class Parser::Ruby27
31
- include Console1984::Freezeable
32
- end
@@ -3,22 +3,20 @@ class Console1984::Shield::MethodInvocationShell
3
3
  include Console1984::Freezeable
4
4
 
5
5
  class << self
6
- def install_for(config)
7
- Array(config[:user]).each { |invocation| self.new(invocation, only_for_user_commands: true).prevent_methods_invocation }
8
- Array(config[:system]).each { |invocation| self.new(invocation, only_for_user_commands: false).prevent_methods_invocation }
6
+ def install_for(invocations)
7
+ Array(invocations).each { |invocation| self.new(invocation).prevent_methods_invocation }
9
8
  end
10
9
  end
11
10
 
12
11
  attr_reader :class_name, :methods, :only_for_user_commands
13
12
 
14
- def initialize(invocation, only_for_user_commands:)
13
+ def initialize(invocation)
15
14
  @class_name, methods = invocation.to_a
16
15
  @methods = Array(methods)
17
- @only_for_user_commands = only_for_user_commands
18
16
  end
19
17
 
20
18
  def prevent_methods_invocation
21
- class_name.constantize.prepend build_protection_module
19
+ class_name.to_s.constantize.prepend build_protection_module
22
20
  end
23
21
 
24
22
  def build_protection_module
@@ -37,12 +35,8 @@ class Console1984::Shield::MethodInvocationShell
37
35
  def protected_method_invocation_source_for(method)
38
36
  <<~RUBY
39
37
  def #{method}(*args)
40
- if (!#{only_for_user_commands} || Console1984.command_executor.executing_user_command?) && caller.find do |line|
41
- line_from_irb = line =~ /^[^\\/]/
42
- break if !(line =~ /console1984\\/lib/ || line_from_irb)
43
- line_from_irb
44
- end
45
- raise Console1984::Errors::ForbiddenCommand
38
+ if Console1984.command_executor.from_irb?(caller)
39
+ raise Console1984::Errors::ForbiddenCommandAttempted
46
40
  else
47
41
  super
48
42
  end
@@ -30,6 +30,11 @@ class Console1984::Supervisor
30
30
  stop_session
31
31
  end
32
32
 
33
+ def exit_irb
34
+ stop
35
+ IRB.CurrentContext.exit
36
+ end
37
+
33
38
  private
34
39
  def require_dependencies
35
40
  Kernel.silence_warnings do
@@ -1,3 +1,3 @@
1
1
  module Console1984
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console1984
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-05 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize