简体   繁体   English

将Oauth令牌安全地存储在文件中

[英]Securely store Oauth token(s) in file

I'm developing a small webapp in python that'll interact with a users dropbox account. 我正在开发一个python中的小型webapp,它将与用户Dropbox帐户进行交互。 What is the best way to store the Oauth tokens for that account in a flat file? 在平面文件中存储该帐户的Oauth令牌的最佳方法是什么?

Is hashing the tokens secure enough? 散列令牌足够安全吗? Or should I encrypt them? 或者我应该加密它们? If encrypting them is the way to go, how would you suggest storing the key, since 2 way encryption would be necessary to decrypt the tokens for sending to Dropbox? 如果加密它们是要走的路,你会建议如何存储密钥,因为解密令牌以便发送到Dropbox需要双向加密?

I could load up sqlite and store the tokens in there, but I'm wondering if there's a good way to do it using flat files. 我可以加载sqlite并将令牌存储在那里,但我想知道是否有一种使用平面文件的好方法。 Same issue is run into with Sqlite, since its also a file. Sqlite遇到了同样的问题,因为它也是一个文件。 Of course, the file permissions would only be set to the least permissible privilege to be accessed by the webapp. 当然,文件权限只能设置为webapp访问的最小权限。

Hashing won't work, since, as skjaidev mentions, it's one way. 哈希不会起作用,因为正如skjaidev所提到的那样,这是一种方式。

If you have a reasonable fear that your file or database will get stolen(*), encryption is the way to go. 如果您有理由担心您的文件或数据库会被盗(*),那么加密是可行的方法。 But indeed as you mention, your app will need the decryption key, so the question is where to store it. 但实际上,正如您所提到的,您的应用程序将需要解密密钥,因此问题在于存储它的位置。 Obviously storing it in the same spot as the data, doesn't enhance security. 显然,将其存储在与数据相同的位置,不会增强安全性。 Please consider the following: 请考虑以下事项:

  • When your data is in a database, it's (most likely) less secure than in a flat file. 当您的数据位于数据库中时,它(很可能)不如平面文件安全。 This is because there are database injection techniques that may allow you to read the database, but not files. 这是因为有数据库注入技术可以允许您读取数据库,而不是文件。 In this case putting your decryption key somewhere on the file system (in your code) makes sense: the data from the database alone is in that case useless. 在这种情况下,将解密密钥放在文件系统的某个位置(在代码中)是有道理的:在这种情况下,仅数据库中的数据是无用的。
  • Even when your data is in a flat file, putting the decryption key somewhere in a file, can decrease risk. 即使您的数据位于平面文件中,将解密密钥放在文件中的某个位置也可以降低风险。 Many systems get "hacked" when the hacker gets access to a system that wasn't even supposed to contain that data, that contained old backups of the data, or in some other way doesn't (necessarily) contain your code with the decryption key. 当黑客访问一个甚至不包含该数据的系统时,许多系统都会被“黑客入侵”,这些系统包含旧的数据备份,或者以某种其他方式不会(必然)包含您的解密代码键。
  • Best is to have your decryption key not on the filesystem at all, but just in the computer memory. 最好的办法是让你的解密密钥根本不在文件系统上,而只放在计算机内存中。 A good hacker with root access or physical access may still get to it, but I would argue that in 99% of the cases that hackers get access to the file systems, they won't be able to read the memory as well (in the cases they steal backups, steal the physical machine (turning it off in the process), get user-level access, etc). 一个有root访问权限或物理访问权限的好黑客仍然可以使用它,但我认为在99%的黑客可以访问文件系统的情况下,他们也无法读取内存(在他们窃取备份,窃取物理机器(在此过程中将其关闭),获得用户级访问等情况。 This is basically the keychain-approach. 这基本上是钥匙链方法。 Problem is, how to get the decryption key into the memory, and there is only one solution that I know of: type it in (or some other password that decrypts the decryption key) every time the application starts. 问题是,如何将解密密钥放入内存,并且我只知道一种解决方案:每次应用程序启动时输入(或解密解密密钥的其他密码)。 Whether this is acceptable depends on how often your application will restart. 这是否可接受取决于您的应用程序重启的频率。

  • There is one other method. 还有一种方法。 If you only need access to dropbox when your users are actually logged in to your app, you can consider encrypting the token with some unique user property (or instance the password that the user uses to log in to your site, or some random string you set in a cookie on the first visit). 如果您只需要在用户实际登录到您的应用程序时访问Dropbox,您可以考虑使用某些唯一的用户属性加密令牌(或实例用户用来登录您网站的密码,或者一些随机字符串您在第一次访问时设置为cookie)。 In this case you can also consider storing the whole access token encrypted in a cookie (and not on your server at all). 在这种情况下,您还可以考虑将整个访问令牌存储在cookie中(而不是在您的服务器上)。

Whatever method you choose, it will never really protect, as you mention yourself. 无论你选择何种方法,它都无法真正保护,正如你自己提到的那样。 If your app can get to decrypted tokens (which it can, else your app would not need to store them in the first place), some hacker with unlimited privileged can as well. 如果你的应用程序可以获得解密令牌(它可以,否则你的应用程序不需要首先存储它们),一些具有无限特权的黑客也可以。 The nice thing about access tokens is, though, that probably they can be easily revoked, so if they get stolen it's probably not the end of the world; 然而,关于访问令牌的好处是它们可能很容易被撤销,所以如果它们被盗,它可能不是世界末日; and a hacker knows they can be easily revoked so they will hardly be interesting as a target. 黑客知道他们很容易被撤销,所以他们很难成为一个目标。

(*) Note: it's always reasonable to assume that stuff will get stolen eventually one way or another. (*)注意:假设东西最终会以这种或那种方式被盗,这是合理的。 I can imagine though that if you set up a small site for 20 friends on your home PC, you care less about your passwords being stolen, than when you're building the next instagram. 我可以想象,如果你在家用电脑上为20个朋友设置了一个小网站,那么你关心的是你的密码被盗,而不是你建立下一个Instagram时。 It's always a tradeoff between security and amount of work. 它总是在安全性和工作量之间进行权衡。 As mentioned, having your tokens in a flat file in stead of a database (if handled correctly) should make it less likely that they get stolen. 如上所述,将您的令牌放在平面文件而不是数据库(如果处理正确)应该使它们不太可能被盗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM