Sha256: cdc01caac4f107b52a71a8a4d23ba8d377f22e2c28150e686bd2c439313e5f92

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true
require 'forwardable'

module SensitiveDataFilter
  module Middleware
    class EnvParser
      QUERY_STRING = 'QUERY_STRING'.freeze
      RACK_INPUT   = 'rack.input'.freeze

      extend Forwardable

      attr_reader :env

      def initialize(env)
        @env = env
        @request = Rack::Request.new(@env)
        @parameter_parser = ParameterParser.parser_for(@request.media_type)
      end

      def query_params
        Rack::Utils.parse_query(@request.query_string)
      end

      def body_params
        return {} if file_upload?
        body = @request.body.read
        @request.body.rewind
        @parameter_parser.parse(body)
      end

      def query_params=(new_params)
        @env[QUERY_STRING] = Rack::Utils.build_query(new_params)
      end

      def body_params=(new_params)
        @env[RACK_INPUT] = StringIO.new @parameter_parser.unparse(new_params)
      end

      def copy
        self.class.new(@env.clone)
      end

      def mask!
        self.query_params = SensitiveDataFilter::Mask.mask(query_params)
        self.body_params  = SensitiveDataFilter::Mask.mask(body_params)
      end

      def_delegators :@request, :ip, :request_method, :url, :content_type, :session

      private

      def file_upload?
        @request.media_type == 'multipart/form-data'
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sensitive_data_filter-0.3.0 lib/sensitive_data_filter/middleware/env_parser.rb
sensitive_data_filter-0.2.4 lib/sensitive_data_filter/middleware/env_parser.rb
sensitive_data_filter-0.2.3 lib/sensitive_data_filter/middleware/env_parser.rb
sensitive_data_filter-0.2.2 lib/sensitive_data_filter/middleware/env_parser.rb
sensitive_data_filter-0.2.1 lib/sensitive_data_filter/middleware/env_parser.rb
sensitive_data_filter-0.2.0 lib/sensitive_data_filter/middleware/env_parser.rb