[PATCH] deb822: Validate input in Deb822.__setitem__
John Wright
john at johnwright.org
Wed Jul 29 13:17:27 UTC 2009
Try to catch input that would result in multiple stanzas or unparseable
data when dumping the Deb822 object.
This patch also clarifies a comment related to this, and removes an XXX
comment that it addreses.
---
debian_bundle/deb822.py | 35 ++++++++++++++++++++++++++++++++---
1 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index dd1d201..ca35668 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -358,10 +358,12 @@ class Deb822(Deb822Dict):
else:
return_string = False
for key, value in self.iteritems():
+ # We want one space between the "Field:" and value, unless
+ # value starts with a newline (i.e. the value itself started on
+ # the line *after* the field name in the control file), or is
+ # empty. In that case, we avoid trailing whitespace by by not
+ # including a space after the colon.
if not value or value[0] == '\n':
- # Avoid trailing whitespace after "Field:" if it's on its own
- # line or the value is empty
- # XXX Uh, really print value if value == '\n'?
fd.write('%s:%s\n' % (key, value))
else:
fd.write('%s: %s\n' % (key, value))
@@ -538,6 +540,33 @@ class Deb822(Deb822Dict):
return self.gpg_info
+ def validate_input(self, key, value):
+ """Raise ValueError if value is not a valid value for key
+
+ Subclasses that do interesting things for different keys may wish to
+ override this method.
+ """
+
+ strvalue = str(value)
+
+ # The value cannot end in a newline (if it did, dumping the object
+ # would result in multiple stanzas)
+ if strvalue.endswith('\n'):
+ raise ValueError("value must not end in '\\n'")
+
+ # Make sure there are no blank lines (actually, the first one is
+ # allowed to be blank, but no others), and each subsequent line starts
+ # with whitespace
+ for line in strvalue.splitlines()[1:]:
+ if not line:
+ raise ValueError("value must not have blank lines")
+ if not line[0] in string.whitespace:
+ raise ValueError("each line must start with whitespace")
+
+ def __setitem__(self, key, value):
+ self.validate_input(key, value)
+ Deb822Dict.__setitem__(self, key, value)
+
###
# XXX check what happens if input contains more that one signature
--
1.6.3.3
--qDbXVdCdHGoSgWSk--
More information about the pkg-python-debian-discuss
mailing list