From 5d94acce83fc1d752be86c4973567f17cc9245e4 Mon Sep 17 00:00:00 2001 From: Isis Lovecruft Date: Tue, 16 Apr 2013 23:04:30 +0000 Subject: [PATCH] Add _is_hex() function to parsers.py for checking keyid/fingerprints. --- gnupg/parsers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gnupg/parsers.py b/gnupg/parsers.py index 9bd8a68..78e0355 100644 --- a/gnupg/parsers.py +++ b/gnupg/parsers.py @@ -34,6 +34,7 @@ import util ESCAPE_PATTERN = re.compile(r'\\x([0-9a-f][0-9a-f])', re.I) +HEXIDECIMAL = re.compile('([0-9A-F]{2})+') class ProtectedOption(Exception): @@ -327,6 +328,17 @@ def _is_allowed(input): return input return None +def _is_hex(string): + """Check that a string is hexidecimal, with alphabetic characters + capitalized and without whitespace. + + :param str string: The string to check. + """ + matched = HEXIDECIMAL.match(string) + if matched is not None and len(matched.group()) >= 2: + return True + return False + def _sanitise(*args): """Take an arg or the key portion of a kwarg and check that it is in the set of allowed GPG options and flags, and that it has the correct @@ -393,6 +405,12 @@ def _sanitise(*args): else: logger.debug("_check_option(): %s not file: %s" % (flag, val)) + elif flag in ['--default-key']: + if _is_hex(val): + safe_option += (val + " ") + else: + logger.debug("_check_option(): '%s %s' not hex." + % (flag, val)) else: safe_option += (val + " ") logger.debug("_check_option(): No checks for %s"