Skip to content

Commit

Permalink
fix UnicodeDecodeError when parsing openapi spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-back committed Jun 10, 2020
1 parent af78ad4 commit 10087a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions w3af/core/data/parsers/doc/open_api/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"""
import json
import yaml
import logging

from yaml import load
Expand Down Expand Up @@ -232,7 +231,8 @@ def _load_spec_dict(self):
:return: The dict with the open api data
"""
try:
spec_dict = json.loads(self.http_response.body)
decoded_response = self.http_response.body.decode('ascii', 'ignore')
spec_dict = json.loads(decoded_response)
except ValueError:
# Seems like the OpenAPI was specified using Yaml instead of
# JSON. Let's parse the Yaml data!
Expand Down
12 changes: 12 additions & 0 deletions w3af/core/data/parsers/doc/open_api/tests/test_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,18 @@ def test_parameter_handler_multiple_paths_and_headers(self):
handler = SpecificationHandler(http_response)
self.check_parameter_setting(handler)

def test_specification_handler_can_handle_spec_with_non_ascii_chars(self):
with open(
'w3af/core/data/parsers/doc/open_api/tests/data/swagger-special-chars.json',
) as file_:
spec_as_string = file_.read()
http_response = self.generate_response(spec_as_string)
spec_handler = SpecificationHandler(http_response)
result = spec_handler.get_api_information()
for _ in result:
pass
self.assertFalse(spec_handler._parsing_errors)

def check_parameter_setting(self, spec_handler):
data = [d for d in spec_handler.get_api_information()]
self.assertIsNotNone(data)
Expand Down

0 comments on commit 10087a0

Please sign in to comment.