From 119725680e1cc1417ecb348fa06335d810c57add Mon Sep 17 00:00:00 2001 From: Felipe Selmo Date: Fri, 8 Apr 2022 14:58:06 -0600 Subject: [PATCH] Validate forward resolution for reverse resolution by default - As outlined in the warning in the ENS docs, for the reverse resolution of a name for an address to be accurate, the forward resolution address must also match. Source at the time of this commit is here: https://docs.ens.domains/dapp-developer-guide/resolving-names#reverse-resolution. --- ens/main.py | 6 +++++- newsfragments/2420.feature.rst | 1 + tests/ens/test_setup_name.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 newsfragments/2420.feature.rst diff --git a/ens/main.py b/ens/main.py index 5cc4e98fb6..465f640af6 100644 --- a/ens/main.py +++ b/ens/main.py @@ -152,7 +152,11 @@ def name(self, address: ChecksumAddress) -> Optional[str]: :type address: hex-string """ reversed_domain = address_to_reverse_domain(address) - return self.resolve(reversed_domain, get='name') + name = self.resolve(reversed_domain, get='name') + + # To be absolutely certain of the name, via reverse resolution, the address must match in + # the forward resolution + return name if to_checksum_address(address) == self.address(name) else None def setup_address( self, diff --git a/newsfragments/2420.feature.rst b/newsfragments/2420.feature.rst new file mode 100644 index 0000000000..f81eb8df89 --- /dev/null +++ b/newsfragments/2420.feature.rst @@ -0,0 +1 @@ +For ``ENS.name()``, validate that the forward resolution returns the same address as provided by the user as per the ENS documentation recommendation for Reverse Resolution. diff --git a/tests/ens/test_setup_name.py b/tests/ens/test_setup_name.py index 26fb0a7e3b..e81b73d9ae 100644 --- a/tests/ens/test_setup_name.py +++ b/tests/ens/test_setup_name.py @@ -69,6 +69,17 @@ def test_setup_name(ens, name, normalized_name, namehash_hex): # check that the correct owner is set: assert ens.owner(name) == owner + # setup name to point to new address + new_address = ens.w3.eth.accounts[4] + ens.setup_address(name, None) + ens.setup_name(name, new_address) + + # validate that ens.name() only returns a name if the forward resolution also returns the + # address + assert ens.name(new_address) == normalized_name # reverse resolution + assert ens.address(name) == new_address # forward resolution + assert not ens.name(address) + ens.setup_name(None, address) ens.setup_address(name, None) assert not ens.name(address)