保护对 API 端点的客户端调用的最佳实践

Best practice for securing a client side call to an API endpoint

我正在构建一个应用程序,我需要在客户端前端应用程序中向外部发出请求 API,但我对如何最大程度地做到这一点感到有些茫然安全,以便只有有效的请求才能转发到这个外部 API 而不是任何人想要的任何东西。

作为安全的第一步,我已经做到客户端应用程序无法直接与外部 API 通信,而必须访问我们自己的服务器端 API,然后将请求代理到外部 API,以便访问外部 API 的凭据至少仅存储在服务器端而不是客户端。

然而,这导致了相同的基本问题 - 我如何保护我用来验证从客户端应用程序到我们自己的服务器端应用程序的请求的任何 credential/authentication 系统?

问题是这是一个在线餐厅订购服务,所以我们不希望用户在能够下订单之前使用用户名和密码来验证自己,因此下订单会触发外部API 调用,不受任何 username/password 方案的限制,并且必须可供前端应用程序的所有消费者使用。

这里的最佳安全做法是什么?我已启用 CORS 白名单作为最低限度的做法,这样理论上我们的服务器端 API 端点只允许来自我们自己域的请求,但如果有人选择欺骗来源 URL.

还有哪些其他选项可用?我敢肯定我一定是遗漏了一些微不足道的东西,因为这一定是既定最佳实践中一个非常普遍的问题,但我只是不知何故没能找到它。

谢谢!

最终您的客户端需要对第 3 方执行一些操作 API。

所以我们知道应该允许一些操作,并且根据您的描述我们也知道不是每个操作都应该被允许.

所以你的安全应该建立在这个前提下。不要创建一个转发每个请求的哑代理,但你的中间 API 应该只根据你设置的规则专门允许你希望它允许的操作。

如果您没有用户名和密码,您可能还有一些其他类型的规则来识别一个人(email/phone 号码?),这意味着您可以创建一个身份验证系统。

或者您的第 3 方服务可能只应在用户使用信用卡完成订单后调用,该逻辑需要存在于您的 API。

作为 API 和移动安全的开发者倡导者,看到一个真正关心他们的应用程序安全的开发者总是让我微笑,尤其是当他们已经表现出一些努力来保护它时,因此接受我的祝贺你的努力。

我的回答背景

I'm building an application where I need to make a request in the client-side frontend app to an external API, and I'm at a bit of a loss for how to make this maximally secure so that only valid requests can be forwarded to this external API and not whatever anyone wants.

因此,您没有详细说明它是 Web 应用程序还是移动应用程序,一旦我的专业知识依赖于移动和 API 安全性,我将假设它是移动应用程序来回答。

挑战

The issue is this is an online restaurant ordering service, and so we don't expect users to authenticate themselves with say, usernames and passwords before being able to place orders necessarily, and so order placement, which triggers the external API call, isn't gated behind any username/password scheme, and must be available to all consumers of the frontend app.

你有一个复杂的挑战要解决,因为你有一个应用程序对 public 开放,没有任何类型的用户 authentication/identification,但这需要访问下划线的规则资源好像是在用户认证和授权的背后,但即使是,它仍然容易被滥用。

为了理解为什么我需要澄清一个误解,这个误解通常是我在任何资历的开发人员中发现的,那就是 whowhat[= 之间的区别100=] 正在访问 API 服务器。

访问 API 服务器的 WHO 和 WHAT 的区别

我写了一系列关于API和移动安全的文章,在文章Why Does Your Mobile App Need An Api Key?中你可以详细阅读who之间的区别什么 正在访问您的 API 服务器,但我将在这里提取主要内容:

The what is the thing making the request to the API server. Is it really a genuine instance of your mobile app, or is it a bot, an automated script or an attacker manually poking around your API server with a tool like Postman?

The who is the user of the mobile app that we can authenticate, authorize and identify in several ways, like using OpenID Connect or OAUTH2 flows.

考虑 who 作为用户,您的 API 服务器将能够验证和授权对数据的访问,并考虑 what 作为代表用户提出请求的软件。

因此,在您的情况下,您无法识别请求中的 ,因此您需要一个能够为 API 后端,该请求确实来自 它所期望的,一个真实且未修改的应用程序实例。

可能的解决方案

I'm building an application where I need to make a request in the client-side frontend app to an external API, and I'm at a bit of a loss for how to make this maximally secure so that only valid requests can be forwarded to this external API and not whatever anyone wants.

这需要非常先进的解决方案才能正确保护,因此实现起来并不像您想象的那么简单:

I'm sure I must just be missing something trivial, since this must be an extraordinarily common issue with an established best practice, but I'm just somehow failing to find it.

是的,这是一个经常被忽视或没有妥善解决的常见问题,解决它的第一步是清楚地了解 who什么在请求中,否则设计的解决方案将无法正确解决问题。

对于移动应用程序

在这里,我建议您阅读 我提出的问题 How to secure an API REST for mobile app?,尤其是部分加固和屏蔽移动应用保护API服务器可能更好的解决方案 .

此答案将向您展示多种解决方案,如 WAF 和 UBA,但最后建议使用移动应用认证概念。

简而言之,移动应用证明将使 API 后端非常确信请求确实来自它所期望的 ,一个移动应用程序的真实和修改实例。

对于网络应用程序

您可以学习一些有用的技术来帮助您的 API 后端尝试仅响应来自 您所期望的 的请求,您的正版网络应用程序,以及这样做 我邀请您阅读 my answer 问题 保护 api 来自应用程序调用的数据 ,尤其是致力于捍卫 API 服务器.

您想加倍努力吗?

在任何对安全问题的回答中,我总是喜欢引用 OWASP 基金会的优秀作品。

对于APIS

OWASP API Security Top 10

The OWASP API Security Project seeks to provide value to software developers and security assessors by underscoring the potential risks in insecure APIs, and illustrating how these risks may be mitigated. In order to facilitate this goal, the OWASP API Security Project will create and maintain a Top 10 API Security Risks document, as well as a documentation portal for best practices when creating or assessing APIs.

对于移动应用程序

OWASP Mobile Security Project - Top 10 risks

The OWASP Mobile Security Project is a centralized resource intended to give developers and security teams the resources they need to build and maintain secure mobile applications. Through the project, our goal is to classify mobile security risks and provide developmental controls to reduce their impact or likelihood of exploitation.

OWASP - Mobile Security Testing Guide:

The Mobile Security Testing Guide (MSTG) is a comprehensive manual for mobile app security development, testing and reverse engineering.

对于网络应用程序

The Web Security Testing Guide:

The OWASP Web Security Testing Guide includes a "best practice" penetration testing framework which users can implement in their own organizations and a "low level" penetration testing guide that describes techniques for testing most common web application and web service security issues.