首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过JavaScript调用的安全.php文件

通过JavaScript调用的安全.php文件
EN

Stack Overflow用户
提问于 2017-05-06 09:03:04
回答 2查看 91关注 0票数 1

我们的客户使用如下代码使用我们的免费服务:

代码语言:javascript
复制
<script type='text/javascript'>id='example'; width='640'; height='480';</script><script type='text/javascript' src='http://example.com/example.js'></script>

example.js看起来像这样:

代码语言:javascript
复制
if (typeof (width) == "undefined") {
    var width = '100%';
}
if (typeof (height) == "undefined") {
    var height = '100%';
}
if (typeof (p) == "undefined") {
    var p = '0';
}
if (typeof (c) == "undefined") {
    var c = '0';
}
if (typeof (stretching) == "undefined") {
    var stretching = 'uniform';
}
document.write('<iframe allowfullscreen width="' + width + '" height="' + height + '" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true" src="http://example.com/examplefile.php?id=' + id + '&p=' + p + '&c=' + c + '&stretching=' + stretching + '"></iframe>');

问题是人们正在榨取examplefile.php。我们尝试在nginx中使用secure_link,它工作得很好,但只适用于那些能够在自己的站点中使用PHP代码的客户端,生成一个带有密钥的随机安全令牌。其他一些客户端只能嵌入HTML代码。有没有一种方法可以保护examplefile.php的安全,或者随机更改examplefile.php名称,并针对我们的服务器进行验证,以阻止恶意入侵?

也许使用jQuery?我们需要能够确保此JavaScript代码开始调用examplefile.php,而不是从外部站点手动添加为iframe。

EN

回答 2

Stack Overflow用户

发布于 2017-05-06 10:29:15

如果我理解正确的话,您需要确保您是唯一使用此资源的人。一种方法是用生成的JS文件example.php替换example.js

这个文件有两个职责:

  1. 针对您的服务器验证请求
  2. 输出纯JS内容,就像它是JS文件(具有适当的头数据)一样。

更新

具体来说,这是我的方法:

通过使用example.php文件(而不是example.js),每次用户加载该文件时,都会为客户端初始化一个惟一的会话令牌,您将在examplefile.php中立即在其中进行验证。这样,您就可以确保(在某种程度上)请求来自example.php

票数 0
EN

Stack Overflow用户

发布于 2017-05-06 10:47:56

您可以使用AJAX请求替换JavaScript,该请求发送带有令牌的自定义HTTP请求头部。在验证令牌时,您的服务器将使用在iframe中使用的URL进行响应。此解决方案为您提供了控制URL的机会,因此您可以对其进行随机化。

另一种方法是将请求发送到一个URL,该URL指示您访问该资源的意图。它可以用会话cookie来响应,该会话cookie将由对iframe链接的后续请求携带。

下面是一些简单的JavaScript,可以帮助您开始使用AJAX请求。

代码语言:javascript
复制
var myURL = 'https://www.example.com/path/',
    myCustomKey = 'Custom-Header',
    myCustomValue = 'Custom-Token-Value',
    myRequest = new XMLHttpRequest();

// open request so that custom header can be added
myRequest.open('GET', myURL, true);
// set custom header for request
myRequest.setRequestHeader(myCustomKey, myCustomValue);

myRequest.onload = function () {
    // Request finished. Do processing here.
    if (myRequest.status === 200) {
        // Request was successful
        // use the response
        console.log(myRequest.response);
    }
};

myRequest.send(null);

您必须配置服务器以支持CORS。请参阅https://enable-cors.org/server.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43815656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档