-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic rules transformation (except for v1 <-> v2 conversion) #1223
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,8 @@ internal class FortifyFprConverter : ToolFileConverterBase | |
private string _originalUriBasePath; | ||
private List<Result> _results = new List<Result>(); | ||
private HashSet<FileData> _files; | ||
private Dictionary<string, Rule> _ruleDictionary; | ||
private List<Rule> _rules; | ||
private Dictionary<string, int> _ruleIdToIndexMap; | ||
private Dictionary<ThreadFlowLocation, string> _tflToNodeIdDictionary; | ||
private Dictionary<ThreadFlowLocation, string> _tflToSnippetIdDictionary; | ||
private Dictionary<Location, string> _locationToSnippetIdDictionary; | ||
|
@@ -50,7 +51,8 @@ public FortifyFprConverter() | |
|
||
_results = new List<Result>(); | ||
_files = new HashSet<FileData>(FileData.ValueComparer); | ||
_ruleDictionary = new Dictionary<string, Rule>(); | ||
_rules = new List<Rule>(); | ||
_ruleIdToIndexMap = new Dictionary<string, int>(); | ||
_tflToNodeIdDictionary = new Dictionary<ThreadFlowLocation, string>(); | ||
_tflToSnippetIdDictionary = new Dictionary<ThreadFlowLocation, string>(); | ||
_locationToSnippetIdDictionary = new Dictionary<Location, string>(); | ||
|
@@ -90,7 +92,8 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm | |
_invocation.ToolNotifications = new List<Notification>(); | ||
_results.Clear(); | ||
_files.Clear(); | ||
_ruleDictionary.Clear(); | ||
_rules.Clear(); | ||
_ruleIdToIndexMap.Clear(); | ||
_tflToNodeIdDictionary.Clear(); | ||
_tflToSnippetIdDictionary.Clear(); | ||
_locationToSnippetIdDictionary.Clear(); | ||
|
@@ -116,7 +119,7 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm | |
Files = new List<FileData>(_files), | ||
Tool = tool, | ||
Invocations = new[] { _invocation }, | ||
Resources = new Resources { Rules = _ruleDictionary} | ||
Resources = new Resources { Rules = _rules } | ||
}; | ||
|
||
if (!string.IsNullOrWhiteSpace(_originalUriBasePath)) | ||
|
@@ -342,6 +345,11 @@ private void ParseVulnerability() | |
if (AtStartOfNonEmpty(_strings.ClassId)) | ||
{ | ||
result.RuleId = _reader.ReadElementContentAsString(); | ||
|
||
if (_ruleIdToIndexMap.TryGetValue(result.RuleId, out int ruleIndex)) | ||
{ | ||
result.RuleIndex = ruleIndex; | ||
} | ||
} | ||
else if (AtStartOfNonEmpty(_strings.ReplacementDefinitions)) | ||
{ | ||
|
@@ -661,7 +669,8 @@ private void ParseRuleFromDescription() | |
} | ||
} | ||
|
||
_ruleDictionary.Add(rule.Id, rule); | ||
_ruleIdToIndexMap[rule.Id] = _ruleIdToIndexMap.Count; | ||
_rules.Add(rule); | ||
} | ||
|
||
private void ParseNodes() | ||
|
@@ -915,38 +924,37 @@ private void AddMessagesToResults() | |
{ | ||
foreach (Result result in _results) | ||
{ | ||
Rule rule; | ||
if (_ruleDictionary.TryGetValue(result.RuleId, out rule)) | ||
{ | ||
Message message = rule.ShortDescription ?? rule.FullDescription; | ||
Dictionary<string, string> replacements = null; | ||
int ruleIndex = _ruleIdToIndexMap[result.RuleId]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Don't you need a test similar to the old code, where you do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't believe so. i think the previous test was unnecessary, as the fortify report consistently contains rules metadata. will verify. In reply to: 249143447 [](ancestors = 249143447) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. verified this data s.be present, no need for check In reply to: 249147574 [](ancestors = 249147574,249143447) |
||
result.RuleIndex = ruleIndex; | ||
|
||
Rule rule = _rules[ruleIndex]; | ||
Message message = rule.ShortDescription ?? rule.FullDescription; | ||
Dictionary<string, string> replacements = null; | ||
|
||
if (_resultToReplacementDefinitionDictionary.TryGetValue(result, out replacements)) | ||
if (_resultToReplacementDefinitionDictionary.TryGetValue(result, out replacements)) | ||
{ | ||
string messageText = message?.Text; | ||
foreach (string key in replacements.Keys) | ||
{ | ||
string messageText = message?.Text; | ||
foreach (string key in replacements.Keys) | ||
{ | ||
string value = replacements[key]; | ||
string value = replacements[key]; | ||
|
||
if (SupportedReplacementTokens.Contains(key)) | ||
{ | ||
// Replace the token with an embedded hyperlink | ||
messageText = messageText.Replace(string.Format(ReplacementTokenFormat, key), | ||
string.Format(EmbeddedLinkFormat, value)); | ||
} | ||
else | ||
{ | ||
// Replace the token with plain text | ||
messageText = messageText.Replace(string.Format(ReplacementTokenFormat, key), value); | ||
} | ||
if (SupportedReplacementTokens.Contains(key)) | ||
{ | ||
// Replace the token with an embedded hyperlink | ||
messageText = messageText.Replace(string.Format(ReplacementTokenFormat, key), | ||
string.Format(EmbeddedLinkFormat, value)); | ||
} | ||
else | ||
{ | ||
// Replace the token with plain text | ||
messageText = messageText.Replace(string.Format(ReplacementTokenFormat, key), value); | ||
} | ||
|
||
message = message.DeepClone(); | ||
message.Text = messageText; | ||
} | ||
|
||
result.Message = message; | ||
message = message.DeepClone(); | ||
message.Text = messageText; | ||
} | ||
result.Message = message; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,16 +59,9 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm | |
|
||
if (rules.Count > 0) | ||
{ | ||
IDictionary<string, Rule> rulesDictionary = new Dictionary<string, Rule>(); | ||
|
||
foreach (Rule rule in rules) | ||
{ | ||
rulesDictionary[rule.Id] = rule; | ||
} | ||
|
||
run.Resources = new Resources | ||
{ | ||
Rules = rulesDictionary | ||
Rules = rules | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, that's a nice simple change! #ByDesign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: This only works if Fortify doesn't have rule collisions (if it did, a single rule id could map to multiple indices). #Closed