用flask或django还需要防sql注入吗

2012-09-21 20:17:11 +08:00
 loading
记得flask文档说我们可以专注于开发,安全问题不怎么需要担心?
用peewee或者其他orm就不用管这个问题了吧,如果我直接g.db.execu呢
11237 次点击
所在节点    Python
8 条回复
fanzheng
2012-09-21 20:24:40 +08:00
http://flask.pocoo.org/docs/patterns/sqlite3/#easy-querying

To pass variable parts to the SQL statement, use a question mark in the statement and pass in the arguments as a list. Never directly add them to the SQL statement with string formatting because this makes it possible to attack the application using SQL Injections.
Brutal
2012-09-21 20:32:30 +08:00
我现在在犹豫要不要用ORM。。。
loading
2012-09-21 21:02:14 +08:00
@fanzheng 下面这样就不会被注入了么?
user = query_db('select * from users where username = ?',
[the_username], one=True)

请问怎么写的才是能被注入的,上面的我看起来像可以被注入的。。。
例如the_username=";delete * from users"
fanzheng
2012-09-21 21:23:45 +08:00
@loading 我对这个也不怎么熟的。。他说用?用列表不会,

但是 the_username=";delete * from users" 因为没有参数肯定也不能被注入。
fanzheng
2012-09-21 21:26:39 +08:00
他说不要这样
user = query_db('select * from users where username = %s' % 请求的参数)

因为%s 里面可以另外构造一个SQL语句。
loading
2012-09-21 21:32:44 +08:00
@fanzheng 了然,感谢:-)
gamexg
2012-09-22 09:10:07 +08:00
user = query_db('select * from users where username = ?',
[the_username], one=True)

这里系统会自动对危险字符进行转义。一般将客户端输入的内容作为 query_db 之类函数参数进行提交的都不用担心注入,而自己通过 %s 之类的构建查询字符串就要小心了。
stackpop
2012-09-22 17:43:53 +08:00
我用django是自己实现了一个简单的db类

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/48423

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX