module SecureHeaders::TaskHelper

Constants

INLINE_HASH_SCRIPT_HELPER_REGEX
INLINE_HASH_STYLE_HELPER_REGEX
INLINE_SCRIPT_REGEX
INLINE_STYLE_REGEX

Public Instance Methods

dynamic_content?(filename, inline_script) click to toggle source
# File lib/secure_headers/task_helper.rb, line 30
def dynamic_content?(filename, inline_script)
  !!(
    (is_mustache?(filename) && inline_script =~ /\{\{.*\}\}/) ||
    (is_erb?(filename) && inline_script =~ /<%.*%>/)
    )
end
generate_inline_script_hashes(filename) click to toggle source
# File lib/secure_headers/task_helper.rb, line 12
def generate_inline_script_hashes(filename)
  hashes = []

  hashes.concat find_inline_content(filename, INLINE_SCRIPT_REGEX, false)
  hashes.concat find_inline_content(filename, INLINE_HASH_SCRIPT_HELPER_REGEX, true)

  hashes
end
generate_inline_style_hashes(filename) click to toggle source
# File lib/secure_headers/task_helper.rb, line 21
def generate_inline_style_hashes(filename)
  hashes = []

  hashes.concat find_inline_content(filename, INLINE_STYLE_REGEX, false)
  hashes.concat find_inline_content(filename, INLINE_HASH_STYLE_HELPER_REGEX, true)

  hashes
end

Private Instance Methods

find_inline_content(filename, regex, strip_trailing_whitespace) click to toggle source
# File lib/secure_headers/task_helper.rb, line 39
def find_inline_content(filename, regex, strip_trailing_whitespace)
  hashes = []
  file = File.read(filename)
  file.scan(regex) do # TODO don't use gsub
    inline_script = Regexp.last_match.captures.last
    inline_script.gsub!(/(\r?\n)[\t ]+\z/, '\1') if strip_trailing_whitespace
    if dynamic_content?(filename, inline_script)
      puts "Looks like there's some dynamic content inside of a tag :-/"
      puts "That pretty much means the hash value will never match."
      puts "Code: " + inline_script
      puts "=" * 20
    end

    hashes << hash_source(inline_script)
  end
  hashes
end
is_erb?(filename) click to toggle source
# File lib/secure_headers/task_helper.rb, line 57
def is_erb?(filename)
  filename =~ /\.erb\Z/
end
is_mustache?(filename) click to toggle source
# File lib/secure_headers/task_helper.rb, line 61
def is_mustache?(filename)
  filename =~ /\.mustache\Z/
end