Changes between Initial Version and Version 2 of Ticket #14976
- Timestamp:
- Dec 27, 2010, 9:02:01 PM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #14976
- Property Triage Stage Unreviewed → Accepted
- Property Summary Add is_safe flag to contrib.messages → Add is_html flag to contrib.messages
-
Ticket #14976 – Description
initial v2 1 I would like to have add a message.is_safe flag to the Message model of the contrib.messages app.[[BR]] 2 [[BR]] 3 The flag would be set to False by default and could be explicitly overridden for trusted messages. There are times when it would be helpful to the end user to include an html link in a message ("Welcome, click here to create a profile", "You've sent 25 points to user_b, click here to see your balance," etc.), and with the current message system there is not a good way to do this.[[BR]] 4 [[BR]] 1 I would like to have add a message.is_html flag to the Message model of the contrib.messages app. 5 2 6 Adding the is_safe flag would require a minor set of backward compatible changes:[[BR]] 3 The flag would be set to False by default and could be explicitly overridden for messages that are HTML. There are times when it would be helpful to the end user to include an html link in a message ("Welcome, click here to create a profile", "You've sent 25 points to user_b, click here to see your balance," etc.), and with the current message system there is not a good way to do this. 4 5 Adding the is_html flag would require a minor set of backward compatible changes: 7 6 8 7 {{{ 9 8 def success(request, message, extra_tags='', fail_silently=False): 10 9 to 11 def success(request, message, extra_tags='', fail_silently=False, is_ safe=False):10 def success(request, message, extra_tags='', fail_silently=False, is_html=False): 12 11 13 12 def add_message(request, level, message, extra_tags='', fail_silently=False): 14 13 to 15 def add_message(request, level, message, extra_tags='', fail_silently=False, is_ safe=False):14 def add_message(request, level, message, extra_tags='', fail_silently=False, is_html=False): 16 15 17 16 def __init__(self, level, message, extra_tags=None): 18 17 to 19 def __init__(self, level, message, extra_tags=None, is_ safe=False):18 def __init__(self, level, message, extra_tags=None, is_html=False): 20 19 21 20 #add to __init__ 22 self.is_ safe = is_safe21 self.is_html = is_html 23 22 }}} 24 23 25 Then in the template: {% if message.is_safe %} {{ message|safe }}{% else %}{{ message }}{% endif %}.[[BR]] 26 [[BR]] 24 Then in the template: 25 {{{ 26 {% if message.is_html %}{{ message|safe }}{% else %}{{ message }}{% endif %}. 27 }}} 27 28 28 29 29 Alternative ways to do this:[[BR]] 30 1. Run all messages through the safe filter[[BR]] 31 This would require a code-wide policy of "make sure you escape anything in a message that might have user input" such as if my message is "your post %s is now published" % blog.post or "%s has sent you the message %s" %(user, message.content). I would then have to worry about every variable I use in a message string, if it could contain script, and if it is already escaped (or escape everything again). I would also have to worry if everyone else working on the codebase is doing this correctly. [[BR]] 30 Alternative ways to do this: 32 31 33 2.Use a tag[[BR]] 34 I could have a policy of adding "safe" to the tags I want to run through the safe filter, but this is also fraught with downsides. Since all tags get output into html, the safe flag would end up output to the end user. The template logic is less clear and error prone {{ "test" in message.extra_tags }} would work, but would return a false positive if you tried to use "contest" as a tag. Granted "contest" as a message tag is a rare case; it is just another layer of messiness of security code mashed in with markup.[[BR]] 35 [[BR]] 32 33 1. Run all messages through the safe filter[[BR]][[BR]] 34 This would require a code-wide policy of "make sure you escape anything in a message that might have user input" such as if my message is "your post %s is now published" % blog.post or "%s has sent you the message %s" %(user, message.content). I would then have to worry about every variable I use in a message string, if it could contain script, and if it is already escaped (or escape everything again). I would also have to worry if everyone else working on the codebase is doing this correctly. 35 36 2. Use a tag[[BR]][[BR]] 37 I could have a policy of adding "html" to the tags I want to run through the safe filter, but this is also fraught with downsides. Since all tags get output into html, the safe flag would end up output to the end user. The template logic is less clear and error prone. 38 36 39 If this isn't violating a core django design precept, I'll get started on a patch in the next few days.