Skip to content

Commit a5bcfb0

Browse files
committed
config: validate domains when formatting them
1 parent 1fee90e commit a5bcfb0

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

chatmaild/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies = [
1010
"filelock",
1111
"requests",
1212
"crypt-r >= 3.13.1 ; python_version >= '3.11'",
13+
"domain-validator",
1314
]
1415

1516
[tool.setuptools]

chatmaild/src/chatmaild/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
import iniconfig
6+
from domain_validator import DomainValidator
67

78
from chatmaild.user import User
89

@@ -178,10 +179,12 @@ def format_arpa_address(address: str) -> str:
178179
if is_valid_ipv4(address):
179180
parts = address.split(".")
180181
return f"{parts[3]}.{parts[2]}.{parts[1]}.{parts[0]}.in-addr.arpa"
182+
DomainValidator().validate_domain_re(address)
181183
return address
182184

183185

184186
def format_deliverable_domain(mail_domain: str) -> str:
185187
if is_valid_ipv4(mail_domain):
186188
return f"[{mail_domain}]"
189+
DomainValidator().validate_domain_re(mail_domain)
187190
return mail_domain

chatmaild/src/chatmaild/tests/test_config.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from contextlib import nullcontext as does_not_raise
2+
13
import pytest
24

3-
from chatmaild.config import read_config
5+
from chatmaild.config import read_config, is_valid_ipv4, format_arpa_address, format_deliverable_domain
46

57

68
def test_read_config_basic(example_config):
@@ -16,6 +18,11 @@ def test_read_config_basic(example_config):
1618
assert example_config.mail_domain_deliverable == "chat.example.org"
1719

1820

21+
def test_read_config_deliverable(ipv4_config):
22+
assert ipv4_config.mail_domain == "1.3.3.7"
23+
assert ipv4_config.mail_domain_deliverable == "[1.3.3.7]"
24+
25+
1926
def test_read_config_basic_using_defaults(tmp_path, maildomain):
2027
inipath = tmp_path.joinpath("chatmail.ini")
2128
inipath.write_text(f"[params]\nmail_domain = {maildomain}")
@@ -122,3 +129,45 @@ def test_config_tls_external_bad_format(make_config):
122129
"tls_external_cert_and_key": "/only/one/path.pem",
123130
},
124131
)
132+
133+
134+
@pytest.mark.parametrize(
135+
["input", "result"],
136+
[
137+
("example.org", False),
138+
("1.3.3.7", True),
139+
("fe::1", False),
140+
("ad.1e.dag.adf", False),
141+
("12394142", False),
142+
]
143+
)
144+
def test_is_valid_ipv4(input, result):
145+
assert result == is_valid_ipv4(input)
146+
147+
148+
@pytest.mark.parametrize(
149+
["input", "result", "exception"],
150+
[
151+
("example.org", "example.org", does_not_raise()),
152+
("1.3.3.7", "7.3.3.1.in-addr.arpa", does_not_raise()),
153+
("fe::1", None, pytest.raises(ValueError)),
154+
("12394142", None, pytest.raises(ValueError)),
155+
]
156+
)
157+
def test_format_arpa_address(input, result, exception):
158+
with exception:
159+
assert result == format_arpa_address(input)
160+
161+
162+
@pytest.mark.parametrize(
163+
["input", "result", "exception"],
164+
[
165+
("example.org", "example.org", does_not_raise()),
166+
("1.3.3.7", "[1.3.3.7]", does_not_raise()),
167+
("fe::1", None, pytest.raises(ValueError)),
168+
("12394142", None, pytest.raises(ValueError)),
169+
]
170+
)
171+
def test_format_deliverable_domain(input, result, exception):
172+
with exception:
173+
assert result == format_deliverable_domain(input)

cmdeploy/src/cmdeploy/tests/test_cmdeploy.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ def test_init_not_overwrite(self, capsys, tmp_path, monkeypatch):
4040
assert "deleting config file" in out.lower()
4141

4242
def test_dns_skip_on_ip(self, capsys):
43-
parser = get_parser()
44-
parser.parse_args([])
4543
assert main(["init", "1.3.3.7"]) == 0
4644
assert main(["dns"]) == 0
4745
out, err = capsys.readouterr()

0 commit comments

Comments
 (0)