From 790671b55db914b7e8a2e01165cdb0ed1a60d50c Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 18 Jun 2020 16:02:50 -0400 Subject: [PATCH 1/8] securesystemslib: Add AnyNonemptyString, use for PATH_SCHEMA --- securesystemslib/formats.py | 2 +- securesystemslib/schema.py | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/securesystemslib/formats.py b/securesystemslib/formats.py index 1120cfdf..401525fb 100755 --- a/securesystemslib/formats.py +++ b/securesystemslib/formats.py @@ -128,7 +128,7 @@ SCHEME_SCHEMA = SCHEMA.AnyString() # A path string, whether relative or absolute, e.g. 'metadata/root/' -PATH_SCHEMA = SCHEMA.AnyString() +PATH_SCHEMA = SCHEMA.AnyNonemptyString() PATHS_SCHEMA = SCHEMA.ListOf(PATH_SCHEMA) # An integer representing logger levels, such as logging.CRITICAL (=50). diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index 1eb48137..855c9fd2 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -206,6 +206,47 @@ def check_match(self, object): +class AnyNonemptyString(Schema): + """ + + Matches any string with one or more characters. + This schema can be viewed as the Any() schema applied to Strings, but an + additional check is performed to ensure only strings are considered and + that said strings have at least one character. + + Supported methods include: + matches(): returns a Boolean result. + check_match(): raises 'exceptions.FormatError' on a mismatch. + + + + >>> schema = AnyNonemptyString() + >>> schema.matches('') + False + >>> schema.matches('a string') + True + >>> schema.matches(['a']) + False + >>> schema.matches(3) + False + >>> schema.matches(u'a unicode string') + True + >>> schema.matches({}) + False + """ + + def __init__(self): + pass + + + def check_match(self, object): + if not isinstance(object, six.string_types): + raise securesystemslib.exceptions.FormatError('Expected a string' + ' but got ' + repr(object)) + + if object == "": + raise securesystemslib.exceptions.FormatError('Expected a string' + ' with at least one character but got' + repr(object)) class AnyBytes(Schema): From e84c99dbb69548af5907e5668d617e6856df2cd9 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 18 Jun 2020 16:07:53 -0400 Subject: [PATCH 2/8] securesystemslib/schema: Spacing --- securesystemslib/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index 855c9fd2..124b5dcd 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -246,7 +246,7 @@ def check_match(self, object): if object == "": raise securesystemslib.exceptions.FormatError('Expected a string' - ' with at least one character but got' + repr(object)) + ' with at least one character but got ' + repr(object)) class AnyBytes(Schema): From 19877378850cf1a3c55d755d182bb3c6fb8bce25 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 10:43:34 -0400 Subject: [PATCH 3/8] securesystemslib/schema: AnyNonemptyString inherits from AnyString --- securesystemslib/schema.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index 124b5dcd..f9dfdaa6 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -206,7 +206,7 @@ def check_match(self, object): -class AnyNonemptyString(Schema): +class AnyNonemptyString(AnyString): """ Matches any string with one or more characters. @@ -235,14 +235,8 @@ class AnyNonemptyString(Schema): False """ - def __init__(self): - pass - - def check_match(self, object): - if not isinstance(object, six.string_types): - raise securesystemslib.exceptions.FormatError('Expected a string' - ' but got ' + repr(object)) + super().check_match() if object == "": raise securesystemslib.exceptions.FormatError('Expected a string' From d24fc41be2a8e3c9f2b2af8be2863571ee529f1f Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 10:45:24 -0400 Subject: [PATCH 4/8] securesystemslib/util: PATHS_SCHEMA -> NAMES_SCHEMA --- securesystemslib/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/securesystemslib/util.py b/securesystemslib/util.py index 7af85c94..b9926812 100755 --- a/securesystemslib/util.py +++ b/securesystemslib/util.py @@ -210,7 +210,7 @@ def file_in_confined_directories(filepath, confined_directories): # Do the arguments have the correct format? # Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch. securesystemslib.formats.PATH_SCHEMA.check_match(filepath) - securesystemslib.formats.PATHS_SCHEMA.check_match(confined_directories) + securesystemslib.formats.NAMES_SCHEMA.check_match(confined_directories) for confined_directory in confined_directories: # The empty string (arbitrarily chosen) signifies the client is confined From b3b5159c0bb2e62814144e89bd3842bf6c34d660 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 11:11:26 -0400 Subject: [PATCH 5/8] securesystemslib/schema: Fix missing argument --- securesystemslib/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index f9dfdaa6..a898d75a 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -236,7 +236,7 @@ class AnyNonemptyString(AnyString): """ def check_match(self, object): - super().check_match() + super().check_match(object) if object == "": raise securesystemslib.exceptions.FormatError('Expected a string' From 60cd0829be0e5a69c6a61707b48ed8e48e837714 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 11:16:54 -0400 Subject: [PATCH 6/8] test_schema: Add test_AnyNonemptyString --- tests/test_schema.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_schema.py b/tests/test_schema.py index 2f035323..1d7f9a6b 100755 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -131,6 +131,19 @@ def test_AnyString(self): + def test_AnyNonemptyString(self): + anynonemptystring_schema = SCHEMA.AnyNonemptyString() + + self.assertTrue(anynonemptystring_schema.matches("foo")) + + # Test conditions for invalid arguments. + self.assertFalse(anynonemptystring_schema.matches('')) + self.assertFalse(anynonemptystring_schema.matches(['a'])) + self.assertFalse(anynonemptystring_schema.matches(3)) + self.assertFalse(anynonemptystring_schema.matches({'a': 'string'})) + + + def test_OneOf(self): # Test conditions for valid arguments. oneof_schema = SCHEMA.OneOf([SCHEMA.ListOf(SCHEMA.Integer()), From 26f3d3e5c5c17f860a8881fb1e04c3841632205c Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 11:25:52 -0400 Subject: [PATCH 7/8] securesystemslib/schema: Avoid use of super() Makes Python 2.7 unhappy without "new-style" classes. --- securesystemslib/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index a898d75a..0fe63f60 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -236,7 +236,7 @@ class AnyNonemptyString(AnyString): """ def check_match(self, object): - super().check_match(object) + AnyString.check_match(self, object) if object == "": raise securesystemslib.exceptions.FormatError('Expected a string' From a3d0f5854ed503fd3160bb43868243fa33f05f5e Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 19 Jun 2020 13:23:00 -0400 Subject: [PATCH 8/8] securesystemslib/schema: Spacing --- securesystemslib/schema.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/securesystemslib/schema.py b/securesystemslib/schema.py index 0fe63f60..fc50ad65 100755 --- a/securesystemslib/schema.py +++ b/securesystemslib/schema.py @@ -206,6 +206,8 @@ def check_match(self, object): + + class AnyNonemptyString(AnyString): """ @@ -243,6 +245,9 @@ def check_match(self, object): ' with at least one character but got ' + repr(object)) + + + class AnyBytes(Schema): """