@@ -20,8 +20,8 @@ def __init__(self, template: Template) -> None:
2020 converted = format (converted , format_spec )
2121 params .append (converted )
2222 items .append ("?" )
23- object .__setattr__ (self , "statement" , "" .join (items ))
24- object .__setattr__ (self , "params" , params )
23+ super () .__setattr__ ("statement" , "" .join (items ))
24+ super () .__setattr__ ("params" , params )
2525
2626
2727def find_users_query_v1 (name : str ) -> str :
@@ -48,6 +48,17 @@ def render(template: Template) -> str:
4848 )
4949
5050
51+ def safer_render (template : Template ) -> str :
52+ items = []
53+ for item in template :
54+ if isinstance (item , str ):
55+ items .append (item )
56+ else :
57+ sanitized = str (item .value ).replace ("'" , "''" )
58+ items .append (sanitized )
59+ return "" .join (items )
60+
61+
5162if __name__ == "__main__" :
5263 # Insecure f-strings
5364 print (find_users_query_v1 ("' OR '1'='1" ))
@@ -60,5 +71,8 @@ def render(template: Template) -> str:
6071 # # Insecure way of rendering t-strings into plain strings
6172 # print(render(find_users_query_v2("' OR '1'='1")))
6273 #
74+ # # More secure way of rendering t-strings
75+ # print(safer_render(find_users_query_v2("' OR '1'='1")))
76+ #
6377 # # Rendering t-strings into an alternative representation
6478 # print(find_users("' OR '1'='1"))
0 commit comments