V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
loading
V2EX  ›  Python

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

  •  
  •   loading ·
    ycf · 2012-09-21 20:17:11 +08:00 · 11231 次点击
    这是一个创建于 4233 天前的主题,其中的信息可能已经有所发展或是发生改变。
    记得flask文档说我们可以专注于开发,安全问题不怎么需要担心?
    用peewee或者其他orm就不用管这个问题了吧,如果我直接g.db.execu呢
    8 条回复    1970-01-01 08:00:00 +08:00
    fanzheng
        1
    fanzheng  
       2012-09-21 20:24:40 +08:00   ❤️ 1
    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
        2
    Brutal  
       2012-09-21 20:32:30 +08:00
    我现在在犹豫要不要用ORM。。。
    loading
        3
    loading  
    OP
       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
        4
    fanzheng  
       2012-09-21 21:23:45 +08:00
    @loading 我对这个也不怎么熟的。。他说用?用列表不会,

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

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

    这里系统会自动对危险字符进行转义。一般将客户端输入的内容作为 query_db 之类函数参数进行提交的都不用担心注入,而自己通过 %s 之类的构建查询字符串就要小心了。
    stackpop
        8
    stackpop  
       2012-09-22 17:43:53 +08:00
    我用django是自己实现了一个简单的db类
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5244 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:16 · PVG 09:16 · LAX 18:16 · JFK 21:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.