是否可以限制php的cURL库可以访问哪些外部域?

时间:2016-08-11 21:36:37

标签: php curl

我的网络应用程序使用cURL与外部域通信。只有一个外部域可以与应用程序连接和通信。是否可以限制PHP cURL库可以访问哪些外部域作为应用程序环境中的额外安全措施?

2 个答案:

答案 0 :(得分:2)

从技术上讲,这可以通过在用户shell中定义一个函数来完成:

Mac Shell: Downloads/>$ curl(){
> echo "test";
> }
Mac Shell: Downloads/>$ curl
test
Mac Shell: Downloads/>$ which curl
/usr/bin/curl
Mac Shell: Downloads/>$ type -a curl
curl is a function
curl () 
{ 
    echo "test"
}
curl is /usr/bin/curl
Mac Shell: Downloads/>$

你可以做这样的事情:

curl(){
    domain="$1"
    [[ "$domain" == "mydomain.com" ]] && {
        /usr/bin/curl "$domain";
    } || {
        printf "That is not the domain that you are supposed to use\n";
    };
};

只要用户没有指定文件路径,这应该有效。

我不建议这样做。

编辑:

以下是此解决方案的验证:

Mac Shell: Downloads/>$ curl(){
>     domain="$1"
>     [[ "$domain" == "mydomain.com" ]] && {
>         /usr/bin/curl "$domain";
>     } || {
>         printf "That is not the domain that you are supposed to use\n";
>     };
> };
Mac Shell: Downloads/>$ curl test
That is not the domain that you are supposed to use
Mac Shell: Downloads/>$ curl mydomain.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="URL">here</A>.<P>
</BODY></HTML>
Mac Shell: Downloads/>$

EDIT2:

这也必须来源。

例如,如果您为某个用户添加了.bash_profie个文件(还有其他更多可以放置的创意地点),那么您必须退出并重新登录以查看效果或输入:

source /<some_path>/.bash_profile

EDIT3:

只是为每个人澄清这是一个概念证明。将需要更广泛的逻辑测试。例如,如果此人尝试使用不同的子域或端口,路径,查询或片段卷曲此域,则提供的逻辑测试将失败。

我再也不建议以这种方式限制用户。

答案 1 :(得分:0)

只需制作白名单并使用parse_url解析网址

out
相关问题