针对在 AWS Lambda / API Gateway 中运行的无服务器 .NET Web API 的 CSRF/XSRF 保护

发布于 2025-01-13 13:05:22 字数 694 浏览 3 评论 0原文

我们希望将 .NET Web API 项目作为托管在 API Gateway 后面的 AWS Lambda 中的无服务器应用程序运行[遵循如下方法:https://aws.amazon.com/blogs/developer/deploy-an-existing-asp-net -core-web-api-to-aws-lambda/]。

对于需要使用 JWT 在 API 上进行身份验证的应用程序,我们一直在研究在本地存储中存储 JWT 的方法,以及在与 API 通信的前端 SPA 中存储 cookie 的方法。

使用 cookie 存储 JWT(特别是 AccessToken)的一大警告似乎是 XSRF/CSRF 攻击的可能性。对于服务器端渲染应用程序,使用嵌入 html 有效负载的防伪令牌似乎有助于减轻这种风险。然而,对于 SPA 和我们在 Lambda 中运行的 Web API,每个客户端请求都可能由任何当前正在执行的 Lambda 上下文(或者如果需要的话启动一个新的上下文)处理,这似乎很难实现,因为不会有持久的客户端请求/服务器响应之间的状态。

似乎根据用户访问令牌中找到的范围生成类似的临时秘密可能是在无状态环境中处理此问题的一种方法。然而,对于与安全相关的一切,滚动你的总是似乎是一个糟糕的主意。

我的问题是,是否有任何已知的包/库或方法适用于此类用例,可以帮助缓解在分布式无服务器环境(例如 AWS Lambda)中运行的 .NET 的 CSRF/XSRF。或者是使用本地存储的推荐方法?

We are looking to run .NET Web API projects as serverless applications hosted in AWS Lambda behind API Gateway [following an approach like this: https://aws.amazon.com/blogs/developer/deploy-an-existing-asp-net-core-web-api-to-aws-lambda/].

For applications that require authentication on the API with a JWT, we have been researching the methods for storing JWTs in local storage vs cookies in our front end SPAs that talk to the API.

It seems like one of the big caveats of using cookies for storing JWTs, specifically the AccessToken, is the potential for XSRF/CSRF attacks. With server side render apps, the use of anti-forgery tokens embedded in the html payload seems to help mitigate this risk. However with a SPA and our Web API running within Lambda - with each client request potentially being handled by any currently executing Lambda context (or a new one spun up if needed) this seems like it may be hard to achieve since there would be no persisted state between client request/response from the server.

It seems like generating an analogous temporary secret based on the scopes found in the user's access token might be a way to handle this in a stateless environment. However, with everything related to security, rolling your always seems like a terrible idea.

My question is if there are any known packages/libraries or approaches for this type of use case that can help mitigate CSRF/XSRF for .NET running in a distributed, serverless environment such as AWS Lambda. Or is the recommended approach to use local storage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文