From 1bf72fe42b84f8edc57c0e94f8c8c7d2f7219c9b Mon Sep 17 00:00:00 2001 From: Zerthick Date: Sun, 9 Aug 2026 23:11:59 -0700 Subject: [PATCH] fix: guard enclosure against None/non-positive length values When a remote image returns 404, the length field becomes None, producing invalid XML. Added guard to skip enclosure when length is None or non-positive. - Add guard condition in rss.xml.jinja2 template - Add test verifying no invalid enclosure length values --- mkdocs_rss_plugin/templates/rss.xml.jinja2 | 2 +- tests/test_enclosure_guard.py | 78 ++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/test_enclosure_guard.py diff --git a/mkdocs_rss_plugin/templates/rss.xml.jinja2 b/mkdocs_rss_plugin/templates/rss.xml.jinja2 index 5328672..8950dc1 100644 --- a/mkdocs_rss_plugin/templates/rss.xml.jinja2 +++ b/mkdocs_rss_plugin/templates/rss.xml.jinja2 @@ -52,7 +52,7 @@ {% if item.link is not none %}{{ feed.title }}{% endif %} {% if item.comments_url is not none %}{{ item.comments_url|e }}{% endif %} {% if item.guid is not none %}{{ item.guid }}{% endif %} - {% if item.image is not none %} + {% if item.image is not none and item.image[2] is not none and item.image[2] > 0 %} {% endif %} diff --git a/tests/test_enclosure_guard.py b/tests/test_enclosure_guard.py new file mode 100644 index 0000000..d741503 --- /dev/null +++ b/tests/test_enclosure_guard.py @@ -0,0 +1,78 @@ +#! python3 # noqa: E265 + +"""Test that is omitted when image length is None or non-positive. + +When a remote image returns 404, the length field becomes None, +which would produce invalid XML like . + +Usage from the repo root folder: + + python -m unittest tests.test_enclosure_guard + +""" + +# ############################################################################# +# ########## Libraries ############# +# ################################## + +# Standard library +import logging +import tempfile +from pathlib import Path +from traceback import format_exception + +# test suite +from tests.base import BaseTest + +# -- Globals -- +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +OUTPUT_RSS_FEED_CREATED = "feed_rss_created.xml" + +# ############################################################################# +# ########## Classes ############### +# ################################## + + +class TestEnclosureGuard(BaseTest): + """Test that is properly guarded against invalid length values.""" + + def test_enclosure_guard_none_length(self): + """Verify that is omitted when image length is None or non-positive. + + When a remote image returns 404, the length field becomes None, + which would produce invalid XML like . + """ + with tempfile.TemporaryDirectory() as tmpdirname: + cli_result = self.build_docs_setup( + testproject_path="docs", + mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"), + output_path=tmpdirname, + strict=True, + ) + + if cli_result.exception is not None: + e = cli_result.exception + logger.debug(format_exception(type(e), e, e.__traceback__)) + + self.assertEqual(cli_result.exit_code, 0) + self.assertIsNone(cli_result.exception) + + # Read raw XML content + rss_path = Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED + rss_content = rss_path.read_text() + + # Check that no enclosure has length="None" or length="0" or negative + self.assertNotIn( + 'length="None"', + rss_content, + "Found enclosure with length='None'. " + "The template should guard against None length values.", + ) + self.assertNotIn( + 'length="0"', + rss_content, + "Found enclosure with length='0'. " + "The template should guard against zero length values.", + )