File: Microsoft.NetCore.Analyzers\Security\DoNotUseDataSetReadXml.cs
Web Access
Project: src\sdk\src\Microsoft.CodeAnalysis.NetAnalyzers\src\Microsoft.CodeAnalysis.NetAnalyzers\Microsoft.CodeAnalysis.NetAnalyzers.csproj (Microsoft.CodeAnalysis.NetAnalyzers)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.NetCore.Analyzers.Security.Helpers;

namespace Microsoft.NetCore.Analyzers.Security
{
    using static MicrosoftNetCoreAnalyzersResources;

    /// <summary>
    /// For detecting deserialization with <see cref="T:System.Data.DataSet"/>.
    /// </summary>
    [SuppressMessage("Documentation", "CA1200:Avoid using cref tags with a prefix", Justification = "The comment references a type that is not referenced by this compilation.")]
    [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
    internal class DoNotUseDataSetReadXml : DoNotUseInsecureDeserializerMethodsBase
    {
        internal static readonly DiagnosticDescriptor RealMethodUsedDescriptor =
            SecurityHelpers.CreateDiagnosticDescriptor(
                "CA2351",
                nameof(DataSetReadXmlTitle),
                nameof(DataSetReadXmlMessage),
                RuleLevel.Disabled,
                isPortedFxCopRule: false,
                isDataflowRule: false,
                isReportedAtCompilationEnd: false);

        internal static readonly DiagnosticDescriptor RealMethodUsedInAutogeneratedDescriptor =
            SecurityHelpers.CreateDiagnosticDescriptor(
                "CA2361",
                nameof(DataSetReadXmlAutogeneratedTitle),
                nameof(DataSetReadXmlAutogeneratedMessage),
                RuleLevel.Disabled,
                isPortedFxCopRule: false,
                isDataflowRule: false,
                isReportedAtCompilationEnd: false);

        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
            ImmutableArray.Create(RealMethodUsedDescriptor, RealMethodUsedInAutogeneratedDescriptor);

        protected override DiagnosticDescriptor? ChooseDiagnosticDescriptor(OperationAnalysisContext operationAnalysisContext, WellKnownTypeProvider wellKnownTypeProvider)
        {
            bool isProbablyAutogeneratedForGuiApp =
                SecurityHelpers.IsOperationInsideAutogeneratedCodeForGuiApp(operationAnalysisContext, wellKnownTypeProvider);
            return isProbablyAutogeneratedForGuiApp ? RealMethodUsedInAutogeneratedDescriptor : RealMethodUsedDescriptor;
        }

        protected override string DeserializerTypeMetadataName =>
            WellKnownTypeNames.SystemDataDataSet;

        protected override ImmutableHashSet<string> DeserializationMethodNames =>
            SecurityHelpers.DataSetDeserializationMethods;

        protected override DiagnosticDescriptor MethodUsedDescriptor => RealMethodUsedDescriptor;
    }
}