Skip to content

Commit

Permalink
fix: Rule out CSV file format when line looks like json formatted line
Browse files Browse the repository at this point in the history
  • Loading branch information
davidandreoletti committed Jan 23, 2024
1 parent 47531b6 commit 0270a3b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pass_import/formats/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ def is_format(self):
return False
self.file.seek(0)
self.reader = csv.DictReader(self.file, dialect=dialect)

# Context:
# 1password can export data in:
# - 1PIF (json like format)
# - CSV
# Problem:
# CSV sniffer considers the following line as a CSV
# line and will provide a dialect for it.
#
# line: {"field0":"foo", ..., fieldX:"baz"}
#
# This is clearly a JSON formatted line.
# Solution:
# If the line looks like a JSON, then consider the
# file not a CSV file
def is_json_key_value_format(value):
values = value.split(':', 2)
return len(values) > 1

# Skip header
_ = next(self.reader)
# Read first line
row = next(self.reader)
keys = [k for k in row.keys() if k is not None]
if all(map(is_json_key_value_format, keys)):
return False
except (csv.Error, UnicodeDecodeError):
return False
return True
Expand Down

0 comments on commit 0270a3b

Please sign in to comment.