如何限制除您的应用程序用户以外的其他人使用您的网站 api?

How to restrict others from using your web api except your app users?

我的应用程序的用户不需要注册或做任何类似的事情,他们可以在 phone.

上安装应用程序后立即使用。

如何防止其他人使用我的网站 apis/services 并将其仅限于我的应用程序?

为了给出更具体的案例,假设我正在使用 Firebase 实时数据库服务。如何防止他人以我的名义使用此服务?

上下文

How can I prevent others from using my web apis/services and restrict it only to my app?

您在此处和该问题的标签中提到了 web,但并未明确表示您指的是网络应用程序还是移动应用程序。

The users of my app do not need to signup or do anything like that, and they can use the app as soon as they install it on their phone.

因此我假设您的应用程序是移动应用程序,因为这句话:他们可以在安装应用程序后立即使用 phone,并假设当您提到 my web apis/services 时,您确实指的是 REST API 和 Firebase 等第三方服务,您也提到了在问题中。

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

How can I prevent others from using my web apis/services and restrict it only to my app?

你说的其他人似乎是指另一个人,即请求中的 who,但你可能想说 what software/script/bot 正在尝试使用我的网站 apis/services。

了解 什么 正在向您的 API 服务器发出请求与了解代表 请求之间的区别正在变得至关重要,这对于了解您需要在用例中应用的安全态势和防御措施至关重要。

我写了一系列关于API和移动安全的文章,在文章Why Does Your Mobile App Need An Api Key?你中我更详细地介绍了who[=106=之间的区别] 和 什么 正在访问您的 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 作为代表用户提出该请求的软件。

在您的用例中,who 不相关,因为您的移动应用程序中没有用户身份验证,因此您需要找到一种方法来识别 what 正在执行请求。因此,您希望将 API 服务器锁定到您的移动应用程序,并且您发现自己是一个非常具有挑战性的问题。

锁定 API/SERVICES

对于 API 服务器

How can I prevent others from using my web apis/services and restrict it only to my app?

为了让 API 服务器高度确信 正在执行的请求确实是您的正版移动应用程序,而不是 bot/script 或您的移动应用程序的篡改版本,移动应用程序证明概念可以是 used/implemented,我邀请您阅读 问题 如何保护 API REST for mobile app?A Possible Better Solution 部分了解这个概念。

NOTE: Just in case you indeed have a web app, and not a mobile app, then I recommend you to read instead this I gave to the question How to secure own backend API which serves only my frontend? and read the section Possible Solutions.

对于 Firebase

To give a more concrete case, lets say I am using Firebase Realtime Database service. How can I prevent others from using this service on my behalf?

我不是 Firebase 专家,因此我会向您指出 问题 Locking down Firebase DB access to specific apps.

在我看来,Firebase 安全性是围绕 security rules and user authentication:

实施的

A common first step in securing your app is identifying your users. This process is called authentication. You can use Firebase Authentication to have users to sign in to your app.

但您无法确定在使用您的移动应用程序,因此他们的第一步不适用于您的用例,但他们似乎支持anonymous user authentication:

You can use Firebase Authentication to create and use temporary anonymous accounts to authenticate with Firebase. These temporary anonymous accounts can be used to allow users who haven't yet signed up to your app to work with data protected by security rules.

所以这可能是您使用 Firebase 锁定您的移动应用程序的最佳机会。

简而言之,Firebase 似乎没有任何内置功能来识别什么 正在执行请求,因为他们的重点似乎更多在 正在执行请求,但如果我不是这方面的专家,我建议您谨慎对待此声明并进行自己的研究。

应用安全规则时,您可能需要考虑 resolve insecurities page and use the Firebase Simulator 以测试不同的规则。

您想加倍努力吗?

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

对于移动应用程序

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.

对于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.