Page MenuHomePhabricator

In a compiled module, a sufficiently complex Regex.Replace can loop if \r characters are still in the article text
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue

  • As reported on [[:en:w:Wikipedia talk:AutoWikiBrowser]], create a C# module with Regex.Replace, using an insanely complex RE (that happens to contain \r characters, although removing them does not help).
  • Start to load [[:en:w:German Empire]].

What happens?:
AWB hangs indefinitely (the green bar just animates continuously).

What should have happened instead?:
AWB completes the RE evaluation in 40-50 seconds on a typical laptop, as it does when the equivalent RE replacement is specified in the Advanced F/R instead..

Software version (skip for WMF-hosted wikis like Wikipedia):
AWB 6.2.1.1

The problem is circumvented by removing \r characters from ArticleText before calling the custom module. However, I haven't analyzed why that makes a huge difference, and whether that would introduce any regressions. Sample fix: in Article.cs:SendPageToCustomModule, replace

string strTemp = module.ProcessArticle(processArticleEventArgs.ArticleText,

with

string localText = Tools.ConvertFromLocalLineEndings(processArticleEventArgs.ArticleText);
string strTemp = module.ProcessArticle(localText,

and

_customModuleMadeChanges = !strTemp.Equals(processArticleEventArgs.ArticleText);

with

_customModuleMadeChanges = !strTemp.Equals(localText);

Event Timeline

DavidBrooks renamed this task from A sufficiently complex Regex.Replace can loop if \r characters are still in the article text to In a compiled module, a sufficiently complex Regex.Replace can loop if \r characters are still in the article text.Nov 6 2023, 10:54 PM

Thinking about it overnight, I realize the suggested fix could be a regression, for those module authors who assume \r\n sequences in the text. An alternative would be to set a timeout on all user-supplied REs (say 2 minutes) and catch the exception with a "please simplify" dialog. But that would be a more significant amount of coding. Given that it's a rare occurrence (i.e. reported once after all this time), Won't Fix would be fine with me.

Regex backtracking is an inherent issue/limitation of regex engines. Taking out newlines might help in one case but there is no generic cure-all.

Users building their own C# custom modules can set a regex timeout: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.-ctor?view=netframework-4.5#system-text-regularexpressions-regex-ctor(system-string-system-text-regularexpressions-regexoptions-system-timespan)

It is apparently possible to set an applicationlevel default for that, though in my (brief) testing only the timeout on each regex worked for me: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.matchtimeout?view=netframework-4.5

I don't think I want to enforce a timeout on those using a custom module (users should know what they are doing and/or be prepared to mitigate themselves).

However, a timeout on Find & Replace and Typo Fixing seems sensible as a mitigation.

Rjwilmsi claimed this task.

rev 12651: 1-minute timeout to guard against regex backtracking in F&R, Typos

Will be in next release of AWB

Fair point. My immediate reaction was maybe to put a "an article load hangs and you are using a module or plugin, look to your RE's" in the documentation. But as you say, people should know what they are doing (although the OR apparently didn't figure it out).


From: Rjwilmsi <no-reply@phabricator.wikimedia.org>
Sent: Tuesday, January 14, 2025 6:36 AM
To: Phabricator <no-reply@phabricator.wikimedia.org>
Cc: d_w_brooks@hotmail.com <d_w_brooks@hotmail.com>
Subject: [Maniphest] [Commented On] T350636: In a compiled module, a sufficiently complex Regex.Replace can loop if \r characters are still in the article text

Rjwilmsi added a comment. View Taskhttps://phabricator.wikimedia.org/T350636

I don't think I want to enforce a timeout on those using a custom module (users should know what they are doing and/or be prepared to mitigate themselves).

However, a timeout on Find & Replace and Typo Fixing seems sensible as a mitigation.

TASK DETAIL
https://phabricator.wikimedia.org/T350636

EMAIL PREFERENCES
https://phabricator.wikimedia.org/settings/panel/emailpreferences/

To: Rjwilmsi
Cc: Rjwilmsi, Aklapper, DavidBrooks, Florian_COLLIN, Ganeshk, Eihel, Shaded0, SwiftyPeep, Brightgalrs, JJMC89, Grind24, Magioladitis, Dinoguy1000

Guys, do you think it's time for an official 6.4.0.1 release? Some users have been managing with my private builds.

Since the 6.4.0.0 release, I've worked on Email Auth, changes in rate limitation, and the default removal of notifications from bot-password accounts (which were an approved workaround for the email auth thing). Together they will block any new users. You guys have done many syntax-related improvements, of course.

There doesn't seem to be anyone currently reporting blocked on the talk page, but I don't know if there is anyone who hasn't been following those updates.

Thoughts?

@DavidBrooks From my perspective, happy for there to be an official AWB release whenever you like / after every fix/change of significant use to a user, if you have the access to do the release and the time to do the admin/effort. We can make those releases optional so we don't force users to upgrade too often (if the new releases are frequent and contain changes that most users may not need straight away).

Apologies for the above. Continuing on wikimail.