php - How to Secure Data Submitted Through CKEditor -
i using ckeditor in site let users post comments. ckeditor has many buttons compose comment. suppose if user makes comment bold , italic such like
this comment
and ckeditor ouput following html
<i><strong>this comment</strong></i> now, if store html in mysql database , output on webpage is, without wrapping htmlspecialchars(), comment shown on page bold , italic , want.
but on other hand if wrap comment htmlspecialchars() , displays on webpage shown
<i><strong>this comment</strong></i> but not want show this, want user formatting. if not wrap htmlspecialchars(), risky , can cause xss attack , other security risks.
how can achieve both purposes (1). keep user formatting (2). secure html contents
you need draw whitelist of elements , attributes want allow users include (eg allow <strong> not <script>; allow <a href> not <div onmouseover>), , enforce parsing input, removing elements , attributes don't fit pattern, , serialising results html.
this hard job cannot done few simple regexes or strip_tags (which not adequate solution xss if did fit needs). advised use existing library - html purifier 1 such php.
Comments
Post a Comment