Exemplo n.º 1
0
        private bool addFixed(Dictionary <long, Dictionary <DateTime, Dictionary <string, ReportResultExtended> > > fix, List <ReportResultExtended> report_output)
        {
            foreach (KeyValuePair <long, Dictionary <DateTime, Dictionary <string, ReportResultExtended> > > projects in fix)
            {
                Dictionary <DateTime, Dictionary <string, ReportResultExtended> > scanDate = projects.Value;
                var scan_date = from entry in scanDate orderby entry.Key ascending select entry;
                KeyValuePair <DateTime, Dictionary <string, ReportResultExtended> > keyValuePair = new KeyValuePair <DateTime, Dictionary <string, ReportResultExtended> >();

                foreach (KeyValuePair <DateTime, Dictionary <string, ReportResultExtended> > kv_dt in scan_date)
                {
                    if (keyValuePair.Key != DateTime.MinValue)
                    {
                        Dictionary <string, ReportResultExtended> last_scan    = keyValuePair.Value;
                        Dictionary <string, ReportResultExtended> current_scan = kv_dt.Value;
                        if (token.debug && token.verbosity > 0)
                        {
                            Console.WriteLine("Compare: {0} {1}", keyValuePair.Key, kv_dt.Key);
                        }
                        foreach (string key in last_scan.Keys)
                        {
                            if (token.debug && token.verbosity > 0)
                            {
                                Console.WriteLine("Project {0}, key {1}", last_scan[key].projectName, key);
                            }
                            if (!current_scan.ContainsKey(key))
                            {
                                ReportResultExtended reportResult = last_scan[key];
                                reportResult.status = "Fixed";
                                report_output.Add(reportResult);
                            }
                        }
                    }
                    keyValuePair = kv_dt;
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        private bool process_CxResponse(long report_id, XElement result, Dictionary <string, ReportResultExtended> response, Dictionary <long, Dictionary <DateTime, Dictionary <string, ReportResultExtended> > > fix, List <ReportResultExtended> report_output)
        {
            try
            {
                IEnumerable <XElement> newVulerability = from el in result.Descendants("Query").Descendants("Result")
                                                         where (string)el.Attribute("Status").Value == "New"
                                                         select el;

                foreach (XElement el in newVulerability)
                {
                    XElement query    = el.Parent;
                    XElement root     = query.Parent;
                    XElement path     = el.Descendants("Path").FirstOrDefault();
                    XElement pathNode = path.Descendants("PathNode").FirstOrDefault();
                    XElement snippet  = pathNode.Descendants("Snippet").FirstOrDefault();
                    XElement line     = (snippet != null) ? snippet.Descendants("Line").FirstOrDefault() : null;

                    //long ResultId = Convert.ToInt64(path.Attribute("ResultId").Value.ToString());
                    //string key = "New-" + ResultId.ToString();
                    long   SimilarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString());
                    string key          = "New-" + SimilarityId.ToString();
                    ReportResultExtended resultExtended = response.GetValueOrDefault(key);
                    if (resultExtended == null)
                    {
                        ReportResultExtended isnew = new ReportResultExtended()
                        {
                            Query        = query.Attribute("name").Value.ToString(),
                            Group        = query.Attribute("group").Value.ToString(),
                            projectName  = root.Attribute("ProjectName").Value.ToString(),
                            presetName   = root.Attribute("Preset").Value.ToString(),
                            teamName     = root.Attribute("Team").Value.ToString(),
                            scanDate     = Convert.ToDateTime(root.Attribute("ScanStart").Value.ToString()),
                            projectId    = Convert.ToInt64(root.Attribute("ProjectId").Value.ToString()),
                            resultId     = Convert.ToInt64(path.Attribute("ResultId").Value.ToString()),
                            reportId     = report_id,
                            nodeId       = Convert.ToInt64(el.Attribute("NodeId").Value.ToString()),
                            scanId       = Convert.ToInt64(root.Attribute("ScanId").Value.ToString()),
                            status       = el.Attribute("Status").Value.ToString(),
                            Severity     = el.Attribute("Severity").Value.ToString(),
                            similarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString()),
                            pathId       = Convert.ToInt64(path.Attribute("PathId").Value.ToString()),
                            state        = Convert.ToInt32(el.Attribute("state").Value.ToString()),
                            fileName     = el.Attribute("FileName").Value.ToString(),
                            lineNo       = Convert.ToInt32(el.Attribute("Line").Value.ToString()),

                            column    = Convert.ToInt32(el.Attribute("Column").Value.ToString()),
                            firstLine = (line != null) ? line.Descendants("Code").FirstOrDefault().Value.ToString() : "",
                            queryId   = Convert.ToInt64(query.Attribute("id").Value.ToString())
                        };
                        response.Add(key, isnew);
                        report_output.Add(isnew);
                    }
                }
                IEnumerable <XElement> recurringVulerability = from el in result.Descendants("Query").Descendants("Result")
                                                               where (string)el.Attribute("Status").Value == "Recurrent"
                                                               select el;
                foreach (XElement el in recurringVulerability)
                {
                    XElement query    = el.Parent;
                    XElement root     = query.Parent;
                    XElement path     = el.Descendants("Path").FirstOrDefault();
                    XElement pathNode = path.Descendants("PathNode").FirstOrDefault();
                    XElement snippet  = pathNode.Descendants("Snippet").FirstOrDefault();
                    XElement line     = (snippet != null) ? snippet.Descendants("Line").FirstOrDefault() : null;

                    //long ResultId = Convert.ToInt64(path.Attribute("ResultId").Value.ToString());
                    //string key = "Recurring-" + ResultId.ToString();
                    long   SimilarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString());
                    string key          = "Recurring-" + SimilarityId.ToString();
                    ReportResultExtended resultExtended = response.GetValueOrDefault(key);
                    if (resultExtended == null)
                    {
                        ReportResultExtended isrecurring = new ReportResultExtended()
                        {
                            Query        = query.Attribute("name").Value.ToString(),
                            Group        = query.Attribute("group").Value.ToString(),
                            projectName  = root.Attribute("ProjectName").Value.ToString(),
                            presetName   = root.Attribute("Preset").Value.ToString(),
                            teamName     = root.Attribute("Team").Value.ToString(),
                            scanDate     = Convert.ToDateTime(root.Attribute("ScanStart").Value.ToString()),
                            projectId    = Convert.ToInt64(root.Attribute("ProjectId").Value.ToString()),
                            scanId       = Convert.ToInt64(root.Attribute("ScanId").Value.ToString()),
                            resultId     = Convert.ToInt64(path.Attribute("ResultId").Value.ToString()),
                            reportId     = report_id,
                            nodeId       = Convert.ToInt64(el.Attribute("NodeId").Value.ToString()),
                            status       = el.Attribute("Status").Value.ToString(),
                            Severity     = el.Attribute("Severity").Value.ToString(),
                            similarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString()),
                            pathId       = Convert.ToInt64(path.Attribute("PathId").Value.ToString()),
                            state        = Convert.ToInt32(el.Attribute("state").Value.ToString()),
                            fileName     = el.Attribute("FileName").Value.ToString(),
                            lineNo       = Convert.ToInt32(el.Attribute("Line").Value.ToString()),
                            column       = Convert.ToInt32(el.Attribute("Column").Value.ToString()),
                            firstLine    = (line != null) ? line.Descendants("Code").FirstOrDefault().Value.ToString() : "",
                            queryId      = Convert.ToInt64(query.Attribute("id").Value.ToString())
                        };
                        response.Add(key, isrecurring);
                        report_output.Add(isrecurring);
                    }
                    else
                    {
                        int currentstate = Convert.ToInt32(el.Attribute("state").Value.ToString());
                        ReportResultExtended reportResult = response[key];
                        if (currentstate != reportResult.state)
                        {
                            ReportResultExtended isrecurring = new ReportResultExtended()
                            {
                                Query        = query.Attribute("name").Value.ToString(),
                                Group        = query.Attribute("group").Value.ToString(),
                                projectName  = root.Attribute("ProjectName").Value.ToString(),
                                presetName   = root.Attribute("Preset").Value.ToString(),
                                teamName     = root.Attribute("Team").Value.ToString(),
                                scanDate     = Convert.ToDateTime(root.Attribute("ScanStart").Value.ToString()),
                                projectId    = Convert.ToInt64(root.Attribute("ProjectId").Value.ToString()),
                                scanId       = Convert.ToInt64(root.Attribute("ScanId").Value.ToString()),
                                status       = el.Attribute("Status").Value.ToString(),
                                nodeId       = Convert.ToInt64(el.Attribute("NodeId").Value.ToString()),
                                Severity     = el.Attribute("Severity").Value.ToString(),
                                resultId     = Convert.ToInt64(path.Attribute("ResultId").Value.ToString()),
                                reportId     = report_id,
                                similarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString()),
                                pathId       = Convert.ToInt64(path.Attribute("PathId").Value.ToString()),
                                state        = Convert.ToInt32(el.Attribute("state").Value.ToString()),
                                fileName     = el.Attribute("FileName").Value.ToString(),
                                lineNo       = Convert.ToInt32(el.Attribute("Line").Value.ToString()),
                                column       = Convert.ToInt32(el.Attribute("Column").Value.ToString()),
                                firstLine    = (line != null) ? line.Descendants("Code").FirstOrDefault().Value.ToString() : "",
                                queryId      = Convert.ToInt64(query.Attribute("id").Value.ToString())
                            };
                            response[key] = isrecurring;
                            report_output.Add(isrecurring);
                        }
                    }
                }
                IEnumerable <XElement> fixedVulerability = from el in result.Descendants("Query").Descendants("Result")
                                                           select el;
                foreach (XElement el in fixedVulerability)
                {
                    XElement             query        = el.Parent;
                    XElement             root         = query.Parent;
                    XElement             path         = el.Descendants("Path").FirstOrDefault();
                    XElement             pathNode     = path.Descendants("PathNode").FirstOrDefault();
                    XElement             snippet      = pathNode.Descendants("Snippet").FirstOrDefault();
                    XElement             line         = (snippet != null) ? snippet.Descendants("Line").FirstOrDefault() : null;
                    long                 SimilarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString());
                    ReportResultExtended isfixed      = new ReportResultExtended()
                    {
                        Query        = query.Attribute("name").Value.ToString(),
                        Group        = query.Attribute("group").Value.ToString(),
                        projectName  = root.Attribute("ProjectName").Value.ToString(),
                        presetName   = root.Attribute("Preset").Value.ToString(),
                        teamName     = root.Attribute("Team").Value.ToString(),
                        scanDate     = Convert.ToDateTime(root.Attribute("ScanStart").Value.ToString()),
                        projectId    = Convert.ToInt64(root.Attribute("ProjectId").Value.ToString()),
                        scanId       = Convert.ToInt64(root.Attribute("ScanId").Value.ToString()),
                        status       = el.Attribute("Status").Value.ToString(),
                        Severity     = el.Attribute("Severity").Value.ToString(),
                        resultId     = Convert.ToInt64(path.Attribute("ResultId").Value.ToString()),
                        reportId     = report_id,
                        nodeId       = Convert.ToInt64(el.Attribute("NodeId").Value.ToString()),
                        similarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString()),
                        pathId       = Convert.ToInt64(path.Attribute("PathId").Value.ToString()),
                        state        = Convert.ToInt32(el.Attribute("state").Value.ToString()),
                        fileName     = el.Attribute("FileName").Value.ToString(),
                        lineNo       = Convert.ToInt32(el.Attribute("Line").Value.ToString()),
                        column       = Convert.ToInt32(el.Attribute("Column").Value.ToString()),
                        firstLine    = (line != null) ? line.Descendants("Code").FirstOrDefault().Value.ToString() : "",
                        queryId      = Convert.ToInt64(query.Attribute("id").Value.ToString())
                    };
                    string mix = String.Format("{0}-{1}-{2}-{3}-{4}", isfixed.projectId, isfixed.queryId, isfixed.lineNo, isfixed.column, isfixed.similarityId);
                    if (!fix.ContainsKey(isfixed.projectId))
                    {
                        fix.Add(isfixed.projectId, new Dictionary <DateTime, Dictionary <string, ReportResultExtended> >());
                        fix[isfixed.projectId].Add(isfixed.scanDate, new Dictionary <string, ReportResultExtended>());
                        fix[isfixed.projectId][isfixed.scanDate].Add(mix, isfixed);
                        if (token.debug && token.verbosity > 0)
                        {
                            Console.WriteLine("Unique keys: {0}, {1}, {2} {3} {4} {5}", isfixed.projectName, isfixed.similarityId, isfixed.projectId, isfixed.scanId, isfixed.queryId, isfixed.scanDate);
                        }
                    }
                    else
                    {
                        if (!fix[isfixed.projectId].ContainsKey(isfixed.scanDate))
                        {
                            fix[isfixed.projectId].Add(isfixed.scanDate, new Dictionary <string, ReportResultExtended>());
                        }
                        if (!fix[isfixed.projectId][isfixed.scanDate].TryAdd(mix, isfixed))
                        {
                            if (token.debug && token.verbosity > 0)
                            {
                                Console.WriteLine("Duplicate keys: {0}, {1}, {2} {3} {4} {5}", isfixed.projectName, isfixed.similarityId, isfixed.nodeId, isfixed.scanId, isfixed.queryId, isfixed.scanDate);
                            }
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failure reading XML from scan: report ID: {0}", report_id);
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                if (token.debug && token.verbosity > 1)
                {
                    Console.Error.WriteLine("Dumping XML:");
                    Console.Error.Write(result.ToString());
                }
                return(true);
            }
        }
Exemplo n.º 3
0
        private bool process_CxResponse(XElement result, long report_id, long projectId, DateTimeOffset?scanDate, Dictionary <DateTimeOffset, Dictionary <long, Dictionary <string, ReportResultExtended> > > extendedScan)
        {
            try
            {
                Dictionary <long, Dictionary <string, ReportResultExtended> > scanByProject = new Dictionary <long, Dictionary <string, ReportResultExtended> >();
                Dictionary <string, ReportResultExtended> scanByUnique = new Dictionary <string, ReportResultExtended>();
                IEnumerable <XElement> fixedVulerability = from el in result.Descendants("Query").Descendants("Result")
                                                           select el;
                foreach (XElement el in fixedVulerability)
                {
                    XElement query    = el.Parent;
                    XElement root     = query.Parent;
                    XElement path     = el.Descendants("Path").FirstOrDefault();
                    XElement pathNode = path.Descendants("PathNode").FirstOrDefault();
                    //List<XElement> allNode = path.Descendants("PathNode").Elements().ToList();
                    //IEnumerable<XElement> allNode = path.Descendants("PathNode").Elements();
                    XElement             lastNode     = path.Descendants("PathNode").LastOrDefault();
                    XElement             snippet      = pathNode.Descendants("Snippet").FirstOrDefault();
                    XElement             line         = (snippet != null) ? snippet.Descendants("Line").FirstOrDefault() : null;
                    long                 SimilarityId = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString());
                    ReportResultExtended isfixed      = new ReportResultExtended()
                    {
                        Query           = query.Attribute("name").Value.ToString(),
                        Group           = query.Attribute("group").Value.ToString(),
                        projectName     = root.Attribute("ProjectName").Value.ToString(),
                        presetName      = root.Attribute("Preset").Value.ToString(),
                        teamName        = root.Attribute("TeamFullPathOnReportDate").Value.ToString(),
                        scanDate        = Convert.ToDateTime(root.Attribute("ScanStart").Value.ToString()),
                        projectId       = Convert.ToInt64(root.Attribute("ProjectId").Value.ToString()),
                        scanId          = Convert.ToInt64(root.Attribute("ScanId").Value.ToString()),
                        status          = el.Attribute("Status").Value.ToString(),
                        Severity        = el.Attribute("Severity").Value.ToString(),
                        isFalsePositive = el.Attribute("FalsePositive").Value.ToString(),
                        resultId        = Convert.ToInt64(path.Attribute("ResultId").Value.ToString()),
                        reportId        = report_id,
                        nodeId          = Convert.ToInt64(el.Attribute("NodeId").Value.ToString()),
                        similarityId    = Convert.ToInt64(path.Attribute("SimilarityId").Value.ToString()),
                        pathId          = Convert.ToInt64(path.Attribute("PathId").Value.ToString()),
                        state           = Convert.ToInt32(el.Attribute("state").Value.ToString()),
                        fileName        = el.Attribute("FileName").Value.ToString(),
                        lineNo          = Convert.ToInt32(el.Attribute("Line").Value.ToString()),
                        column          = Convert.ToInt32(el.Attribute("Column").Value.ToString()),
                        firstLine       = (line != null) ? line.Descendants("Code").FirstOrDefault().Value.ToString(): "",
                        nodeName        = pathNode.Descendants("Name").FirstOrDefault().Value.ToString(),
                        queryId         = Convert.ToInt64(query.Attribute("id").Value.ToString()),
                        remark          = el.Attribute("Remark").Value.ToString(),
                        deepLink        = el.Attribute("DeepLink").Value.ToString()
                    };
                    string uniqueKey = String.Format("{0}_{1}_{2}_{3}", isfixed.similarityId, isfixed.queryId, isfixed.lineNo, isfixed.column);
                    uniqueKey = makeHash(pathNode, lastNode, uniqueKey);
                    //uniqueKey = makeHash(allNode, uniqueKey);

                    ; if (token.debug && token.verbosity > 0)
                    {
                        Console.WriteLine(String.Format("Processing: project:{0} scanDate: {1} uniquekey: {2} pathId: {3} nodeId: {4} line:{5} column:{6}", isfixed.projectName, scanDate, uniqueKey, isfixed.pathId, isfixed.nodeId, isfixed.lineNo, isfixed.column));
                    }
                    if (!scanByUnique.TryAdd(uniqueKey, isfixed))
                    {
                        Console.Error.WriteLine(String.Format("Duplicate key: project:{0} scanDate: {1} uniquekey: {2} pathId: {3} nodeId: {4} line:{5} column:{6}", isfixed.projectName, scanDate, uniqueKey, isfixed.pathId, isfixed.nodeId, isfixed.lineNo, isfixed.column));
                    }
                }
                scanByProject.Add(projectId, scanByUnique);
                extendedScan.TryAdd((DateTimeOffset)scanDate, scanByProject);

                return(true);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failure reading XML from scan: report ID: {0}", report_id);
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);
                if (token.debug && token.verbosity > 1)
                {
                    Console.Error.WriteLine("Dumping XML:");
                    Console.Error.Write(result.ToString());
                }
                return(true);
            }
        }