lib/contrast/utils/class_util.rb in contrast-agent-3.10.2 vs lib/contrast/utils/class_util.rb in contrast-agent-3.11.0
- old
+ new
@@ -1,9 +1,10 @@
# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true
cs__scoped_require 'contrast/extensions/ruby_core/module'
+cs__scoped_require 'contrast/utils/object_share'
module Contrast
module Utils
# Utility methods for exploring the complete space of Objects
class ClassUtil
@@ -54,19 +55,22 @@
# @param object [Object] the entity to convert to a String
# @return [String] the human readable form of the String, as defined by
# https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/capture-snapshot.md
def to_contrast_string object
if object.cs__is_a?(String)
- return Contrast::Utils::ObjectShare::EMPTY_STRING if object.empty?
+ cached = to_cached_string(object)
+ return cached if cached
object.dup
elsif object.cs__is_a?(Symbol)
":#{ object }"
elsif object.cs__is_a?(Numeric)
object.to_s
elsif object.cs__is_a?(Module) || object.cs__is_a?(Class)
"#{ object.cs__name }@#{ object.__id__ }"
+ elsif object.cs__is_a?(Regexp)
+ object.source
else
"#{ object.cs__class.cs__name }@#{ object.__id__ }"
end
end
@@ -104,9 +108,24 @@
return mod if mod.singleton_class?
return mod.cs__singleton_class unless is_instance
mod
+ end
+
+ # If the String matches a common String in our ObjectShare, return that
+ # rather that for use as the representation of the String rather than
+ # forcing a duplication of the String.
+ #
+ # @param string [String] some string of which we want a Contrast
+ # representation.
+ # @return [String,nil] the ObjectShare version of the String or nil
+ def to_cached_string string
+ return Contrast::Utils::ObjectShare::EMPTY_STRING if string.empty?
+ return Contrast::Utils::ObjectShare::SLASH if string == Contrast::Utils::ObjectShare::SLASH
+ return Contrast::Utils::ObjectShare::EQUALS if string == Contrast::Utils::ObjectShare::EQUALS
+
+ nil
end
end
end
end
end