Skip to content

Commit 86b6a56

Browse files
committed
Validate values for Integer and Numeric type filter parameters rather than
passing output down to db level. Initial patch at: http://hg.python.org/tracker/roundup/rev/98508a47c126 by Martin.V.Loewis. Numeric test patch applied, Integer code and tests developed by John Rouillard.
1 parent a2bf952 commit 86b6a56

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ Fixed:
143143
Applied by John Rouillard with some modification to properly
144144
identify if the bad property is a sort or grouping property. Tests
145145
added.
146+
- Validate Integer and Numeric type filter parameters rather than
147+
passing output down to db level. Initial patch at:
148+
http://hg.python.org/tracker/roundup/rev/98508a47c126 by
149+
Martin.V.Loewis. Numeric test patch applied, Integer code and tests
150+
developed by John Rouillard.
146151

147152
2016-01-11: 1.5.1
148153

roundup/cgi/actions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ def fakeFilterVars(self):
247247
# replace the single value with the split list
248248
for v in l:
249249
self.form.value.append(cgi.MiniFieldStorage(key, v))
250+
elif isinstance(prop, hyperdb.Number):
251+
try:
252+
float(self.form[key].value)
253+
except ValueError:
254+
raise exceptions.FormError, "Invalid number: "+self.form[key].value
255+
elif isinstance(prop, hyperdb.Integer):
256+
try:
257+
val=self.form[key].value
258+
if ( str(int(val)) == val ):
259+
pass
260+
else:
261+
raise ValueError
262+
except ValueError:
263+
raise exceptions.FormError, "Invalid integer: "+val
250264

251265
self.form.value.append(cgi.MiniFieldStorage('@filter', key))
252266

test/test_actions.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from roundup.date import Date, Interval
66
from roundup.cgi.actions import *
77
from roundup.cgi.client import add_message
8-
from roundup.cgi.exceptions import Redirect, Unauthorised, SeriousError
8+
from roundup.cgi.exceptions import Redirect, Unauthorised, SeriousError, FormError
99

1010
from mocknull import MockNull
1111

@@ -135,6 +135,34 @@ def testStringKey(self):
135135
self.form.value.append(MiniFieldStorage('foo', 'hello'))
136136
self.assertFilterEquals('foo')
137137

138+
def testNumKey(self): # testing patch: http://hg.python.org/tracker/roundup/rev/98508a47c126
139+
for val in [ "-1000a", "test", "o0.9999", "o0", "1.00/10" ]:
140+
print "testing ", val
141+
self.client.db.classes.get_transitive_prop = lambda x: hyperdb.Number()
142+
self.form.value.append(MiniFieldStorage('foo', val)) # invalid numbers
143+
self.assertRaises(FormError, self.action.fakeFilterVars)
144+
del self.form.value[:]
145+
146+
for val in [ "-1000.7738", "-556", "-0.9999", "-.456", "-5E-5", "0.00", "0",
147+
"1.00", "0556", "7.56E2", "1000.7738"]:
148+
self.form.value.append(MiniFieldStorage('foo', val))
149+
self.action.fakeFilterVars() # this should run and return. No errors, nothing to check.
150+
del self.form.value[:]
151+
152+
def testIntKey(self): # testing patch: http://hg.python.org/tracker/roundup/rev/98508a47c126
153+
for val in [ "-1000a", "test", "-5E-5", "0.9999", "0.0", "1.000", "0456", "1E4" ]:
154+
print "testing ", val
155+
self.client.db.classes.get_transitive_prop = lambda x: hyperdb.Integer()
156+
self.form.value.append(MiniFieldStorage('foo', val))
157+
self.assertRaises(FormError, self.action.fakeFilterVars)
158+
del self.form.value[:]
159+
160+
for val in [ "-1000", "-512", "0", "1", "100", "248" ]: # no scientific notation apparently
161+
self.client.db.classes.get_transitive_prop = lambda x: hyperdb.Integer()
162+
self.form.value.append(MiniFieldStorage('foo', val))
163+
self.action.fakeFilterVars() # this should run and return. No errors, nothing to check.
164+
del self.form.value[:]
165+
138166
def testTokenizedStringKey(self):
139167
self.client.db.classes.get_transitive_prop = lambda x: hyperdb.String()
140168
self.form.value.append(MiniFieldStorage('foo', 'hello world'))

0 commit comments

Comments
 (0)