Skip to content

Commit

Permalink
Fix hash generation for indented helper methods
Browse files Browse the repository at this point in the history
The helper outputs the closing </script> or </style> unindented
if it is on its own line. Needed to remove any extra whitespace
the regex captures on that last line.
  • Loading branch information
rahearn committed Aug 27, 2021
1 parent 8e28012 commit a0072e0
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/tasks/tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ namespace :secure_headers do
(is_erb?(filename) && inline_script =~ /<%.*%>/)
end

def find_inline_content(filename, regex, hashes)
def find_inline_content(filename, regex, hashes, strip_trailing_whitespace)
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."
Expand All @@ -38,19 +39,17 @@ namespace :secure_headers do
def generate_inline_script_hashes(filename)
hashes = []

[INLINE_SCRIPT_REGEX, INLINE_HASH_SCRIPT_HELPER_REGEX].each do |regex|
find_inline_content(filename, regex, hashes)
end
find_inline_content(filename, INLINE_SCRIPT_REGEX, hashes, false)
find_inline_content(filename, INLINE_HASH_SCRIPT_HELPER_REGEX, hashes, true)

hashes
end

def generate_inline_style_hashes(filename)
hashes = []

[INLINE_STYLE_REGEX, INLINE_HASH_STYLE_HELPER_REGEX].each do |regex|
find_inline_content(filename, regex, hashes)
end
find_inline_content(filename, INLINE_STYLE_REGEX, hashes, false)
find_inline_content(filename, INLINE_HASH_STYLE_HELPER_REGEX, hashes, true)

hashes
end
Expand Down

0 comments on commit a0072e0

Please sign in to comment.