output_safety.rb 7.9 KB
Newer Older
1
require 'erb'
2
require 'active_support/core_ext/kernel/singleton_class'
3
require 'active_support/deprecation'
4 5 6

class ERB
  module Util
7
    HTML_ESCAPE = { '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;', "'" => '&#39;' }
G
Godfrey Chan 已提交
8
    JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
9
    HTML_ESCAPE_REGEXP = /[&"'><]/
10
    HTML_ESCAPE_ONCE_REGEXP = /["><']|&(?!([a-zA-Z]+|(#\d+)|(#[xX][\dA-Fa-f]{1,4}));)/
G
Godfrey Chan 已提交
11
    JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
12

13 14 15 16 17 18
    # A utility method for escaping HTML tag characters.
    # This method is also aliased as <tt>h</tt>.
    #
    # In your ERB templates, use this method to escape any unsafe content. For example:
    #   <%=h @person.name %>
    #
19
    #   puts html_escape('is a > 0 & a < 10?')
20 21
    #   # => is a &gt; 0 &amp; a &lt; 10?
    def html_escape(s)
22
      unwrapped_html_escape(s).html_safe
23 24
    end

R
R.T. Lechow 已提交
25
    # Aliasing twice issues a warning "discarding old...". Remove first to avoid it.
26
    remove_method(:h)
27 28 29 30
    alias h html_escape

    module_function :h

31 32 33
    singleton_class.send(:remove_method, :html_escape)
    module_function :html_escape

34 35 36 37 38 39 40 41 42 43 44 45
    # HTML escapes strings but doesn't wrap them with an ActiveSupport::SafeBuffer.
    # This method is not for public consumption! Seriously!
    def unwrapped_html_escape(s) # :nodoc:
      s = s.to_s
      if s.html_safe?
        s
      else
        s.gsub(HTML_ESCAPE_REGEXP, HTML_ESCAPE)
      end
    end
    module_function :unwrapped_html_escape

V
Vijay Dev 已提交
46
    # A utility method for escaping HTML without affecting existing escaped entities.
47
    #
48
    #   html_escape_once('1 < 2 &amp; 3')
49 50
    #   # => "1 &lt; 2 &amp; 3"
    #
51
    #   html_escape_once('&lt;&lt; Accept & Checkout')
52 53
    #   # => "&lt;&lt; Accept &amp; Checkout"
    def html_escape_once(s)
A
Aman Gupta 已提交
54
      result = s.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
55 56 57 58 59
      s.html_safe? ? result.html_safe : result
    end

    module_function :html_escape_once

60
    # A utility method for escaping HTML entities in JSON strings. Specifically, the
61
    # &, > and < characters are replaced with their equivalent unicode escaped form -
G
Godfrey Chan 已提交
62
    # \u0026, \u003e, and \u003c. The Unicode sequences \u2028 and \u2029 are also
63 64
    # escaped as they are treated as newline characters in some JavaScript engines.
    # These sequences have identical meaning as the original characters inside the
G
Godfrey Chan 已提交
65
    # context of a JSON string, so assuming the input is a valid and well-formed
66 67
    # JSON value, the output will have equivalent meaning when parsed:
    #
68 69
    #   json = JSON.generate({ name: "</script><script>alert('PWNED!!!')</script>"})
    #   # => "{\"name\":\"</script><script>alert('PWNED!!!')</script>\"}"
70
    #
71 72
    #   json_escape(json)
    #   # => "{\"name\":\"\\u003C/script\\u003E\\u003Cscript\\u003Ealert('PWNED!!!')\\u003C/script\\u003E\"}"
73
    #
74 75
    #   JSON.parse(json) == JSON.parse(json_escape(json))
    #   # => true
76
    #
77 78
    # The intended use case for this method is to escape JSON strings before including
    # them inside a script tag to avoid XSS vulnerability:
79 80
    #
    #   <script>
81
    #     var currentUser = <%= raw json_escape(current_user.to_json) %>;
82
    #   </script>
83
    #
84 85 86 87 88 89 90 91 92 93 94
    # It is necessary to +raw+ the result of +json_escape+, so that quotation marks
    # don't get converted to <tt>&quot;</tt> entities. +json_escape+ doesn't
    # automatically flag the result as HTML safe, since the raw value is unsafe to
    # use inside HTML attributes.
    #
    # If you need to output JSON elsewhere in your HTML, you can just do something
    # like this, as any unsafe characters (including quotation marks) will be
    # automatically escaped for you:
    #
    #   <div data-user-info="<%= current_user.to_json %>">...</div>
    #
95 96 97 98
    # WARNING: this helper only works with valid JSON. Using this on non-JSON values
    # will open up serious XSS vulnerabilities. For example, if you replace the
    # +current_user.to_json+ in the example above with user input instead, the browser
    # will happily eval() that string as JavaScript.
99
    #
100
    # The escaping performed in this method is identical to those performed in the
101
    # Active Support JSON encoder when +ActiveSupport.escape_html_entities_in_json+ is
102 103
    # set to true. Because this transformation is idempotent, this helper can be
    # applied even if +ActiveSupport.escape_html_entities_in_json+ is already true.
104
    #
105 106 107
    # Therefore, when you are unsure if +ActiveSupport.escape_html_entities_in_json+
    # is enabled, or if you are unsure where your JSON string originated from, it
    # is recommended that you always apply this helper (other libraries, such as the
108 109
    # JSON gem, do not provide this kind of protection by default; also some gems
    # might override +to_json+ to bypass Active Support's encoder).
110
    def json_escape(s)
A
Aman Gupta 已提交
111
      result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
112
      s.html_safe? ? result.html_safe : result
113 114 115 116 117 118
    end

    module_function :json_escape
  end
end

119 120 121 122 123 124
class Object
  def html_safe?
    false
  end
end

125
class Numeric
126 127 128 129 130
  def html_safe?
    true
  end
end

131 132
module ActiveSupport #:nodoc:
  class SafeBuffer < String
A
Alexey Gaziev 已提交
133 134
    UNSAFE_STRING_METHODS = %w(
      capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
135
      slice squeeze strip sub succ swapcase tr tr_s upcase
A
Alexey Gaziev 已提交
136
    )
137

138 139 140 141 142
    alias_method :original_concat, :concat
    private :original_concat

    class SafeConcatError < StandardError
      def initialize
143
        super 'Could not concatenate to the buffer because it is not html safe.'
144 145 146
      end
    end

147
    def [](*args)
A
Alexey Gaziev 已提交
148 149
      if args.size < 2
        super
150
      else
A
Alexey Gaziev 已提交
151 152 153 154 155 156 157
        if html_safe?
          new_safe_buffer = super
          new_safe_buffer.instance_eval { @html_safe = true }
          new_safe_buffer
        else
          to_str[*args]
        end
158 159 160
      end
    end

161
    def safe_concat(value)
162
      raise SafeConcatError unless html_safe?
163 164
      original_concat(value)
    end
165

166
    def initialize(*)
167
      @html_safe = true
168 169 170 171 172
      super
    end

    def initialize_copy(other)
      super
173
      @html_safe = other.html_safe?
174 175
    end

A
Akira Matsuda 已提交
176
    def clone_empty
177
      self[0, 0]
A
Akira Matsuda 已提交
178 179
    end

180 181
    def concat(value)
      super(html_escape_interpolated_argument(value))
182
    end
183
    alias << concat
J
Joshua Peek 已提交
184

185 186 187 188
    def prepend(value)
      super(html_escape_interpolated_argument(value))
    end

189 190 191 192 193
    def prepend!(value)
      ActiveSupport::Deprecation.deprecation_warning "ActiveSupport::SafeBuffer#prepend!", :prepend
      prepend value
    end

194 195 196 197
    def +(other)
      dup.concat(other)
    end

198
    def %(args)
199 200 201 202 203
      case args
      when Hash
        escaped_args = Hash[args.map { |k,arg| [k, html_escape_interpolated_argument(arg)] }]
      else
        escaped_args = Array(args).map { |arg| html_escape_interpolated_argument(arg) }
204 205
      end

206
      self.class.new(super(escaped_args))
207 208
    end

209
    def html_safe?
210
      defined?(@html_safe) && @html_safe
211
    end
J
Joshua Peek 已提交
212

213 214 215
    def to_s
      self
    end
216

217 218 219 220
    def to_param
      to_str
    end

221 222 223 224
    def encode_with(coder)
      coder.represent_scalar nil, to_str
    end

225
    UNSAFE_STRING_METHODS.each do |unsafe_method|
226
      if unsafe_method.respond_to?(unsafe_method)
227 228 229 230 231 232 233 234 235 236
        class_eval <<-EOT, __FILE__, __LINE__ + 1
          def #{unsafe_method}(*args, &block)       # def capitalize(*args, &block)
            to_str.#{unsafe_method}(*args, &block)  #   to_str.capitalize(*args, &block)
          end                                       # end

          def #{unsafe_method}!(*args)              # def capitalize!(*args)
            @html_safe = false                      #   @html_safe = false
            super                                   #   super
          end                                       # end
        EOT
237
      end
238
    end
239 240 241 242

    private

    def html_escape_interpolated_argument(arg)
243 244
      (!html_safe? || arg.html_safe?) ? arg :
        arg.to_s.gsub(ERB::Util::HTML_ESCAPE_REGEXP, ERB::Util::HTML_ESCAPE)
245
    end
246
  end
247
end
J
Joshua Peek 已提交
248

249 250 251 252
class String
  def html_safe
    ActiveSupport::SafeBuffer.new(self)
  end
253
end