Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion flow/record/adapter/csvfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import csv
import sys
from pathlib import Path
Expand Down Expand Up @@ -89,7 +90,8 @@ def __init__(

self.dialect = "excel"
if self.fp.seekable():
self.dialect = csv.Sniffer().sniff(self.fp.read(1024))
with contextlib.suppress(csv.Error):
self.dialect = csv.Sniffer().sniff(self.fp.read(1024))
self.fp.seek(0)
self.reader = csv.reader(self.fp, dialect=self.dialect)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,5 +711,15 @@ def test_rdump_selected_fields(capsysbinary: pytest.CaptureFixture) -> None:
assert captured.out == b"Q42eWSaF,A sample pastebin record,text\r\n"


def test_rdump_csv_sniff(tmp_path: Path, capsysbinary: pytest.CaptureFixture) -> None:
csv_path = tmp_path / "test.csv"
csv_path.write_text("ip,common_name,vulnerable\n127.0.0.1,localhost,1\n192.168.4.20,")
rdump.main([str(csv_path)])

captured = capsysbinary.readouterr()
assert b"<csv/reader ip='127.0.0.1' common_name='localhost' vulnerable='1'>" in captured.out
assert b"<csv/reader ip='192.168.4.20' common_name='' vulnerable=None>" in captured.out


if __name__ == "__main__":
__import__("standalone_test").main(globals())