1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-29 18:34:44 +00:00

Protect against excessively recursive functions

This commit is contained in:
jeremy@jermolene.com 2022-07-21 09:17:57 +01:00
parent 745707fea3
commit 17c9bf7546
2 changed files with 32 additions and 3 deletions

View File

@ -330,7 +330,7 @@ exports.compileFilter = function(filterString) {
})()); })());
}); });
// Return a function that applies the operations to a source iterator of tiddler titles // Return a function that applies the operations to a source iterator of tiddler titles
var compiled = $tw.perf.measure("filter: " + filterString,function filterFunction(source,widget) { var fnMeasured = $tw.perf.measure("filter: " + filterString,function filterFunction(source,widget) {
if(!source) { if(!source) {
source = self.each; source = self.each;
} else if(typeof source === "object") { // Array or hashmap } else if(typeof source === "object") { // Array or hashmap
@ -345,6 +345,17 @@ exports.compileFilter = function(filterString) {
}); });
return results.toArray(); return results.toArray();
}); });
var fnGuarded = function(source,widget) {
var results;
self.filterRecursionCount = (self.filterRecursionCount || 0) + 1;
if(self.filterRecursionCount < 300) {
results = fnMeasured(source,widget);
} else {
results = ["/**-- Excessive filter recursion --**/"];
}
self.filterRecursionCount = self.filterRecursionCount - 1;
return results;
};
if(this.filterCacheCount >= 2000) { if(this.filterCacheCount >= 2000) {
// To prevent memory leak, we maintain an upper limit for cache size. // To prevent memory leak, we maintain an upper limit for cache size.
// Reset if exceeded. This should give us 95% of the benefit // Reset if exceeded. This should give us 95% of the benefit
@ -352,9 +363,9 @@ exports.compileFilter = function(filterString) {
this.filterCache = Object.create(null); this.filterCache = Object.create(null);
this.filterCacheCount = 0; this.filterCacheCount = 0;
} }
this.filterCache[filterString] = compiled; this.filterCache[filterString] = fnGuarded;
this.filterCacheCount++; this.filterCacheCount++;
return compiled; return fnGuarded;
}; };
})(); })();

View File

@ -0,0 +1,18 @@
title: Functions/RecursiveFunctions
description: Recursive functions
type: text/vnd.tiddlywiki-multiple
tags: [[$:/tags/wiki-test-spec]]
title: Output
\whitespace trim
\function .buffalo(p)
[.buffalo<p>]
\end
<$text text=<<.buffalo 8>>/>
+
title: ExpectedResult
/**-- Excessive filter recursion --**/