From 48b54bbd710306e46dd6afbb4eea899735f4dd80 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Mon, 16 Sep 2024 16:14:25 +0200 Subject: [PATCH] Improve parsing To make the logic more resilient towards potential errors or future output changes, not only consider the string index but also validate it against a set of expected positions. Signed-off-by: Georg Pfuetzenreuter --- pyacl/acl.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/pyacl/acl.py b/pyacl/acl.py index 6cb1af7..ce29ddf 100644 --- a/pyacl/acl.py +++ b/pyacl/acl.py @@ -43,22 +43,32 @@ def parse_permission(strpermission): if len(strpermission) != MAX_PERMBITS: return ValueError('Invalid permission') - permap = { + permap_i = { 0: 'read', 1: 'write', 2: 'execute', } + permap_s = { + 'r': 'read', + 'w': 'write', + 'e': 'execute', + } + outmap = DEFAULT_PERMISSIONS.copy() - for i, perm in permap.items(): - permval = strpermission[i] - if permval in ['r', 'w', 'x']: - outmap[perm] = True - elif permval == '-': - outmap[perm] = False + for i, spval in enumerate(strpermission): + permval = permap_i[i] + if spval == '-': + outmap[permval] = False else: - return ValueError('Invalid permission flag') + spermval = permap_s.get(spval) + if spermval and spermval in outmap: + if spermval != permval: + raise ValueError('Unexpected permission mismatch') + outmap[spermval] = True + else: + return ValueError('Invalid permission flag') return outmap