10

質問

Bottle - Python Web Frameworkで文字のエスケープを防ぐにはどうすればよいですか?

バックグラウンド

私はボトル(python)で歌の歌詞のウェブアプリを作っています.データベースに挿入する前にすべてのデータが正しいことをテストしているので、今のところ、基本的に「曲名」、「アーティスト」、「歌詞」(テキストエリア内)、それだけです。

フォームが送信されると、上記の 3 つの入力値 (曲、アーティスト、歌詞) を含むページが読み込まれ、すべてが期待どおりに機能していますが、歌詞の html がエスケープされています (歌詞をテンプレートに送信する前に、すべてを に置き換えました\n) <br>。 .

だから私は調査を行い、ボトルピー.orgのこのチュートリアルから 、ボトルがHTMLタグをエスケープしてXSS攻撃を防ぎ、「!」を配置することでこれを無効にできることを発見しました。変数名の前に、AWESOME! 解決策を見つけましたが...使用しようとするとエラーが発生しました:

エラー -コンピュータ上のエラーのスクリーンショット

Exception:

SyntaxError('invalid syntax', ('H:\\Server\\htdocs\\letras\\prueba.tpl', 4, 27, "u'<div>Letra: ', _escape( !letra['letra'] ), u'</div>'])\n"))

Traceback (most recent call last):
    File "H:\Server\htdocs\letras\bottle.py", line 764, in _handle
      return route.call(**args)
    File "H:\Server\htdocs\letras\bottle.py", line 1575, in wrapper
      rv = callback(*a, **ka)
    File "index.py", line 41, in guardar_letra
      return template('prueba.tpl', letra = data)
    File "H:\Server\htdocs\letras\bottle.py", line 3117, in template
      return TEMPLATES[tplid].render(kwargs)
    File "H:\Server\htdocs\letras\bottle.py", line 3090, in render
      self.execute(stdout, kwargs)
    File "H:\Server\htdocs\letras\bottle.py", line 3078, in execute
      eval(self.co, env)
    File "H:\Server\htdocs\letras\bottle.py", line 185, in __get__
      value = obj.__dict__[self.func.__name__] = self.func(obj)
    File "H:\Server\htdocs\letras\bottle.py", line 2977, in co
      return compile(self.code, self.filename or '<string>', 'exec')
    File "H:\Server\htdocs\letras\prueba.tpl", line 4
      u'<div>Letra: ', _escape( !letra['letra'] ), u'</div>'])
                                ^
  SyntaxError: invalid syntax

index.py - github の要点

from bottle import Bottle, route, run, template, static_file, get, post, request, response
from passlib.hash import sha256_crypt
import MySQLdb as mdb
import time
import re

@get('/enviar')
def enviar_letra():
    return template('enviar_letra.tpl')

@post('/enviar')
def guardar_letra():
    titulo = request.forms.get('titulo').capitalize() # Gets the song title from the form
    artista = request.forms.get('artista') # Gets the artist
    letra = request.forms.get('letra') # Gets the lyrics
    fecha_envio = time.strftime('%Y-%m-%d %H:%M:%S') # Date the lyrics were sent
    titulo = re.sub('[^\w|!|\s|\.|,]', '', titulo) # I delete every character except: words, exclamation, spaces, dots, commas
    url = titulo + "-" + artista # concatenate the song's title and the artist name to make a nice url
    url = re.sub('\W+|_', '-', url).lower() # lower all the characters from the url
    url = url.strip("-") # strips "-" at the beginning and the end
    letra = letra.replace("\n", "<br>") # replaces \n from the lyrics text area with <br>
    data = { "titulo": titulo, "artista": artista, "letra": letra, "url": url, "Fecha_envio": fecha_envio } # song dictionary
    return template('prueba.tpl', letra = data) # loads prueba.tpl template and send "data" dictionary as "letra" (letra is lyric in spanish)

run(host='localhost', port=8080, debug=True)

HTML テンプレート - github の要点

<h1>Letra de {{ letra['titulo'] }}</h1>
<h2>Por: {{ letra['artista'] }}</h2>
<div>Fecha: {{ letra['Fecha_envio'] }}</div>
<div>Letra: {{ !letra['letra'] }}</div>

<br>Bottle に歌詞の html をエスケープさせた場合の動作/外観は次のとおりです (プレーンテキストとして表示される方法に注意してください)。

http://i.stack.imgur.com/fxz7o.png

そして最後に、これが ITS SUPPOSED の外観です

http://i.stack.imgur.com/6b58J.png

4

1 に答える 1

18

ボトルを認識するには、ボトルの開口部の直後に感嘆符を配置する必要があります。{{

<div>Letra: {{! letra['letra'] }}</div>

できれば、安全のために、そこにあるスペースを完全に省略したいと考えています。

<div>Letra: {{!letra['letra']}}</div>
于 2013-06-22T09:01:39.643 に答える