rattfieldnz / safe-urls

一个使用Google安全浏览API检查URL的Laravel包。


README

PHP Version PHP Version License: MIT Latest Version on Packagist Total Downloads Build Status StyleCI Scrutinizer Code Quality codecov

一个使用Google安全浏览API(查找)检查URL的Laravel包。

受到https://github.com/snipe/Safebrowsing的另一个类似包的启发。

查看contributing.md以查看待办事项列表。

请注意

此包需要您拥有有效的Google安全浏览API密钥。没有它将无法工作。创建API密钥是免费的。

Google还会限制API使用,所以如果您有一个高流量站点,您可能需要构建一个缓存层或类似的东西,以避免快速耗尽您的请求。您可以通过Google API控制台监视您的使用情况。

此项目尚未准备好在生产环境中使用。当它准备好时,将会有第一个主要版本(即1.0.0)。

关于Google安全浏览API结果的说明

在测试期间,有几次API将一些恶意网站显示为“安全”,而实际上它们并不是。

例如,运行PHPUnit测试将以下示例站点显示为“安全”;然而,在Postman中运行API产生了预期的结果。我还没有找到解决方案;然而,任何反馈/建议都受欢迎,包括pull请求等。以下是我阅读关于此问题的链接

安装

通过Composer

$ composer require rattfieldnz/safe-urls

更新您的配置

对于使用Laravel < 5.4的应用程序

打开config/app.php并将以下内容添加到您的providers数组中,并将以下内容添加到您的aliases数组中:

RattfieldNz\SafeUrls\SafeUrlsServiceProvider::class,

到您的providers数组在config/app.php中,和

'SafeUrls' => RattfieldNz\SafeUrls\Facades\SafeUrlsFacade::class,

到您的aliases数组在config/app.php中。

发布配置

php artisan vendor:publish

这将把一个safe-urls.php配置文件添加到您的项目的config文件夹中。

设置您的Google安全浏览API密钥

在您的.env文件中添加:

GOOGLE_API_KEY=YOUR-ACTUAL-API-KEY
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_VERSION=1.0 (or your preferred number)
GOOGLE_CURL_TIMEOUT=10 (in seconds)

配置文件中还有一些额外选项,这些选项与您希望检查的具体威胁类型以及希望检查的平台有关,但除非您想要检查更少的事情,否则您实际上无需过多担心,因为它已经相当全面了。

使用方法

使用 Blade 语法

@if(isset(SafeUrls::check($urls, true)["response"]["matches"]))
    <p>There are {{ count(SafeUrls::check($urls, true)["response"]["matches"]) }} dangerous URLs.</p>
@else
    <p>No results were found</p>
@endif

其中 $urls 可以是一个待检查的 URL 数组,也许是通过您的控制器传递到视图中的。

true 将以关联数组的形式返回结果。

false(或没有第二个参数)将以 JSON 编码字符串的形式返回结果。

@if (SafeUrls::isDangerous('http://twitter.com/'))
    // do something if the url is flagged as suspicious
@else
    // hooray - it's not flagged!
@endif

使用外观

SafeUrls::add(['http://ianfette.org']);
SafeUrls::add(['http://malware.testing.google.test/testing/malware/']);
SafeUrls::execute();
print('Status of the third URL is: '.SafeUrls::isDangerous('http://twitter.com/'));

输入和输出示例

如果 $urls 的值为

$urls = [
    'http://www.yahoo.com/'
    'http://www.google.com/'
    'http://malware.testing.google.test/testing/malware/'
    'http://twitter.com/'
    'http://ianfette.org'
    'https://github.com/'
    'https://testsafebrowsing.appspot.com/s/phishing.html'
    'https://testsafebrowsing.appspot.com/s/malware.html'
    'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/'
    'http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/'
];   

SafeUrls::check($urls, true) 将返回以下关联数组

[
    "status" => 200,
    "response" => 
        [
            "matches" => [
                [
                    "threatType" => "MALWARE",
                    "platformType" => "ANY_PLATFORM",
                    "threat" => [
                        "url" => "http://malware.testing.google.test/testing/malware/"
                    ],
                    "cacheDuration" => "300s",
                    "threatEntryType" => "URL"
                ],
                [
                    "threatType" => "SOCIAL_ENGINEERING",
                    "platformType" => "ANY_PLATFORM",
                    "threat" => [
                        "url" => "http://malware.testing.google.test/testing/malware/"
                    ],
                    "cacheDuration" => "300s",
                    "threatEntryType" => "URL"
                ],
                [
                    "threatType" => "SOCIAL_ENGINEERING",
                    "platformType" => "ANY_PLATFORM",
                    "threat" => [
                        "url" => "https://testsafebrowsing.appspot.com/s/phishing.html"
                    ],
                    "cacheDuration" => "300s",
                    "threatEntryType" => "URL"
                ],
                [
                    "threatType" => "MALWARE",
                    "platformType" => "ANY_PLATFORM",
                    "threat" => [
                        "url" => "https://testsafebrowsing.appspot.com/s/malware.html"
                    ],
                "cacheDuration" => "300s",
                "threatEntryType" => "URL"
            ],
            [
                "threatType" => "MALWARE",
                "platformType" => "ANY_PLATFORM",
                "threat" => [
                    "url" => "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/MALWARE/URL/"
                ],
                "cacheDuration" => "300s",
                "threatEntryType" => "URL"
            ],
            [
                "threatType" => "SOCIAL_ENGINEERING",
                "platformType" => "ANY_PLATFORM",
                "threat" => [
                    "url" => "http://testsafebrowsing.appspot.com/apiv4/ANY_PLATFORM/SOCIAL_ENGINEERING/URL/"
                ],
                "cacheDuration" => "300s",
                "threatEntryType" => "URL"
            ],
        ],
    ],
]

SafeUrls::check($urls)(或 SafeUrls::check($urls, false))将返回以下 JSON 编码字符串

{  
   "status":200,
   "response":{  
      "matches":[  
         {  
            "threatType":"MALWARE",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"http:\/\/malware.testing.google.test\/testing\/malware\/"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         },
         {  
            "threatType":"SOCIAL_ENGINEERING",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"http:\/\/malware.testing.google.test\/testing\/malware\/"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         },
         {  
            "threatType":"SOCIAL_ENGINEERING",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"https:\/\/testsafebrowsing.appspot.com\/s\/phishing.html"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         },
         {  
            "threatType":"MALWARE",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"https:\/\/testsafebrowsing.appspot.com\/s\/malware.html"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         },
         {  
            "threatType":"MALWARE",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"http:\/\/testsafebrowsing.appspot.com\/apiv4\/ANY_PLATFORM\/MALWARE\/URL\/"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         },
         {  
            "threatType":"SOCIAL_ENGINEERING",
            "platformType":"ANY_PLATFORM",
            "threat":{  
               "url":"http:\/\/testsafebrowsing.appspot.com\/apiv4\/ANY_PLATFORM\/SOCIAL_ENGINEERING\/URL\/"
            },
            "cacheDuration":"300s",
            "threatEntryType":"URL"
         }
      ]
   }
}

两种输出都将取决于您在 config/safe-urls.php 文件中设置的选项。

测试 URL

以下是一些在实验 Laravel 包时可以使用的方便的测试 URL。

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

从本包的根目录内部

$ ./run_phpunit YOUR_GOOGLE_API_KEY

YOUR_GOOGLE_API_KEY 替换为您的密钥。您可以通过访问 https://developers.google.com/safe-browsing/v4/get-started 来获取密钥。

贡献

有关详细信息,请参阅 contributing.md

安全

如果您发现任何与安全相关的问题,请通过电子邮件而不是问题跟踪器联系作者。

致谢

许可

有关更多信息,请参阅许可文件