简体   繁体   English

如何保护 Web API 不被未经授权的 SPA 访问

[英]How to secure a web API from being accessed from unauthorized SPAs

I am building a B2B service whose API can be accessed by third-parties on a subscription basis.我正在构建一个 B2B 服务,第三方可以在订阅的基础上访问其 API。 Basically, we provide a customizable widget that our customers can embed on their website to make it available to their customers (eg a button that opens a modal).基本上,我们提供了一个可定制的小部件,我们的客户可以将其嵌入到他们的网站上,以供他们的客户使用(例如,一个打开模态的按钮)。 While it is clear how to make this work in a traditional web app, I am not sure how to guarantee this in a single-page app.虽然很清楚如何在传统的网络应用程序中实现这一点,但我不确定如何在单页应用程序中保证这一点。 Is it at all possible to make this work without a redirect URI as used in OAuth?是否有可能在没有 OAuth 中使用的重定向 URI 的情况下完成这项工作? That is, the modal triggers AJAX requests to our API and we want to make sure it comes from a script from an authorized origin without redirects.也就是说,模态会触发对我们 API 的 AJAX 请求,我们希望确保它来自没有重定向的授权来源的脚本。 We could of course simply check Origin header, but what is there to prevent someone from constructing a request with such a header on their backend manually, even though they couldn't do it in the browser.我们当然可以简单地检查 Origin 标头,但是有什么可以防止有人在他们的后端手动构造一个带有这样一个标头的请求,即使他们不能在浏览器中这样做。

The Problem问题

While it is clear how to make this work in a traditional web app, I am not sure how to guarantee this in a single-page app.虽然很清楚如何在传统的网络应用程序中实现这一点,但我不确定如何在单页应用程序中保证这一点。

From a web app you only need to see the html source code to be able to find the API keys or other secrets.从 Web 应用程序中,您只需要查看 html 源代码即可找到 API 密钥或其他秘密。 Even if you use a traditional web server, cookies can also be obtained to automate attacks against it.即使您使用传统的 Web 服务器,也可以获取 cookie 以自动对其进行攻击。

While this series of articles about Mobile API Security Techniques are in the context of mobile devices, some of the techniques used are also valid in other type of APIs, like APIs for Web/SPAs apps, and you can see how API keys, OUATH tokens and HMAC secrets can be used to protect an API and bypassed.虽然这一系列关于移动 API 安全技术的文章是在移动设备的上下文中使用的,但所使用的一些技术在其他类型的 API 中也有效,例如用于 Web/SPA 应用程序的 API,您可以看到 API 密钥、OUATH 令牌如何和 HMAC 机密可用于保护 API 并绕过。

Possible Solution可能的解决方案

You can try to make it hard to find the API key with a Javascript Obfuscator , but bear in mind that this only delays an attacker in succeeding.您可以尝试使用Javascript Obfuscator使查找 API 密钥变得困难,但请记住,这只会延迟攻击者的成功。

So, how can I block an attacker?那么,我怎样才能阻止攻击者呢?

Well the cruel truth is... You can't!!!好吧,残酷的事实是……你不能!!!

But you can try, by using reCAPTCHA V3 from Google, that works in the background, therefore doesn't require user interaction.但是您可以尝试使用来自 Google 的reCAPTCHA V3 ,它在后台运行,因此不需要用户交互。 The drawback here is that all your B2B clients would need to implemente it across all pages of their websites, thus may not be the way to go for your use case...这里的缺点是您的所有 B2B 客户都需要在其网站的所有页面上实施它,因此可能不适用于您的用例......

reCAPTCHA V3 :验证码 V3

reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA 是一项免费服务,可保护您的网站免受垃圾邮件和滥用。 reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep automated software from engaging in abusive activities on your site. reCAPTCHA 使用先进的风险分析引擎和自适应挑战来防止自动化软件在您的网站上从事滥用活动。 It does this while letting your valid users pass through with ease.它做到了这一点,同时让您的有效用户轻松通过。

If your B2B solution really needs to protect it at all costs then you need to employ Web Application Firewalls(WAF) and User Behavior Analytics solutions, also know as UBA, that use Artificial Intelligence and Machine Learning to prevent abuse, but once more they cannot guarantee 100% blocking and both have false positives.如果您的 B2B 解决方案确实需要不惜一切代价保护它,那么您需要使用 Web 应用程序防火墙 (WAF) 和用户行为分析解决方案,也称为 UBA,它们使用人工智能和机器学习来防止滥用,但同样,它们不能保证 100% 阻塞并且两者都有误报。

WAF :华夫

A web application firewall (or WAF) filters, monitors, and blocks HTTP traffic to and from a web application. Web 应用程序防火墙(或 WAF)过滤、监视和阻止进出 Web 应用程序的 HTTP 流量。 A WAF is differentiated from a regular firewall in that a WAF is able to filter the content of specific web applications while regular firewalls serve as a safety gate between servers. WAF 与常规防火墙的区别在于 WAF 能够过滤特定 Web 应用程序的内容,而常规防火墙则充当服务器之间的安全门。 By inspecting HTTP traffic, it can prevent attacks stemming from web application security flaws, such as SQL injection, cross-site scripting (XSS), file inclusion, and security misconfigurations.通过检查 HTTP 流量,它可以防止源自 Web 应用程序安全缺陷的攻击,例如 SQL 注入、跨站点脚本 (XSS)、文件包含和安全配置错误。

UBA :

User behavior analytics (UBA) as defined by Gartner is a cybersecurity process about detection of insider threats, targeted attacks, and financial fraud. Gartner 定义的用户行为分析 (UBA) 是一个关于检测内部威胁、针对性攻击和金融欺诈的网络安全过程。 UBA solutions look at patterns of human behavior, and then apply algorithms and statistical analysis to detect meaningful anomalies from those patterns—anomalies that indicate potential threats. UBA 解决方案着眼于人类行为模式,然后应用算法和统计分析从这些模式中检测有意义的异常——表明潜在威胁的异常。 Instead of tracking devices or security events, UBA tracks a system's users. UBA 不跟踪设备或安全事件,而是跟踪系统的用户。 Big data platforms like Apache Hadoop are increasing UBA functionality by allowing them to analyze petabytes worth of data to detect insider threats and advanced persistent threats. Apache Hadoop 等大数据平台允许它们分析 PB 级数据以检测内部威胁和高级持续威胁,从而增强了 UBA 功能。

Conclusion结论

In the end of the day you can only protect your B2B back-end in a best effort basis, that must be proportional to the value it holds for the business.归根结底,您只能尽最大努力保护您的 B2B 后端,这必须与它为企业带来的价值成正比。

A 100% solution doesn't exist for the web, due to the way it was designed to work!!!网络上不存在 100% 的解决方案,因为它的工作方式是这样设计的!!!

Does it matter where the request comes from or who is making the request?请求来自何处或是谁提出请求是否重要? If the latter needs confirmation then you could require an authorization token along with the request.如果后者需要确认,那么您可能需要授权令牌以及请求。 Normally, you would do this in such a way that you could decode the token and confirm a match with an authorized party.通常,您可以通过解码令牌并确认与授权方匹配的方式来执行此操作。

So basically you want system security, you can use Oauth2.0 grant type = client credentials.所以基本上你想要系统安全,你可以使用 Oauth2.0 grant type = client credentials。 This will make sure you api's is being used by only authorized clients.这将确保您的 api 仅被授权客户使用。

Working of it is very simple, client hit an Oauth2.0 server with client_id and client_pass and Oauth server returns you a token, the same token client will pass to the server and when you verify that token by hitting the Oauth server with server_id,server_pass+token to Oauth server it returns validation with client id and based on that you can expose your services.它的工作非常简单,客户端使用 client_id 和 client_pass 访问 Oauth2.0 服务器,Oauth 服务器返回一个令牌,相同的令牌客户端将传递给服务器,当您通过使用 server_id,server_pass 访问 Oauth 服务器验证该令牌时+token 到 Oauth 服务器,它返回带有客户端 ID 的验证,并基于此您可以公开您的服务。 You do not need to worry about redirection as client credentials does not require that.您无需担心重定向,因为客户端凭据不需要。

I suggest using OAuth2 Auth Code with PKCE.我建议将 OAuth2 Auth Code 与 PKCE 一起使用。 It's designed to allow securely calling APIs from SPAs (and native apps).它旨在允许从 SPA(和本机应用程序)安全地调用 API。 Basically, it relies on the fact that the SPA calling your API has some well known URL, and this is what disallows its usage outside of your SPA.基本上,它依赖于调用您的 API 的 SPA 具有一些众所周知的 URL 的事实,这就是不允许在您的 SPA 之外使用它的原因。

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

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