tcell_agent 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +9 -22
  3. data/bin/tcell_agent +127 -132
  4. data/lib/tcell_agent/agent/event_processor.rb +23 -22
  5. data/lib/tcell_agent/agent/fork_pipe_manager.rb +7 -7
  6. data/lib/tcell_agent/agent/policy_manager.rb +20 -15
  7. data/lib/tcell_agent/agent/policy_types.rb +5 -11
  8. data/lib/tcell_agent/agent/static_agent.rb +5 -1
  9. data/lib/tcell_agent/agent.rb +6 -4
  10. data/lib/tcell_agent/api.rb +7 -9
  11. data/lib/tcell_agent/appsensor/meta_data.rb +11 -4
  12. data/lib/tcell_agent/authlogic.rb +3 -3
  13. data/lib/tcell_agent/cmdi.rb +6 -4
  14. data/lib/tcell_agent/config/unknown_options.rb +3 -1
  15. data/lib/tcell_agent/configuration.rb +47 -49
  16. data/lib/tcell_agent/devise.rb +2 -2
  17. data/lib/tcell_agent/hooks/login_fraud.rb +58 -29
  18. data/lib/tcell_agent/instrumentation.rb +11 -10
  19. data/lib/tcell_agent/logger.rb +2 -2
  20. data/lib/tcell_agent/patches/meta_data.rb +9 -13
  21. data/lib/tcell_agent/patches.rb +7 -10
  22. data/lib/tcell_agent/policies/clickjacking_policy.rb +4 -5
  23. data/lib/tcell_agent/policies/content_security_policy.rb +6 -12
  24. data/lib/tcell_agent/policies/dataloss_policy.rb +2 -2
  25. data/lib/tcell_agent/policies/http_redirect_policy.rb +2 -2
  26. data/lib/tcell_agent/policies/policy.rb +0 -2
  27. data/lib/tcell_agent/policies/rust_policies.rb +90 -0
  28. data/lib/tcell_agent/policies/secure_headers_policy.rb +2 -2
  29. data/lib/tcell_agent/rails/auth/authlogic.rb +42 -24
  30. data/lib/tcell_agent/rails/auth/devise.rb +44 -23
  31. data/lib/tcell_agent/rails/auth/doorkeeper.rb +33 -15
  32. data/lib/tcell_agent/rails/better_ip.rb +1 -1
  33. data/lib/tcell_agent/rails/csrf_exception.rb +2 -2
  34. data/lib/tcell_agent/rails/dlp/process_request.rb +1 -1
  35. data/lib/tcell_agent/rails/dlp.rb +6 -6
  36. data/lib/tcell_agent/rails/dlp_handler.rb +1 -1
  37. data/lib/tcell_agent/rails/js_agent_insert.rb +1 -1
  38. data/lib/tcell_agent/rails/middleware/body_filter_middleware.rb +1 -1
  39. data/lib/tcell_agent/rails/middleware/context_middleware.rb +3 -2
  40. data/lib/tcell_agent/rails/middleware/headers_middleware.rb +10 -9
  41. data/lib/tcell_agent/rails/routes/grape.rb +6 -6
  42. data/lib/tcell_agent/rails/routes.rb +8 -11
  43. data/lib/tcell_agent/rust/libtcellagent-0.11.1.dylib +0 -0
  44. data/lib/tcell_agent/rust/{libtcellagent-0.6.1.so → libtcellagent-0.11.1.so} +0 -0
  45. data/lib/tcell_agent/rust/models.rb +16 -0
  46. data/lib/tcell_agent/rust/tcellagent-0.11.1.dll +0 -0
  47. data/lib/tcell_agent/rust/whisperer.rb +119 -48
  48. data/lib/tcell_agent/sensor_events/appsensor_meta_event.rb +17 -20
  49. data/lib/tcell_agent/sensor_events/command_injection.rb +50 -5
  50. data/lib/tcell_agent/sensor_events/login_fraud.rb +34 -18
  51. data/lib/tcell_agent/sensor_events/patches.rb +21 -0
  52. data/lib/tcell_agent/sensor_events/server_agent.rb +3 -3
  53. data/lib/tcell_agent/sensor_events/util/utils.rb +4 -3
  54. data/lib/tcell_agent/servers/puma.rb +2 -2
  55. data/lib/tcell_agent/servers/unicorn.rb +1 -1
  56. data/lib/tcell_agent/utils/passwords.rb +28 -0
  57. data/lib/tcell_agent/version.rb +1 -1
  58. data/lib/tcell_agent.rb +1 -5
  59. data/spec/apps/rails-3.2/config/tcell_agent.config +15 -0
  60. data/spec/apps/rails-3.2/log/development.log +0 -0
  61. data/spec/apps/rails-3.2/log/test.log +12 -0
  62. data/spec/apps/rails-4.1/log/test.log +0 -0
  63. data/spec/lib/tcell_agent/agent/fork_pipe_manager_spec.rb +46 -45
  64. data/spec/lib/tcell_agent/agent/policy_manager_spec.rb +276 -164
  65. data/spec/lib/tcell_agent/agent/static_agent_spec.rb +44 -47
  66. data/spec/lib/tcell_agent/api/api_spec.rb +16 -16
  67. data/spec/lib/tcell_agent/appsensor/injections_reporter_spec.rb +131 -116
  68. data/spec/lib/tcell_agent/appsensor/meta_data_spec.rb +55 -51
  69. data/spec/lib/tcell_agent/cmdi_spec.rb +413 -436
  70. data/spec/lib/tcell_agent/config/unknown_options_spec.rb +145 -128
  71. data/spec/lib/tcell_agent/configuration_spec.rb +165 -169
  72. data/spec/lib/tcell_agent/hooks/login_fraud_spec.rb +144 -153
  73. data/spec/lib/tcell_agent/instrumentation_spec.rb +84 -85
  74. data/spec/lib/tcell_agent/patches_spec.rb +70 -111
  75. data/spec/lib/tcell_agent/policies/appsensor_policy_spec.rb +313 -244
  76. data/spec/lib/tcell_agent/policies/clickjacking_policy_spec.rb +28 -28
  77. data/spec/lib/tcell_agent/policies/command_injection_policy_spec.rb +643 -513
  78. data/spec/lib/tcell_agent/policies/content_security_policy_spec.rb +55 -102
  79. data/spec/lib/tcell_agent/policies/dataloss_policy_spec.rb +111 -134
  80. data/spec/lib/tcell_agent/policies/http_redirect_policy_spec.rb +141 -146
  81. data/spec/lib/tcell_agent/policies/http_tx_policy_spec.rb +8 -8
  82. data/spec/lib/tcell_agent/policies/login_policy_spec.rb +15 -17
  83. data/spec/lib/tcell_agent/policies/patches_policy_spec.rb +231 -559
  84. data/spec/lib/tcell_agent/policies/secure_headers_policy_spec.rb +27 -27
  85. data/spec/lib/tcell_agent/rails/better_ip_spec.rb +30 -34
  86. data/spec/lib/tcell_agent/rails/logger_spec.rb +50 -49
  87. data/spec/lib/tcell_agent/rails/middleware/appsensor_middleware_spec.rb +182 -199
  88. data/spec/lib/tcell_agent/rails/middleware/dlp_middleware_spec.rb +110 -84
  89. data/spec/lib/tcell_agent/rails/middleware/global_middleware_spec.rb +107 -85
  90. data/spec/lib/tcell_agent/rails/middleware/redirect_middleware_spec.rb +68 -40
  91. data/spec/lib/tcell_agent/rails/middleware/tcell_body_proxy_spec.rb +81 -67
  92. data/spec/lib/tcell_agent/rails/responses_spec.rb +33 -37
  93. data/spec/lib/tcell_agent/rails/routes/grape_spec.rb +116 -121
  94. data/spec/lib/tcell_agent/rails/routes/route_id_spec.rb +25 -28
  95. data/spec/lib/tcell_agent/rails/routes/routes_spec.rb +87 -85
  96. data/spec/lib/tcell_agent/rails_spec.rb +1 -6
  97. data/spec/lib/tcell_agent/rust/models_spec.rb +112 -0
  98. data/spec/lib/tcell_agent/rust/whisperer_spec.rb +502 -179
  99. data/spec/lib/tcell_agent/sensor_events/appsensor_meta_event_spec.rb +44 -33
  100. data/spec/lib/tcell_agent/sensor_events/dlp_spec.rb +4 -4
  101. data/spec/lib/tcell_agent/sensor_events/sessions_metric_spec.rb +183 -169
  102. data/spec/lib/tcell_agent/sensor_events/util/sanitizer_utilities_spec.rb +25 -25
  103. data/spec/lib/tcell_agent/utils/bounded_queue_spec.rb +17 -20
  104. data/spec/lib/tcell_agent/utils/params_spec.rb +28 -28
  105. data/spec/lib/tcell_agent/utils/passwords_spec.rb +143 -0
  106. data/spec/lib/tcell_agent/utils/strings_spec.rb +35 -35
  107. data/spec/lib/tcell_agent_spec.rb +8 -8
  108. data/spec/spec_helper.rb +4 -4
  109. data/spec/support/middleware_helper.rb +10 -10
  110. data/spec/support/static_agent_overrides.rb +16 -12
  111. data/tcell_agent.gemspec +17 -33
  112. metadata +43 -198
  113. data/LICENSE_libinjection +0 -32
  114. data/Readme.txt +0 -7
  115. data/ext/libinjection/extconf.rb +0 -3
  116. data/ext/libinjection/libinjection.h +0 -65
  117. data/ext/libinjection/libinjection_html5.c +0 -847
  118. data/ext/libinjection/libinjection_html5.h +0 -54
  119. data/ext/libinjection/libinjection_sqli.c +0 -2317
  120. data/ext/libinjection/libinjection_sqli.h +0 -295
  121. data/ext/libinjection/libinjection_sqli_data.h +0 -9004
  122. data/ext/libinjection/libinjection_wrap.c +0 -3525
  123. data/ext/libinjection/libinjection_xss.c +0 -531
  124. data/ext/libinjection/libinjection_xss.h +0 -21
  125. data/lib/tcell_agent/appsensor/injections_matcher.rb +0 -155
  126. data/lib/tcell_agent/appsensor/rules/appsensor_rule_manager.rb +0 -49
  127. data/lib/tcell_agent/appsensor/rules/appsensor_rule_set.rb +0 -67
  128. data/lib/tcell_agent/appsensor/rules/baserules.json +0 -467
  129. data/lib/tcell_agent/patches/block_rule.rb +0 -93
  130. data/lib/tcell_agent/patches/sensors_matcher.rb +0 -31
  131. data/lib/tcell_agent/policies/appsensor/cmdi_sensor.rb +0 -23
  132. data/lib/tcell_agent/policies/appsensor/fpt_sensor.rb +0 -23
  133. data/lib/tcell_agent/policies/appsensor/injection_sensor.rb +0 -117
  134. data/lib/tcell_agent/policies/appsensor/nullbyte_sensor.rb +0 -26
  135. data/lib/tcell_agent/policies/appsensor/retr_sensor.rb +0 -22
  136. data/lib/tcell_agent/policies/appsensor/sqli_sensor.rb +0 -34
  137. data/lib/tcell_agent/policies/appsensor/xss_sensor.rb +0 -34
  138. data/lib/tcell_agent/policies/appsensor_policy.rb +0 -49
  139. data/lib/tcell_agent/policies/command_injection_policy.rb +0 -196
  140. data/lib/tcell_agent/policies/honeytokens_policy.rb +0 -69
  141. data/lib/tcell_agent/policies/patches_policy.rb +0 -84
  142. data/lib/tcell_agent/rust/libtcellagent-0.6.1.dylib +0 -0
  143. data/lib/tcell_agent/rust/tcellagent-0.6.1.dll +0 -0
  144. data/spec/apps/rails-3.2/Gemfile +0 -25
  145. data/spec/apps/rails-3.2/Gemfile.lock +0 -126
  146. data/spec/apps/rails-3.2/Rakefile +0 -7
  147. data/spec/apps/rails-3.2/app/assets/images/rails.png +0 -0
  148. data/spec/apps/rails-3.2/app/assets/javascripts/application.js +0 -15
  149. data/spec/apps/rails-3.2/app/assets/stylesheets/application.css +0 -13
  150. data/spec/apps/rails-3.2/app/controllers/application_controller.rb +0 -3
  151. data/spec/apps/rails-3.2/app/controllers/t_cell_app_controller.rb +0 -5
  152. data/spec/apps/rails-3.2/app/helpers/application_helper.rb +0 -2
  153. data/spec/apps/rails-3.2/app/views/layouts/application.html.erb +0 -14
  154. data/spec/apps/rails-3.2/app/views/t_cell_app/index.html.erb +0 -1
  155. data/spec/apps/rails-3.2/config/application.rb +0 -63
  156. data/spec/apps/rails-3.2/config/boot.rb +0 -6
  157. data/spec/apps/rails-3.2/config/environment.rb +0 -5
  158. data/spec/apps/rails-3.2/config/environments/test.rb +0 -37
  159. data/spec/apps/rails-3.2/config/routes.rb +0 -11
  160. data/spec/apps/rails-3.2/config.ru +0 -4
  161. data/spec/apps/rails-4.1/Gemfile +0 -7
  162. data/spec/apps/rails-4.1/Gemfile.lock +0 -114
  163. data/spec/apps/rails-4.1/Rakefile +0 -6
  164. data/spec/apps/rails-4.1/app/assets/javascripts/application.js +0 -16
  165. data/spec/apps/rails-4.1/app/assets/stylesheets/application.css +0 -15
  166. data/spec/apps/rails-4.1/app/controllers/application_controller.rb +0 -5
  167. data/spec/apps/rails-4.1/app/controllers/t_cell_app_controller.rb +0 -5
  168. data/spec/apps/rails-4.1/app/helpers/application_helper.rb +0 -2
  169. data/spec/apps/rails-4.1/app/views/layouts/application.html.erb +0 -14
  170. data/spec/apps/rails-4.1/app/views/t_cell_app/index.html.erb +0 -1
  171. data/spec/apps/rails-4.1/config/application.rb +0 -24
  172. data/spec/apps/rails-4.1/config/boot.rb +0 -4
  173. data/spec/apps/rails-4.1/config/environment.rb +0 -5
  174. data/spec/apps/rails-4.1/config/environments/test.rb +0 -41
  175. data/spec/apps/rails-4.1/config/initializers/assets.rb +0 -8
  176. data/spec/apps/rails-4.1/config/initializers/backtrace_silencers.rb +0 -7
  177. data/spec/apps/rails-4.1/config/initializers/cookies_serializer.rb +0 -3
  178. data/spec/apps/rails-4.1/config/initializers/filter_parameter_logging.rb +0 -4
  179. data/spec/apps/rails-4.1/config/initializers/inflections.rb +0 -16
  180. data/spec/apps/rails-4.1/config/initializers/mime_types.rb +0 -4
  181. data/spec/apps/rails-4.1/config/initializers/session_store.rb +0 -3
  182. data/spec/apps/rails-4.1/config/initializers/wrap_parameters.rb +0 -14
  183. data/spec/apps/rails-4.1/config/locales/en.yml +0 -23
  184. data/spec/apps/rails-4.1/config/routes.rb +0 -12
  185. data/spec/apps/rails-4.1/config/secrets.yml +0 -22
  186. data/spec/apps/rails-4.1/config.ru +0 -4
  187. data/spec/controllers/application_controller.rb +0 -12
  188. data/spec/lib/tcell_agent/appsensor/injections_matcher_spec.rb +0 -522
  189. data/spec/lib/tcell_agent/appsensor/rules/appsensor_rule_manager_spec.rb +0 -23
  190. data/spec/lib/tcell_agent/appsensor/rules/appsensor_rule_set_spec.rb +0 -159
  191. data/spec/lib/tcell_agent/patches/block_rule_spec.rb +0 -458
  192. data/spec/lib/tcell_agent/patches/sensors_matcher_spec.rb +0 -35
  193. data/spec/lib/tcell_agent/policies/appsensor/cmdi_sensor_spec.rb +0 -139
  194. data/spec/lib/tcell_agent/policies/appsensor/fpt_sensor_spec.rb +0 -139
  195. data/spec/lib/tcell_agent/policies/appsensor/nullbyte_sensor_spec.rb +0 -167
  196. data/spec/lib/tcell_agent/policies/appsensor/retr_sensor_spec.rb +0 -139
  197. data/spec/lib/tcell_agent/policies/appsensor/sqli_sensor_spec.rb +0 -246
  198. data/spec/lib/tcell_agent/policies/appsensor/xss_sensor_spec.rb +0 -882
  199. data/spec/lib/tcell_agent/policies/honeytokens_policy_spec.rb +0 -22
@@ -8,60 +8,53 @@ module TCellAgent
8
8
  module Instrumentation
9
9
  module Rails
10
10
  module Middleware
11
-
12
-
13
11
  class MockDLPRackApp
14
-
15
12
  attr_reader :request_body
16
13
 
17
- def initialize(body="OK", route_id=nil, session_id=nil)
14
+ def initialize(body = 'OK', route_id = nil, session_id = nil)
18
15
  @route_id = route_id
19
16
  @session_id = session_id
20
17
  @request_headers = {}
21
18
  @body = body
22
19
  end
23
20
 
24
- def loop_params_hash(method, param_hash, prefix, &block)
21
+ def loop_params_hash(method, param_hash, _prefix, &block)
25
22
  param_hash.each do |param_name, param_value|
26
23
  if param_value && param_value.is_a?(Hash)
27
24
  loop_params_hash(method, param_value, 'hash', &block)
28
- elsif !param_value || !param_value.instance_of?(String) || param_value == ""
25
+ elsif !param_value || !param_value.instance_of?(String) || param_value == ''
29
26
  next
30
27
  else
31
- block.call(method, param_name, param_value)
28
+ yield(method, param_name, param_value)
32
29
  end
33
30
  end
34
31
  end
35
32
 
36
33
  def for_params(request, &block)
37
34
  get_params = request.GET
38
- if get_params
39
- self.loop_params_hash('get', get_params, nil, &block)
40
- end
35
+ loop_params_hash('get', get_params, nil, &block) if get_params
41
36
  post_params = request.POST
42
- if post_params
43
- self.loop_params_hash('post', post_params, nil, &block)
44
- end
45
- end
37
+ loop_params_hash('post', post_params, nil, &block) if post_params
38
+ end
46
39
 
47
40
  def call(env)
48
41
  @env = env
49
42
  rack_request = Rack::Request.new(env)
50
- response_headers = {'Content-Type' => 'text/html'}
51
- env["tcell.request_data"].transaction_id = "a-b-c-d-e-f"
52
- env["tcell.request_data"].session_id = @session_id
53
- env["tcell.request_data"].route_id = @route_id
54
- tcell_context = env["tcell.request_data"]
43
+ response_headers = { 'Content-Type' => 'text/html' }
44
+ env['tcell.request_data'].transaction_id = 'a-b-c-d-e-f'
45
+ env['tcell.request_data'].session_id = @session_id
46
+ env['tcell.request_data'].route_id = @route_id
47
+ tcell_context = env['tcell.request_data']
55
48
  dlp_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DataLoss)
56
49
  if dlp_policy
57
- action_objs = dlp_policy.get_actions_for_table("*", "*", "tablex", "columnb", tcell_context.route_id)
50
+ action_objs = dlp_policy.get_actions_for_table('*', '*', 'tablex', 'columnb', tcell_context.route_id)
58
51
  if action_objs
59
52
  action_objs.each do |action_obj|
60
- tcell_context.add_response_db_filter("secretvalue", action_obj, "databx", "*", "tablex", "columnb")
53
+ tcell_context.add_response_db_filter('secretvalue', action_obj, 'databx', '*', 'tablex', 'columnb')
61
54
  end
62
55
  end
63
56
  TCellAgent::DLP.handle_request_dlp_parameters(rack_request)
64
- #if tcell_context && dlp_policy && dlp_policy.has_actions_for_form_parameter?
57
+ # if tcell_context && dlp_policy && dlp_policy.has_actions_for_form_parameter?
65
58
  # for_params(rack_request) { |method, param_name, param_value|
66
59
  # actions = dlp_policy.get_actions_for_form_parameter(param_name, tcell_context.route_id)
67
60
  # if actions
@@ -71,7 +64,7 @@ module TCellAgent
71
64
  # }
72
65
  # end
73
66
  # }
74
- #end
67
+ # end
75
68
  end
76
69
  tcell_context.filter_body!(@body)
77
70
  [200, response_headers, [@body]]
@@ -80,92 +73,125 @@ module TCellAgent
80
73
  def [](key)
81
74
  @env[key]
82
75
  end
83
-
84
76
  end
85
77
 
86
78
  describe HeadersMiddleware do
87
-
88
79
  let(:app) { MockDLPRackApp.new }
89
- let(:app2) { MockDLPRackApp.new(body="My secretvalue othervalue test", route_id="myrouteid", session_id="plainsessionid") }
80
+ let(:app2) { MockDLPRackApp.new('My secretvalue othervalue test', 'myrouteid', 'plainsessionid') }
90
81
 
91
- subject { withTCellMiddleware( app ) }
82
+ subject { with_tcell_middleware(app) }
92
83
 
93
- context "DLP Middleware" do
84
+ context 'DLP Middleware' do
94
85
  before(:each) do
95
86
  TCellAgent.configuration = TCellAgent::Configuration.new
96
- TCellAgent.configuration.read_config_from_file(get_test_resource_path("normal_config.json"))
87
+ TCellAgent.configuration.read_config_from_file(get_test_resource_path('normal_config.json'))
97
88
  end
98
89
  let(:request) { Rack::MockRequest.new(subject) }
99
- let(:request2) { Rack::MockRequest.new( withTCellMiddleware( app2 )) }
90
+ let(:request2) { Rack::MockRequest.new(with_tcell_middleware(app2)) }
100
91
  let(:agent) { ::TCellAgent::Agent.new }
101
- context "Event" do
92
+ context 'Event' do
102
93
  before(:each) do
103
- TCellAgent.thread_agent.processPolicyJson({"dlp" => {
104
- "policy_id"=>"x1a1",
105
- "data"=>{
106
- "db_protections"=>[
107
- {
108
- "scope"=>"route",
109
- "route_ids"=>["myrouteid"],
110
- "databases"=>["*"],
111
- "schemas"=>["*"],
112
- "tables"=>["tablex"],
113
- "fields"=>["columnb"],
114
- "id"=>"323213",
115
- "actions"=>{
116
- "log"=>["redact"],
117
- "body"=>["redact"]
94
+ TCellAgent.thread_agent.processPolicyJson(
95
+ {
96
+ 'dlp' => {
97
+ 'policy_id' => 'x1a1',
98
+ 'data' => {
99
+ 'db_protections' => [
100
+ {
101
+ 'scope' => 'route',
102
+ 'route_ids' => ['myrouteid'],
103
+ 'databases' => ['*'],
104
+ 'schemas' => ['*'],
105
+ 'tables' => ['tablex'],
106
+ 'fields' => ['columnb'],
107
+ 'id' => '323213',
108
+ 'actions' => {
109
+ 'log' => ['redact'],
110
+ 'body' => ['redact']
111
+ }
118
112
  }
119
- }
120
- ]
113
+ ]
114
+ }
121
115
  }
122
- }}, cache=false)
116
+ },
117
+ false
118
+ )
123
119
  TCellAgent.empty_event_queue
124
120
  end
125
- it "redacts body" do
126
- response = request2.get("/some/path2?x=abc", 'CONTENT_TYPE' => 'text/html', 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6')
127
- expect(response.body).to eq("My [redacted] othervalue test")
128
- #expect(response['Location']).to eq("https://www.google.com")
129
- expected_as = {"event_type" => "dlp", "rid" => "myrouteid", "found_in" => "body", "rule" => "323213", "type" => "db", "db" => "databx", "schema" => "*", "table" => "tablex", "field" => "columnb", "uri" => "/some/path2?x="}
121
+ it 'redacts body' do
122
+ response = request2.get(
123
+ '/some/path2?x=abc',
124
+ 'CONTENT_TYPE' => 'text/html',
125
+ 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6'
126
+ )
127
+ expect(response.body).to eq('My [redacted] othervalue test')
128
+ # expect(response['Location']).to eq("https://www.google.com")
129
+ expected_as = {
130
+ 'event_type' => 'dlp',
131
+ 'rid' => 'myrouteid',
132
+ 'found_in' => 'body',
133
+ 'rule' => '323213',
134
+ 'type' => 'db',
135
+ 'db' => 'databx',
136
+ 'schema' => '*',
137
+ 'table' => 'tablex',
138
+ 'field' => 'columnb',
139
+ 'uri' => 'http://example.org/some/path2?x='
140
+ }
130
141
  expect(TCellAgent.event_queue).to include(expected_as)
131
142
  end
132
- end #/conext
133
-
143
+ end
134
144
 
135
- context "Event for request dlp" do
145
+ context 'Event for request dlp' do
136
146
  before(:each) do
137
- TCellAgent.thread_agent.processPolicyJson({"dlp" => {
138
- "policy_id"=>"x1a1",
139
- "data"=>{
140
- "request_protections"=>[
141
- {
142
- "variable_context"=>"form",
143
- "scope"=>"route",
144
- "route_ids"=>["myrouteid"],
145
- "variables"=>["test333"],
146
- "id"=>"08080808",
147
- "actions"=>{
148
- "log"=>["redact"],
149
- "body"=>["event"]
150
- }
147
+ TCellAgent.thread_agent.processPolicyJson(
148
+ {
149
+ 'dlp' => {
150
+ 'policy_id' => 'x1a1',
151
+ 'data' => {
152
+ 'request_protections' => [
153
+ {
154
+ 'variable_context' => 'form',
155
+ 'scope' => 'route',
156
+ 'route_ids' => ['myrouteid'],
157
+ 'variables' => ['test333'],
158
+ 'id' => '08080808',
159
+ 'actions' => {
160
+ 'log' => ['redact'],
161
+ 'body' => ['event']
162
+ }
163
+ }
164
+ ]
151
165
  }
152
- ]
153
- }
154
- }}, cache=false)
166
+ }
167
+ },
168
+ false
169
+ )
155
170
  TCellAgent.empty_event_queue
156
171
  end
157
- it "redacts body" do
158
- response = request2.get("/some/path2?test333=othervalue", 'CONTENT_TYPE' => 'text/html', 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6')
159
- expect(response.body).to eq("My secretvalue othervalue test")
160
- expected_as = {"event_type" => "dlp", "rid" => "myrouteid", "found_in" => "body", "rule" => "08080808", "type" => "req", "context" => "form", "variable" => "test333", "uri" => "/some/path2?test333="}
172
+
173
+ it 'redacts body' do
174
+ response = request2.get(
175
+ '/some/path2?test333=othervalue',
176
+ 'CONTENT_TYPE' => 'text/html',
177
+ 'REMOTE_ADDR' => '1.3.3.4,3.4.5.6'
178
+ )
179
+ expect(response.body).to eq('My secretvalue othervalue test')
180
+ expected_as = {
181
+ 'event_type' => 'dlp',
182
+ 'rid' => 'myrouteid',
183
+ 'found_in' => 'body',
184
+ 'rule' => '08080808',
185
+ 'type' => 'req',
186
+ 'context' => 'form',
187
+ 'variable' => 'test333',
188
+ 'uri' => 'http://example.org/some/path2?test333='
189
+ }
161
190
  expect(TCellAgent.event_queue).to include(expected_as)
162
191
  end
163
- end #/conext
164
-
165
- end #/context
166
- end #/describe
167
-
168
-
192
+ end
193
+ end
194
+ end
169
195
  end
170
196
  end
171
197
  end
@@ -6,9 +6,7 @@ module TCellAgent
6
6
  module Instrumentation
7
7
  module Rails
8
8
  module Middleware
9
-
10
9
  class MockRackApp
11
-
12
10
  attr_reader :request_body
13
11
 
14
12
  def initialize
@@ -19,138 +17,163 @@ module TCellAgent
19
17
  @env = env
20
18
  @request_body = env['rack.input'].read
21
19
  rack_request = Rack::Request.new(env)
22
- response_headers = {'Content-Type' => 'text/html'}
23
- if (rack_request.params['rv'])
24
- response_headers["Location"] = rack_request.params['rv']
20
+ response_headers = { 'Content-Type' => 'text/html' }
21
+ if rack_request.params['rv']
22
+ response_headers['Location'] = rack_request.params['rv']
25
23
  end
26
- env["tcell.request_data"].transaction_id = "a-b-c-d-e-f"
27
- #env["tcell.request_data"].route_id = "x-b-c-d-e-f"
24
+ env['tcell.request_data'].transaction_id = 'a-b-c-d-e-f'
25
+ # env["tcell.request_data"].route_id = "x-b-c-d-e-f"
28
26
  [200, response_headers, ['OK']]
29
27
  end
30
28
 
31
29
  def [](key)
32
30
  @env[key]
33
31
  end
34
-
35
32
  end
36
33
 
37
34
  describe HeadersMiddleware do
38
-
39
35
  let(:app) { MockRackApp.new }
40
- subject { withTCellMiddleware( app ) }
36
+ subject { with_tcell_middleware(app) }
41
37
 
42
- context "Redirect Middleware" do
38
+ context 'Redirect Middleware' do
43
39
  let(:request) { Rack::MockRequest.new(subject) }
44
40
  let(:agent) { ::TCellAgent::Agent.new(Process.pid) }
45
41
  before(:each) do
46
42
  TCellAgent.configuration = TCellAgent::Configuration.new
47
- TCellAgent.configuration.read_config_from_file(get_test_resource_path("normal_config.json"))
43
+ TCellAgent.configuration.read_config_from_file(get_test_resource_path('normal_config.json'))
48
44
 
49
45
  # avoid running start_policy_polling for these specs
50
- expect(agent).to receive(:start_policy_polling).at_most(50)
46
+ expect(agent).to receive(:start_policy_polling_loop).at_most(50)
51
47
 
52
48
  agent.start
53
49
  TCellAgent.thread_agent = agent
54
50
  end
55
51
 
56
- context "not enabled" do
57
- it "passes through unchanged" do
58
- agent.processPolicyJson({"http-redirect"=>{
59
- "policy_id"=>"153ed270-7481-11e5-9194-95dad9b9dec3",
60
- "data"=>{
61
- "enabled"=>false,
62
- "block"=>true,
63
- "whitelist"=>[]
64
- }
65
- }}, cache=false)
66
- tid_len = "78e596b7-e772-4caf-92eb-645fdbdec473".length + 1
67
- response = request.get("/some/path?rv=https://www.google.com", 'CONTENT_TYPE' => 'text/html')
68
- expect(response['Location']).to eq("https://www.google.com")
52
+ context 'not enabled' do
53
+ it 'passes through unchanged' do
54
+ agent.processPolicyJson(
55
+ {
56
+ 'http-redirect' => {
57
+ 'policy_id' => '153ed270-7481-11e5-9194-95dad9b9dec3',
58
+ 'data' => {
59
+ 'enabled' => false,
60
+ 'block' => true,
61
+ 'whitelist' => []
62
+ }
63
+ }
64
+ },
65
+ false
66
+ )
67
+ response = request.get('/some/path?rv=https://www.google.com', 'CONTENT_TYPE' => 'text/html')
68
+ expect(response['Location']).to eq('https://www.google.com')
69
69
  end
70
70
  end
71
71
 
72
72
  context "doesn't block simple whitelist" do
73
- it "passes through unchanged" do
74
- agent.processPolicyJson({"http-redirect"=>{
75
- "policy_id"=>"153ed270-7481-11e5-9194-95dad9b9dec3",
76
- "data"=>{
77
- "enabled"=>true,
78
- "block"=>true,
79
- "whitelist"=>["www.google.com"]
80
- }
81
- }}, cache=false)
82
-
83
- tid_len = "78e596b7-e772-4caf-92eb-645fdbdec473".length + 1
84
- response = request.get("/some/path?rv=https://www.google.com", 'CONTENT_TYPE' => 'text/html')
85
- expect(response['Location']).to eq("https://www.google.com")
73
+ it 'passes through unchanged' do
74
+ agent.processPolicyJson(
75
+ {
76
+ 'http-redirect' => {
77
+ 'policy_id' => '153ed270-7481-11e5-9194-95dad9b9dec3',
78
+ 'data' => {
79
+ 'enabled' => true,
80
+ 'block' => true,
81
+ 'whitelist' => ['www.google.com']
82
+ }
83
+ }
84
+ },
85
+ false
86
+ )
87
+
88
+ response = request.get('/some/path?rv=https://www.google.com', 'CONTENT_TYPE' => 'text/html')
89
+ expect(response['Location']).to eq('https://www.google.com')
86
90
  end
87
91
  end
88
92
 
89
93
  context "doesn't block wildcard whitelist" do
90
- it "passes through unchanged" do
91
- agent.processPolicyJson({"http-redirect"=>{
92
- "policy_id"=>"153ed270-7481-11e5-9194-95dad9b9dec3",
93
- "data"=>{
94
- "enabled"=>true,
95
- "block"=>true,
96
- "whitelist"=>["*.google.com"]
97
- }
98
- }}, cache=false)
99
- response = request.get("/some/path?rv=https://www.google.com", 'CONTENT_TYPE' => 'text/html')
100
- expect(response['Location']).to eq("https://www.google.com")
94
+ it 'passes through unchanged' do
95
+ agent.processPolicyJson(
96
+ {
97
+ 'http-redirect' => {
98
+ 'policy_id' => '153ed270-7481-11e5-9194-95dad9b9dec3',
99
+ 'data' => {
100
+ 'enabled' => true,
101
+ 'block' => true,
102
+ 'whitelist' => ['*.google.com']
103
+ }
104
+ }
105
+ },
106
+ false
107
+ )
108
+ response = request.get('/some/path?rv=https://www.google.com', 'CONTENT_TYPE' => 'text/html')
109
+ expect(response['Location']).to eq('https://www.google.com')
101
110
  end
102
111
  end
103
112
 
104
- context "DOES block wildcard whitelist" do
105
- it "replaces the value with /" do
106
- agent.processPolicyJson({"http-redirect"=>{
107
- "policy_id"=>"153ed270-7481-11e5-9194-95dad9b9dec3",
108
- "data"=>{
109
- "enabled"=>true,
110
- "block"=>true,
111
- "whitelist"=>["*.google-test.com"]
112
- }
113
- }}, cache=false)
114
-
115
- tid_len = "78e596b7-e772-4caf-92eb-645fdbdec473".length + 1
116
- response = request.get("/some/path?rv=https://www.google.com", 'CONTENT_TYPE' => 'text/html')
117
- expect(response['Location']).to eq("/")
113
+ context 'DOES block wildcard whitelist' do
114
+ it 'replaces the value with /' do
115
+ agent.processPolicyJson(
116
+ {
117
+ 'http-redirect' => {
118
+ 'policy_id' => '153ed270-7481-11e5-9194-95dad9b9dec3',
119
+ 'data' => {
120
+ 'enabled' => true,
121
+ 'block' => true,
122
+ 'whitelist' => ['*.google-test.com']
123
+ }
124
+ }
125
+ },
126
+ false
127
+ )
128
+
129
+ response = request.get('/some/path?rv=https://www.google.com', 'CONTENT_TYPE' => 'text/html')
130
+ expect(response['Location']).to eq('/')
118
131
  end
119
132
  end
120
-
121
133
  end
122
134
 
123
- context "CSP Middleware" do
135
+ context 'CSP Middleware' do
124
136
  let(:request) { Rack::MockRequest.new(subject) }
125
137
  let(:agent) { ::TCellAgent::Agent.new(Process.pid) }
126
138
 
127
139
  before(:each) do
128
140
  TCellAgent.configuration = TCellAgent::Configuration.new
129
- TCellAgent.configuration.read_config_from_file(get_test_resource_path("normal_config.json"))
141
+ TCellAgent.configuration.read_config_from_file(get_test_resource_path('normal_config.json'))
130
142
 
131
143
  # avoid running start_policy_polling for these specs
132
- expect(agent).to receive(:start_policy_polling).at_most(50)
144
+ expect(agent).to receive(:start_policy_polling_loop).at_most(50)
133
145
 
134
146
  agent.start
135
147
  TCellAgent.thread_agent = agent
136
148
  end
137
149
 
138
- context "Standard CSP Header" do
139
- it "CSP Header is Added" do
140
- agent.processPolicyJson({"csp-headers"=>{
141
- "policy_id"=>"153ed270-7481-11e5-9194-95dad9b9dec3",
142
- "headers"=>[{
143
- "name"=>"Content-Security-Policy-Report-Only",
144
- "value"=>"script-src 'unsafe-inline'",
145
- "report-uri"=>"http://test.tcell.io/report"
146
- }]
147
- }}, cache=false)
148
- tid_len = "78e596b7-e772-4caf-92eb-645fdbdec473".length + 1
149
- response = request.get("/some/path", 'CONTENT_TYPE' => 'text/plain', 'action_dispatch.request_id'=>'35281717-247e-44e6-bd42-0fb1417e80d')
150
- expect(response['Content-Security-Policy-Report-Only']).to eq("script-src 'unsafe-inline'; report-uri http://test.tcell.io/report?tid=a-b-c-d-e-f&c=-654192056")
150
+ context 'Standard CSP Header' do
151
+ it 'CSP Header is Added' do
152
+ agent.processPolicyJson(
153
+ {
154
+ 'csp-headers' => {
155
+ 'policy_id' => '153ed270-7481-11e5-9194-95dad9b9dec3',
156
+ 'headers' => [
157
+ {
158
+ 'name' => 'Content-Security-Policy-Report-Only',
159
+ 'value' => "script-src 'unsafe-inline'",
160
+ 'report-uri' => 'http://test.tcell.io/report'
161
+ }
162
+ ]
163
+ }
164
+ },
165
+ false
166
+ )
167
+ response = request.get(
168
+ '/some/path',
169
+ 'CONTENT_TYPE' => 'text/plain',
170
+ 'action_dispatch.request_id' => '35281717-247e-44e6-bd42-0fb1417e80d'
171
+ )
172
+ expect(response['Content-Security-Policy-Report-Only']).to eq(
173
+ "script-src 'unsafe-inline'; report-uri http://test.tcell.io/report?tid=a-b-c-d-e-f&c=-654192056"
174
+ )
151
175
  end
152
176
  end
153
-
154
177
  end
155
178
 
156
179
  # context "when called with a POST request" do
@@ -172,10 +195,10 @@ module TCellAgent
172
195
  # "value"=>"script-src 'unsafe-inline'",
173
196
  # "report-uri"=>"http://test.tcell.io/report"
174
197
  # }
175
- # }})
198
+ # }})
176
199
 
177
200
  # #noop = Proc.new {[200, {}, ["hello"]]}
178
- # #middleware = ActionDispatch::Static.new(noop, "/my_rails_app/public")
201
+ # #middleware = ActionDispatch::Static.new(noop, "/my_rails_app/public")
179
202
  # #request = Rack::MockRequest.new(middleware)
180
203
  # #puts request.get("/path_i_want_to_hit")
181
204
 
@@ -188,7 +211,6 @@ module TCellAgent
188
211
  # end
189
212
  # end
190
213
  end
191
-
192
214
  end
193
215
  end
194
216
  end