Skip to content

Fix item get_base#744

Merged
mblaney merged 3 commits intosimplepie:masterfrom
FreshRSS:fix-base-url
Sep 22, 2022
Merged

Fix item get_base#744
mblaney merged 3 commits intosimplepie:masterfrom
FreshRSS:fix-base-url

Conversation

@Alkarex
Copy link
Contributor

@Alkarex Alkarex commented Aug 30, 2022

SimplePie was not using the best base link for an item content (instead, it was directly using the feed base).
See <xml:base> example in https://datatracker.ietf.org/doc/html/rfc4287#section-1.1
First uses item <xml:base> if it exists, or the item own link, or the feed's base URL rules (feed URL, or Web site URL)
Downstream issue FreshRSS/FreshRSS#4562
Downstream PR FreshRSS/FreshRSS#4565

SimplePie was not using the best base link for an item content (instead, it was directly using the feed base).
See `<xml:base>` example in https://datatracker.ietf.org/doc/html/rfc4287#section-1.1
First uses item `<xml:base>` if it exists, or the item own link, or the feed's base URL rules (feed URL, or Web site URL)
Downstream issue FreshRSS/FreshRSS#4562
Downstream PR FreshRSS/FreshRSS#4565
@Alkarex
Copy link
Contributor Author

Alkarex commented Aug 30, 2022

Example of buggy feed https://about.gitlab.com/atom.xml

https://simplepie.org/demo/?feed=https%3A%2F%2Fabout.gitlab.com%2Fatom.xml

See article https://about.gitlab.com/releases/2022/08/30/critical-security-release-gitlab-15-3-2-released

<a href="#remote-command-execution-via-github-import">Remote Command Execution via GitHub import</a>

Should resolve to https://about.gitlab.com/releases/2022/08/30/critical-security-release-gitlab-15-3-2-released/#remote-command-execution-via-github-import but SimplePie produced https://about.gitlab.com/blog/#remote-command-execution-via-github-import

@Alkarex
Copy link
Contributor Author

Alkarex commented Aug 30, 2022

P.S. I cannot think of a rationale for the current logic of falling back directly to the feed's URL instead of using the item's URL.

@Art4
Copy link
Contributor

Art4 commented Aug 31, 2022

Would it be possible to create a unit test for it? This would help to document and enforce the behavior in the future.

@Alkarex
Copy link
Contributor Author

Alkarex commented Aug 31, 2022

Would it be possible to create a unit test for it? This would help to document and enforce the behavior in the future.

Most likely, but independently of this PR, tests are failing on my machine and I did not look into the details (probably a wrong version constraint in composer.json and/or lack of composer.lock):

$ php -v
PHP 8.1.2 (cli) (built: Jul 21 2022 12:10:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

$ composer test
> phpunit
PHPUnit 9.5.16 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

.............................................................   61 / 1892 (  3%)
.............................................................  122 / 1892 (  6%)
.............................................................  183 / 1892 (  9%)
.............................................................  244 / 1892 ( 12%)
.............................................................  305 / 1892 ( 16%)
.............................................................  366 / 1892 ( 19%)
.............................................................  427 / 1892 ( 22%)
.............................................................  488 / 1892 ( 25%)
.............................................................  549 / 1892 ( 29%)
.............................................................  610 / 1892 ( 32%)
.............................................................  671 / 1892 ( 35%)
.............................................................  732 / 1892 ( 38%)
.............................................................  793 / 1892 ( 41%)
.............................................................  854 / 1892 ( 45%)
.............................................................  915 / 1892 ( 48%)
.............................................................  976 / 1892 ( 51%)
............................................................. 1037 / 1892 ( 54%)
............................................................. 1098 / 1892 ( 58%)
............................................................. 1159 / 1892 ( 61%)
............................................................. 1220 / 1892 ( 64%)
............................................................. 1281 / 1892 ( 67%)
............................................................. 1342 / 1892 ( 70%)
............EEEEEEEEEE..............EEE...................... 1403 / 1892 ( 74%)
............................................................. 1464 / 1892 ( 77%)
............................................................. 1525 / 1892 ( 80%)
............................................................. 1586 / 1892 ( 83%)
............................................................. 1647 / 1892 ( 87%)
.FFF......................................................... 1708 / 1892 ( 90%)
.....PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 3, column 8 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 3, column 8 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.................................................... 1769 / 1892 ( 93%)
............................................................. 1830 / 1892 ( 96%)
............................................................. 1891 / 1892 ( 99%)
.                                                             1892 / 1892 (100%)

Time: 00:05.429, Memory: 22.00 MB

There were 13 errors:

1) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #0 ('application/rss+xml')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

2) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #1 ('application/rdf+xml')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

3) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #2 ('text/rdf')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

4) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #3 ('application/atom+xml')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

5) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #4 ('text/xml')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

6) SimplePie\Tests\Unit\LocatorTest::testAutodiscoverOnFeed with data set #5 ('application/xml')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:86

7) SimplePie\Tests\Unit\LocatorTest::testInvalidMIMEType
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:101

8) SimplePie\Tests\Unit\LocatorTest::testDirectNoDOM
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:116

9) SimplePie\Tests\Unit\LocatorTest::testFailDiscoveryNoDOM
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:131

10) SimplePie\Tests\Unit\LocatorTest::test_from_file with data set #0 (SimplePie\File Object (...))
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\FileMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/LocatorTest.php:186

11) SimplePie\Tests\Unit\MiscTest::test_convert_UTF8_mbstring with data set #0 ('��', '∞', 'EUC-KR')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\MiscWithPublicStaticMethodsMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/MiscTest.php:131

12) SimplePie\Tests\Unit\MiscTest::test_convert_UTF8_iconv with data set #0 (Binary String: 0xfeff221e, '∞', 'UTF-16')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\MiscWithPublicStaticMethodsMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/MiscTest.php:157

13) SimplePie\Tests\Unit\MiscTest::test_convert_UTF8_uconverter with data set #0 (Binary String: 0xfeff221e, '∞', 'UTF-16')
include(/home/alexandre/GitHub/simplepie/library/SimplePie\Tests\Fixtures\MiscWithPublicStaticMethodsMock.php): Failed to open stream: No such file or directory

/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/autoloader.php:126
/home/alexandre/GitHub/simplepie/tests/Unit/MiscTest.php:183

--

There were 3 failures:

1) SimplePie\Tests\Unit\SimplePieTest::testLegacyCallOfSetCacheClass
Failed asserting that exception of type "PHPUnit\Framework\Error\Error" is thrown.

2) SimplePie\Tests\Unit\SimplePieTest::testDirectOverrideNew
Failed asserting that exception of type "SimplePie\Tests\Fixtures\Exception\SuccessException" is thrown.

3) SimplePie\Tests\Unit\SimplePieTest::testDirectOverrideLegacy
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'https://example.com/feed/2019-10-07'
+'http://example.com/feed/'

/home/alexandre/GitHub/simplepie/tests/Unit/SimplePieTest.php:281

ERRORS!
Tests: 1892, Assertions: 2652, Errors: 13, Failures: 3.
Script phpunit handling the test event returned with error code 2

@Alkarex
Copy link
Contributor Author

Alkarex commented Aug 31, 2022

I tried again and this time it worked (though with some warnings). Maybe some unreliable auto-loading?

$ composer test
> phpunit
PHPUnit 9.5.16 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

.............................................................   61 / 1892 (  3%)
.............................................................  122 / 1892 (  6%)
.............................................................  183 / 1892 (  9%)
.............................................................  244 / 1892 ( 12%)
.............................................................  305 / 1892 ( 16%)
.............................................................  366 / 1892 ( 19%)
.............................................................  427 / 1892 ( 22%)
.............................................................  488 / 1892 ( 25%)
.............................................................  549 / 1892 ( 29%)
.............................................................  610 / 1892 ( 32%)
.............................................................  671 / 1892 ( 35%)
.............................................................  732 / 1892 ( 38%)
.............................................................  793 / 1892 ( 41%)
.............................................................  854 / 1892 ( 45%)
.............................................................  915 / 1892 ( 48%)
.............................................................  976 / 1892 ( 51%)
............................................................. 1037 / 1892 ( 54%)
............................................................. 1098 / 1892 ( 58%)
............................................................. 1159 / 1892 ( 61%)
............................................................. 1220 / 1892 ( 64%)
............................................................. 1281 / 1892 ( 67%)
............................................................. 1342 / 1892 ( 70%)
............................................................. 1403 / 1892 ( 74%)
............................................................. 1464 / 1892 ( 77%)
............................................................. 1525 / 1892 ( 80%)
............................................................. 1586 / 1892 ( 83%)
............................................................. 1647 / 1892 ( 87%)
............................................................. 1708 / 1892 ( 90%)
.....PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 3, column 8 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 3, column 8 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.PHP Notice:  is invalid XML, likely due to invalid characters. XML error: Invalid document end at line 5, column 11 in /home/alexandre/GitHub/simplepie/src/SimplePie.php on line 1511
.................................................... 1769 / 1892 ( 93%)
............................................................. 1830 / 1892 ( 96%)
............................................................. 1891 / 1892 ( 99%)
.                                                             1892 / 1892 (100%)

Time: 00:00.335, Memory: 22.00 MB

OK (1892 tests, 2670 assertions)

@pull-request-size pull-request-size bot added size/M and removed size/S labels Aug 31, 2022
*/
public function get_base($element = [])
{
if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The xml_base_explicit is a boolean so it might be slightly easier to understand, if more verbose:

Suggested change
if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) {
if (isset($element['xml_base_explicit']) && $element['xml_base_explicit'] && isset($element['xml_base'])) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same code as

simplepie/src/SimplePie.php

Lines 2071 to 2080 in cce6ad2

public function get_base($element = [])
{
if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) {
return $element['xml_base'];
} elseif ($this->get_link() !== null) {
return $this->get_link();
}
return $this->subscribe_url();
}

I am not found of the proposition, because that is exactly what a standard empty() is for, and I personally find it easier to read, but I do not mind much 🤓

if (!empty($element['xml_base_explicit']) && isset($element['xml_base'])) {
return $element['xml_base'];
}
$link = $this->get_permalink();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder (have not checked) if some pathological feed can create an infinite loop (get_permalinkget_linkget_linksget_base).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not believe it can lead to a loop

if ($link != null) {
return $link;
}
return $this->feed->get_base($element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method looks to me like it already checks the xml:base attribute on $element, so this PR makes the order as follows:

  1. xml:base
  2. Item’s permalink
  3. xml:base
  4. First rel="alternate" link in the feed
  5. Feed URL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but I did not want to change the feeds own method, as it is called from several other places

@Alkarex
Copy link
Contributor Author

Alkarex commented Aug 31, 2022

Would it be possible to create a unit test for it? This would help to document and enforce the behavior in the future.

Here you are 55ccddd

Maybe can help with strange CI bugs
@mblaney mblaney merged commit 33f86e6 into simplepie:master Sep 22, 2022
@Art4 Art4 mentioned this pull request Sep 22, 2022
@Alkarex Alkarex deleted the fix-base-url branch September 22, 2022 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants