Skip to content

Commit 3235b44

Browse files
committed
More secure use of linq inside OutputCacheManager.RemoveItems.
The use of linq to generate keys is sometimes producing weird behaviour, like producing NullReferenceExceptions. We have only this issue on heavy loaded production servers, this will need more investigation.
1 parent 13226bf commit 3235b44

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

DevTrends.MvcDonutCaching/KeyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public string BuildKey(string controllerName, string actionName)
3232

3333
public string BuildKey(string controllerName, string actionName, RouteValueDictionary routeValues)
3434
{
35-
var builder = new StringBuilder(_cacheKeyPrefix);
35+
var builder = new StringBuilder(CacheKeyPrefix);
3636

3737
if (controllerName != null)
3838
{

DevTrends.MvcDonutCaching/OutputCacheManager.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class OutputCacheManager : IExtendedOutputCacheManager
1212
private readonly OutputCacheProvider _outputCacheProvider;
1313
private readonly IKeyBuilder _keyBuilder;
1414

15-
public OutputCacheManager() : this(OutputCache.Instance, new KeyBuilder())
15+
public OutputCacheManager()
16+
: this(OutputCache.Instance, new KeyBuilder())
1617
{
1718
}
1819

@@ -110,14 +111,27 @@ public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction]
110111

111112
var key = _keyBuilder.BuildKey(controllerName, actionName);
112113

113-
var keysToDelete = enumerableCache.Where(x => x.Key.StartsWith(key)).Select(x => x.Key);
114+
if (string.IsNullOrEmpty(key))
115+
{
116+
return;
117+
}
118+
119+
var keysToDelete = enumerableCache
120+
.Where(_ => !string.IsNullOrEmpty(_.Key) && _.Key.StartsWith(key))
121+
.Select(_ => _.Key);
114122

115123
if (routeValues != null)
116124
{
117125
foreach (var routeValue in routeValues)
118126
{
119-
var value = routeValue;
120-
keysToDelete = keysToDelete.Where(x => x.Contains(_keyBuilder.BuildKeyFragment(value)));
127+
var keyFrag = _keyBuilder.BuildKeyFragment(routeValue);
128+
129+
if (string.IsNullOrEmpty(keyFrag))
130+
{
131+
continue;
132+
}
133+
134+
keysToDelete = keysToDelete.Where(_ => !string.IsNullOrEmpty(_) && _.Contains(keyFrag));
121135
}
122136
}
123137

0 commit comments

Comments
 (0)