Skip to content

Commit

Permalink
Prevent CSV injection attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Feb 19, 2024
1 parent a527cf9 commit ad3cd94
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/phlex/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ def render(renderable)
end

def escape(value)
value = value.to_s
value = value.to_s.dup
value.strip!

if value.include?('"') || value.include?(",") || value.include?("\n")
if value.start_with?("=") || value.start_with?("+") || value.start_with?("-") || value.start_with?("@")
# Prefix a tab to prevent Excel and Google Docs from interpreting the value as a formula
%("\t#{value.gsub('"', '""')}")
elsif value.include?('"') || value.include?(",") || value.include?("\n")
%("#{value.gsub('"', '""')}")
else
value
Expand Down

0 comments on commit ad3cd94

Please sign in to comment.