# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'digest' require 'contrast/utils/hash_digest_extend' module Contrast module Utils # We use this class to provide hashes for our Request and Finding objects # based upon our definitions of uniqueness. # While the uniqueness of the request object is something internal to the # Ruby agent, the uniqueness of the Finding hash is defined by a # specification shared across all agent teams. The spec can be found here: # https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/preflight.md class HashDigest < Digest::Class include Digest::Instance extend Contrast::Utils::HashDigestExtend CONTENT_LENGTH_HEADER = 'Content-Length' CRYPTO_RULES = %w[crypto-bad-ciphers crypto-bad-mac].cs__freeze CONFIG_PATH_KEY = 'path' CONFIG_SESSION_ID_KEY = 'sessionId' CLASS_SOURCE_KEY = 'source' CLASS_CONSTANT_NAME_KEY = 'name' CLASS_LINE_NO_KEY = 'lineNo' def update_on_request finding, request if (route = finding.routes[0]) update(route.route) update(route.verb) elsif request ||= Contrast::Agent::REQUEST_TRACKER.current&.request update(request.normalized_uri) update(request.request_method) end end def update_on_sources events return unless events&.any? events.each do |event| if event.cs__is_a?(Contrast::Api::Dtm::TraceEvent) event.event_sources&.each do |source| update(source.type) update(source.name) # rubocop:disable Security/Module/Name end elsif event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent) update(event.source_type) update(event.source_name) end end end CHARS = %w[a b c d e f g].cs__freeze # This method converts and integer value for length into a string value # that we can hash on, based on the logarithmic value of the length, and # updates the current hash with that value. # @param chr [Numeric] the length to translate def update_on_content_length chr update(CHARS[Math.log10(chr.to_s.length).to_i] || CHARS[-1]) end def initialize super @crc32 = 0 end def update str return unless str @crc32 = Zlib.crc32(str, @crc32) end def finish @crc32.to_s end end end end