From 34398957797cac698462deedb4af4ac58a283a39 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 3 Sep 2019 19:04:23 -0400 Subject: [PATCH] Normalize URLs before establishing feed uniqueness --- lib/Database.php | 3 +++ tests/cases/Database/SeriesSubscription.php | 11 ++++++++--- tests/cases/Misc/TestURL.php | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index 875a36b6..71febcc1 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -12,6 +12,7 @@ use JKingWeb\Arsse\Misc\Query; use JKingWeb\Arsse\Context\Context; use JKingWeb\Arsse\Misc\Date; use JKingWeb\Arsse\Misc\ValueInfo; +use JKingWeb\Arsse\Misc\URL; /** The high-level interface with the database * @@ -992,6 +993,8 @@ class Database { * @param boolean $discover Whether to perform newsfeed discovery if $url points to an HTML document */ public function feedAdd(string $url, string $fetchUser = "", string $fetchPassword = "", bool $discover = true): int { + // normalize the input URL + $url = URL::normalize($url); // check to see if the feed already exists $check = $this->db->prepare("SELECT id from arsse_feeds where url = ? and username = ? and password = ?", "str", "str", "str"); $feedID = $check->run($url, $fetchUser, $fetchPassword)->getValue(); diff --git a/tests/cases/Database/SeriesSubscription.php b/tests/cases/Database/SeriesSubscription.php index f812f80d..fbf0b64f 100644 --- a/tests/cases/Database/SeriesSubscription.php +++ b/tests/cases/Database/SeriesSubscription.php @@ -202,9 +202,10 @@ trait SeriesSubscription { $url = "http://example.org/feed1"; $feedID = $this->nextID("arsse_feeds"); Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException($url, new \PicoFeed\Client\InvalidUrlException())); + $this->assertException("invalidUrl", "Feed"); try { Arsse::$db->subscriptionAdd($this->user, $url, "", "", false); - } catch (FeedException $e) { + } finally { Phake::verify(Arsse::$user)->authorize($this->user, "subscriptionAdd"); Phake::verify(Arsse::$db)->feedUpdate($feedID, true); $state = $this->primeExpectations($this->data, [ @@ -212,8 +213,6 @@ trait SeriesSubscription { 'arsse_subscriptions' => ['id','owner','feed'], ]); $this->compareExpectations(static::$drv, $state); - $this->assertException("invalidUrl", "Feed"); - throw $e; } } @@ -223,6 +222,12 @@ trait SeriesSubscription { Arsse::$db->subscriptionAdd($this->user, $url); } + public function testAddADuplicateSubscriptionWithEquivalentUrl() { + $url = "http://EXAMPLE.COM/feed2"; + $this->assertException("constraintViolation", "Db", "ExceptionInput"); + Arsse::$db->subscriptionAdd($this->user, $url); + } + public function testAddADuplicateSubscriptionViaRedirection() { $url = "http://localhost:8000/Feed/Parsing/Valid"; Arsse::$db->subscriptionAdd($this->user, $url); diff --git a/tests/cases/Misc/TestURL.php b/tests/cases/Misc/TestURL.php index 3c08a3d4..44bfd05c 100644 --- a/tests/cases/Misc/TestURL.php +++ b/tests/cases/Misc/TestURL.php @@ -23,6 +23,8 @@ class TestURL extends \JKingWeb\Arsse\Test\AbstractTest { return [ ["/", "/"], ["//example.com/", "//example.com/"], + ["/ ", "/ "], + ["//EXAMPLE.COM/", "//EXAMPLE.COM/"], ["http://example.com/", "http://example.com/"], ["HTTP://example.com/", "http://example.com/"], ["http://example.com", "http://example.com/"],