首页 > 其他 > 详细

django set_signed_cookie 方法理解

时间:2020-02-21 10:34:22      阅读:316      评论:0      收藏:0      [点我收藏+]

djang set_signed_cookie 理解

signed_cookie 只是加了签名的 cookie, 而不是被加密的 cookie.

 

signed_cookie 的作用是防止用户私自纂改.参考: Securing Web Cookies With Signatures

So once I’ve logged in, we set a username cookie containing “Michael Brunton-Spall”, or uid=1 or something.
The problem with this is that the user is in total control of this cookie
单纯的记录 uid 或者用户名在 cookie 中很容易被篡改(也是不建议将用户敏感信息记录在cookie中的原因), 万一攻击者把uid=1换成uid=2岂不是可以访问 uid=2用户的资源了吗? 而如果换成uid=2:1fPjh2:iQGDDYNcgSYkIFqa2ixqakj6-gI那么服务端不仅检验uid, 还检验uid=2后面的签名字段, 即是调用HttpRequest.get_signed_cookie(key=key, salt=salt), 这样即使用户把 cookie 中的 value 换成 uid=2, 但是没有签名, 服务端照样拒绝访问资源.

另外, Django 的 cookie 签名是用的Base64_with_hmac, 参考: Source code for django.core.signing

如果需要在 cookie 里设置被加密的 value, 需要自行对 value 进行加密(好像只能是对称加密), 比如使用hashlib.sha256{参考: hashlib — Secure hashes and message digests}:

 

cookie签名代码:

response.set_signed_cookie("qyUserName", "WANGCONGXING", salt="d8s#wc%0t7")

设置cookie后的效果:

技术分享图片

django set_signed_cookie 方法理解

原文:https://www.cnblogs.com/wangcongxing/p/12340228.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!