如何在Python的Flask中正确安全地处理Cookie和会话? [英] How to properly and securely handle cookies and sessions in Python's Flask?

查看:83
本文介绍了如何在Python的Flask中正确安全地处理Cookie和会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在向用户浏览器中保存当前存储有会话ID的cookie的那一刻,我正在编写应用程序,该ID用作对存储在数据库中的会话的引用,该会话包含用户信息,包括以下事实:用户已正确登录.

In application I am writing at the moment I've been saving in users browser a cookie that had session ID stored inside, and that ID was used as a reference to a session stored in the database containing user's information including the fact if the user is logged in properly.

我想回顾一下我的解决方案的安全性,我盯着研究如何在登录时设置cookie,在服务器端存储的会话中存储什么以及如何在注销时销毁这些信息,因为到目前为止,我的用户保持登录了很长一段时间,这不是我的意图.

I wanted to review the security of my solution and I stared to look into how I should be setting up cookies upon login, what to store in server side stored session and how to destroy that information on logout since as of now my users were staying logged in for ages, which was not my intention.

我的问题不是在Flask中如何正确处理整个用户登录/会话/注销问题的确切答案-有些人正在谈论使用Flask的Response.delete_cookie()函数,另一些人正在使用.set_cookie(),且到期时间为零,其他人则提到了Flask的会话模块,其他危险模块...

The problem I have is no definite answer on how to handle the whole user login/session/logout issue properly in Flask - some people are talking about using Flask's Response.delete_cookie() function, others to expire it using .set_cookie() with zero expiration time, others are mentioning Flask's session module, other itsdangerous module...

就应与Flask一起使用的模块,代码示例等而言,最安全,正确,正确的处理方式是什么?

What is the most secure, right and proper way of handling that in terms of modules that should be used with Flask, code examples and so on?

推荐答案

背景

方法#1

处理会话的一种简单而安全的方法是执行以下操作:

The background

Method #1

An easy and safe way to handle sessions is to do the following:

  • 使用包含会话ID(随机数)的会话cookie.
  • 使用密钥对会话cookie进行签名(以防止回火-这就是 itsdangerous 的作用).
  • 将实际会话数据存储在服务器端的数据库中(按ID索引,或使用NoSQL键/值存储).
  • 当用户访问您的页面时,您将从数据库中读取数据.
  • 用户注销后,您将从数据库中删除数据.

请注意,有一些缺点.

  • 您需要维护该数据库后端(更多维护)
  • 您需要为每个请求访问数据库(性能较低)

另一个选择是将所有数据存储在cookie中,并签名(以及可选地加密)该cookie.但是,这种方法也有很多缺点:

Another option is to store all the data in the cookie, and sign (and optionally encrypt) said cookie. This method, however, has numerous shortcomings too:

  • 这在后端更容易(更少的维护,更好的性能).
  • 您需要小心,不要在会话中包含用户不应该看到的数据(除非您正在加密).
  • 您可以保存在Cookie中的数据量是有限的.
  • 您不能使单个会话无效(!).

Flask实际上已经实现了签名会话cookie ,因此它实现了方法2

Flask actually implements signed session cookies already, so it implements method #2.

要从#2升至#1,您要做的就是:

To get from #2 to #1, all you have to do is:

  • 生成随机会话ID(您可以使用 os.urandom + base64 ).
  • 将会话数据保存在数据库后端中,并按会话ID进行索引(如果需要Python对象,请使用JSON,Picke序列化,但请尽量避免).
  • 当用户注销时,从数据库后端删除会话.

确保您受到会话固定攻击的保护.为此,请确保您在用户登录时生成一个新的会话ID ,并且不要重复使用他们现有的会话ID.

Make sure you're protected against session fixation attacks. To do so, make sure you generate a new session ID when a user logs in, and do not reuse their existing session ID.

此外,请确保您在会话上实现过期(只是添加最近见"的时间戳).

Also, make sure you implement expiration on your sessions (just a matter of adding a "last-seen" timestamp).

您很有可能从Django的启发实施.

这篇关于如何在Python的Flask中正确安全地处理Cookie和会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆