Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ from m in r.Messages
RuleID = r.ID,
HelpUri = r.HelpUri,
Severity = r.Severity,
Diagram = m.Diagram != null ?
report.DiagramSummaries.FirstOrDefault(e => e.ID == m.Diagram.Value) :
Diagram = m.Diagram is Guid diagramId ?
report.DiagramSummaries.FirstOrDefault(e => e.ID == diagramId) :
null,
Entity = m.Entity,
Text = m.Text,
Expand Down
8 changes: 0 additions & 8 deletions src/ThreatModelForge.Analysis.Reporting/ReportWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ namespace ThreatModelForge.Analysis.Reporting
/// </summary>
public abstract class ReportWriter : IDisposable
{
/// <summary>
/// Finalizes an instance of the <see cref="ReportWriter"/> class.
/// </summary>
~ReportWriter()
{
this.Dispose(false);
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public override void Write(ModelReport report)
{
foreach (var ruleReport in modelReport.RuleReports.Where(r => r.Messages.Any() || r.SuppressedMessages.Any()))
{
ReportingDescriptor rule = this.ExtractRule(ruleReport);
_ = this.ExtractRule(ruleReport);
this.PersistResults(sarifLogger, this.ExtractResults(ruleReport));
}

Expand Down
56 changes: 20 additions & 36 deletions src/ThreatModelForge.Analysis.Rules/DataClassificationTagSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,11 @@ public bool TryGetDefaultTag(DrawingSurfaceModel diagram, out string? tag)
return true;
}

foreach (Entity note in diagram.Borders.Values.OfType<Entity>().Where(e => e.IsTextAnnotation()))
{
if (this.TryGetTagFromText(note.Name() ?? string.Empty, DefaultTypePropertyName, out tag))
{
return true;
}
}

return false;
tag = diagram.Borders.Values.OfType<Entity>()
.Where(e => e.IsTextAnnotation())
.Select(note => this.TryGetTagFromText(note.Name() ?? string.Empty, DefaultTypePropertyName, out string? t) ? t : null)
.FirstOrDefault(t => t != null);
return tag != null;
}

/// <summary>
Expand Down Expand Up @@ -151,26 +147,23 @@ private static bool TryGetExplicitTagFromText(

string sep = tokens[i + 1];
string value = tokens[i + 2];
if (sep.Length == 1)
if (sep.Length == 1 && (sep[0] == ':' || sep[0] == '='))
{
if (sep[0] == ':' || sep[0] == '=')
if (!string.Equals(value, "(", StringComparison.InvariantCultureIgnoreCase))
{
if (!string.Equals(value, "(", StringComparison.InvariantCultureIgnoreCase))
{
tag = value;
return true;
}

i++;
List<string> result = new ();
while (i < tokens.Count && !string.Equals(tokens[i], ")", StringComparison.InvariantCultureIgnoreCase))
{
result.Add(tokens[i++]);
}

tag = string.Join(" ", result);
tag = value;
return true;
}

i++;
List<string> result = new ();
while (i < tokens.Count && !string.Equals(tokens[i], ")", StringComparison.InvariantCultureIgnoreCase))
{
result.Add(tokens[i++]);
}

tag = string.Join(" ", result);
return true;
}
}

Expand All @@ -195,17 +188,8 @@ private bool TryGetTagFromText(string text, string propName, out string? tag)
return true;
}

foreach (string tagToMatch in this.Tags)
{
if ((text ?? string.Empty).Contains(tagToMatch))
{
tag = tagToMatch;
return true;
}
}

tag = null;
return false;
tag = this.Tags.FirstOrDefault(tagToMatch => (text ?? string.Empty).Contains(tagToMatch));
return tag != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,26 @@ public override void Evaluate(RuleEvaluationContext context)
_ = context ?? throw new ArgumentNullException(nameof(context));
foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList)
{
foreach (Entity c in diagram.Components())
foreach (Entity c in diagram.Components().Where(c => c.IsGenericComponent()))
{
if (c.IsGenericComponent())
string? name = c.Name();
string? headerName = c.HeaderName();
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(headerName))
{
string? name = c.Name();
string? headerName = c.HeaderName();
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(headerName))
{
continue;
}
continue;
}

if (string.Equals(headerName!.Trim(), name!.Trim(), StringComparison.OrdinalIgnoreCase))
{
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.GenericComponentNameShouldNotMatchTypeNameMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
if (string.Equals(headerName!.Trim(), name!.Trim(), StringComparison.OrdinalIgnoreCase))
{
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.GenericComponentNameShouldNotMatchTypeNameMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,26 @@ public override void Evaluate(RuleEvaluationContext context)
GeneralPurposeComponentSet compSet = GeneralPurposeComponentSet.FromContext(context);
foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList)
{
foreach (Entity c in diagram.Components())
foreach (Entity c in diagram.Components().Where(c => compSet.IsGeneralPurposeComponent(c)))
{
if (compSet.IsGeneralPurposeComponent(c))
string? name = c.Name();
string? headerName = c.HeaderName();
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(headerName))
{
string? name = c.Name();
string? headerName = c.HeaderName();
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(headerName))
{
continue;
}
continue;
}

if (string.Equals(name!.Trim(), headerName!.Trim(), StringComparison.OrdinalIgnoreCase))
{
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.GeneralPurposeComponentNameShouldNotMatchTypeNameMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
if (string.Equals(name!.Trim(), headerName!.Trim(), StringComparison.OrdinalIgnoreCase))
{
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.GeneralPurposeComponentNameShouldNotMatchTypeNameMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ public override void Evaluate(RuleEvaluationContext context)
}

// Otherwise, check each edge for a classification.
foreach (Connector c in diagram.Lines.Values.OfType<Connector>())
foreach (Connector c in diagram.Lines.Values.OfType<Connector>().Where(c => !tagSet.TryGetTag(c, out string? _)))
{
if (!tagSet.TryGetTag(c, out string? _))
{
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.EdgeMissingDataClassificationMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
string text = string.Format(
System.Globalization.CultureInfo.CurrentCulture,
Properties.Resources.EdgeMissingDataClassificationMessageText,
GetEntityDisplayText(c));
Message m = this.CreateMessage(
c,
diagram,
text);
context.Writer.Write(m);
}
}
}
Expand Down
38 changes: 15 additions & 23 deletions src/ThreatModelForge.Analysis.Rules/EdgeProtocolInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ public EdgeProtocolInfo(
_ = infoSet ?? throw new ArgumentNullException(nameof(infoSet));

string? headerName = edge.HeaderName();
if (!string.IsNullOrWhiteSpace(headerName))
if (!string.IsNullOrWhiteSpace(headerName)
&& infoSet.Protocols.TryGetValue(headerName!, out var stencilProto))
{
if (infoSet.Protocols.TryGetValue(headerName!, out var proto))
{
return new EdgeProtocolInfo(edge, proto, EdgeProtocolSpecificationMethod.ByStencilType);
}
return new EdgeProtocolInfo(edge, stencilProto, EdgeProtocolSpecificationMethod.ByStencilType);
}

if (TryGetProtocol(edge, out string? protoAttrib))
Expand Down Expand Up @@ -152,13 +150,11 @@ public EdgeProtocolInfo(
else
{
// see if a known protocol is mentioned free form.
foreach (var protoName in infoSet.Protocols.Keys)
string? mentioned = infoSet.Protocols.Keys
.FirstOrDefault(protoName => tokens.Contains(protoName, StringComparer.OrdinalIgnoreCase));
if (mentioned != null)
{
if (tokens.Contains(protoName, StringComparer.OrdinalIgnoreCase))
{
info = infoSet.Protocols[protoName];
break;
}
info = infoSet.Protocols[mentioned];
}
}

Expand Down Expand Up @@ -186,13 +182,11 @@ private static bool TryGetExplicitPortFromText(
out int? value)
{
value = null;
if (TryGetExplicitPropertyValueFromText(text, PortCustomAttributeName, out string? valueString))
if (TryGetExplicitPropertyValueFromText(text, PortCustomAttributeName, out string? valueString)
&& int.TryParse(valueString!, NumberStyles.None, CultureInfo.InvariantCulture, out int val))
{
if (int.TryParse(valueString!, NumberStyles.None, CultureInfo.InvariantCulture, out int val))
{
value = val;
return true;
}
value = val;
return true;
}

return false;
Expand Down Expand Up @@ -230,13 +224,11 @@ private static bool TryGetProtocol(Connector edge, out string? protocol) =>

private static bool TryGetPort(Connector edge, out int? port)
{
if (edge.TryGetCustomPropertyValue(PortCustomAttributeName, out string? portString))
if (edge.TryGetCustomPropertyValue(PortCustomAttributeName, out string? portString)
&& int.TryParse(portString, NumberStyles.None, CultureInfo.InvariantCulture, out int portVal))
{
if (int.TryParse(portString, NumberStyles.None, CultureInfo.InvariantCulture, out int portVal))
{
port = portVal;
return true;
}
port = portVal;
return true;
}

port = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
throw new ArgumentNullException(nameof(context));
}

foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList)
foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList.Where(diagram => diagram.ExternalInteractors().Any()))
{
if (diagram.ExternalInteractors().Any())
{
return;
}
return;
}

Message message = this.CreateMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@

foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList)
{
foreach (Connector c in diagram.Lines.Values.OfType<Connector>())
foreach (Connector c in diagram.Lines.Values.OfType<Connector>().Where(c => diagram.TrustBoundaryCrossings(c).Any()))
{
if (diagram.TrustBoundaryCrossings(c).Any())
{
return;
}
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
throw new ArgumentNullException(nameof(context));
}

foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList)
foreach (DrawingSurfaceModel diagram in context.Model.DrawingSurfaceList.Where(diagram => diagram.TrustBoundaryBorders().Any() || diagram.TrustBoundaryLines().Any()))
{
if (diagram.TrustBoundaryBorders().Any() || diagram.TrustBoundaryLines().Any())
{
return;
}
return;
}

Message message = this.CreateMessage(
Expand Down
15 changes: 6 additions & 9 deletions src/ThreatModelForge.Analysis.Rules/ProtocolInfoSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,15 @@ public static ProtocolInfoSet FromContext(RuleEvaluationContext context)
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(e => e.Trim())
.Where(e => !string.IsNullOrEmpty(e));
foreach (var entry in entries)
{
var nameValuePair = entry
var nameValuePairs = entries
.Select(entry => entry
.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)
.Select(e => e.Trim())
.Where(e => !string.IsNullOrEmpty(e))
.ToArray();
if (nameValuePair.Length != 2)
{
continue;
}

.ToArray())
.Where(nameValuePair => nameValuePair.Length == 2);
foreach (var nameValuePair in nameValuePairs)
{
string name = nameValuePair[0];
if (!int.TryParse(nameValuePair[1], NumberStyles.None, CultureInfo.InvariantCulture, out int port))
{
Expand Down
7 changes: 2 additions & 5 deletions src/ThreatModelForge.Analysis.Rules/RulePackCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ public static class RulePackCatalog
/// <returns>The human-readable display name for the pack.</returns>
public static string DisplayName(string packId)
{
foreach (KeyValuePair<string, string> pack in OrderedPacks)
foreach (KeyValuePair<string, string> pack in OrderedPacks.Where(pack => string.Equals(pack.Key, packId, StringComparison.Ordinal)))
{
if (string.Equals(pack.Key, packId, StringComparison.Ordinal))
{
return pack.Value;
}
return pack.Value;
}

return packId;
Expand Down
Loading
Loading