diff --git a/acme/src/acme/_internal/tests/challenges_test.py b/acme/src/acme/_internal/tests/challenges_test.py index f95fecfb2..44b3f5dd2 100644 --- a/acme/src/acme/_internal/tests/challenges_test.py +++ b/acme/src/acme/_internal/tests/challenges_test.py @@ -244,6 +244,12 @@ class HTTP01Test(unittest.TestCase): assert 'http://example.com/.well-known/acme-challenge/' \ 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ self.msg.uri('example.com') + assert 'http://1.2.3.4/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ + self.msg.uri('1.2.3.4') + assert 'http://[::1]/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ + self.msg.uri('::1') def test_to_partial_json(self): assert self.jmsg == self.msg.to_partial_json() diff --git a/acme/src/acme/challenges.py b/acme/src/acme/challenges.py index 03398d8a5..6b979e906 100644 --- a/acme/src/acme/challenges.py +++ b/acme/src/acme/challenges.py @@ -2,6 +2,7 @@ import abc import functools import hashlib +import ipaddress import logging from typing import Any from typing import cast @@ -365,17 +366,24 @@ class HTTP01(KeyAuthorizationChallenge): """ return '/' + self.URI_ROOT_PATH + '/' + self.encode('token') - def uri(self, domain: str) -> str: + def uri(self, identifier: str) -> str: """Create an URI to the provisioned resource. Forms an URI to the HTTPS server provisioned resource (containing :attr:`~SimpleHTTP.token`). - :param str domain: Domain name being verified. + :param str identifier: Domain name or IP address being verified. :rtype: str """ - return "http://" + domain + self.path + try: + # https://datatracker.ietf.org/doc/html/rfc2732#section-2 + # IPv6 addresses in URLs should be enclosed in brackets. + ipaddress.IPv6Address(identifier) + identifier = "[" + identifier + "]" + except ipaddress.AddressValueError: + pass + return "http://" + identifier + self.path def validation(self, account_key: jose.JWK, **unused_kwargs: Any) -> str: """Generate validation. diff --git a/newsfragments/10548.fixed b/newsfragments/10548.fixed new file mode 100644 index 000000000..b971d51a9 --- /dev/null +++ b/newsfragments/10548.fixed @@ -0,0 +1 @@ +The HTTP01.uri method will now properly enclose IPv6 addresses in square brackets.