Module: ActionInterceptor::ActionController::ClassMethods

Defined in:
lib/action_interceptor/action_controller.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_interceptor(options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/action_interceptor/action_controller.rb', line 86

def acts_as_interceptor(options = {})
  self.is_interceptor = true
  self.use_interceptor = options[:override_url_options].nil? ? \
                           ActionInterceptor.override_url_options : \
                           options[:override_url_options]

  class_exec do

    attr_writer :intercepted_url

    skip_before_filter :delete_intercepted_url

    # Ensure that we always store the intercepted url
    before_filter :intercepted_url

    helper_method :intercepted_url, :intercepted_url_hash

    protected

    def intercepted_url
      return @intercepted_url if @intercepted_url

      key = ActionInterceptor.intercepted_url_key
      encrypted_url = params[key]

      begin
        # URL params are the most reliable, as they preserve
        # state even if the user presses the back button
        # We need to sign them to prevent the Open Redirect vulnerability
        @intercepted_url = Encryptor.decrypt_and_verify(encrypted_url)

        # If we got this far, the encrypted url is valid, so reuse it
        @intercepted_url_hash = {key => encrypted_url}
      rescue ActiveSupport::MessageVerifier::InvalidSignature
        # If the param is not available, use our best guess
        # Session and referer are safe for redirects (for that user)
        # Also, can't call root_url here, so use '/' instead
        @intercepted_url = session[key] || request.referer || '/'
      end
      # Session is a signed plaintext in Rails 3
      # In Rails 4, it is encrypted by default
      session[key] = @intercepted_url
      @intercepted_url
    end

    def intercepted_url_hash
      # Run intercepted_url to verify the params in case the
      # encrypted url is in there and can be reused
      unencrypted_url = intercepted_url
      return @intercepted_url_hash if @intercepted_url_hash

      url = Encryptor.encrypt_and_sign(unencrypted_url)
      key = ActionInterceptor.intercepted_url_key

      @intercepted_url_hash = {key => url}
    end

    def redirect_back(options = {})
      url = intercepted_url

      # Disable the return_to param
      without_interceptor do
        # Convert '/' back to root_url
        # Also, prevent self redirects
        url = root_url if url == '/' || current_page?(url)

        redirect_to url, options
      end
    end

  end
end

#interceptor(*interceptor_names, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/action_interceptor/action_controller.rb', line 56

def interceptor(*interceptor_names, &block)
  options = interceptor_names.extract_options!
  filter_name = options.delete(:filter_name)
  fnames = interceptor_names.collect do |iname|
    fname = filter_name || iname
    interceptor_filters[iname] = fname

    define_method fname do
      blk = block || ActionInterceptor.interceptors[iname]
      raise UndefinedInterceptor, iname unless blk

      with_interceptor &blk
    end

    fname
  end

  before_filter *fnames, options
end

#skip_interceptor(*interceptor_names) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/action_interceptor/action_controller.rb', line 76

def skip_interceptor(*interceptor_names)
  options = interceptor_names.extract_options!
  filter_name = options.delete(:filter_name)
  fnames = interceptor_names.collect do |iname|
    filter_name || interceptor_filters[iname] || iname
  end

  skip_before_filter *fnames, options
end