简体   繁体   English

如何避免 Chrome 扩展中的“虚假”AJAX 请求

[英]How to avoid "fake" AJAX requests in Chrome extension

I am writing a small Chrome extension, and my question mostly about the algorithms.我正在编写一个小的 Chrome 扩展程序,我的问题主要是关于算法的。 Suppose, my extension should send some AJAX requests to my server.假设,我的扩展程序应该向我的服务器发送一些 AJAX 请求。 Is there any way to be sure that this particular AJAX request was received exactly from my extension?有什么方法可以确保这个特定的 AJAX 请求是从我的扩展中准确收到的? I mean, make sure that this is not the user sent this request by falsifying it.我的意思是,确保这不是用户通过伪造发送此请求。 I will be grateful for any ideas.我将不胜感激任何想法。

You need to check request origin on your server which must contain your extension ID.您需要检查服务器上的请求来源,其中必须包含您的扩展 ID。 When you send AJAX request from your extension the Origin parameter will be like this当您从扩展程序发送 AJAX 请求时,Origin 参数将如下所示

chrome-extension://<extension_id> 

Now on server you need to check this origin.现在在服务器上你需要检查这个来源。 Example in php php中的例子

$extensionID = "YOUR_EXTENSION_ID";
$origin = $_SERVER['HTTP_ORIGIN'];
if (strpos($origin, $extensionID) === false) {
// exit from code 
 exit();
}

Here is complete anwser how to find origin from request.这是如何从请求中找到来源的完整答案 Now your server will receive AJAX request only from your extension.现在您的服务器将仅从您的扩展接收 AJAX 请求。 If someone copy your code and run from another extension, your server will not handle that request.如果有人复制您的代码并从另一个扩展程序运行,您的服务器将不会处理该请求。

Note that this will protect you from falsifying requests from other extensions.请注意,这将防止您伪造来自其他扩展程序的请求。 User still can open your extension background page and send AJAX request from console.用户仍然可以打开您的扩展后台页面并从控制台发送 AJAX 请求。

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

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