python - Flask / Jinja2 - best pratice of HTML escaping -



python - Flask / Jinja2 - best pratice of HTML escaping -

i'm using flask build webapplication database in backend. info need escape ensure no sitemanipulations xss?

the jinja2 template engine provides '|safe' filter ensure such escaping. comments , other stuff, user can edit (and perchance manipulate), seeems logically escape, other content vulnerable too? filters, extenions or tricks should used in context of flask , jinja2?

you want enable automatic filtering using autoescape extension. the docs:

env = environment(autoescape=guess_autoescape, loader=packageloader('mypackage'), extensions=['jinja2.ext.autoescape'])

this has some performance overhead, experience shows very easy forget escape 1 variable, gives xss (even big sites, ebay, have fallen victim this).

this answers question, 'what should escaped?'. in larger applications, it's not easy determine variables can straight (or indirectly!) influenced users. in addition, escaping not security feature, since fixed string such this <test> & string needs escaping < , &.

you can still print html safe filter, ie. my_string|safe.

edit: reply questions:

would overkill escape userstring or similar things?

what if username <script>alert('boo!')</script>? or if &fancy&? suppose around disallowing characters during registration, sure there's no way circumvent this? if fill in \x26 in username? or if can around check other way? if (in future) want allow such characters, or in future connect external login service (facebook, google, github) allows such characters?

yes, there (small) performance overhead, it's more secure, , it's easier program. whole point of environments python & jinja optimize programmer productivity @ expense of performance :)

some webapplications database administrated tools phmyadmin (which folks forget remove or protect). scenario hacker manipulate info within database without touching application itself. worst case , harm reduced escaping (see 1.)

yes. of disaster. however, if attacker gains total access database he/she doesn't need bother xss attacks, since info @ his/her fingertips. however, there less malicious way things can go wrong, example, administrator manually created (or edits) user character needs escaping.

python html flask escaping jinja2

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -