A free, self-hostable news aggregator…
Improve WebSub security (#9330)
Alexandre Alapetite64f7f24c6e2c
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.phpindex afc8ce50d..3a0b9a7da 100644--- a/app/Controllers/feedController.php+++ b/app/Controllers/feedController.php@@ -775,14 +775,18 @@ class FreshRSS_feed_Controller extends FreshRSS_ActionController {$feedProperties['url'] = $feed->url();} elseif ($simplePiePush !== null && $selfUrl !== '' && $selfUrl !== $feed->url()) { // selfUrl has priority for WebSub// https://github.com/pubsubhubbub/PubSubHubbub/wiki/Moving-Feeds-or-changing-Hubs- Minz_Log::debug('WebSub unsubscribe ' . $feed->url(includeCredentials: false));- if (!$feed->pubSubHubbubSubscribe(false)) { //Unsubscribe- Minz_Log::warning('Error while WebSub unsubscribing from ' . $feed->url(includeCredentials: false));+ if (str_starts_with($feed->url(), 'https://') && !str_starts_with($selfUrl, 'https://')) {+ Minz_Log::debug('WebSub: refusing to downgrade to ' . \SimplePie\Misc::url_remove_credentials($selfUrl));+ } else {+ Minz_Log::debug('WebSub unsubscribe ' . $feed->url(includeCredentials: false));+ if (!$feed->pubSubHubbubSubscribe(false)) { //Unsubscribe+ Minz_Log::warning('Error while WebSub unsubscribing from ' . $feed->url(includeCredentials: false));+ }+ $feed->_url($selfUrl);+ Minz_Log::warning('Feed ' . \SimplePie\Misc::url_remove_credentials($url) .+ ' canonical address moved to ' . $feed->url(includeCredentials: false));+ $feedProperties['url'] = $feed->url();}- $feed->_url($selfUrl);- Minz_Log::warning('Feed ' . \SimplePie\Misc::url_remove_credentials($url) .- ' canonical address moved to ' . $feed->url(includeCredentials: false));- $feedProperties['url'] = $feed->url();}if ($simplePie != null) {diff --git a/app/Models/Feed.php b/app/Models/Feed.phpindex c7b0b4111..3f45cd300 100644--- a/app/Models/Feed.php+++ b/app/Models/Feed.php@@ -1360,12 +1360,18 @@ class FreshRSS_Feed extends Minz_Model {//<WebSub>public function pubSubHubbubEnabled(): bool {+ // Whether the current user is enrolled in the WebSub of that topic+ $currentUser = Minz_User::name() ?? '';+ if ($currentUser === '') {+ return false;+ }$url = $this->selfUrl ?: $this->url;- $hubFilename = PSHB_PATH . '/feeds/' . sha1($url) . '/!hub.json';- if (($hubFile = @file_get_contents($hubFilename)) != false) {+ $path = PSHB_PATH . '/feeds/' . sha1($url);+ if (($hubFile = @file_get_contents($path . '/!hub.json')) != false) {$hubJson = json_decode($hubFile, true);if (is_array($hubJson) && empty($hubJson['error']) &&- (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {+ (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time()) &&+ file_exists($path . '/' . $currentUser . '.txt')) {return true;}}@@ -1399,6 +1405,7 @@ class FreshRSS_Feed extends Minz_Model {$this->hubUrl !== '' && $this->selfUrl !== '' && @is_dir(PSHB_PATH)) {$path = PSHB_PATH . '/feeds/' . sha1($this->selfUrl);$hubFilename = $path . '/!hub.json';+ $currentUser = Minz_User::name() ?? '';if (($hubFile = @file_get_contents($hubFilename)) != false) {$hubJson = json_decode($hubFile, true);if (!is_array($hubJson) || empty($hubJson['key']) || !is_string($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {@@ -1407,6 +1414,16 @@ class FreshRSS_Feed extends Minz_Model {Minz_Log::warning($text, PSHB_LOG);return false;}+ if (($hubJson['hub'] ?? null) !== $this->hubUrl) {+ if (FreshRSS_user_Controller::checkUsername($currentUser) && file_exists($path . '/' . $currentUser . '.txt')) {+ unlink($path . '/' . $currentUser . '.txt');+ }+ $text = 'WebSub: Hub ' . $this->hubUrl . ' currently advertised by ' . $this->url .+ ' does not match the hub file! Not subscribing ' . $currentUser;+ Minz_Log::warning($text);+ Minz_Log::warning($text, PSHB_LOG);+ return false;+ }if (!empty($hubJson['lease_end']) && is_int($hubJson['lease_end']) && $hubJson['lease_end'] < (time() + (3600 * 23))) { //TODO: Make a better policy$text = 'WebSub lease ends at '. date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])@@ -1420,7 +1437,7 @@ class FreshRSS_Feed extends Minz_Model {}} else {@mkdir($path, 0770, true);- $key = sha1($path . FreshRSS_Context::systemConf()->salt);+ $key = bin2hex(random_bytes(32));$hubJson = ['hub' => $this->hubUrl,'key' => $key,@@ -1432,7 +1449,6 @@ class FreshRSS_Feed extends Minz_Model {Minz_Log::debug($text);Minz_Log::debug($text, PSHB_LOG);}- $currentUser = Minz_User::name() ?? '';if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {touch($path . '/' . $currentUser . '.txt');}diff --git a/p/api/pshb.php b/p/api/pshb.phpindex acba28e7e..4eaaf4cc8 100644--- a/p/api/pshb.php+++ b/p/api/pshb.php@@ -128,10 +128,10 @@ if ($httpLink !== '' && preg_match_all('/<([^>]+)>;\\s*rel="([^"]+)"/', $httpLin}}-if (FreshRSS_http_Util::compareUrlIgnoringHttps($self, $canonical) !== 0) {- //header('HTTP/1.1 422 Unprocessable Entity');+if ($self !== '' && FreshRSS_http_Util::compareUrlIgnoringHttps($self, $canonical) !== 0) {+ header('HTTP/1.1 422 Unprocessable Entity');Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . $canonical, PSHB_LOG);- //die('Self URL does not match registered canonical URL!');+ die('Self URL does not match registered canonical URL!');}Minz_ExtensionManager::init();