Rails API 会话安全和 CSRF
我正在用铁轨(仅API)进行API。安全对我来说是如此重要。我有2个问题。 1-)我将用户的ID保留在session [:authid]中,这样使用是否安全? 2-)我需要CSRF保护只有API的保护吗?我正在为客户端使用next.js。如果需要,我该如何?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Q1。我将用户的ID保留在会话中[:authid],这样使用是否安全?
ans - 不好将用户ID保持在会话中,因为它可以揭示您的表结构(不是完全但仍然是仍然) 。而不是您可以使用设备token-auth 宝石(可以找到详细信息在这里和在这里)
Q2。我需要CSRF保护只有API的保护吗?
ans - 跨站点或跨节点请求伪造要求用户拥有浏览器和另一个值得信赖的网站。这与API 无关,因为它们 不在浏览器中运行 /strong> 会话。因此,您应该禁用API CSRF。您可以检查有关详细信息。
希望简介对您有帮助。
谢谢
Q1. I am keeping user's id in session[:authID], is it safe to use like this?
ANS - It's not good practice to keep user ids in session as it reveals your table structure (not fully but still) like where your user table's primary key starts from. Rather than that you can use device-token-auth gem (details can be found here and here)
Q2. Do i need CSRF protection for api-only?
ANS - Cross-Site or Cross-Session Request Forgery requires a user to have a browser and another trusted website. This is not relevant for APIs, since they don't run in the browser and don't maintain any session. Therefore, you should disable CSRF for APIs. You can check here for details.
Hope the brief helps you.
Thank you