From 87b506dc9b4831d11b4fd04fd6849532db074f90 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 5 Feb 2021 06:55:06 -0800 Subject: [PATCH] fix for spaces in dig answer data --- CHANGELOG | 3 +++ jc/cli.py | 2 +- jc/parsers/dig.py | 8 +++++-- setup.py | 2 +- tests/fixtures/generic/dig-answer-spaces.json | 1 + tests/fixtures/generic/dig-answer-spaces.out | 23 +++++++++++++++++++ tests/test_dig.py | 12 ++++++++++ 7 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/generic/dig-answer-spaces.json create mode 100644 tests/fixtures/generic/dig-answer-spaces.out diff --git a/CHANGELOG b/CHANGELOG index 511b2969..2eed170d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ jc changelog +20210205 v1.14.2 +- Update dig parser to fix cases where there are spaces in the answer data (e.g. TXT records) + 20210106 v1.14.1 - Add iw-scan parser tested on linux (beta) - Update date parser for Ubuntu 20.04 support diff --git a/jc/cli.py b/jc/cli.py index ae0e4abc..6cdaa19a 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -21,7 +21,7 @@ import jc.appdirs as appdirs class info(): - version = '1.14.1' + version = '1.14.2' description = 'JSON CLI output utility' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/jc/parsers/dig.py b/jc/parsers/dig.py index c14f6b04..3eb23d9c 100644 --- a/jc/parsers/dig.py +++ b/jc/parsers/dig.py @@ -333,7 +333,7 @@ import jc.utils class info(): - version = '1.3' + version = '1.4' description = 'dig command parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -526,7 +526,11 @@ def parse_answer(answer): answer_class = answer[2] answer_type = answer[3] answer_ttl = answer[1] - answer_data = answer[4] + answer_data = ' '.join(answer[4:]) + + # remove surrounding quotation marks from answer_data if they exist + if answer_data.startswith('"') and answer_data.endswith('"'): + answer_data = answer_data[1:-1] return {'name': answer_name, 'class': answer_class, diff --git a/setup.py b/setup.py index cdde27e4..359da564 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.14.1', + version='1.14.2', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='Converts the output of popular command-line tools and file-types to JSON.', diff --git a/tests/fixtures/generic/dig-answer-spaces.json b/tests/fixtures/generic/dig-answer-spaces.json new file mode 100644 index 00000000..c22963ed --- /dev/null +++ b/tests/fixtures/generic/dig-answer-spaces.json @@ -0,0 +1 @@ +[{"id": 26965, "opcode": "QUERY", "status": "NXDOMAIN", "flags": ["qr", "rd", "ra"], "query_num": 1, "answer_num": 0, "authority_num": 1, "additional_num": 1, "question": {"name": "x.y.z.w.bl.spamcop.net.", "class": "IN", "type": "TXT"}, "authority": [{"name": "bl.spamcop.net.", "class": "IN", "type": "SOA", "ttl": 0, "data": "bl.spamcop.net."}], "answer": [{"name": "x.y.z.w.bl.spamcop.net.", "class": "IN", "type": "TXT", "ttl": 2100, "data": "Blocked - see https://www.spamcop.net/bl.shtml?w.z.y.x"}], "query_time": 297, "server": "192.168.1.254#53(192.168.1.254)", "when": "Fri Feb 05 06:28:58 PST 2021", "rcvd": 104}] diff --git a/tests/fixtures/generic/dig-answer-spaces.out b/tests/fixtures/generic/dig-answer-spaces.out new file mode 100644 index 00000000..692371af --- /dev/null +++ b/tests/fixtures/generic/dig-answer-spaces.out @@ -0,0 +1,23 @@ + +; <<>> DiG 9.10.6 <<>> TXT x.y.z.w.bl.spamcop.net +;; global options: +cmd +;; Got answer: +;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 26965 +;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 + +;; OPT PSEUDOSECTION: +; EDNS: version: 0, flags:; udp: 4096 +;; QUESTION SECTION: +;x.y.z.w.bl.spamcop.net. IN TXT + +;; AUTHORITY SECTION: +bl.spamcop.net. 0 IN SOA bl.spamcop.net. hostmaster.admin.spamcop.net. 1612535191 3600 1800 3600 0 + +;; ANSWER SECTION: +x.y.z.w.bl.spamcop.net. 2100 IN TXT "Blocked - see https://www.spamcop.net/bl.shtml?w.z.y.x" + +;; Query time: 297 msec +;; SERVER: 192.168.1.254#53(192.168.1.254) +;; WHEN: Fri Feb 05 06:28:58 PST 2021 +;; MSG SIZE rcvd: 104 + diff --git a/tests/test_dig.py b/tests/test_dig.py index c124dd51..50706f7e 100644 --- a/tests/test_dig.py +++ b/tests/test_dig.py @@ -55,6 +55,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-axfr.out'), 'r', encoding='utf-8') as f: self.osx_10_14_6_dig_axfr = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-answer-spaces.out'), 'r', encoding='utf-8') as f: + self.generic_dig_answer_spaces = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/dig.json'), 'r', encoding='utf-8') as f: self.centos_7_7_dig_json = json.loads(f.read()) @@ -101,6 +104,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/dig-axfr.json'), 'r', encoding='utf-8') as f: self.osx_10_14_6_dig_axfr_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/dig-answer-spaces.json'), 'r', encoding='utf-8') as f: + self.generic_dig_answer_spaces_json = json.loads(f.read()) + def test_dig_nodata(self): """ Test 'dig' with no data @@ -197,6 +203,12 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.dig.parse(self.osx_10_14_6_dig_axfr, quiet=True), self.osx_10_14_6_dig_axfr_json) + def test_dig_answer_spaces(self): + """ + Test 'dig' with spaces in the answer data (e.g. TXT responses) + """ + self.assertEqual(jc.parsers.dig.parse(self.generic_dig_answer_spaces, quiet=True), self.generic_dig_answer_spaces_json) + if __name__ == '__main__': unittest.main()