Skip to content

Commit

Permalink
Merge pull request #125 from ninoseki/return-error-as-json
Browse files Browse the repository at this point in the history
Change to return an error as JSON
  • Loading branch information
thisismyrobot committed Dec 11, 2019
2 parents 10fe569 + 76d8d4d commit 61a6403
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 21 deletions.
30 changes: 9 additions & 21 deletions dnstwister/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ def whois(hexdomain):
"""Returns whois information."""
domain = tools.parse_domain(hexdomain)
if domain is None:
flask.abort(
400,
'Malformed domain or domain not represented in hexadecimal format.'
)
return flask.jsonify(error='Malformed domain or domain not represented in hexadecimal format.'), 400

payload = standard_api_values(domain, skip='whois')
try:
idna_domain = domain.encode('idna')
Expand All @@ -84,10 +82,8 @@ def parked_score(hexdomain):
"""Calculates "parked" scores from 0-1."""
domain = tools.parse_domain(hexdomain)
if domain is None:
flask.abort(
400,
'Malformed domain or domain not represented in hexadecimal format.'
)
return flask.jsonify(error='Malformed domain or domain not represented in hexadecimal format.'), 400

payload = standard_api_values(domain, skip='parked_score')
score, score_text, redirects, dressed, dest = parked.get_score(domain)
payload['score'] = score
Expand All @@ -103,10 +99,8 @@ def safebrowsing_check(hexdomain):
"""Returns number of hits in Google Safe Browsing."""
domain = tools.parse_domain(hexdomain)
if domain is None:
flask.abort(
400,
'Malformed domain or domain not represented in hexadecimal format.'
)
return flask.jsonify(error='Malformed domain or domain not represented in hexadecimal format.'), 400

payload = standard_api_values(domain, skip='safebrowsing')
payload['issue_detected'] = safebrowsing.get_report(domain) != 0
return flask.jsonify(payload)
Expand All @@ -117,10 +111,7 @@ def resolve_ip(hexdomain):
"""Resolves Domains to IPs."""
domain = tools.parse_domain(hexdomain)
if domain is None:
flask.abort(
400,
'Malformed domain or domain not represented in hexadecimal format.'
)
return flask.jsonify(error='Malformed domain or domain not represented in hexadecimal format.'), 400

ip_addr, error = tools.resolve(domain)

Expand All @@ -135,7 +126,7 @@ def domain_to_hex(domain):
"""Helps you convert domains to hex."""
hexdomain = tools.encode_domain(domain)
if tools.parse_domain(hexdomain) is None:
flask.abort(400, 'Malformed domain.')
return flask.jsonify(error='Malformed domain.'), 400

payload = standard_api_values(domain, skip='domain_to_hex')
payload['domain_as_hexadecimal'] = hexdomain
Expand All @@ -147,10 +138,7 @@ def fuzz(hexdomain):
"""Calculates the dnstwist "fuzzy domains" for a domain."""
domain = tools.parse_domain(hexdomain)
if domain is None:
flask.abort(
400,
'Malformed domain or domain not represented in hexadecimal format.'
)
return flask.jsonify(error='Malformed domain or domain not represented in hexadecimal format.'), 400

fuzz_result = tools.fuzzy_domains(domain)
fuzz_payload = []
Expand Down
13 changes: 13 additions & 0 deletions tests/api/test_api_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ def test_fuzzer(webapp):
u'resolve_ip_url': u'http://localhost/api/ip/7777772e6578616d706c652e636f6d',
u'url': u'http://localhost/api/fuzz/7777772e6578616d706c652e636f6d'
}


def test_fuzzer_with_invalid_input(webapp):
"""Test the fuzzer with an invalid input."""
domain = "foobar"

hexdomain = binascii.hexlify(domain)

response = webapp.get('/api/fuzz/{}'.format(hexdomain),
expect_errors=True).json

error = response['error']
assert error == 'Malformed domain or domain not represented in hexadecimal format.'
13 changes: 13 additions & 0 deletions tests/api/test_api_hexlify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ def test_to_hex(webapp):
response = webapp.get('/api/to_hex/{}'.format(domain))

assert response.json['domain_as_hexadecimal'] == hexdomain


def test_to_hex_with_invalid_input(webapp):
"""Test the hex helper with an invalid input."""
domain = 'foobar'

hexdomain = binascii.hexlify(domain)

response = webapp.get('/api/to_hex/{}'.format(domain),
expect_errors=True).json

error = response['error']
assert error == 'Malformed domain.'
12 changes: 12 additions & 0 deletions tests/api/test_api_parked.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,15 @@ def test_dressed_check():
assert parked_api.dressed('www.example.com', 'example.com.au')

assert not parked_api.dressed('www.example.com', 'www.examples.com')


def test_parked_with_invalid_input(webapp):
"""Test with an invalid input."""
domain = 'foobar'
hexdomain = binascii.hexlify(domain)

response = webapp.get(
'/api/parked/{}'.format(hexdomain), expect_errors=True).json

error = response['error']
assert error == 'Malformed domain or domain not represented in hexadecimal format.'
13 changes: 13 additions & 0 deletions tests/integration/test_ip_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ def test_failed_resolve(webapp):
assert response.status_code == 200
assert response.json['ip'] is False
assert response.json['error'] is False


def test_with_invalid_input(webapp):
"""Test with an invalid input."""
domain = "foobar"

hexdomain = binascii.hexlify(domain)

response = webapp.get('/api/ip/{}'.format(hexdomain),
expect_errors=True).json

error = response['error']
assert error == 'Malformed domain or domain not represented in hexadecimal format.'
13 changes: 13 additions & 0 deletions tests/integration/test_safebrowsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ def test_safebrowsing_with_bad_domain(webapp):

assert response.status_code == 200
assert response.json['issue_detected'] is True


def test_with_invalid_input(webapp):
"""Test with an invalid input."""
domain = "foobar"

hexdomain = binascii.hexlify(domain)

response = webapp.get('/api/safebrowsing/{}'.format(hexdomain),
expect_errors=True).json

error = response['error']
assert error == 'Malformed domain or domain not represented in hexadecimal format.'
13 changes: 13 additions & 0 deletions tests/integration/test_whois.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ def test_whois_query(webapp):
}

assert 'Domain Name: dnstwister.report' in whois_text


def test_with_invalid_input(webapp):
"""Test with an invalid input."""
domain = "foobar"

hexdomain = binascii.hexlify(domain)

response = webapp.get('/api/whois/{}'.format(hexdomain),
expect_errors=True).json

error = response['error']
assert error == 'Malformed domain or domain not represented in hexadecimal format.'

0 comments on commit 61a6403

Please sign in to comment.