Skip to content

Commit 438a703

Browse files
author
Richard Jones
committed
handle invalid data input in forms better
1 parent 10e7b8e commit 438a703

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

roundup/cgi/client.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: client.py,v 1.110 2003-03-26 03:35:00 richard Exp $
1+
# $Id: client.py,v 1.111 2003-03-26 06:46:17 richard Exp $
22

33
__doc__ = """
44
WWW request handler (also used in the stand-alone server).
@@ -1732,36 +1732,41 @@ class <designator> (where <designator> must be
17321732
# other types should be None'd if there's no value
17331733
value = None
17341734
else:
1735-
if isinstance(proptype, hyperdb.String):
1736-
if (hasattr(value, 'filename') and
1737-
value.filename is not None):
1738-
# skip if the upload is empty
1739-
if not value.filename:
1740-
continue
1741-
# this String is actually a _file_
1742-
# try to determine the file content-type
1743-
filename = value.filename.split('\\')[-1]
1744-
if propdef.has_key('name'):
1745-
props['name'] = filename
1746-
# use this info as the type/filename properties
1747-
if propdef.has_key('type'):
1748-
props['type'] = mimetypes.guess_type(filename)[0]
1749-
if not props['type']:
1750-
props['type'] = "application/octet-stream"
1751-
# finally, read the content
1752-
value = value.value
1753-
else:
1754-
# normal String fix the CRLF/CR -> LF stuff
1755-
value = fixNewlines(value)
1756-
1757-
elif isinstance(proptype, hyperdb.Date):
1758-
value = date.Date(value, offset=timezone)
1759-
elif isinstance(proptype, hyperdb.Interval):
1760-
value = date.Interval(value)
1761-
elif isinstance(proptype, hyperdb.Boolean):
1762-
value = value.lower() in ('yes', 'true', 'on', '1')
1763-
elif isinstance(proptype, hyperdb.Number):
1764-
value = float(value)
1735+
# handle ValueErrors for all these in a similar fashion
1736+
try:
1737+
if isinstance(proptype, hyperdb.String):
1738+
if (hasattr(value, 'filename') and
1739+
value.filename is not None):
1740+
# skip if the upload is empty
1741+
if not value.filename:
1742+
continue
1743+
# this String is actually a _file_
1744+
# try to determine the file content-type
1745+
fn = value.filename.split('\\')[-1]
1746+
if propdef.has_key('name'):
1747+
props['name'] = fn
1748+
# use this info as the type/filename properties
1749+
if propdef.has_key('type'):
1750+
props['type'] = mimetypes.guess_type(fn)[0]
1751+
if not props['type']:
1752+
props['type'] = "application/octet-stream"
1753+
# finally, read the content
1754+
value = value.value
1755+
else:
1756+
# normal String fix the CRLF/CR -> LF stuff
1757+
value = fixNewlines(value)
1758+
1759+
elif isinstance(proptype, hyperdb.Date):
1760+
value = date.Date(value, offset=timezone)
1761+
elif isinstance(proptype, hyperdb.Interval):
1762+
value = date.Interval(value)
1763+
elif isinstance(proptype, hyperdb.Boolean):
1764+
value = value.lower() in ('yes', 'true', 'on', '1')
1765+
elif isinstance(proptype, hyperdb.Number):
1766+
value = float(value)
1767+
except ValueError, msg:
1768+
raise ValueError, _('Error with %s property: %s')%(
1769+
propname, msg)
17651770

17661771
# get the old value
17671772
if nodeid and not nodeid.startswith('-'):

test/test_cgi.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_cgi.py,v 1.13 2003-03-18 00:37:25 richard Exp $
11+
# $Id: test_cgi.py,v 1.14 2003-03-26 06:46:17 richard Exp $
1212

1313
import unittest, os, shutil, errno, sys, difflib, cgi, re
1414

@@ -385,6 +385,9 @@ def testEmptyNumber(self):
385385
({('test', None): {}}, []))
386386
self.assertRaises(ValueError, self.parseForm, {'number': ['', '']})
387387

388+
def testInvalidNumber(self):
389+
self.assertRaises(ValueError, self.parseForm, {'number': 'hi, mum!'})
390+
388391
def testSetNumber(self):
389392
self.assertEqual(self.parseForm({'number': '1'}),
390393
({('test', None): {'number': 1}}, []))
@@ -415,6 +418,9 @@ def testEmptyDate(self):
415418
({('test', None): {}}, []))
416419
self.assertRaises(ValueError, self.parseForm, {'date': ['', '']})
417420

421+
def testInvalidDate(self):
422+
self.assertRaises(ValueError, self.parseForm, {'date': '12'})
423+
418424
def testSetDate(self):
419425
self.assertEqual(self.parseForm({'date': '2003-01-01'}),
420426
({('test', None): {'date': date.Date('2003-01-01')}}, []))

0 commit comments

Comments
 (0)