Пример #1
0
        public void AnalyzeConfigurationFile(string content, AdditionalText file, CompilationAnalysisContext context)
        {
            XDocument doc = XDocument.Load(new StringReader(content));

            //ValidateRequest
            {
                IEnumerable <XElement> pagesNodes = from c in doc.Descendants("pages")
                                                    where AttributeInsensitive(c, "validateRequest")?.Value != "true"
                                                    select c;

                foreach (var page in pagesNodes)
                {
                    if (AttributeInsensitive(page, "validateRequest")?.Value.ToLower() == "false")
                    {
                        var lineInfo   = (IXmlLineInfo)page;
                        int lineNumber = lineInfo.HasLineInfo() ? lineInfo.LineNumber : 1;

                        Location loc = AnalyzerUtil.CreateLocation(file.Path, lineNumber);
                        context.ReportDiagnostic(Diagnostic.Create(RuleValidateRequest, loc));
                    }
                }
            }
            //EnableEventValidation
            {
                IEnumerable <XElement> pagesNodes = from c in doc.Descendants("pages")
                                                    where AttributeInsensitive(c, "enableEventValidation")?.Value != "true"
                                                    select c;
                foreach (var page in pagesNodes)
                {
                    if (AttributeInsensitive(page, "enableEventValidation")?.Value.ToLower() == "false")
                    {
                        var lineInfo   = (IXmlLineInfo)page;
                        int lineNumber = lineInfo.HasLineInfo() ? lineInfo.LineNumber : 1;

                        Location loc = AnalyzerUtil.CreateLocation(file.Path, lineNumber);
                        context.ReportDiagnostic(Diagnostic.Create(RuleEnableEventValidation, loc));
                    }
                }
            }
            //ViewStateEncryptionMode
            {
                IEnumerable <XElement> pagesNodes = from c in doc.Descendants("pages")
                                                    where AttributeInsensitive(c, "viewStateEncryptionMode")?.Value != "true"
                                                    select c;
                foreach (var page in pagesNodes)
                {
                    if (AttributeInsensitive(page, "viewStateEncryptionMode")?.Value.ToLower() == "auto" ||
                        AttributeInsensitive(page, "viewStateEncryptionMode")?.Value.ToLower() == "never") //Always, Auto & Never
                    {
                        var lineInfo   = (IXmlLineInfo)page;
                        int lineNumber = lineInfo.HasLineInfo() ? lineInfo.LineNumber : 1;

                        Location loc = AnalyzerUtil.CreateLocation(file.Path, lineNumber);
                        context.ReportDiagnostic(Diagnostic.Create(RuleViewStateEncryptionMode, loc));
                    }
                }
            }
            //EnableViewStateMac: https://blogs.msdn.microsoft.com/webdev/2014/09/09/farewell-enableviewstatemac/
            {
                IEnumerable <XElement> pagesNodes = from c in doc.Descendants("pages")
                                                    where AttributeInsensitive(c, "enableViewStateMac")?.Value != "true"
                                                    select c;
                foreach (var page in pagesNodes)
                {
                    if (AttributeInsensitive(page, "enableViewStateMac")?.Value.ToLower() == "false")
                    {
                        var lineInfo   = (IXmlLineInfo)page;
                        int lineNumber = lineInfo.HasLineInfo() ? lineInfo.LineNumber : 1;

                        Location loc = AnalyzerUtil.CreateLocation(file.Path, lineNumber);
                        context.ReportDiagnostic(Diagnostic.Create(RuleEnableViewStateMac, loc));
                    }
                }
            }
        }