Re: [Python-de] bottle-template: syntax error
Marcel Hellkamp wrote:
On 03.09.2013 14:52, Christopher Arndt wrote:
On 03.09.2013 09:24, Michael Ströder wrote:
Peter Otten wrote:
(und bottle verwendet eval() für templates): Ist eval() speziell in Web-Anwendungen nicht ziemlich unangebracht - insbesondere beim Rendering von Web-Seiten, welche wiederrum Benutzereingaben enthalten können? Es wäre für mich auf jeden Fall ein weiterer Grund (neben anderen Erwägungen, wie mehr Features etc.), gleich eine zusätzliche Template-Engine, wie z.B. Jinja2, mit bottle zu verwenden.
Um das mal klar zu stellen:
(Fast) jede Template Engine übersetzt Template Code in Python Code und jagt den durch eval(). Auch Jinja2, Mako, Cheeta und Andere. Das ist die einzige möglichkeit, einigermaßen schnelle Templates zu bekommen. [..] Die Benutzer-Eingaben, die in ein Template hinein gerendert sind, sind etwas völlig anderes. Die werden nicht ausgeführt oder durch eval() gejagt. Das ist bei keiner Template Engine so, das wäre ja auch reichlich blöd.
Nun ja, kommt halt drauf an, ob die Erkennung der Code-Grenzen in den Templates immer richtig funktioniert. Ciao, Michael.
On 03.09.2013 21:14, Michael Ströder wrote:
Marcel Hellkamp wrote:
Die Benutzer-Eingaben, die in ein Template hinein gerendert sind, sind etwas völlig anderes. Die werden nicht ausgeführt oder durch eval() gejagt. Das ist bei keiner Template Engine so, das wäre ja auch reichlich blöd.
Nun ja, kommt halt drauf an, ob die Erkennung der Code-Grenzen in den Templates immer richtig funktioniert.
Nein, darauf kommt es eben nicht an. Benutzereinhaben (Template Variablen) werden niemals ausgeführt oder als Code interpretiert. Ich versuche es mal mit einem Beispiel: Ein Template:: Die Variable 'var' enthält: {{var}} Wird (einmalig) in folgenden Python Code übersetzt (stark vereinfacht): return u'Die Variable \'var\' enthält: ' + escape_html(unicode(var)) + u'\r\n' Dieser Schnipsel wird mit compile() in ein Code Object übersetzt, das man mit eval ausführen kann. Jerdes mal, wenn du ein Template renders, passiert zum Beispiel das hier: eval(compiled_template, {'var': bottle.request.forms.field_name}) Der Inhalt von 'bottle.request.forms.field_name' steht also in der variable 'var' wenn das template ausgeführt wird. var ist ein String, wird als String verarbeitet und kann keinen Schaden an richten. Ganz egal was in 'var' steht, das "rendern" eines Templates ist sicher, solange im Template selbst kein Schadcode steht.
participants (2)
-
Marcel Hellkamp
-
Michael Ströder