From 8cfd5f1d1ee2a8159a728aa51de2f20ea4f47bdd Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 20 Feb 2016 10:36:52 -0800 Subject: [PATCH] move blacklist checking into a function --- controllers/API.php | 8 ++++++++ controllers/Controller.php | 11 +---------- lib/Telegraph/Webmention.php | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/controllers/API.php b/controllers/API.php index efa85fd..71a1871 100644 --- a/controllers/API.php +++ b/controllers/API.php @@ -84,6 +84,14 @@ class API { ]); } + # Check the blacklist of domains that are known to not accept webmentions + if($target && !Telegraph\Webmention::isProbablySupported($target)) { + return $this->respond($response, 400, [ + 'error' => 'not_supported', + 'error_description' => 'The target domain is known to not accept webmentions. If you believe this is in error, please file an issue at https://github.com/aaronpk/Telegraph/issues' + ]); + } + # Synchronously check the source URL and verify that it actually contains # a link to the target. This way we prevent this API from sending known invalid mentions. $sourceData = $this->http->get($source); diff --git a/controllers/Controller.php b/controllers/Controller.php index cb812ef..1121359 100644 --- a/controllers/Controller.php +++ b/controllers/Controller.php @@ -213,16 +213,7 @@ class Controller { $targetURL = $request->get('target'); - // Reject links that are known to not accept webmentions - $host = str_replace('www.','',parse_url($targetURL, PHP_URL_HOST)); - - $unsupported = [ - 'twitter.com', - 'instagram.com', - 'facebook.com', - ]; - - if(!$host || in_array($host, $unsupported) || preg_match('/.+\.amazonaws\.com/', $host)) { + if(!Telegraph\Webmention::isProbablySupported($targetURL)) { $status = 'none'; $cached = -1; } else { diff --git a/lib/Telegraph/Webmention.php b/lib/Telegraph/Webmention.php index cc24f5a..bf292af 100644 --- a/lib/Telegraph/Webmention.php +++ b/lib/Telegraph/Webmention.php @@ -7,6 +7,28 @@ class Webmention { private static $http = false; + // Returns false if the target URL is known to not accept webmentions + public static function isProbablySupported($targetURL) { + // Reject links that are known to not accept webmentions + $host = str_replace('www.','',parse_url($targetURL, PHP_URL_HOST)); + + if(!$host) return false; + + $unsupported = [ + 'twitter.com', + 'instagram.com', + 'facebook.com', + ]; + + if(in_array($host, $unsupported)) + return false; + + if(preg_match('/.+\.amazonaws\.com/', $host)) + return false; + + return true; + } + private static function updateStatus($webmention, $http_code, $code, $raw=null) { $status = ORM::for_table('webmention_status')->create(); $status->webmention_id = $webmention->id;