Skip to content

Commit 157da6f

Browse files
committed
Merge remote-tracking branch 'origin/pr/176'
* origin/pr/176: Provide clear error on invalid policy file name
2 parents d941252 + 098eb78 commit 157da6f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

qrexec/policy/admin.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import contextlib
2525
import fcntl
2626
import os
27+
import re
2728
import hashlib
2829

2930
from .parser import ValidateParser, FilePolicy, get_invalid_characters
@@ -87,7 +88,11 @@ def handle_request(
8788
Throws PolicyAdminException in case of user error.
8889
"""
8990

90-
assert all(char in RPCNAME_ALLOWED_CHARSET for char in arg)
91+
if not all(char in RPCNAME_ALLOWED_CHARSET for char in arg):
92+
raise PolicyAdminException(
93+
"Invalid argument: \"{}\"\n"
94+
"Valid characters are letters, numbers, dot, plus, hyphen and "
95+
"underline".format(arg))
9196

9297
func = self._find_method(service_name)
9398
if not func:
@@ -266,7 +271,12 @@ def policy_get_files(self, arg):
266271

267272
# helpers
268273

269-
def _get_path(self, arg: str, dir_path, suffix: str) -> Path:
274+
def _get_path(self, arg: str, dir_path: str , suffix: str) -> Path:
275+
if not re.compile(r'^[\w-]+$').match(arg):
276+
raise PolicyAdminException(
277+
f"Invalid policy file name: {arg}\n"
278+
"Names must contain only alphanumeric characters, "
279+
"underscore and hyphen.")
270280
path = dir_path / (arg + suffix)
271281
path = path.resolve()
272282
if path.parent != dir_path:

qrexec/tests/policy_admin.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ def test_api_get(policy_dir, api):
7676
with pytest.raises(PolicyAdminException, match="Not found"):
7777
api.handle_request("policy.Get", "nonexistent", b"")
7878

79-
with pytest.raises(PolicyAdminException, match="Expecting a path inside"):
79+
with pytest.raises(PolicyAdminException, match="Invalid policy file"):
80+
api.handle_request("policy.Get", ".hidden_evil_policy", b"")
81+
82+
with pytest.raises(PolicyAdminException, match="Invalid policy file"):
8083
api.handle_request("policy.include.Get", "..", b"")
8184

82-
with pytest.raises(PolicyAdminException, match="Expecting a path inside"):
85+
with pytest.raises(PolicyAdminException, match="Invalid policy file"):
8386
api.handle_request("policy.include.Get", "", b"")
8487

88+
with pytest.raises(PolicyAdminException, match="Invalid argument"):
89+
api.handle_request("policy.include.Get", "space in argument", b"")
90+
8591

8692
def test_api_replace(policy_dir, api):
8793
api.handle_request("policy.Replace", "file1", b"any\n")

0 commit comments

Comments
 (0)