Django

Code

Changeset 6680

Show
Ignore:
Timestamp:
11/17/07 06:11:26 (10 months ago)
Author:
mtredinnick
Message:

Fixed #5945 -- Treat string literals in template filter arguments as safe
strings for auto-escaping purposes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/__init__.py

    r6679 r6680  
    595595            for lookup, arg in args: 
    596596                if not lookup: 
    597                     arg_vals.append(arg
     597                    arg_vals.append(mark_safe(arg)
    598598                else: 
    599599                    arg_vals.append(arg.resolve(context)) 
     
    708708            # we're also dealing with a literal. 
    709709            if var[0] in "\"'" and var[0] == var[-1]: 
    710                 self.literal = var[1:-1] 
     710                self.literal = mark_safe(var[1:-1]) 
    711711            else: 
    712712                # Otherwise we'll set self.lookups so that resolve() knows we're 
  • django/trunk/docs/templates.txt

    r6673 r6680  
    401401variables that use the ``escape`` filter do not have further automatic 
    402402escaping applied to them. 
     403 
     404String literals and automatic escaping 
     405-------------------------------------- 
     406 
     407Sometimes you will pass a string literal as an argument to a filter. For 
     408example:: 
     409 
     410    {{ data|default:"This is a string literal." }} 
     411 
     412All string literals are inserted **without** any automatic escaping into the 
     413template, if they are used (it's as if they were all passed through the 
     414``safe`` filter). The reasoning behind this is that the template author is in 
     415control of what goes into the string literal, so they can make sure the text 
     416is correctly escaped when the template is written. 
     417 
     418This means you would write :: 
     419 
     420    {{ data|default:"3 > 2" }} 
     421 
     422...rather than :: 
     423 
     424    {{ data|default:"3 > 2" }}  <-- Bad! Don't do this. 
     425 
     426This doesn't affect what happens to data coming from the variable itself. 
     427The variable's contents are still automatically escaped, if necessary, since 
     428they're beyond the control of the template author. 
    403429 
    404430Using the built-in reference 
  • django/trunk/tests/regressiontests/templates/filters.py

    r6671 r6680  
    178178        'filter-unordered_list05': ('{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}', {"a": ["x>", [["<y", []]]]}, "\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"), 
    179179 
    180         # If the input to "default" filter is marked as safe, then so is the 
    181         # output. However, if the default arg is used, auto-escaping kicks in 
    182         # (if enabled), because we cannot mark the default as safe. 
     180        # Literal string arguments to the default filter are always treated as 
     181        # safe strings, regardless of the auto-escaping state. 
    183182        # 
    184183        # Note: we have to use {"a": ""} here, otherwise the invalid template 
    185184        # variable string interferes with the test result. 
    186         'filter-default01': ('{{ a|default:"x<" }}', {"a": ""}, "x&lt;"), 
     185        'filter-default01': ('{{ a|default:"x<" }}', {"a": ""}, "x<"), 
    187186        'filter-default02': ('{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}', {"a": ""}, "x<"), 
    188187        'filter-default03': ('{{ a|default:"x<" }}', {"a": mark_safe("x>")}, "x>"), 
    189188        'filter-default04': ('{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}', {"a": mark_safe("x>")}, "x>"), 
    190189 
    191         'filter-default_if_none01': ('{{ a|default:"x<" }}', {"a": None}, "x&lt;"), 
     190        'filter-default_if_none01': ('{{ a|default:"x<" }}', {"a": None}, "x<"), 
    192191        'filter-default_if_none02': ('{% autoescape off %}{{ a|default:"x<" }}{% endautoescape %}', {"a": None}, "x<"), 
    193192 
  • django/trunk/tests/regressiontests/templates/tests.py

    r6679 r6680  
    319319            'filter-syntax09': ('{{ var|removetags:"b i"|upper|lower }}', {"var": "<b><i>Yes</i></b>"}, "yes"), 
    320320 
    321             # Escaped string as argument 
     321            # Literal string as argument is always "safe" from auto-escaping.. 
    322322            'filter-syntax10': (r'{{ var|default_if_none:" endquote\" hah" }}', 
    323                     {"var": None}, ' endquote&quot; hah'), 
     323                    {"var": None}, ' endquote" hah'), 
    324324 
    325325            # Variable as argument 
     
    736736 
    737737            # translation of constant strings 
    738             'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'), 
     738            'i18n13': ('{{ _("Password") }}', {'LANGUAGE_CODE': 'de'}, 'Passwort'), 
    739739            'i18n14': ('{% cycle "foo" _("Password") _(\'Password\') as c %} {% cycle c %} {% cycle c %}', {'LANGUAGE_CODE': 'de'}, 'foo Passwort Passwort'), 
    740740            'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 
     741            'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 
    741742 
    742743            ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 
     
    886887            'autoescape-tag07': ("{% autoescape on %}{{ first }}{% endautoescape %}", {"first": mark_safe(u"<b>Apple</b>")}, u"<b>Apple</b>"), 
    887888 
    888             # String arguments to filters, if used in the result, are escaped, 
    889             # too
    890             'basic-syntax08': (r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}', {"var": None}, ' endquote&quot; hah'), 
     889            # Literal string arguments to filters, if used in the result, are 
     890            # safe
     891            'basic-syntax08': (r'{% autoescape on %}{{ var|default_if_none:" endquote\" hah" }}{% endautoescape %}', {"var": None}, ' endquote" hah'), 
    891892 
    892893            # The "safe" and "escape" filters cannot work due to internal