contrast-agent 3.16.0 → 4.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -0
  3. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +22 -10
  4. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +4 -3
  5. data/lib/contrast/agent.rb +2 -3
  6. data/lib/contrast/agent/assess/contrast_event.rb +49 -130
  7. data/lib/contrast/agent/assess/contrast_object.rb +51 -0
  8. data/lib/contrast/agent/assess/events/source_event.rb +4 -9
  9. data/lib/contrast/agent/assess/policy/patcher.rb +4 -3
  10. data/lib/contrast/agent/assess/policy/policy_node.rb +31 -59
  11. data/lib/contrast/agent/assess/policy/policy_scanner.rb +17 -6
  12. data/lib/contrast/agent/assess/policy/preshift.rb +3 -3
  13. data/lib/contrast/agent/assess/policy/propagation_method.rb +13 -19
  14. data/lib/contrast/agent/assess/policy/propagation_node.rb +12 -24
  15. data/lib/contrast/agent/assess/policy/propagator/append.rb +1 -2
  16. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -2
  17. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  18. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  19. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -2
  20. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -2
  21. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
  22. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -2
  23. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -2
  24. data/lib/contrast/agent/assess/policy/propagator/remove.rb +2 -4
  25. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -2
  26. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -2
  27. data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -4
  28. data/lib/contrast/agent/assess/policy/propagator/splat.rb +2 -4
  29. data/lib/contrast/agent/assess/policy/propagator/split.rb +73 -117
  30. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +11 -11
  31. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -7
  32. data/lib/contrast/agent/assess/policy/source_method.rb +2 -14
  33. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -8
  34. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +1 -1
  35. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -1
  36. data/lib/contrast/agent/assess/property/tagged.rb +21 -15
  37. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +3 -2
  38. data/lib/contrast/agent/assess/rule/redos.rb +1 -1
  39. data/lib/contrast/agent/assess/tracker.rb +16 -18
  40. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +7 -0
  41. data/lib/contrast/agent/inventory.rb +15 -0
  42. data/lib/contrast/agent/inventory/dependencies.rb +50 -0
  43. data/lib/contrast/agent/inventory/dependency_analysis.rb +37 -0
  44. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +104 -0
  45. data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +38 -0
  46. data/lib/contrast/agent/middleware.rb +51 -3
  47. data/lib/contrast/agent/patching/policy/method_policy.rb +1 -1
  48. data/lib/contrast/agent/patching/policy/patch.rb +6 -0
  49. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +47 -1
  50. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +4 -3
  51. data/lib/contrast/agent/protect/policy/rule_applicator.rb +53 -0
  52. data/lib/contrast/agent/protect/rule/base.rb +63 -14
  53. data/lib/contrast/agent/protect/rule/cmd_injection.rb +12 -28
  54. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -4
  55. data/lib/contrast/agent/protect/rule/deserialization.rb +4 -1
  56. data/lib/contrast/agent/protect/rule/no_sqli.rb +3 -3
  57. data/lib/contrast/agent/protect/rule/sqli.rb +3 -3
  58. data/lib/contrast/agent/protect/rule/xxe.rb +32 -11
  59. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +10 -6
  60. data/lib/contrast/agent/reaction_processor.rb +1 -1
  61. data/lib/contrast/agent/request_handler.rb +1 -1
  62. data/lib/contrast/agent/response.rb +5 -5
  63. data/lib/contrast/agent/rewriter.rb +3 -3
  64. data/lib/contrast/agent/scope.rb +81 -55
  65. data/lib/contrast/agent/static_analysis.rb +14 -8
  66. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  67. data/lib/contrast/agent/version.rb +1 -1
  68. data/lib/contrast/api/decorators.rb +3 -0
  69. data/lib/contrast/api/decorators/address.rb +0 -1
  70. data/lib/contrast/api/decorators/application_update.rb +1 -1
  71. data/lib/contrast/api/decorators/library.rb +54 -0
  72. data/lib/contrast/api/decorators/library_usage_update.rb +31 -0
  73. data/lib/contrast/api/decorators/trace_event.rb +19 -31
  74. data/lib/contrast/api/decorators/trace_event_object.rb +11 -3
  75. data/lib/contrast/api/decorators/trace_event_signature.rb +27 -5
  76. data/lib/contrast/api/decorators/user_input.rb +2 -1
  77. data/lib/contrast/common_agent_configuration.rb +2 -1
  78. data/lib/contrast/components/agent.rb +6 -5
  79. data/lib/contrast/components/assess.rb +36 -0
  80. data/lib/contrast/components/config.rb +29 -37
  81. data/lib/contrast/components/interface.rb +30 -6
  82. data/lib/contrast/components/inventory.rb +6 -1
  83. data/lib/contrast/components/scope.rb +72 -6
  84. data/lib/contrast/components/settings.rb +6 -3
  85. data/lib/contrast/config/assess_configuration.rb +2 -1
  86. data/lib/contrast/config/inventory_configuration.rb +2 -2
  87. data/lib/contrast/extension/assess/array.rb +2 -3
  88. data/lib/contrast/extension/assess/erb.rb +1 -3
  89. data/lib/contrast/extension/assess/exec_trigger.rb +1 -4
  90. data/lib/contrast/extension/assess/fiber.rb +2 -3
  91. data/lib/contrast/extension/assess/hash.rb +4 -2
  92. data/lib/contrast/extension/assess/kernel.rb +1 -2
  93. data/lib/contrast/extension/assess/marshal.rb +34 -26
  94. data/lib/contrast/extension/assess/regexp.rb +3 -8
  95. data/lib/contrast/extension/assess/string.rb +1 -2
  96. data/lib/contrast/framework/base_support.rb +51 -53
  97. data/lib/contrast/framework/manager.rb +3 -2
  98. data/lib/contrast/framework/rack/patch/session_cookie.rb +2 -2
  99. data/lib/contrast/framework/rack/support.rb +2 -1
  100. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
  101. data/lib/contrast/framework/rails/patch/assess_configuration.rb +1 -1
  102. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  103. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +1 -1
  104. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +1 -1
  105. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +1 -1
  106. data/lib/contrast/framework/rails/support.rb +5 -1
  107. data/lib/contrast/framework/sinatra/support.rb +3 -2
  108. data/lib/contrast/logger/application.rb +1 -4
  109. data/lib/contrast/utils/duck_utils.rb +1 -1
  110. data/lib/contrast/utils/heap_dump_util.rb +1 -1
  111. data/lib/contrast/utils/inventory_util.rb +0 -7
  112. data/lib/contrast/utils/object_share.rb +3 -3
  113. data/lib/contrast/utils/preflight_util.rb +1 -1
  114. data/lib/contrast/utils/prevent_serialization.rb +1 -1
  115. data/lib/contrast/utils/resource_loader.rb +1 -1
  116. data/lib/contrast/utils/sha256_builder.rb +2 -14
  117. data/lib/contrast/utils/string_utils.rb +1 -1
  118. data/lib/contrast/utils/tag_util.rb +9 -13
  119. data/resources/assess/policy.json +9 -9
  120. data/resources/deadzone/policy.json +150 -0
  121. data/resources/protect/policy.json +12 -0
  122. data/ruby-agent.gemspec +10 -6
  123. data/service_executables/VERSION +1 -1
  124. data/service_executables/linux/contrast-service +0 -0
  125. data/service_executables/mac/contrast-service +0 -0
  126. metadata +76 -27
  127. data/lib/contrast/utils/boolean_util.rb +0 -30
  128. data/lib/contrast/utils/gemfile_reader.rb +0 -193
@@ -379,6 +379,18 @@
379
379
  "method_visibility": "public",
380
380
  "applicator_method": "apply_rule__io",
381
381
  "properties": {}
382
+ },{
383
+ "class_name": "Nokogiri::XML::Document",
384
+ "method_name": "parse",
385
+ "instance_method": false,
386
+ "method_visibility": "public",
387
+ "properties": {}
388
+ }, {
389
+ "class_name": "Nokogiri::XML::SAX::Parser",
390
+ "method_name": "parse",
391
+ "instance_method": true,
392
+ "method_visibility": "public",
393
+ "properties": {}
382
394
  },{
383
395
  "class_name": "Nokogiri::XML::SAX::Parser",
384
396
  "method_name": "parse_memory",
data/ruby-agent.gemspec CHANGED
@@ -14,12 +14,14 @@ def self.add_authors spec
14
14
  donald.propst@contrastsecurity.com
15
15
  alex.macdonald@contrastsecurity.com
16
16
  mark.petersen@contrastsecurity.com
17
+ joshua.reed@contrastsecurity.com
17
18
  ]
18
19
  end
19
20
 
20
21
  # Add those dependencies required to develop or test the Agent
21
22
  def self.add_dev_dependencies spec
22
23
  spec.add_development_dependency 'amazing_print'
24
+ spec.add_development_dependency 'benchmark-ips'
23
25
  spec.add_development_dependency 'bundler'
24
26
  spec.add_development_dependency 'climate_control' # mock ENV
25
27
  spec.add_development_dependency 'debase'
@@ -33,16 +35,18 @@ def self.add_dev_dependencies spec
33
35
  spec.add_development_dependency 'parser', '~> 2.6'
34
36
  spec.add_development_dependency 'pry'
35
37
  spec.add_development_dependency 'rails', '>= 3'
36
- spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rake', '>= 12.3.3'
37
39
  spec.add_development_dependency 'rake-compiler', '~> 0'
38
40
  spec.add_development_dependency 'rspec', '~> 3.0'
39
41
  spec.add_development_dependency 'rspec-benchmark'
40
42
  spec.add_development_dependency 'rspec_junit_formatter', '0.3.0'
41
- spec.add_development_dependency 'rubocop', '0.93.1'
42
- spec.add_development_dependency 'rubocop-performance', '1.8.1'
43
- spec.add_development_dependency 'rubocop-rspec', '1.43.2'
43
+ spec.add_development_dependency 'rubocop', '1.6.1'
44
+ spec.add_development_dependency 'rubocop-performance', '1.9.1'
45
+ spec.add_development_dependency 'rubocop-rails', '2.9.1'
46
+ spec.add_development_dependency 'rubocop-rake', '0.5.1'
47
+ spec.add_development_dependency 'rubocop-rspec', '2.1.0'
44
48
  spec.add_development_dependency 'ruby-debug-ide'
45
- spec.add_development_dependency 'simplecov', '~> 0.18'
49
+ spec.add_development_dependency 'simplecov', '0.20.0'
46
50
  spec.add_development_dependency 'sinatra', '>= 2'
47
51
  spec.add_development_dependency 'sqlite3', '1.3.9'
48
52
  spec.add_development_dependency 'therubyracer'
@@ -58,7 +62,7 @@ end
58
62
  # corresponding update to the fake gem server data in TeamServer.
59
63
  def self.add_dependencies spec
60
64
  spec.add_dependency 'ougai', '~> 1.8'
61
- spec.add_dependency 'parser', '~> 2.6'
65
+ spec.add_dependency 'parser', '~> 2.6' # TODO: RUBY-714 remove w/ EOL of 2.5
62
66
  spec.add_dependency 'protobuf', '~> 3.10'
63
67
  spec.add_dependency 'rack', '~> 2.0'
64
68
  end
@@ -1 +1 @@
1
- 2.14.3
1
+ 2.17.2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contrast-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.16.0
4
+ version: 4.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - galen.palmer@contrastsecurity.com
@@ -9,10 +9,11 @@ authors:
9
9
  - donald.propst@contrastsecurity.com
10
10
  - alex.macdonald@contrastsecurity.com
11
11
  - mark.petersen@contrastsecurity.com
12
+ - joshua.reed@contrastsecurity.com
12
13
  autorequire:
13
14
  bindir: exe
14
15
  cert_chain: []
15
- date: 2020-10-26 00:00:00.000000000 Z
16
+ date: 2021-02-04 00:00:00.000000000 Z
16
17
  dependencies:
17
18
  - !ruby/object:Gem::Dependency
18
19
  name: amazing_print
@@ -28,6 +29,20 @@ dependencies:
28
29
  - - ">="
29
30
  - !ruby/object:Gem::Version
30
31
  version: '0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: benchmark-ips
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
31
46
  - !ruby/object:Gem::Dependency
32
47
  name: bundler
33
48
  requirement: !ruby/object:Gem::Requirement
@@ -214,16 +229,16 @@ dependencies:
214
229
  name: rake
215
230
  requirement: !ruby/object:Gem::Requirement
216
231
  requirements:
217
- - - "~>"
232
+ - - ">="
218
233
  - !ruby/object:Gem::Version
219
- version: '10.0'
234
+ version: 12.3.3
220
235
  type: :development
221
236
  prerelease: false
222
237
  version_requirements: !ruby/object:Gem::Requirement
223
238
  requirements:
224
- - - "~>"
239
+ - - ">="
225
240
  - !ruby/object:Gem::Version
226
- version: '10.0'
241
+ version: 12.3.3
227
242
  - !ruby/object:Gem::Dependency
228
243
  name: rake-compiler
229
244
  requirement: !ruby/object:Gem::Requirement
@@ -286,42 +301,70 @@ dependencies:
286
301
  requirements:
287
302
  - - '='
288
303
  - !ruby/object:Gem::Version
289
- version: 0.93.1
304
+ version: 1.6.1
290
305
  type: :development
291
306
  prerelease: false
292
307
  version_requirements: !ruby/object:Gem::Requirement
293
308
  requirements:
294
309
  - - '='
295
310
  - !ruby/object:Gem::Version
296
- version: 0.93.1
311
+ version: 1.6.1
297
312
  - !ruby/object:Gem::Dependency
298
313
  name: rubocop-performance
299
314
  requirement: !ruby/object:Gem::Requirement
300
315
  requirements:
301
316
  - - '='
302
317
  - !ruby/object:Gem::Version
303
- version: 1.8.1
318
+ version: 1.9.1
304
319
  type: :development
305
320
  prerelease: false
306
321
  version_requirements: !ruby/object:Gem::Requirement
307
322
  requirements:
308
323
  - - '='
309
324
  - !ruby/object:Gem::Version
310
- version: 1.8.1
325
+ version: 1.9.1
326
+ - !ruby/object:Gem::Dependency
327
+ name: rubocop-rails
328
+ requirement: !ruby/object:Gem::Requirement
329
+ requirements:
330
+ - - '='
331
+ - !ruby/object:Gem::Version
332
+ version: 2.9.1
333
+ type: :development
334
+ prerelease: false
335
+ version_requirements: !ruby/object:Gem::Requirement
336
+ requirements:
337
+ - - '='
338
+ - !ruby/object:Gem::Version
339
+ version: 2.9.1
340
+ - !ruby/object:Gem::Dependency
341
+ name: rubocop-rake
342
+ requirement: !ruby/object:Gem::Requirement
343
+ requirements:
344
+ - - '='
345
+ - !ruby/object:Gem::Version
346
+ version: 0.5.1
347
+ type: :development
348
+ prerelease: false
349
+ version_requirements: !ruby/object:Gem::Requirement
350
+ requirements:
351
+ - - '='
352
+ - !ruby/object:Gem::Version
353
+ version: 0.5.1
311
354
  - !ruby/object:Gem::Dependency
312
355
  name: rubocop-rspec
313
356
  requirement: !ruby/object:Gem::Requirement
314
357
  requirements:
315
358
  - - '='
316
359
  - !ruby/object:Gem::Version
317
- version: 1.43.2
360
+ version: 2.1.0
318
361
  type: :development
319
362
  prerelease: false
320
363
  version_requirements: !ruby/object:Gem::Requirement
321
364
  requirements:
322
365
  - - '='
323
366
  - !ruby/object:Gem::Version
324
- version: 1.43.2
367
+ version: 2.1.0
325
368
  - !ruby/object:Gem::Dependency
326
369
  name: ruby-debug-ide
327
370
  requirement: !ruby/object:Gem::Requirement
@@ -340,16 +383,16 @@ dependencies:
340
383
  name: simplecov
341
384
  requirement: !ruby/object:Gem::Requirement
342
385
  requirements:
343
- - - "~>"
386
+ - - '='
344
387
  - !ruby/object:Gem::Version
345
- version: '0.18'
388
+ version: 0.20.0
346
389
  type: :development
347
390
  prerelease: false
348
391
  version_requirements: !ruby/object:Gem::Requirement
349
392
  requirements:
350
- - - "~>"
393
+ - - '='
351
394
  - !ruby/object:Gem::Version
352
- version: '0.18'
395
+ version: 0.20.0
353
396
  - !ruby/object:Gem::Dependency
354
397
  name: sinatra
355
398
  requirement: !ruby/object:Gem::Requirement
@@ -498,20 +541,20 @@ executables:
498
541
  - contrast_service
499
542
  extensions:
500
543
  - ext/cs__common/extconf.rb
544
+ - ext/cs__assess_string_interpolation26/extconf.rb
545
+ - ext/cs__contrast_patch/extconf.rb
501
546
  - ext/cs__assess_module/extconf.rb
502
- - ext/cs__protect_kernel/extconf.rb
503
- - ext/cs__assess_array/extconf.rb
504
- - ext/cs__assess_hash/extconf.rb
505
547
  - ext/cs__assess_marshal_module/extconf.rb
506
- - ext/cs__assess_kernel/extconf.rb
507
- - ext/cs__assess_regexp/extconf.rb
508
- - ext/cs__assess_yield_track/extconf.rb
548
+ - ext/cs__assess_hash/extconf.rb
549
+ - ext/cs__assess_array/extconf.rb
550
+ - ext/cs__assess_basic_object/extconf.rb
551
+ - ext/cs__protect_kernel/extconf.rb
509
552
  - ext/cs__assess_string/extconf.rb
510
- - ext/cs__contrast_patch/extconf.rb
511
553
  - ext/cs__assess_active_record_named/extconf.rb
554
+ - ext/cs__assess_regexp/extconf.rb
555
+ - ext/cs__assess_yield_track/extconf.rb
512
556
  - ext/cs__assess_fiber_track/extconf.rb
513
- - ext/cs__assess_basic_object/extconf.rb
514
- - ext/cs__assess_string_interpolation26/extconf.rb
557
+ - ext/cs__assess_kernel/extconf.rb
515
558
  extra_rdoc_files: []
516
559
  files:
517
560
  - ".clang-format"
@@ -708,6 +751,7 @@ files:
708
751
  - lib/contrast/agent.rb
709
752
  - lib/contrast/agent/assess.rb
710
753
  - lib/contrast/agent/assess/contrast_event.rb
754
+ - lib/contrast/agent/assess/contrast_object.rb
711
755
  - lib/contrast/agent/assess/events/event_factory.rb
712
756
  - lib/contrast/agent/assess/events/source_event.rb
713
757
  - lib/contrast/agent/assess/finalizers/freeze.rb
@@ -770,6 +814,11 @@ files:
770
814
  - lib/contrast/agent/deadzone/policy/policy.rb
771
815
  - lib/contrast/agent/disable_reaction.rb
772
816
  - lib/contrast/agent/exclusion_matcher.rb
817
+ - lib/contrast/agent/inventory.rb
818
+ - lib/contrast/agent/inventory/dependencies.rb
819
+ - lib/contrast/agent/inventory/dependency_analysis.rb
820
+ - lib/contrast/agent/inventory/dependency_usage_analysis.rb
821
+ - lib/contrast/agent/inventory/gemfile_digest_cache.rb
773
822
  - lib/contrast/agent/inventory/policy/datastores.rb
774
823
  - lib/contrast/agent/inventory/policy/policy.rb
775
824
  - lib/contrast/agent/inventory/policy/trigger_node.rb
@@ -847,6 +896,8 @@ files:
847
896
  - lib/contrast/api/decorators/application_update.rb
848
897
  - lib/contrast/api/decorators/http_request.rb
849
898
  - lib/contrast/api/decorators/input_analysis.rb
899
+ - lib/contrast/api/decorators/library.rb
900
+ - lib/contrast/api/decorators/library_usage_update.rb
850
901
  - lib/contrast/api/decorators/message.rb
851
902
  - lib/contrast/api/decorators/rasp_rule_sample.rb
852
903
  - lib/contrast/api/decorators/route_coverage.rb
@@ -941,11 +992,9 @@ files:
941
992
  - lib/contrast/tasks/service.rb
942
993
  - lib/contrast/utils/assess/sampling_util.rb
943
994
  - lib/contrast/utils/assess/tracking_util.rb
944
- - lib/contrast/utils/boolean_util.rb
945
995
  - lib/contrast/utils/class_util.rb
946
996
  - lib/contrast/utils/duck_utils.rb
947
997
  - lib/contrast/utils/env_configuration_item.rb
948
- - lib/contrast/utils/gemfile_reader.rb
949
998
  - lib/contrast/utils/hash_digest.rb
950
999
  - lib/contrast/utils/heap_dump_util.rb
951
1000
  - lib/contrast/utils/invalid_configuration_util.rb
@@ -1,30 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- require 'contrast/utils/object_share'
5
-
6
- module Contrast
7
- module Utils
8
- # Utility methods for asserting truthy or falsy state of a value expected
9
- # to equate to a boolean
10
- class BooleanUtil
11
- class << self
12
- def false? config
13
- return false if config == true
14
- return true if config == false
15
- return false unless config.cs__is_a?(String)
16
-
17
- Contrast::Utils::ObjectShare::FALSE.casecmp?(config)
18
- end
19
-
20
- def true? config
21
- return false if config == false
22
- return true if config == true
23
- return false unless config.cs__is_a?(String)
24
-
25
- Contrast::Utils::ObjectShare::TRUE.casecmp?(config)
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,193 +0,0 @@
1
- # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- require 'set'
5
- require 'contrast/utils/sha256_builder'
6
- require 'contrast/utils/string_utils'
7
- require 'contrast/components/interface'
8
- require 'contrast/api'
9
-
10
- module Contrast
11
- module Utils
12
- # GemfileReader has methods for extracting information from gem specs
13
- # it also has a cache of library file digests to the lines of code found.
14
- class GemfileReader
15
- include Singleton
16
- include Contrast::Components::Interface
17
-
18
- access_component :config, :logging
19
-
20
- CONTRAST_AGENT = 'contrast-agent'
21
-
22
- def initialize
23
- # Map of a Gem's Spec Digest to all loaded files from that Gem
24
- @spec_to_files = {}
25
- end
26
-
27
- # the #clone is necessary here, as a require in another thread could
28
- # potentially result in adding a key to the loaded_specs hash during
29
- # iteration. (as in RUBY-330)
30
- def loaded_specs
31
- Gem.loaded_specs.clone
32
- end
33
-
34
- # indicates if there's been an update to library information, allowing us
35
- # to only serialize this information on change.
36
- def updated?
37
- @updated
38
- end
39
-
40
- def updated!
41
- @updated = true
42
- end
43
-
44
- # Once we're Contrasted, we intercept require calls to do inventory.
45
- # In order to catch up, we do a one-time manual catchup, & inventory
46
- # all the already-loaded gems.
47
- def map_loaded_classes
48
- loaded_specs.each do |name, spec|
49
- # Don't count Contrast gems
50
- next if contrast_gems.include? name
51
-
52
- # Get a digest of the Gem file itself
53
- next unless (digest = Contrast::Utils::Sha256Builder.build_from_spec(spec))
54
-
55
- paths = get_by_digest(digest)
56
- path = spec.full_gem_path
57
- $LOADED_FEATURES.each do |line|
58
- next unless line.cs__is_a?(String)
59
- next unless line.start_with?(path)
60
-
61
- logger.trace('Recording loaded gem for inventory analysis', line: line)
62
- updated!
63
- paths << adjust_lib(line)
64
- end
65
- end
66
- end
67
-
68
- def map_class path
69
- path = adjust_lib(path)
70
-
71
- return unless (spec = Gem::Specification.find_by_path(path))
72
- return unless (digest = Contrast::Utils::Sha256Builder.build_from_spec(spec))
73
-
74
- updated!
75
- get_by_digest(digest) << path
76
- end
77
-
78
- def library_pb_list
79
- loaded_specs.each_with_object([]) do |(name, spec), arr|
80
- next if contrast_gems.include? name
81
- next unless spec
82
- next unless (digest = Contrast::Utils::Sha256Builder.build_from_spec(spec))
83
-
84
- arr << build_library_pb(digest, spec)
85
- end
86
- end
87
-
88
- def generate_library_usage activity = nil
89
- return unless updated?
90
-
91
- @spec_to_files.each_pair do |digest, files|
92
- usage = Contrast::Api::Dtm::LibraryUsageUpdate.new
93
- usage.hash_code = Contrast::Utils::StringUtils.force_utf8(digest)
94
- activity.library_usages[usage.hash_code] = usage if activity
95
- # TODO: RUBY-882 once TS switches to take filenames, remove the count setter and
96
- # send the class names in usage.class_names
97
- usage.count = files.size
98
- end
99
- # TODO: RUBY-882 once TS switches to take filenames, clear this and remove the
100
- # @updated variable
101
-
102
- # @spec_to_files.clear
103
- @updated = false
104
- end
105
-
106
- private
107
-
108
- # marker for the lib dir in an absolute file path. purposefully includes
109
- # the trailing '/'
110
- LIB = '/lib/'
111
-
112
- # Kernel#load uses the absolute path, but Gems / Specs use the path after
113
- # `/lib`. This method accounts for that and trims out the `/lib/` section
114
- # and starts with the first `/` after and the trailing file extension, if
115
- # present.
116
- #
117
- # @param path [String] the path to parse
118
- # @return [String] the relative path of the file, after the lib directory
119
- def adjust_lib path
120
- idx = path.index(LIB)
121
- path = path[(idx + 4)..-1] if idx
122
- idx = path.rindex(Contrast::Utils::ObjectShare::PERIOD)
123
- path = path[0..idx] if idx
124
- path
125
- end
126
-
127
- def get_by_digest digest
128
- @spec_to_files[digest] = Set.new unless @spec_to_files.key?(digest)
129
- @spec_to_files[digest]
130
- end
131
-
132
- def build_library_pb digest, spec
133
- lib = Contrast::Api::Dtm::Library.new
134
- lib.file_path = Contrast::Utils::StringUtils.force_utf8(spec.name)
135
- lib.hash_code = Contrast::Utils::StringUtils.force_utf8(digest)
136
- lib.version = Contrast::Utils::StringUtils.force_utf8(spec.version)
137
- lib.manifest = Contrast::Utils::StringUtils.force_utf8(build_manifest(spec))
138
- lib.external_ms = date_to_ms(spec.date)
139
- lib.internal_ms = lib.external_ms
140
- lib.url = Contrast::Utils::StringUtils.force_utf8(spec.homepage)
141
- # Library tags are appended in the ApplicationUpdate delegator
142
- update_class_counts(lib, digest, spec)
143
- lib
144
- end
145
-
146
- def date_to_ms date
147
- (date.to_f * 1000.0).to_i
148
- end
149
-
150
- def update_class_counts lib, digest, spec
151
- # Updating the class counts
152
- path = spec.full_gem_path.to_s
153
- lib.class_count = all_files(path).length
154
- lib.used_class_count = @spec_to_files.key?(digest) ? get_by_digest(digest).size : 0
155
- lib
156
- end
157
-
158
- def build_manifest spec
159
- Contrast::Utils::StringUtils.force_utf8(spec.to_yaml.to_s) if defined?(YAML)
160
- rescue StandardError
161
- nil
162
- end
163
-
164
- # These are all the code files that are located in the Gem directory loaded
165
- # by the current environment; this includes more than Ruby files
166
- def all_files path
167
- Contrast::Utils::Sha256Builder.instance.files(path)
168
- end
169
-
170
- # Go through all dependents, given as a pair from the DependencyList: `dependency`
171
- # is the dependency itself, filled with all its specs. `dependents` is the array of reverse
172
- # dependencies for the aforementioned dependency. If the dependency is also in contrast_dep_set,
173
- # then contrast depends on it. If its array of dependents is 1, then contrast is the
174
- # only dependency in that list. Since only contrast depends on it, we should ignore it.
175
- def contrast_gems
176
- @_contrast_gems ||= find_contrast_gems
177
- end
178
-
179
- def find_contrast_gems
180
- ignore = Set.new([CONTRAST_AGENT])
181
- contrast_specs = Gem::DependencyList.from_specs.specs.find do |dependency|
182
- dependency.name == CONTRAST_AGENT
183
- end
184
- contrast_dep_set = contrast_specs.dependencies.map(&:name).to_set
185
-
186
- Gem::DependencyList.from_specs.spec_predecessors.each_pair do |dependency, dependents|
187
- ignore.add(dependency.name) if contrast_dep_set.include?(dependency.name) && dependents.length == 1
188
- end
189
- ignore
190
- end
191
- end
192
- end
193
- end