Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit ac914ce

Browse files
authored
[ci] Add DevDiv required Roslyn analyzers, fix errors (#704)
As part of building secure software, Microsoft DevDiv has a set of [Roslyn anaylzers][0] dealing with security that should be run on every managed assembly. Adds these analyzers and fix any errors they introduce. ~~ Running Analyzers ~~ In order to run the Roslyn analyzers, the NuGet package [`Microsoft.CodeAnalysis.FxCopAnalyzers`][1] must be added to each project. Rather than do this manually now, and for each new project in the future, we instead add this to the `Directory.Build.props` file, which automatically adds it to all projects. By default, adding the NuGet package runs all included analyzers at each analyzer's default severity level. At this time, we are only concerned with the prescribed security set, so we use `.editorconfig` to set those analyzers as `error`, and all other analyzers as `none`. Projects that wish to opt out of running the analyzers can set `<DisableRoslynAnalyzers>True</DisableRoslynAnalyzers>`. ~~ Fixing Errors ~~ The only errors surfaced by these analyzers is [CA3075: Insecure DTD Processing][2]. These were fixed by using `new XmlReaderSettings { XmlResolver = null }`, which will not attempt to resolve and download any DTD files. ~~ Move `NullableAttributes.cs` ~~ `NullableAttributes.cs` is moved to the `src\utils` directory. This file was added to `Java.Interop.Tools.JavaCallableWrappers.csproj` via `..\Java.Interop\`. However, because the file resided in the directory containing the strict `.editorconfig` for `Java.Interop.dll`, it was applying those `.editorconfig` rules to `Java.Interop.Tools.JavaCallableWrappers.dll`. Moving it to a neutral directory fixed this. ~~ Other Notes ~~ Updating the `Java.Interop.dll` to the latest analyzer NuGet version triggered some errors we had handled for that specific assembly, which likely did not exist in the old analyzers and thus were not being surfaced as errors. They do not appear to be rules that we are actually concerned with, so they were disabled: * CA1021 - Don't use out parameters * CA1045 - Don't use reference parameters * CA1822 - Mark methods static if they don't reference instance members * CA1002 - Don't expose generic Lists [0]: https://github.com/dotnet/roslyn-analyzers [1]: https://www.nuget.org/packages/Microsoft.CodeAnalysis.FxCopAnalyzers/ [2]: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca3075?view=vs-2019
1 parent a98c1ae commit ac914ce

9 files changed

Lines changed: 315 additions & 13 deletions

File tree

‎.editorconfig‎

Lines changed: 301 additions & 0 deletions
Large diffs are not rendered by default.

‎Directory.Build.props‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@
6868
<_RunJNIEnvGenCondition=" '$(JIBuildingForNetCoreApp)' == 'True' ">dotnet "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
6969
<_RunJNIEnvGenCondition=" '$(JIBuildingForNetCoreApp)' != 'True' ">$(Runtime) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
7070
</PropertyGroup>
71+
72+
<!-- Add Roslyn analyzers NuGet to all projects -->
73+
<ItemGroupCondition=" '$(DisableRoslynAnalyzers)' != 'True' ">
74+
<PackageReferenceInclude="Microsoft.CodeAnalysis.FxCopAnalyzers"Version="3.3.0">
75+
<PrivateAssets>all</PrivateAssets>
76+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
77+
</PackageReference>
78+
</ItemGroup>
79+
7180
</Project>

‎src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<CompileInclude="..\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs">
2626
<Link>JavaNativeTypeManager.cs</Link>
2727
</Compile>
28-
<CompileInclude="..\Java.Interop\NullableAttributes.cs">
28+
<CompileInclude="..\utils\NullableAttributes.cs">
2929
<Link>NullableAttributes.cs</Link>
3030
</Compile>
3131
</ItemGroup>

‎src/Java.Interop/.editorconfig‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ dotnet_diagnostic.CA2213.severity = error
1818
dotnet_diagnostic.CA2242.severity = error
1919
dotnet_diagnostic.CA2000.severity = error
2020
dotnet_diagnostic.CA2220.severity = error
21-
dotnet_diagnostic.CA1822.severity = error
2221
dotnet_diagnostic.CA2241.severity = error
2322
dotnet_diagnostic.CA1012.severity = error
2423
dotnet_diagnostic.CA1019.severity = error
2524
dotnet_diagnostic.CA1040.severity = error
2625
dotnet_diagnostic.CA1023.severity = error
2726
dotnet_diagnostic.CA1044.severity = error
28-
dotnet_diagnostic.CA1021.severity = error
29-
dotnet_diagnostic.CA1045.severity = error
3027
dotnet_diagnostic.CA1020.severity = error
3128
dotnet_diagnostic.CA1051.severity = error
3229
dotnet_diagnostic.CA1034.severity = error
@@ -67,7 +64,6 @@ dotnet_diagnostic.CA1027.severity = error
6764
dotnet_diagnostic.CA1005.severity = error
6865
dotnet_diagnostic.CA1004.severity = error
6966
dotnet_diagnostic.CA1000.severity = error
70-
dotnet_diagnostic.CA1002.severity = error
7167
dotnet_diagnostic.CA1006.severity = error
7268
dotnet_diagnostic.CA1010.severity = error
7369
dotnet_diagnostic.CA1007.severity = error

‎src/Java.Interop/Java.Interop.csproj‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<DefineConstants>DEBUG;$(DefineConstants)</DefineConstants>
2020
</PropertyGroup>
2121
<ItemGroup>
22-
<CompileCondition=" '$(TargetFramework)' != 'netstandard2.0' "Remove="NullableAttributes.cs" />
22+
<CompileCondition=" '$(TargetFramework)' == 'netstandard2.0' "Include="..\utils\NullableAttributes.cs" />
2323
<CompileRemove="Java.Interop\JniLocationException.cs" />
2424
</ItemGroup>
2525
<PropertyGroup>
@@ -49,10 +49,6 @@
4949
<NoneInclude="Documentation\Java.Interop\IJavaPeerable.xml" />
5050
<NoneInclude="Documentation\Java.Interop\JniManagedPeerStates.xml" />
5151
<NoneInclude="Documentation\Java.Interop\JniEnvironment.References.xml" />
52-
<PackageReferenceInclude="Microsoft.CodeAnalysis.FxCopAnalyzers"Version="2.9.7">
53-
<PrivateAssets>all</PrivateAssets>
54-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
55-
</PackageReference>
5652
<ProjectReferenceInclude="..\..\build-tools\jnienv-gen\jnienv-gen.csproj"
5753
ReferenceOutputAssembly="false"
5854
/>

‎tests/Xamarin.Android.Tools.ApiXmlAdjuster-Tests/JavaApiTestHelper.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class JavaApiTestHelper
2020
publicstaticJavaApiGetLoadedApi()
2121
{
2222
varapi=newJavaApi();
23-
using(varxr=XmlReader.Create(ApiPath))
23+
using(varxr=XmlReader.Create(ApiPath,newXmlReaderSettings{XmlResolver=null}))
2424
api.Load(xr,false);
2525
returnapi;
2626
}

‎tools/generator/ApiVersionsProvider.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ApiVersionsProvider
99
{
1010
publicvoidParse(stringapiVersionsFilePath)
1111
{
12-
using(varreader=XmlReader.Create(apiVersionsFilePath))
12+
using(varreader=XmlReader.Create(apiVersionsFilePath,newXmlReaderSettings{XmlResolver=null}))
1313
Parse(reader);
1414
}
1515

‎tools/generator/CodeGenerator.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
8585
stringapiXmlFile=filename;
8686
stringapiSourceAttr=null;
8787

88-
using(varxr=XmlReader.Create(filename)){
88+
using(varxr=XmlReader.Create(filename,newXmlReaderSettings{XmlResolver=null})){
8989
xr.MoveToContent();
9090
apiSourceAttr=xr.GetAttribute("api-source");
9191
}

0 commit comments

Comments
 (0)