From 29cf44b71ca0e579c693c79d8e74c820a42b5910 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Sun, 31 Mar 2024 17:30:44 +0800 Subject: [PATCH] Improved resilience of privacy plugin --- material/plugins/privacy/plugin.py | 16 +++++++++++++--- src/plugins/privacy/plugin.py | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/material/plugins/privacy/plugin.py b/material/plugins/privacy/plugin.py index f7c6fd5b7..31638a785 100644 --- a/material/plugins/privacy/plugin.py +++ b/material/plugins/privacy/plugin.py @@ -33,6 +33,7 @@ from concurrent.futures import Future, ThreadPoolExecutor, wait from hashlib import sha1 from mkdocs.config.config_options import ExtraScriptValue from mkdocs.config.defaults import MkDocsConfig +from mkdocs.exceptions import PluginError from mkdocs.plugins import BasePlugin, event_priority from mkdocs.structure.files import File, Files from mkdocs.utils import is_error_template @@ -241,9 +242,18 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): parser.feed(fragment) parser.close() - # Return element - assert isinstance(parser.result, Element) - return parser.result + # Check parse result and return element + if isinstance(parser.result, Element): + return parser.result + + # Otherwise, raise a plugin error - if the author accidentally used + # invalid HTML inside of the tag, e.g., forget a opening or closing + # quote, we need to catch this here, as we're using pretty basic + # regular expression based extraction + raise PluginError( + f"Could not parse due to possible syntax error in HTML: \n\n" + + fragment + ) # Parse and extract all external assets from a media file using a preset # regular expression, and return all URLs found. diff --git a/src/plugins/privacy/plugin.py b/src/plugins/privacy/plugin.py index f7c6fd5b7..31638a785 100644 --- a/src/plugins/privacy/plugin.py +++ b/src/plugins/privacy/plugin.py @@ -33,6 +33,7 @@ from concurrent.futures import Future, ThreadPoolExecutor, wait from hashlib import sha1 from mkdocs.config.config_options import ExtraScriptValue from mkdocs.config.defaults import MkDocsConfig +from mkdocs.exceptions import PluginError from mkdocs.plugins import BasePlugin, event_priority from mkdocs.structure.files import File, Files from mkdocs.utils import is_error_template @@ -241,9 +242,18 @@ class PrivacyPlugin(BasePlugin[PrivacyConfig]): parser.feed(fragment) parser.close() - # Return element - assert isinstance(parser.result, Element) - return parser.result + # Check parse result and return element + if isinstance(parser.result, Element): + return parser.result + + # Otherwise, raise a plugin error - if the author accidentally used + # invalid HTML inside of the tag, e.g., forget a opening or closing + # quote, we need to catch this here, as we're using pretty basic + # regular expression based extraction + raise PluginError( + f"Could not parse due to possible syntax error in HTML: \n\n" + + fragment + ) # Parse and extract all external assets from a media file using a preset # regular expression, and return all URLs found.