jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1337001?usp=email )

Change subject: textlib: Reject nested wikilinks in file captions
......................................................................

textlib: Reject nested wikilinks in file captions

Do not match a file link when a wikilink in its caption contains
another wikilink. MediaWiki treats this malformed markup as plain
text, so replaceExcept must not skip replacements there.

Prevent the single-bracket fallback from accepting double brackets.
Require each repeated caption alternative to consume characters, avoiding
empty matches that hang the PyPy regex engine on malformed captions.
Keep the existing unbalanced input as a regression test, preserving
its brackets and replacing both x occurrences. Remove expectedFailure
and rename the test to describe the malformed input.

Bug: T396721
Change-Id: Ib1cec8ddd36d1b2454bf176603e841413d9e0b88
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 13 insertions(+), 16 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 44cbcae..58bcc75 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -72,9 +72,8 @@

 # Regex matching file links with optional parameters.
 #
-# Captures the filename and parameters, including nested links
-# within the parameters. The regex safely matches the closing
-# brackets even if inner wikilinks contain [[ or ]].
+# Captures the filename and parameters, including links within the
+# parameters. Inner wikilinks cannot contain nested wikilinks.
 # The Namespace names must be substituted into the pattern, e.g.:
 #     FILE_LINK_REGEX % 'File'
 # or: FILE_LINK_REGEX % '|'.join(site.namespaces[6])
@@ -93,17 +92,16 @@
     (
         \|
         (
-            (
-                (?=(?P<inner_link>
-                    \[\[.*?\]\]
-                ))(?P=inner_link)
-            )?
+            (?=(?P<inner_link>
+                \[\[(?:(?!\[\[).)*?\]\]
+            ))(?P=inner_link)
+        |
             (?=(?P<other_chars>
-                [^\[\]]*
+                [^\[\]]+
             ))(?P=other_chars)
         |
             (?=(?P<not_wikilink>
-                \[[^]]*\]
+                \[(?!\[)[^]]*\]
             ))(?P=not_wikilink)
         )*?
     )??
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 2e0a127..5d13512 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -1448,16 +1448,15 @@
                 'x', 'y', ['file'], site=self.site),
             '[[File:a|[[foo]] [[bar [invalid ]].x]][[y]]')

-    @unittest.expectedFailure  # T396721
-    def test_replace_tag_file_failure(self) -> None:
-        """Test showing limits of the file link regex."""
-        # When the double brackets are unbalanced, the regex
-        # does not correctly detect the end of the file link.
+    def test_replace_tag_file_unbalanced(self) -> None:
+        """Test replacing text in a file link with invalid nested wikilinks."""
+        # MediaWiki does not render this as a file link, so both occurrences
+        # of x should be replaced without changing the malformed brackets.
         self.assertEqual(
             textlib.replaceExcept(
                 '[[File:a|[[foo]] [[bar [[invalid ]].x]][[x]]',
                 'x', 'y', ['file'], site=self.site),
-            '[[File:a|[[foo]] [[bar [invalid] ]].x]][[y]]')
+            '[[File:a|[[foo]] [[bar [[invalid ]].y]][[y]]')

     def test_replace_tags_interwiki(self) -> None:
         """Test replacing not inside interwiki links."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1337001?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib1cec8ddd36d1b2454bf176603e841413d9e0b88
Gerrit-Change-Number: 1337001
Gerrit-PatchSet: 5
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to