I'm working on a web application that allows users to type short descriptions of items in a catalog. I'm allowing Markdown in my textareas so users can do some HTML formatting.

My text sanitization function strips all tags from any inputted text before inserting it in the database:

public function sanitizeText($string, $allowedTags = "") {

$string = strip_tags($string, $allowedTags);

if(get_magic_quotes_gpc()) {

return mysql_real_escape_string(stripslashes($string));

} else {

return mysql_real_escape_string($string);

Essentially, all I'm storing in the database is Markdown--no other HTML, even "basic HTML" (like here at SO) is allowed.

Will allowing markdown present any security threats? Can markdown be XSSed, even though it has no tags?

I think stripping any HTML tag from the input will get you something pretty secure -- except if someone find a way to inject some really messed up data into Markdown, having it generate some even more messed-up output ^^

Still, here are two things that come to my mind :

First one : strip_tags is not a miracle function : it has some flaws...

For instance, it'll strip everything after the '

$str = "10 appels is

var_dump(strip_tags($str));

The output I get is :

string '10 appels is ' (length=13)

Which is not that nice for your users :-(

Second one : One day or another, you might want to allow some HTML tags/attributes ; or, even today, you might want to be sure that Markdown doesn't generate some HTML Tags/attributes.

You might be interested by something like HTMLPurifier : it allows you to specify which tags and attributes should be kept, and filters a string, so that only those remain.

It also generates valid HTML code -- which is always nice ;-)

I'm working on a web application that allows users to type short descriptions of items in a catalog. I'm allowing Markdown in my textareas so users can do some HTML formatting.My text sanitization fun... 这种跨站脚本可以执行任何形式的 攻击 ,其 攻击 原理是:程序+数据=结果 当我们的数据中包含一部分程序的时候,原来我们程序的逻辑就会被改变了 页面中的脚本可以干什么, XSS 的脚本就可以看什么,比如 获取页面数据 获取Cookie 劫持前端逻辑 偷取网站任意数据资料,密码,登录态 可见 XSS 是一种非常危险的前端 攻击 手段
XSS 注入的本质就是: 某网页中根据用户的输入, 不期待地生成了可执行的js代码, 并且js得到了浏览器的执行. 意思是说, 发给浏览器的字符串中, 包含了一段非法的js代码, 而这段代码跟用户的输入有关. 常见的 XSS 注入防护, 可以通过简单的 htmlspecialchars(转义HTML特殊字符), strip_tags (清除HTML标签) 来解决, 但是, 还有一些隐蔽的 XSS 注入不
1,flask中内置的过滤器模板中常用方法: {#过滤器调用方式{{变量|过滤器名称}} #} <!-- safe过滤器,可以禁用转义 --> {{'<strong>hello</strong>'|safe}} ... {{' apple '| striptags }}
Django已经内置了一些安全性保护机制,可以帮助开发者预防 XSS 攻击 。以下是一些防范 XSS 攻击 的措施: 1. 对用户输入进行过滤和转义。Django提供了多种过滤和转义函数,如escape、escapejs、 striptags 等,可以有效地防范 XSS 攻击 。 2. 使用Django的模板引擎。Django的模板引擎可以自动对HTML进行转义,避免了手动转义的繁琐和出错的可能性。 3. 设置CSP(Content Security Policy)。CSP可以指定网站只能从哪些来源加载资源,从而限制 XSS 攻击 的范围。 4. 使用HTTPS。HTTPS可以加密用户和服务器之间的通信,防止中间人 攻击 和劫持。 5. 及时更新Django版本。Django的更新版本通常都会修复已知的安全漏洞,及时更新可以避免被已知的 攻击 手段 攻击 。 以上是一些防范 XSS 攻击 的措施,但是并不能保证100%防范 XSS 攻击 。因此,在编写代码时需要时刻保持警惕,对用户输入进行充分的验证和过滤。