Applying a patch with a fuzzFactor can be very very slow
Thanks to the following five security researchers for reporting this:
- [email protected] (Ziyue), https://zyy0530.github.io/
- [email protected] (Chenchen), https://7thparkk.github.io/
- [email protected] (Strick), https://str1ckl4nd.github.io/
- [email protected] (Liyi), https://lzhou1110.github.io/
- [email protected] (Maurice), http://maurice.busystar.org/
Finding 1: Fuzzy patch application can cause algorithmic complexity DoS Affected behavior
applyPatchandapplyPatchessupport fuzzy patch matching. With crafted hunks and a highfuzzFactor, the matching logic can end up exploring many alternatives recursively and spend a large amount of CPU time on a single input.Affected entrypoints
`applyPatch(...)` `applyPatches(...)`Reproduction notes In our reproduction, a normal control case completed in under 1 ms, while a crafted fuzzy patch took about 7.7 seconds on the same setup:
control time: 0.568181 msexploit time: 7737.472789 msslowdown: 13617.9717xThe issue appears to come from recursive branch exploration in the fuzzy patch application logic.
My assorted thoughts follow.
Given the fuzzy-matching logic recurses whenever it pairs a patch line with a non-matching source file line, and it can do that up to fuzzFactor times, I guess the worst case execution time grows exponentially with fuzzFactor.
I nonetheless don't view this as having significant security implications since it will virtually always be the case that:
- the
fuzzFactoris set by the application and not taken from untrusted input fuzzFactoris small- it will occur to an application developer before choosing to use non-zero fuzz factors that this will necessarily have adverse performance implications and plausibly have serious ones, and if this matters to them they will do some benchmarking to ensure the perf they get is acceptable or else take measures to mitigate the problem (like doing their fuzzy-matching within an execution-time-limited subprocess)
I also think this is probably not a serious problem from an ordinary performance perspective either, since, again, fuzzFactor will normally be small!
All that said, it's not ideal. If I'm not mistaken/confused, I think the poor time complexity is surely avoidable! The current fuzzy matching algorithm is effectively using the naive recursive approach to computing Levenshtein distance, when of course much faster approaches are available. As it happens, on various occasions the idea of adding support for Levenshtein diffs to jsdiff has come up on the issue tracker; if I did this, I could use jsdiff's own Levenshtein diffing support to find the optimal pairing of lines from the patch with lines from the file being patched.
(Failing any of that, I could at least warn about the perf implications of a high fuzzFactor in the README!)
I'd like to take this one. My plan is to memoize the fuzzy search on (hunk line, file position, errors left) so each state is explored once, keeping the output identical to today's, with a test that times a pathological fuzzFactor case.
Written with the help of an agent (Claude Code, claude-opus-5-5).
The current fuzzy matching algorithm is effectively using the naive recursive approach to computing Levenshtein distance, when of course much faster approaches are available. As it happens, on various occasions the idea of adding support for Levenshtein diffs to jsdiff has come up on the issue tracker; if I did this, I could use jsdiff's own Levenshtein diffing support to find the optimal pairing of lines from the patch with lines from the file being patched.
I think this past thought of mine doesn't quite make sense, as we're not quite trying to find the Levenshtein distance between two strings, here; rather, we're trying to find a substring (whose exact length is not known) within a limited Levenshtein distance.