urlencode と、
基本的に、
django/html.py at master · django/django
django/defaultfilters.py at master · django/django
前提
以下の
OS
% sw_vers ProductName: Mac OS X ProductVersion: 10.12.6 BuildVersion: 16G29
python の
verion % python -V Python 2.7.10
django の
version % pip list | grep Django Django (1.10.7)
参考
django の デフォルトの 動作に ついて
基本的に
発動を{% autoescape off %}
をsafe
フィルタ を
動作に
【
escape に ついて
HTML で
基本的に、
{% autoescape off %} ........... {% endautoescape %}
{{ blog_post.title|escape}}
と&
を
from __future__ import print_function from django.utils import html print(html.escape("&"))
エスケープされ、
&
>
>
を
from __future__ import print_function from django.utils import html print(html.escape(">"))
エスケープされ
>
HTML の<
>
'
"
&
のみを
from __future__ import print_function from django.utils import html print(html.escape("<>'\"&"))
<>'"&
<bean:write >
タグのStruts リファレンス<bean:write >
HTML タグの
それだけ
ちなみに、
追加の
wicket/Strings.java at master · apache/wicket のescapeMarkup
メソッドが
django のescape
メソッド自体の
HTML でmark_safe
を
ただ、escape
をescape
しないconditional_escape
を
- escape メソッド
_html_escapes = { ord('&'): '&', ord('<'): '<', ord('>'): '>', ord('"'): '"', ord("'"): ''', } @keep_lazy(str, SafeText) def escape(text): """ Return the given text with ampersands, quotes and angle brackets encoded for use in HTML. Always escape input, even if it's already escaped and marked as such. This may result in double-escaping. If this is a concern, use conditional_escape() instead. """ return mark_safe(str(text).translate(_html_escapes))
conditional_escape に ついて
ドキュメント
escape とは
違いは、しない。<wbr>
扱う値が
escapejsに ついて
template 内に
その
ダブルクォート、
- escapejsの
対象文字列の 抜粋 _js_escapes = { ord('\\'): '\\u005C', ord('\''): '\\u0027', ord('"'): '\\u0022', ord('>'): '\\u003E', ord('<'): '\\u003C', ord('&'): '\\u0026', ord('='): '\\u003D', ord('-'): '\\u002D', ord(';'): '\\u003B', ord('`'): '\\u0060', ord('\u2028'): '\\u2028', ord('\u2029'): '\\u2029' } # Escape every ASCII character with a value less than 32. _js_escapes.update((ord('%c' % z), '\\u%04X' % z) for z in range(32))
実際のtranslate
メソッドの
* escapejs
@keep_lazy(str, SafeText) def escapejs(value): """Hex encode characters for use in JavaScript strings.""" return mark_safe(str(value).translate(_js_escapes))
translate
をPythonでの
また、escape
はescapejs
は、
発動想定箇所の
参考記事への
JavaScriptでの
escape、 escapejs 後に、 autoescape は 発動しない。
Built-in template tags and filters | Django documentation | Django
に
urlencodeに ついて
内部実装は
urllib.parse.quote
を
- urlencodeメソッド
@register.filter(is_safe=False) @stringfilter def urlencode(value, safe=None): """ Escape a value for use in a URL. The ``safe`` parameter determines the characters which should not be escaped by Python's quote() function. If not provided, use the default safe characters (but an empty string can be provided when *all* characters should be escaped). """ kwargs = {} if safe is not None: kwargs['safe'] = safe return quote(value, **kwargs)
urlencode の
urlencode 処理はmark_safe
していないので、<
等が
セキュリティ観点で
まとめ
一言、
SQL 等のmark_safe
と
以上です。
コメント