Skip to content

Commit 391fca8

Browse files
author
Michael Fincham
committed
Improves allow-suffix-match to be more clear in its behaviour.
With thanks to @luto for pointing out the potential for misunderstanding here.
1 parent 62f4c74 commit 391fca8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

powerdns_auth_proxy/proxy.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,15 @@ def zone_list():
187187
return zones
188188
elif request.method == 'POST':
189189
requested_name = g.json.get('name', None)
190-
if requested_name and not any(requested_name.lower().endswith(prefix.lower()) for prefix in (g.user['allow-suffix-creation'] if isinstance(g.user['allow-suffix-creation'], list) else [g.user['allow-suffix-creation']])):
190+
if 'allow-suffix-creation' in g.user:
191+
allowed_suffixes = g.user['allow-suffix-creation'] if isinstance(g.user['allow-suffix-creation'], list) else [g.user['allow-suffix-creation']]
192+
allowed = False
193+
for suffix in allowed_suffixes:
194+
if suffix.startswith('.') and requested_name.lower().endswith(suffix.lower()):
195+
allowed = True
196+
elif not suffix.startswith('.') and requested_name.lower() == suffix.lower():
197+
allowed = True
198+
if allowed != True:
191199
raise Forbidden
192200

193201
g.json = sanitise_metadata_updates(g.json, current_app.config['PDNS'])

powerdns_auth_proxy/tests/test_proxy.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ def client():
4343
4444
[user:demo-example-org]
4545
key = dd70d1b0eccd79a0cf5d79ddf6672dce
46-
allow-suffix-creation = example.org.
46+
allow-suffix-creation = example.org. .example.test.
47+
48+
[user:demo-example-net]
49+
key = a70f4f5fe78ea2e89b53c8b3ee133fdf
50+
allow-suffix-creation = example.net.
4751
"""
4852

4953
pdns_db_file, pdns_db_path = tempfile.mkstemp()
@@ -147,10 +151,14 @@ def test_api_auth(client):
147151
assert response.status_code > 400
148152

149153
def test_api_zone_create(client):
150-
# zone that the user is not allowed to create
154+
# zone that the user is not allowed to create because it is not listed at all
151155
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "example.com.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
152156
assert response.status_code > 400
153157

158+
# zone that the user is not allowed to create but which does share a common prefix with one they can create
159+
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "fooexample.org.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
160+
assert response.status_code > 400
161+
154162
# zone belonging to another user
155163
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "example.net.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
156164
assert response.status_code > 400
@@ -163,6 +171,14 @@ def test_api_zone_create(client):
163171
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "example.org.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
164172
assert response.status_code > 400
165173

174+
# suffix matching a wildcard domain
175+
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "bar.example.test.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
176+
assert response.status_code < 400
177+
178+
# disallow suffix on non-wildcard domain
179+
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "bar.example.org.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})
180+
assert response.status_code > 400
181+
166182
def test_api_zone_list(client):
167183
# create a zone to use for testing
168184
response = client.post('/api/v1/servers/localhost/zones', headers=api_key_header(client), json={"masters": [], "name": "example.org.", "nameservers": ["ns1.example.org."], "kind": "MASTER", "soa_edit_api": "INCEPTION-INCREMENT"})

0 commit comments

Comments
 (0)