This commit is contained in:
Gabe Stocco 2023-03-21 14:15:40 -07:00 коммит произвёл GitHub
Родитель f591a0acb7
Коммит 49e51a974d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 101 добавлений и 6 удалений

Просмотреть файл

@ -424,21 +424,29 @@ public class RuleProcessor
List<MatchRecord> removes = new();
foreach (var m in resultsList.Where(x => x.Rule?.Overrides?.Count > 0))
foreach (var matchRecord in resultsList.Where(x => x.Rule?.Overrides?.Count > 0))
{
if (cancellationToken?.IsCancellationRequested is true)
{
return resultsList;
}
foreach (var ovrd in (IList<string>?)m.Rule?.Overrides ?? Array.Empty<string>())
foreach (var idToOverride in matchRecord.Rule?.Overrides ?? Array.Empty<string>())
{
// Find all overriden rules and mark them for removal from issues list
foreach (var om in resultsList.FindAll(x => x.Rule?.Id == ovrd))
if (om.Boundary.Index >= m.Boundary.Index &&
om.Boundary.Index <= m.Boundary.Index + m.Boundary.Length)
foreach (var potentialOverriddenMatch in resultsList.FindAll(x => x.Rule?.Id == idToOverride))
{
removes.Add(om);
// Start after or matching start
if (potentialOverriddenMatch.Boundary.Index >= matchRecord.Boundary.Index &&
// End before or matching end
(potentialOverriddenMatch.Boundary.Index + potentialOverriddenMatch.Boundary.Length)
<= (matchRecord.Boundary.Index + matchRecord.Boundary.Length))
{
removes.Add(potentialOverriddenMatch);
}
}
}
}
// Remove overriden rules

Просмотреть файл

@ -1,6 +1,10 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ApplicationInspector.RulesEngine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Microsoft.CST.RecursiveExtractor.FileEntry;
namespace AppInspector.Tests.RuleProcessor;
@ -65,4 +69,87 @@ public class RuleTests
rule.Disabled = true;
Assert.AreEqual(true, rule.Disabled);
}
private const string overrideRules = @"[
{
""id"": ""SA000005"",
""name"": ""Testing.Rules.Overridee"",
""tags"": [
""Testing.Rules.Overridee""
],
""severity"": ""Critical"",
""description"": ""This rule finds car"",
""patterns"": [
{
""pattern"": ""car"",
""type"": ""regex"",
""confidence"": ""High"",
""scopes"": [
""code""
]
}
],
""_comment"": """"
},
{
""id"": ""SA000006"",
""name"": ""Testing.Rules.Overridee"",
""tags"": [
""Testing.Rules.Overridee""
],
""overrides"": [""SA000005""],
""severity"": ""Critical"",
""description"": ""This rule finds racecar"",
""patterns"": [
{
""pattern"": ""racecar"",
""type"": ""regex"",
""confidence"": ""High"",
""scopes"": [
""code""
]
}
],
""_comment"": """"
},
{
""id"": ""SA000007"",
""name"": ""Testing.Rules.Overridee"",
""tags"": [
""Testing.Rules.Overridee""
],
""overrides"": [""SA000005""],
""severity"": ""Critical"",
""description"": ""This rule finds ar"",
""patterns"": [
{
""pattern"": ""ar"",
""type"": ""regex"",
""confidence"": ""High"",
""scopes"": [
""code""
]
}
],
""_comment"": """"
}
]
";
[TestMethod]
public async Task Overrides()
{
RuleSet rules = new();
var originalSource = "TestRules";
rules.AddString(overrideRules, originalSource);
Microsoft.ApplicationInspector.RulesEngine.RuleProcessor processor = new(rules, new RuleProcessorOptions());
var entry = await FromStreamAsync("dummy", new MemoryStream(Encoding.UTF8.GetBytes("racecar car")));
var langs = new Microsoft.ApplicationInspector.RulesEngine.Languages();
langs.FromFileNameOut("dummy.cs", out LanguageInfo info);
var results = processor.AnalyzeFile(entry, info);
Assert.AreEqual(4, results.Count);
Assert.AreEqual(1, results.Count(x=> x.Rule.Id == "SA000006"));
Assert.AreEqual(1, results.Count(x=> x.Rule.Id == "SA000005"));
Assert.AreEqual(2, results.Count(x=> x.Rule.Id == "SA000007"));
}
}