2828 json_writer ,
2929 xml_writer ,
3030)
31+ # (canada fork only): filename to save stream to
32+ import re
33+
34+
35+ # (canada fork only): filename to save stream to
36+ FILENAME_MATCH = re .compile ('^[\w\-\.]+$' )
3137
3238int_validator = get_validator (u'int_validator' )
3339boolean_validator = get_validator (u'boolean_validator' )
@@ -75,6 +81,18 @@ def exclude_id_from_ds_dump(key, data, errors, context):
7581 data [key ] = value
7682
7783
84+ # (canada fork only): filename to save stream to
85+ def filename_safe (key , data , errors , context ):
86+ """
87+ Makes sure the passed filename is safe to stream back in the response.
88+ """
89+ value = data .get (key )
90+
91+ if not re .search (FILENAME_MATCH , value ):
92+ errors [key ].append (_ ('Invalid characters in filename' ))
93+ raise StopOnError
94+
95+
7896def dump_schema () -> Schema :
7997 return {
8098 u'offset' : [default (0 ), int_validator ],
@@ -88,7 +106,7 @@ def dump_schema() -> Schema:
88106 u'language' : [ignore_missing , unicode_only ],
89107 u'fields' : [exclude_id_from_ds_dump , ignore_missing , list_of_strings_or_string ], # (canada fork only): exclude _id field from Blueprint dump
90108 u'sort' : [default (u'_id' ), list_of_strings_or_string ],
91- 'filename' : [ignore_missing , unicode_only ] # (canada fork only): filename to save stream to
109+ 'filename' : [ignore_missing , unicode_only , filename_safe ] # (canada fork only): filename to save stream to
92110 }
93111
94112
0 commit comments