j0k3r/safecurl
Composer 安装命令:
composer require j0k3r/safecurl
包简介
A drop-in replacement for 'curl_exec', designed to prevent SSRF attacks.
README 文档
README
SafeCurl intends to be a drop-in replacement for the curl_exec function in PHP. SafeCurl validates each part of the URL against a white or black list, to help protect against Server-Side Request Forgery attacks.
For more infomation about the project see the blog post 'SafeCurl: SSRF Protection, and a "Capture the Bitcoins"'.
Protections
Each part of the URL is broken down and validated against a white or black list. This includes resolve a domain name to it's IP addresses.
If you chose to enable "FOLLOWLOCATION", then any redirects are caught, and re-validated.
Installation
SafeCurl can be included in any PHP project using Composer. Include the following in your composer.json file under require.
"require": {
"j0k3r\safecurl": "~3.0"
}
Then update Composer.
composer update
Usage
It's as easy as replacing curl_exec and wrapping it in a try {} catch {} block.
use fin1te\SafeCurl\SafeCurl; use fin1te\SafeCurl\Exception; try { $url = 'http://www.google.com'; $curlHandle = curl_init(); //Your usual cURL options curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (SafeCurl)'); //Execute using SafeCurl $safeCurl = new SafeCurl($curlHandle); $response = $safeCurl->execute($url); } catch (Exception $e) { //URL wasn't safe }
Options
The default options are to not allow access to any private IP addresses, and to only allow HTTP(S) connections.
If you wish to add your own options (such as to blacklist any requests to domains your control), simply get a new SimpleCurl\Options object, add to the white or black lists, and pass it along with the method calls.
Domains are express using regex syntax, whilst IPs, scheme and ports are standard strings (IPs can be specified in CIDR notation).
use fin1te\SafeCurl\SafeCurl; use fin1te\SafeCurl\Options; $options = new Options(); $options->addToList('blacklist', 'domain', '(.*)\.fin1te\.net'); $options->addToList('whitelist', 'scheme', 'ftp'); $curlHandle = curl_init(); //This will now throw an InvalidDomainException $safeCurl = new SafeCurl($curlHandle, $options); $response = $safeCurl->execute('http://safecurl.fin1te.net'); //Whilst this will be allowed, and return the response $safeCurl = new SafeCurl($curlHandle, $options); $response = $safeCurl->execute('ftp://fin1te.net');
Since we can't get access to any already set cURL options (see Caveats section), to enable CURL_FOLLOWREDIRECTS you must call the enableFollowRedirects() method. If you wish to specify a redirect limit, you will need to call setMaxRedirects(). Passing in 0 will allow infinite redirects.
$options = new Options(); $options->enableFollowLocation(); //Abort after 10 redirects $options->setFollowLocationLimit(10);
URL Checking
The URL checking methods are also public, meaning that you can validate a URL before using it elsewhere in your application, although you'd want to try and catch any redirects.
use fin1te\SafeCurl\Url; use fin1te\SafeCurl\Exception; try { $url = 'http://www.google.com'; $validatedUrl = Url::validateUrl($url); $fullUrl = $validatedUrl['url']; } catch (Exception $e) { // URL wasn't safe }
Optional Protections
In addition to the standard checks, two more are available.
The first is to prevent DNS Rebinding attacks. This can be enabled by calling the enablePinDns method on an Options object. There is one major issue with this - the SSL certificate can't be validated. This is due to the real hostname being sent in the Host header, and the URL using the IP address.
$options = new Options(); $options->enablePinDns();
The second disables the use of credentials in a URL, since PHP's parse_url returns values which differ from ones cURL uses. This is a temporary fix.
use fin1te\SafeCurl\SafeCurl; use fin1te\SafeCurl\Exception; use fin1te\SafeCurl\Options; $options = new Options(); $options->disableSendCredentials(); $curlHandle = curl_init(); //This will throw an InvalidURLException $safeCurl = new SafeCurl($curlHandle, $options); $response = $safeCurl->execute('http://user:pass@google.com');
Cavets
Since SafeCurl uses gethostbynamel to resolve domain names, which isn't IPv6 compatible, the class will only work with IPv4 at the moment. See Issue #1.
As mentioned above, we can't fetch the value of any cURL options set against the provided cURL handle. Because SafeCurl handles redirects itself, it will turn off CURLOPT_FOLLOWLOCATION and use the value from the Options object. This is also true of CURLOPT_MAXREDIRECTS.
j0k3r/safecurl 适用场景与选型建议
j0k3r/safecurl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143.15k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「safe」 「safecurl」 「ssrf」 「websec」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 j0k3r/safecurl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 j0k3r/safecurl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 j0k3r/safecurl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A drop-in replacement for 'curl_exec', designed to prevent SSRF attacks.
cURL and cURL based Soap-Client for PHP
Simple cURL wrapper
A drop-in replacement for 'curl_exec', designed to prevent SSRF attacks.
This package provides type-safe extension of the laravel collection, forcing a single type of object.
A fluent PHP CURL wrapper
统计信息
- 总下载量: 143.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-02