Skip to content

Commit 86da8e8

Browse files
committed
Refuse a lookup of anything but an IP address before sending it
The API answers an AS or a domain at the lookup's own path, and that answer is not a lookup's, so sending one came back as three retried server errors the caller could do nothing with. lookup now refuses it with a bad_request naming lookup_asn and lookup_domain, before anything is sent. A batch still sends what it is given, and the API answers each entry for itself.
1 parent 75beb71 commit 86da8e8

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ domain = client.lookup_domain("example.com")
168168
print(domain.is_registered, domain.has_mx) # True True
169169
```
170170

171-
On a paid plan an AS answer also carries its Whois record, its counts and every prefix it announces, and a domain answer its DNS records, its organization's IP ranges and `is_disposable`. Neither is cached, and neither goes through the batch.
171+
On a paid plan an AS answer also carries its Whois record, its counts and every prefix it announces, and a domain answer its DNS records, its organization's IP ranges and `is_disposable`. Neither is cached, and neither goes through the batch. `lookup` takes addresses alone, and refuses an AS or a domain with a `bad_request` before sending anything.
172172

173173
### Errors
174174

‎src/internetdata/_core.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import contextlib
99
import contextvars
1010
import hashlib
11+
import ipaddress
1112
import json
1213
import math
1314
import os
@@ -261,6 +262,23 @@ def check_retries(retries: float) -> float:
261262
return retries
262263

263264

265+
def check_address(ip: str) -> None:
266+
"""Refuse what is not an IP address before anything is sent. The API answers an AS
267+
(`AS15169`) or a domain at the lookup's own path, and that answer is not a lookup's, so
268+
sent, it came back as a retried `server_error` the caller could do nothing with."""
269+
if isinstance(ip, str):
270+
try:
271+
ipaddress.ip_address(ip)
272+
return
273+
except ValueError:
274+
pass
275+
raise InternetDataError(
276+
"bad_request",
277+
f"{ip!r} is not an IP address: look an AS up with lookup_asn, and a domain with "
278+
"lookup_domain",
279+
)
280+
281+
264282
def call_retries(retries: float | None) -> float | None:
265283
"""A per-call `retries`, checked as the client's is, or None for the client's own."""
266284
return None if retries is None else check_retries(retries)

‎src/internetdata/aio.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
build_async_transfer_client,
4646
build_client,
4747
call_retries,
48+
check_address,
4849
check_build,
4950
check_client_concurrency,
5051
check_concurrency,
@@ -169,7 +170,9 @@ async def lookup(
169170
) -> Result:
170171
"""What every database says about one address.
171172
172-
A bogon is answered locally and never reaches the network. Everything else is
173+
A string that is not an IP address is refused as `bad_request` before anything is
174+
sent: look an AS up with `lookup_asn`, and a domain with `lookup_domain`. A bogon is
175+
answered locally and never reaches the network. Everything else is
173176
served, then cached for this instance. Calls that miss the cache while a request
174177
for their address is in flight, a batch's included, await that request rather than
175178
sending their own, and take its answer, sent under the options of the call that led
@@ -178,6 +181,7 @@ async def lookup(
178181
# Here as well as in _bound: a bogon or a cached answer returns before any request.
179182
check_timeout(timeout)
180183
call_retries(retries)
184+
check_address(ip)
181185
ip = unmapped(ip)
182186
if is_bogon(ip):
183187
return bogon_result(ip)

‎src/internetdata/client.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
build_client,
4242
build_transfer_client,
4343
call_retries,
44+
check_address,
4445
check_build,
4546
check_client_concurrency,
4647
check_concurrency,
@@ -190,7 +191,9 @@ def lookup(
190191
) -> Result:
191192
"""What every database says about one address.
192193
193-
A bogon is answered locally and never reaches the network. Everything else is
194+
A string that is not an IP address is refused as `bad_request` before anything is
195+
sent: look an AS up with `lookup_asn`, and a domain with `lookup_domain`. A bogon is
196+
answered locally and never reaches the network. Everything else is
194197
served, then cached for this instance. Calls that miss the cache while a request
195198
for their address is in flight, a batch's included, await that request rather than
196199
sending their own, and take its answer, sent under the options of the call that led
@@ -199,6 +202,7 @@ def lookup(
199202
# Here as well as in _bound: a bogon or a cached answer returns before any request.
200203
check_timeout(timeout)
201204
call_retries(retries)
205+
check_address(ip)
202206
ip = unmapped(ip)
203207
if is_bogon(ip):
204208
return bogon_result(ip)

‎tests/test_lookup.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ def test_a_bogon_lookup_issues_zero_http_requests(make_client: ClientFactory) ->
108108
assert stub.calls == []
109109

110110

111+
# The API answers an AS or a domain at the lookup's own path, with an answer a lookup cannot
112+
# read, so sending one ended as three retried server errors.
113+
@pytest.mark.parametrize(
114+
"target", ["AS9009", "as9009", "9009", "example.com", "notanip", "", " 8.8.8.8", 9009, None]
115+
)
116+
def test_a_lookup_of_anything_but_an_address_is_refused_before_it_is_sent(
117+
make_client: ClientFactory, target: Any
118+
) -> None:
119+
stub = Lookups({})
120+
client = make_client(transport=stub.transport, retries=2)
121+
122+
with pytest.raises(InternetDataError) as caught:
123+
client.lookup(target)
124+
125+
assert (caught.value.kind, caught.value.retryable) == ("bad_request", False)
126+
assert "lookup_asn" in str(caught.value) and "lookup_domain" in str(caught.value)
127+
assert stub.calls == []
128+
129+
130+
def test_a_batch_sends_what_it_is_given_and_the_api_answers_for_it(
131+
make_client: ClientFactory,
132+
) -> None:
133+
stub = Lookups({"1.1.1.1": {"body": answer("1.1.1.1")}})
134+
client = make_client(transport=stub.transport)
135+
136+
got = client.lookup_batch(["1.1.1.1", "AS9009"])
137+
138+
refused = got["AS9009"]
139+
assert isinstance(refused, InternetDataError) and refused.kind == "bad_request"
140+
assert isinstance(got["1.1.1.1"], Result)
141+
assert len(stub.calls) == 1
142+
143+
111144
def test_a_served_answer_is_read_member_by_member(make_client: ClientFactory) -> None:
112145
stub = Lookups({"1.1.1.1": {"body": PAID}})
113146
client = make_client(transport=stub.transport)

0 commit comments

Comments
 (0)