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
1989922yan
V2EX  ›  Python

sqlalchemy 或者 底层 mysqldb 如何防止 sql 注入??

  •  
  •   1989922yan · 2014-12-29 15:40:35 +08:00 · 8075 次点击
    这是一个创建于 3403 天前的主题,其中的信息可能已经有所发展或是发生改变。
    12 条回复    2015-01-06 15:50:53 +08:00
    janxin
        1
    janxin  
       2014-12-29 16:26:52 +08:00
    MySQLdb:

    不要直接拼装SQL语句,比如:
    cmd = "select tab where id='%s'" % id
    curs.execute(cmd)

    如果要拼装,参数一定要用MySQLdb.escape_string()过一下。当然,下面的写法更省事:

    cmd = "select people set name=%s where id=%s"
    curs.execute(cmd, (name, id))

    sqlalchemy没用过,不清楚了,应该也是默认过滤的,只要你不直接传SQL进来
    janxin
        2
    janxin  
       2014-12-29 16:27:56 +08:00
    第二个SQL语句不对:
    cmd = "select tab where id='%s'"
    curs.execute(cmd, (id,))
    010blue
        3
    010blue  
       2014-12-29 16:34:57 +08:00   ❤️ 1
    底层 mysqldb防注入这个想法挺前卫的,可以在mysql数据库之前做一层mysql-proxy,通过这个来对接收的sql语句做一次检查过滤,当然还能顺便把读写给做了;如果再底层到mysql服务器的话,估计要写存储过程了,这个也是要改程序的
    wingyiu
        4
    wingyiu  
       2014-12-29 17:12:20 +08:00
    @janxin `cmd = "select tab where id='%s'"` => `cmd = "select tab where id=%s"`
    MasterYoda
        5
    MasterYoda  
       2014-12-30 10:26:10 +08:00
    Prestatement会好一些。
    1989922yan
        6
    1989922yan  
    OP
       2014-12-30 10:56:44 +08:00
    @janxin
    完美答案。谢谢。
    1989922yan
        7
    1989922yan  
    OP
       2014-12-30 10:58:09 +08:00
    @010blue
    跟数据库交互的api,还是有很多需要注意的,还是需要多学习
    1989922yan
        8
    1989922yan  
    OP
       2014-12-30 11:14:42 +08:00
    @MasterYoda
    刚刚查了查,了解了,java中有的概念,一直python感觉可以学习java很多框架经验啊
    MasterYoda
        9
    MasterYoda  
       2014-12-30 13:13:41 +08:00   ❤️ 1
    @1989922yan
    倒不算Java概念,可以叫绑定参数还是什么的。
    MySQL本身支持而已,Prepared Statement http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
    各个语言的库基本都支持。
    1989922yan
        10
    1989922yan  
    OP
       2014-12-30 14:26:48 +08:00
    @MasterYoda
    新技能。
    我先看看。
    THX:)
    010blue
        11
    010blue  
       2015-01-06 14:08:40 +08:00
    @1989922yan 对,解决安全问题的最佳方案还是在架构层面上,单纯依靠程序员的经验是容易出问题的
    1989922yan
        12
    1989922yan  
    OP
       2015-01-06 15:50:53 +08:00
    @010blue

    扯大了。
    只是说说sql注入,防范方法也就是那些。
    架构也是程序员写的,架构师也都是写积累的。
    大家接触的项目,真的跟12306一样大吗?
    能出了问题让解决了才开心。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3667 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:09 · PVG 08:09 · LAX 17:09 · JFK 20:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.