私のエスケープ(html)

投稿の目的は批判することです。



ブログエンジンを作成して、Djangoを少し学びます。 ユーザーコンテンツの表示に問題がありました。 そのシールド。 私の機能を書きました。 ユーザーデータのスクリーニングのさまざまなイデオロギーを持つさまざまなサイトのコンテンツを作成した経験があります(どちらかと言えば否定的で、ほとんどのサイトは追加の作業を要求されます)。



発言。 私のブログでは、データベースは常に未処理の元のユーザー入力を保存します。 関数のタスクは、この入力からの表示にHTMLを適切にすることです。 最も単純なケースでは、コンテンツが(テンプレートまたはビューで)表示されている間に関数を呼び出すことができます。 パフォーマンスを向上させるために、データベースに別のフィールドを作成できます。これは、シールド機能の結果であり、コンテンツの更新中に更新されます。



したがって、機能の哲学:





それはユーザーにとって非常に簡単です-あなたが望むものを書くと、許可されたタグを除いてすべてがエスケープされます。



マークアップ、bbcode、マークダウンの準備ができていない理由:



かさばるhtmlタグに展開する独自のコンパクトなタグを作成することをお勧めしますが。 ほとんどの場合、ユーザー入力ごとにフィルターを割り当てることができるようにします。



テストから取られたいくつかの例はここにあります:

# New line test

text: 'New' + os.linesep + os.linesep + 'line'

html(text): 'New' + '<br/>' + os.linesep + '<br/>' + os.linesep + 'line')



# Simple smile test

text: ':)'

html(text): '<img src="/files/smile.gif"/>')



# Smile in code block test

text: '<pre><code>there will be no image :)</code></pre>'

html(text): '<pre><code>there will be no image :)</code></pre>')



# Plain text test

text: 'Some simple text without any tags'

html(text): 'Some simple text without any tags')



# Simple tags test

text: 'This is <b>important</b> information'

html(text): 'This is <b>important</b> information')



# Wrong tag test

text: 'This is <b>important</b> information and this is <h5>title</h5>'

html(text): 'This is <b>important</b> information and this is &lt;h5&gt;title&lt;/h5&gt;')



# Wrong attributes test

text: 'This is <b onclick="javascript:alert();">probable XSS</b>'

html(text): 'This is <b>probable XSS</b>')



# Simple tags test

text: 'This is <img src="image.jpg" onclick="javascript:alert();"/>image'

html(text): 'This is <img src="image.jpg"/>image')



# Text without tags

text: 'Sample code: <?php print "Hello World"; ?>'

html(text): 'Sample code: &lt;?php print &quot;Hello World&quot;; ?&gt;')



# Code test

text: 'Sample code: <pre><code><a href="home.html">link</a></code></pre>'

html(text): 'Sample code: <pre><code>&lt;a href=&quot;home.html&quot;&gt;link&lt;/a&gt;</code></pre>')



# Closing tag without opening tag

text: '</b>bold text ended'

html(text): '&lt;/b&gt;bold text ended')



# Autoclosing tags test

text: '<b>bold and <i>italic</i> text'

html(text): '<b>bold and <i>italic</i> text</b>')



# Overlapping areas test

text: 'if 1 < 2 <b>only</b> then it is ok, but if 1 < 2 or 2 < 3 then <i>bad</i>'

html(text): 'if 1 &lt; 2 <b>only</b> then it is ok, but if 1 &lt; 2 or 2 &lt; 3 then <i>bad</i>')








残念ながら、機能を表示できるサーバーはありません。 http://pastebin.com/f591d71e5にあります。



批判をお願いします、ありがとう。



All Articles