doushi7819 2017-06-01 21:48
浏览 54
已采纳

如何使用PHP安全地发送服务器回发?

I'm building an application that will send an http request to a url (I hope..) provided by a user.

Probably most of you will know this as a postback, callback or webhook.

However, I'm concerned about security, because the other server will send a response. That response might contain code or who knows what.

I've considered the following functions so far:

  • Curl()
  • file_get_contents()

What is the most secure way of doing this, without opening up a security vulnerability?

  • 写回答

2条回答 默认 最新

  • dongza5150 2017-06-01 22:04
    关注

    You doesn't have a security problem in any case if you don't process the response of the server.

    For example, when you use:

    $url = 'http://www.example.com/testaddr';
    $result = file_get_contents($url);
    unset($result);
    

    You have a variable with the data. But these data aren't processed yet.

    With cURL, you can get the same approach with these options:

    $url = 'http://www.example.com/testaddr';
    $curl = curl_init();                
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($curl);
    
    //If you need to check result, use this:
    if (!curl_errno($curl)) {
      $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      if ($http_code === 200) {
        echo "OK";
      } else {
        echo 'Unexpected HTTP code: ', $http_code, "
    ";
      }
    }
    curl_close($curl);
    unset($result);
    

    In that case it's the same, you get the response on $result var, but, you didn't use it, in that case, it isn't a security failure.

    Also, in both cases, for security reasons and prevent excessive memory usage, I delete the $result variable after finish the process.

    As you can see on PHP doc:

    CURLOPT_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))