Skip to content

Commit

Permalink
Fixes for #259, #278. Includes correction for unique tags option that…
Browse files Browse the repository at this point in the history
… was not functioning right due to clause misidentification. Improves code by reducing near redundant ScanResult structure to use MatchResult directly and avoid duplication. Adds back 3 test cases post OAT integration. (#292)
  • Loading branch information
guyacosta committed Sep 21, 2020
1 parent 34ee510 commit 79f4b09
Show file tree
Hide file tree
Showing 18 changed files with 311 additions and 298 deletions.
2 changes: 1 addition & 1 deletion AppInspector.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static int VerifyOutputArgsRun(CLIPackRulesCmdOptions options)
WriteOnce.Log = logger;
options.Log = logger;

if (options.RepackDefaultRules && !string.IsNullOrEmpty(options.OutputFilePath)) //dependent local files won't be there; TODO look into dir copy to target!
if (options.RepackDefaultRules && !string.IsNullOrEmpty(options.OutputFilePath))
{
WriteOnce.Info("output file argument ignored for -d option");
}
Expand Down
61 changes: 24 additions & 37 deletions AppInspector.CLI/Writers/AnalyzeHtmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ private void WriteHtmlResult()
hashData.Add(outerKey, KeyedSortedTagInfoLists[outerKey]);
}

//add summary metadata lists TODO remove all these as we already passed metadata obj
hashData["cputargets"] = _appMetaData.CPUTargets;
hashData["apptypes"] = _appMetaData.AppTypes;
hashData["packagetypes"] = _appMetaData.PackageTypes;
Expand Down Expand Up @@ -292,16 +291,16 @@ private List<TagInfo> GetTagInfoListByTagGroup(TagGroup tagGroup, bool addNotFou
result.Add(new TagInfo
{
Tag = tagItem,
Confidence = match.PatternConfidence,
Severity = match.Severity,
Confidence = match.Confidence.ToString(),
Severity = match.Severity.ToString(),
ShortTag = pattern.DisplayName,
StatusIcon = pattern.DetectedIcon,
Detected = true
});

hashSet.Add(pattern.SearchPattern);

pattern.Confidence = match.PatternConfidence;
pattern.Confidence = match.Confidence.ToString();
}
else
{
Expand All @@ -310,20 +309,18 @@ private List<TagInfo> GetTagInfoListByTagGroup(TagGroup tagGroup, bool addNotFou
{
if (updateItem.Tag == tagItem)
{
Confidence oldConfidence, newConfidence;
Confidence oldConfidence;
Enum.TryParse(updateItem.Confidence, out oldConfidence);
Enum.TryParse(match.PatternConfidence, out newConfidence);

if (newConfidence > oldConfidence)
if (match.Confidence > oldConfidence)
{
updateItem.Confidence = match.PatternConfidence;
pattern.Confidence = match.PatternConfidence;
updateItem.Confidence = match.Confidence.ToString();
pattern.Confidence = match.Confidence.ToString();
}

Severity oldSeverity, newtSeverity;
Severity oldSeverity;
Enum.TryParse(updateItem.Severity, out oldSeverity);
Enum.TryParse(match.Severity, out newtSeverity);
if (newtSeverity > oldSeverity)
if (match.Severity > oldSeverity)
{
updateItem.Severity = match.Severity.ToString();
}
Expand Down Expand Up @@ -385,7 +382,7 @@ private List<TagInfo> GetAllMatchingTagInfoList(TagGroup tagGroup, bool addNotFo
result.Add(new TagInfo
{
Tag = tagItem,
Confidence = match.PatternConfidence,
Confidence = match.Confidence.ToString(),
Severity = match.Severity.ToString(),
ShortTag = tagItem.Substring(tagItem.LastIndexOf('.') + 1),
StatusIcon = pattern.DetectedIcon,
Expand All @@ -400,20 +397,18 @@ private List<TagInfo> GetAllMatchingTagInfoList(TagGroup tagGroup, bool addNotFo
{
if (updateItem.Tag == tagItem)
{
Confidence oldConfidence, newConfidence;
Confidence oldConfidence;
Enum.TryParse(updateItem.Confidence, out oldConfidence);
Enum.TryParse(match.PatternConfidence, out newConfidence);

if (newConfidence > oldConfidence)
if (match.Confidence > oldConfidence)
{
updateItem.Confidence = match.PatternConfidence;
pattern.Confidence = match.PatternConfidence;
updateItem.Confidence = match.Confidence.ToString();
pattern.Confidence = match.Confidence.ToString();
}

Severity oldSeverity, newtSeverity;
Severity oldSeverity;
Enum.TryParse(updateItem.Severity, out oldSeverity);
Enum.TryParse(match.Severity, out newtSeverity);
if (newtSeverity > oldSeverity)
if (match.Severity > oldSeverity)
{
updateItem.Severity = match.Severity.ToString();
}
Expand Down Expand Up @@ -453,7 +448,7 @@ private List<TagInfo> GetTagInfoListByName()
result.Add(new TagInfo
{
Tag = testTag,
Confidence = match.PatternConfidence,
Confidence = match.Confidence.ToString(),
Severity = match.Severity.ToString(),
ShortTag = testTag.Substring(testTag.LastIndexOf('.') + 1),
});
Expand All @@ -470,7 +465,6 @@ private List<TagInfo> GetTagInfoListByName()

/// <summary>
/// Tags sorted by confidence
/// Todo: address array of tags in rule
/// </summary>
/// <returns></returns>
private List<TagInfo> GetTagInfoListByConfidence()
Expand All @@ -482,23 +476,20 @@ private List<TagInfo> GetTagInfoListByConfidence()
foreach (string tag in _appMetaData.UniqueTags)
{
var searchPattern = new Regex(tag, RegexOptions.IgnoreCase);
foreach (Confidence test in confidences)
foreach (Confidence confidence in confidences)
{
foreach (var match in _appMetaData.Matches)
{
foreach (string testTag in match.Tags)
{
if (searchPattern.IsMatch(testTag))
{
Confidence matchConfidence;
Enum.TryParse(match.PatternConfidence, out matchConfidence);

if (matchConfidence == test && dupCheck.Add(tag))
if (match.Confidence == confidence && dupCheck.Add(tag))
{
result.Add(new TagInfo
{
Tag = testTag,
Confidence = test.ToString(),
Confidence = confidence.ToString(),
Severity = match.Severity.ToString(),
ShortTag = testTag.Substring(testTag.LastIndexOf('.') + 1),
});
Expand All @@ -523,26 +514,22 @@ private List<TagInfo> GetTagInfoListBySeverity()

foreach (string tag in _appMetaData.UniqueTags)
{
// TODO: How frequently are these generated? Cache?
var searchPattern = new Regex(tag, RegexOptions.IgnoreCase);
foreach (Severity test in severities)
foreach (Severity severity in severities)
{
foreach (var match in _appMetaData.Matches)
{
foreach (string testTag in match.Tags)
{
if (searchPattern.IsMatch(testTag))
{
Severity matchSeverity;
Enum.TryParse(match.Severity, out matchSeverity);

if (matchSeverity == test && dupCheck.Add(tag))
if (match.Severity == severity && dupCheck.Add(tag))
{
result.Add(new TagInfo
{
Tag = testTag,
Confidence = match.Severity,
Severity = test.ToString(),
Confidence = match.Confidence.ToString(),
Severity = severity.ToString(),
ShortTag = testTag.Substring(testTag.LastIndexOf('.') + 1),
});
}
Expand Down
9 changes: 5 additions & 4 deletions AppInspector.CLI/Writers/AnalyzeTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Microsoft.ApplicationInspector.Commands;
using Microsoft.ApplicationInspector.RulesEngine;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -153,16 +154,16 @@ public void WriteAppMeta(MetaData metaData)
public void WriteMatch(MatchRecord match)
{
string output = _formatString.Replace("%F", match.FileName);
output = output.Replace("%l", match.Language.Name);
output = output.Replace("%t", match.Language.Type.ToString());
output = output.Replace("%l", match.LanguageInfo.Name);
output = output.Replace("%t", match.LanguageInfo.Type.ToString());
output = output.Replace("%L", match.StartLocationLine.ToString());
output = output.Replace("%C", match.StartLocationColumn.ToString());
output = output.Replace("%l", match.EndLocationLine.ToString());
output = output.Replace("%c", match.EndLocationColumn.ToString());
output = output.Replace("%R", match.RuleId);
output = output.Replace("%N", match.RuleName);
output = output.Replace("%S", match.Severity);
output = output.Replace("%X", match.PatternConfidence);
output = output.Replace("%S", match.Severity.ToString());
output = output.Replace("%X", match.Confidence.ToString());
output = output.Replace("%D", match.RuleDescription);
output = output.Replace("%m", match.Sample);
output = output.Replace("%T", string.Join(',', match.Tags));
Expand Down
2 changes: 0 additions & 2 deletions AppInspector.CLI/html/partials/_report_summary.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,11 @@
<tr>
<td class="font-weight-bold" style="font-size:16px;text-align:left;">Rule Tag</td>
<td class="font-weight-bold" style="font-size:16px;text-align:center;">Count</td>
<td class="font-weight-bold" style="font-size:16px;text-align:center;">Include as Match</td>
</tr>
{% for counter in tagcounters -%}
<tr>
<td style="font-size:16px;text-align:left;">{{counter.tag}}</td>
<td style="font-size:16px;text-align:center;">{{counter.count}}</td>
<td style="font-size:16px;text-align:center;">{{counter.include_as_match}}</td>
</tr>
{% endfor -%}
</table>
Expand Down
11 changes: 8 additions & 3 deletions AppInspector.CLI/html/resources/js/appinspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
$('#file_listing_modal').on('click', 'a.content-link', (e) => {
const content = $(e.target).data('excerpt');
const startLocationLine = $(e.target).data('startLocationLine');
const endLocationLine = $(e.target).data('endLocationLine');
const editor = ace.edit("editor");

editor.setOption('firstLineNumber', startLocationLine);
editor.getSession().setValue(content);
editor.resize();
editor.scrollToLine(0);
editor.gotoLine(endLocationLine - startLocationLine + 1);

$('editor-container').removeClass('d-none');
});

Expand Down Expand Up @@ -127,7 +130,7 @@ class TemplateInsertion {
combineConfidence(a, b) {
if (a && !b) return a;
if (b && !a) return b;
if (!a && !b) return 'Low';
if (!a && !b) return 'low';

const _a = a.toLowerCase();
const _b = b.toLowerCase();
Expand Down Expand Up @@ -156,12 +159,14 @@ class TemplateInsertion {
if (match.ruleId === ruleId || match.ruleName === ruleId) {
let $li = $('<li>');
let $a = $('<a>');
let $l = match.startLocationLine-3;
if ($l < 0) $l = 1; //fix #183
let $l = match.startLocationLine - 3;
let $e = match.endLocationLine;
if ($l <= 0) $l = 1; //fix #183
$a.addClass('content-link')
.attr('href', '#')
.data('excerpt', excerpt)
.data('startLocationLine', $l)
.data('endLocationLine', $e)
.text(removePrefix(match.fileName));
$li.append($a);
$('#file_listing_modal ul').append($li);
Expand Down

0 comments on commit 79f4b09

Please sign in to comment.