Module: TCellAgent::Instrumentation::Lfi

Defined in:
lib/tcell_agent/instrumentation/lfi.rb

Class Method Summary collapse

Class Method Details

.block_file_access?(path, mode) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tcell_agent/instrumentation/lfi.rb', line 8

def self.block_file_access?(path, mode)
  TCellAgent::Instrumentation.safe_block('Checking Local Files Policy') do
    if TCellAgent::Utils::Strings.present?(path)
      lfi_policy = TCellAgent.policy(TCellAgent::PolicyTypes::LFI)

      request_env = TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS.fetch(
        Thread.current.object_id, {}
      )

      tcell_context = request_env[TCellAgent::Instrumentation::TCELL_ID]
      return lfi_policy.block_file_access?(path, mode, tcell_context)
    end
  end

  false
end

.convert_mode(mode) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/tcell_agent/instrumentation/lfi.rb', line 72

def self.convert_mode(mode)
  if mode.is_a? String
    return 'ReadWrite' if mode.include? '+'
    return 'Write' if (mode.include? 'w') || (mode.include? 'a')
  elsif mode.is_a? Numeric
    return 'ReadWrite' if (mode & ::File::RDWR) != 0
    return 'Write' if (mode & ::File::WRONLY) != 0
  end
  'Read'
end

.extract_path_mode(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tcell_agent/instrumentation/lfi.rb', line 25

def self.extract_path_mode(*args)
  path = ''
  mode = ''

  TCellAgent::Instrumentation.safe_block('LFI Parsing *args') do
    return ['', ''] if args.nil? || args.empty?

    args_copy = Array.new(args)
    path = args_copy.shift
    mode = args_copy.shift || 'r'

    if path && path.to_s[0] != '|'
      path = File.expand_path(path.to_s)

      mode = if mode && mode.is_a?(Hash)
               convert_mode(mode[:mode])
             else
               convert_mode(mode)
             end

      [path, mode]
    else
      ['', '']
    end
  end
end

.extract_path_mode_argfObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tcell_agent/instrumentation/lfi.rb', line 52

def self.extract_path_mode_argf
  path = ''
  mode = 'Read'

  TCellAgent::Instrumentation.safe_block('LFI Parsing ARGF') do
    if ARGF.eof? && !ARGV.empty?
      argv_copy = Array.new(ARGV)
      path = argv_copy.shift
    else
      path = ARGF.filename
    end

    if path && path.to_s[0] != '|'
      [File.expand_path(path.to_s), mode]
    else
      ['', '']
    end
  end
end