Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.

Commit 4d0df9b

Browse files
authored
Escape quotations in CSV imports properly (#1929)
* Parse quotes in imports * Update invalid CSV for test
1 parent 7c66f16 commit 4d0df9b

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

app/controllers/import/uploads_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ def csv_str
2929
end
3030

3131
def csv_valid?(str)
32-
require "csv"
33-
3432
begin
35-
csv = CSV.parse(str || "", headers: true, col_sep: upload_params[:col_sep])
33+
csv = Import.parse_csv_str(str, col_sep: upload_params[:col_sep])
3634
return false if csv.headers.empty?
3735
return false if csv.count == 0
3836
true

app/models/import.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ class Import < ApplicationRecord
3434
has_many :accounts, dependent: :destroy
3535
has_many :entries, dependent: :destroy, class_name: "Account::Entry"
3636

37+
class << self
38+
def parse_csv_str(csv_str, col_sep: ",")
39+
CSV.parse(
40+
(csv_str || "").strip,
41+
headers: true,
42+
col_sep: col_sep,
43+
converters: [ ->(str) { str&.strip } ],
44+
liberal_parsing: true
45+
)
46+
end
47+
end
48+
3749
def publish_later
3850
raise "Import is not publishable" unless publishable?
3951

@@ -178,12 +190,7 @@ def default_currency
178190
end
179191

180192
def parsed_csv
181-
@parsed_csv ||= CSV.parse(
182-
(raw_file_str || "").strip,
183-
headers: true,
184-
col_sep: col_sep,
185-
converters: [ ->(str) { str&.strip } ]
186-
)
193+
@parsed_csv ||= self.class.parse_csv_str(raw_file_str, col_sep: col_sep)
187194
end
188195

189196
def sanitize_number(value)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
name,age
2-
"John Doe,23
3-
"Jane Doe",25
1+
name,description,amount,currency

0 commit comments

Comments
 (0)