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.",
+ )