aboutsummaryrefslogtreecommitdiff
path: root/p/api/pshb.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2022-03-22 23:13:13 +0100
committerGravatar GitHub <noreply@github.com> 2022-03-22 23:13:13 +0100
commite8af54a476a7c311827700dd814b81b43b4180f5 (patch)
tree181d24e085439975276d2c9922282a03776eedf1 /p/api/pshb.php
parentb0a63355b66d0cc431c9b1af96622c2360207565 (diff)
WebSub: Use hash instead of base64 to handle long URLs (#4282)
* WebSub: Use hash instead of base64 to handle long URLs * Use 410 Gone (Part of the WebSub specification https://www.w3.org/TR/websub/ )
Diffstat (limited to 'p/api/pshb.php')
-rw-r--r--p/api/pshb.php35
1 files changed, 15 insertions, 20 deletions
diff --git a/p/api/pshb.php b/p/api/pshb.php
index 9170c1025..26d1e125b 100644
--- a/p/api/pshb.php
+++ b/p/api/pshb.php
@@ -20,28 +20,24 @@ if (!ctype_xdigit($key)) {
die('Invalid feed key format!');
}
chdir(PSHB_PATH);
-$canonical64 = @file_get_contents('keys/' . $key . '.txt');
-if ($canonical64 === false) {
+$canonical = @file_get_contents('keys/' . $key . '.txt');
+if ($canonical === false) {
if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'unsubscribe') {
Minz_Log::warning('Warning: Accept unknown unsubscribe', PSHB_LOG);
header('Connection: close');
exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
}
- header('HTTP/1.1 404 Not Found');
+ header('HTTP/1.1 410 Gone');
Minz_Log::warning('Warning: Feed key not found!: ' . $key, PSHB_LOG);
die('Feed key not found!');
}
-$canonical64 = trim($canonical64);
-if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
- header('HTTP/1.1 500 Internal Server Error');
- Minz_Log::error('Error: Invalid key reference!: ' . $canonical64, PSHB_LOG);
- die('Invalid key reference!');
-}
-$hubFile = @file_get_contents('feeds/' . $canonical64 . '/!hub.json');
+$canonical = trim($canonical);
+$canonicalHash = sha1($canonical);
+$hubFile = @file_get_contents('feeds/' . $canonicalHash . '/!hub.json');
if ($hubFile === false) {
- header('HTTP/1.1 404 Not Found');
+ header('HTTP/1.1 410 Gone');
unlink('keys/' . $key . '.txt');
- Minz_Log::error('Error: Feed info not found!: ' . $canonical64, PSHB_LOG);
+ Minz_Log::error('Error: Feed info not found!: ' . $canonical, PSHB_LOG);
die('Feed info not found!');
}
$hubJson = json_decode($hubFile, true);
@@ -50,18 +46,17 @@ if (!$hubJson || empty($hubJson['key']) || $hubJson['key'] !== $key) {
Minz_Log::error('Error: Invalid key cross-check!: ' . $key, PSHB_LOG);
die('Invalid key cross-check!');
}
-chdir('feeds/' . $canonical64);
+chdir('feeds/' . $canonicalHash);
$users = glob('*.txt', GLOB_NOSORT);
if (empty($users)) {
header('HTTP/1.1 410 Gone');
- $url = base64url_decode($canonical64);
- Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $url, PSHB_LOG);
+ Minz_Log::warning('Warning: Nobody subscribes to this feed anymore!: ' . $canonical, PSHB_LOG);
unlink('../../keys/' . $key . '.txt');
- $feed = new FreshRSS_Feed($url);
+ $feed = new FreshRSS_Feed($canonical);
$feed->pubSubHubbubSubscribe(false);
unlink('!hub.json');
chdir('..');
- recursive_unlink($canonical64);
+ recursive_unlink('feeds/' . $canonicalHash);
die('Nobody subscribes to this feed anymore!');
}
@@ -104,11 +99,11 @@ unset($ORIGINAL_INPUT);
$links = $simplePie->get_links('self');
$self = isset($links[0]) ? $links[0] : null;
-if ($self !== base64url_decode($canonical64)) {
+if ($self !== $canonical) {
//header('HTTP/1.1 422 Unprocessable Entity');
- Minz_Log::warning('Warning: Self URL [' . $self . '] does not match registered canonical URL!: ' . base64url_decode($canonical64), PSHB_LOG);
+ 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!');
- $self = base64url_decode($canonical64);
+ $self = $canonical;
}
Minz_ExtensionManager::init();