-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reg_scrub.py, to cleanse user input prior to use. Mostly checking for invalid characters in the values, and malformed variable names right now. Rearrange a touch in preparation for next usage check (del being the only action if requested).
- Loading branch information
1 parent
6e30fe2
commit c0a79de
Showing
3 changed files
with
53 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import html | ||
import member | ||
import re | ||
|
||
_allowed_fields = member.ordered_field_names() | ||
_allowed_fields.extend(('del', 'mod', 'reg')) | ||
|
||
def sanitize(web_form): | ||
'''Take a cgi.FieldStorage object and scrub it down to sane values. | ||
returns a dict with the cgi vars as keys and their html.escape()ed | ||
values as the values.''' | ||
|
||
# TODO: collect all vars for a given reqid - if the full set's vars | ||
# don't pass muster, drop that reqid entirely. | ||
clean_vars = {} | ||
for var in web_form: | ||
if name_ok(var): | ||
if val_ok(web_form[var].value): | ||
clean_vars[var] = html.escape(web_form[var].value) | ||
return clean_vars | ||
|
||
def name_ok(name): | ||
'''Check that this is a legal name for a variable in the form. | ||
The name part must be one of the fields from above, and the reqid | ||
must consist of only digits.''' | ||
varparts = name.split('_') | ||
if len(varparts) == 2: | ||
if varparts[0] in _allowed_fields: | ||
if not re.search(r'\D', varparts[1]): | ||
return True | ||
return False | ||
|
||
def val_ok(string): | ||
'''Check that this is a valid value from the form.''' | ||
|
||
# If any character besides those listed in the character class below | ||
# are in the string, then we call shenanigans. Otherwise, we'll say | ||
# it's legal. | ||
if not re.search(r'[^-a-zA-Z0-9_.@# \']', string): | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
briangerard
Author
Contributor
|
||
return True | ||
return False |
I'm pretty sure Cristóbal (and likely others) would have an issue with this.