Skip to content

Commit 96df3cd

Browse files
nvsdtleotsarev
authored andcommitted
White list (#5)
1 parent bdbd55d commit 96df3cd

File tree

6 files changed

+126
-16
lines changed

6 files changed

+126
-16
lines changed

Tsarev.Analyzer.Hardcode.Email.Test/Tsarev.Analyzer.Hardcode.Email.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.1</TargetFramework>
44
<AssemblyTitle>Tsarev.Analyzer.Hardcode.Email.Test</AssemblyTitle>
5+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
56
</PropertyGroup>
67
<ItemGroup>
78
<PackageReference Include="xunit" Version="2.4.1" />

Tsarev.Analyzer.Hardcode.Guid.Test/Tsarev.Analyzer.Hardcode.Guid.Test.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.1</TargetFramework>
44
<AssemblyTitle>Tsarev.Analyzer.Hardcode.Guid.Test</AssemblyTitle>
5-
5+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageReference Include="JetBrains.Annotations" Version="11.0.0" />
@@ -13,5 +13,4 @@
1313
<ProjectReference Include="..\Tsarev.Analyzer.Hardcode.Guid\Tsarev.Analyzer.Hardcode.Guid.csproj" />
1414
<ProjectReference Include="..\Tsarev.Analyzer.TestHelpers\Tsarev.Analyzer.TestHelpers.csproj" />
1515
</ItemGroup>
16-
1716
</Project>

Tsarev.Analyzer.Hardcode.Url/UrlHardcodeAnalyzer.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.Collections.Immutable;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
24
using Microsoft.CodeAnalysis;
35
using Microsoft.CodeAnalysis.CSharp;
46
using Microsoft.CodeAnalysis.CSharp.Syntax;
57
using Microsoft.CodeAnalysis.Diagnostics;
68
using System.Linq;
7-
using System.Globalization;
89
using Tsarev.Analyzer.Helpers;
910

1011
namespace Tsarev.Analyzer.Hardcode.Url
@@ -34,6 +35,11 @@ public override void Initialize(AnalysisContext context)
3435
}
3536

3637
private static readonly string[] BlackList = { "http:", "https:", "ftp:", "tcp:"};
38+
39+
private static readonly string[] WhiteList =
40+
{
41+
"http://schemas.xmlsoap.org/"
42+
};
3743

3844
/// <summary>
3945
/// List of attributes that expected to contain URLs, and this is correct.
@@ -57,7 +63,15 @@ private static void AnalyzeLiteral(SyntaxNodeAnalysisContext context)
5763
return;
5864
}
5965

60-
CheckStringValue(context, context.Node.GetLiteralStringValueOrDefault());
66+
var value = context.Node.GetLiteralStringValueOrDefault();
67+
68+
foreach (var url in GetUrls(value))
69+
{
70+
if (!WhiteList.Any(x => url.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)))
71+
{
72+
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), value));
73+
}
74+
}
6175
}
6276

6377
private static bool IsArgumentOfWhiteListedAttribute(SyntaxNode node)
@@ -66,14 +80,50 @@ private static bool IsArgumentOfWhiteListedAttribute(SyntaxNode node)
6680
var attribute = argument?.WalkToAttribute();
6781
return attribute != null && AttributeNameWhiteList.Contains(attribute.GetAttributeName());
6882
}
83+
84+
private static IEnumerable<string> GetUrls(string value)
85+
{
86+
var entries = new List<string>();
6987

70-
private static void CheckStringValue(SyntaxNodeAnalysisContext context, string value)
88+
var indicesOfEntries = GetIndicesOfEntries(value, BlackList).ToArray();
89+
for (var index = 0; index < indicesOfEntries.Length - 1; index++)
90+
{
91+
var entryStart = indicesOfEntries[index];
92+
var entryFinish = indicesOfEntries[index + 1];
93+
var entryLength = entryFinish - entryStart;
94+
var entry = value.Substring(entryStart, entryLength);
95+
entries.Add(entry);
96+
}
97+
entries.Add(value.Substring(indicesOfEntries[indicesOfEntries.Length - 1]));
98+
99+
return entries;
100+
}
101+
102+
private static IEnumerable<int> GetIndicesOfEntries(string value, IReadOnlyCollection<string> entries)
71103
{
72-
var comparer = CultureInfo.InvariantCulture.CompareInfo;
73-
if (BlackList.Any(part => comparer.IndexOf(value, part, CompareOptions.IgnoreCase) >= 0))
104+
var offset = 0;
105+
int indexOfEntry;
106+
while ((indexOfEntry = IndexOfSomeEntry(value, entries, ref offset)) != - 1)
74107
{
75-
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), value));
108+
yield return indexOfEntry;
109+
}
110+
}
111+
112+
private static int IndexOfSomeEntry(string value, IEnumerable<string> entries, ref int offset)
113+
{
114+
var indexOfSomeEntry = -1;
115+
var lengthOfSomeEntry = 0;
116+
foreach (var entry in entries)
117+
{
118+
var indexOfEntry = value.IndexOf(entry, offset, StringComparison.InvariantCultureIgnoreCase);
119+
if (indexOfEntry >= 0 && (indexOfEntry < indexOfSomeEntry || indexOfSomeEntry == -1))
120+
{
121+
indexOfSomeEntry = indexOfEntry;
122+
lengthOfSomeEntry = entry.Length;
123+
}
76124
}
125+
offset = indexOfSomeEntry + lengthOfSomeEntry;
126+
return indexOfSomeEntry;
77127
}
78128
}
79129
}

Tsarev.Analyzer.Web.Test/Tsarev.Analyzer.Web.Test.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.1</TargetFramework>
44
<AssemblyTitle>Tsarev.AsyncControllerAnalyzer.Test</AssemblyTitle>
5-
65
<Product>Tsarev.AsyncControllerAnalyzer.Test</Product>
6+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
77
</PropertyGroup>
8-
98
<ItemGroup>
109
<PackageReference Include="JetBrains.Annotations" Version="11.0.0" />
1110
<PackageReference Include="xunit" Version="2.4.1" />
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
4-
53
<TargetFramework>netcoreapp2.1</TargetFramework>
64
<AssemblyTitle>VatHardcodeAnalyzer.Test</AssemblyTitle>
75
<GenerateDocumentationFile>false</GenerateDocumentationFile>
86
</PropertyGroup>
9-
107
<ItemGroup>
118
<PackageReference Include="JetBrains.Annotations" Version="11.0.0" />
129
<PackageReference Include="xunit" Version="2.4.1" />
1310
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
14-
1511
</ItemGroup>
1612
<ItemGroup>
1713
<ProjectReference Include="..\Tsarev.Analyzer.TestHelpers\Tsarev.Analyzer.TestHelpers.csproj" />
1814
<ProjectReference Include="..\Tsarev.Analyzer.Hardcode.Url\Tsarev.Analyzer.Hardcode.Url.csproj" />
1915
</ItemGroup>
20-
2116
</Project>

UrlHardcodeAnalyzer.Test/UrlHardcodeUnitTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Microsoft.CodeAnalysis;
23
using Microsoft.CodeAnalysis.Diagnostics;
34
using Tsarev.Analyzer.TestHelpers;
@@ -213,7 +214,72 @@ public void Method() {
213214

214215
VerifyCSharpDiagnostic(test, ExpectUlrHardcode(6, 25, "http://example.com"));
215216
}
217+
218+
[Fact]
219+
public void TestClaimsString()
220+
{
221+
var test = @"
222+
namespace ConsoleApplication1
223+
{
224+
class TypeName {
225+
public void Method() {
226+
var test = $""http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"";
227+
}
228+
}
229+
}";
216230

231+
VerifyCSharpDiagnostic(test);
232+
}
233+
234+
[Fact]
235+
public void TestTwoClaimsString()
236+
{
237+
var test = @"
238+
namespace ConsoleApplication1
239+
{
240+
class TypeName {
241+
public void Method() {
242+
var test = $""http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"";
243+
}
244+
}
245+
}";
246+
247+
VerifyCSharpDiagnostic(test);
248+
}
249+
250+
[Fact]
251+
public void TestMultiHostWithWhiteListStart()
252+
{
253+
var test = @"
254+
namespace ConsoleApplication1
255+
{
256+
class TypeName {
257+
public void Method() {
258+
var test = $""ftp://example.com http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"";
259+
}
260+
}
261+
}";
262+
263+
VerifyCSharpDiagnostic(test, ExpectUlrHardcode(6, 25, "ftp://example.com http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"));
264+
}
265+
266+
[Fact]
267+
public void TestMultiHostWithWhiteListEnd()
268+
{
269+
var test = @"
270+
namespace ConsoleApplication1
271+
{
272+
class TypeName {
273+
public void Method() {
274+
var test = $""http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier ftp://example.com"";
275+
}
276+
}
277+
}";
278+
279+
var x = test.Split(new[] {"http://", "TCP://"}, StringSplitOptions.None);
280+
281+
VerifyCSharpDiagnostic(test, ExpectUlrHardcode(6, 25, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier ftp://example.com"));
282+
}
217283

218284
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => new UrlHardcodeAnalyzer();
219285
}

0 commit comments

Comments
 (0)