Sha256: 75eb7b7df116c829e577713e2a39f531c804d1238431ed1cc9efcb28d94daa40

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true
require 'forwardable'
require 'rack'

module SensitiveDataFilter
  module Middleware
    class EnvParser
      QUERY_STRING = 'QUERY_STRING'.freeze
      RACK_INPUT   = 'rack.input'.freeze
      REQUEST_PARAMS = 'action_dispatch.request.request_parameters'.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 request_params
        @env[REQUEST_PARAMS]
      end

      def query_params=(new_params)
        set_key_if_exists(QUERY_STRING, Rack::Utils.build_query(new_params))
      end

      def body_params=(new_params)
        set_key_if_exists(RACK_INPUT, StringIO.new(@parameter_parser.unparse(new_params)))
      end

      def request_params=(new_params)
        set_key_if_exists(REQUEST_PARAMS, new_params)
      end

      def mutate(mutation)
        SensitiveDataFilter::Middleware::FILTERABLE.each do |filterable|
          self.send("#{filterable}=", mutation.send(filterable))
        end
      end

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

      private

      def set_key_if_exists(key, value)
        @env[key] = value if @env.key?(key)
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sensitive_data_filter-0.8.0 lib/sensitive_data_filter/middleware/env_parser.rb