Esempio n. 1
0
        /// <summary>
        /// Interface implementation for converting a stream in Fortify FPR format to a stream in
        /// SARIF format.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="input">Stream in Fortify FPR format.</param>
        /// <param name="output">Stream in SARIF format.</param>
        /// <param name="dataToInsert">Optionally emitted properties that should be written to log.</param>
        public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            _invocation = new Invocation();
            _invocation.ToolExecutionNotifications = new List <Notification>();
            _results.Clear();
            _files.Clear();
            _rules.Clear();
            _ruleIdToIndexMap.Clear();
            _tflToNodeIdDictionary.Clear();
            _tflToSnippetIdDictionary.Clear();
            _locationToSnippetIdDictionary.Clear();
            _resultToSnippetIdDictionary.Clear();
            _resultToReplacementDefinitionDictionary.Clear();
            _nodeIdToLocationDictionary.Clear();
            _nodeIdToActionTypeDictionary.Clear();
            _snippetIdToRegionsDictionary.Clear();

            ParseFprFile(input);
            AddMessagesToResults();
            AddSnippetsToResults();
            AddNodeLocationsToThreadFlowLocations();
            AddSnippetsToThreadFlowLocations();

            var run = new Run()
            {
                Id = new RunAutomationDetails
                {
                    InstanceGuid = _runId,
                    InstanceId   = _automationId + "/"
                },
                Artifacts = new List <Artifact>(_files),
                Tool      = new Tool
                {
                    Driver = new ToolComponent
                    {
                        Name            = ToolName,
                        RuleDescriptors = _rules
                    }
                },
                Invocations = new[] { _invocation },
            };

            if (!string.IsNullOrWhiteSpace(_originalUriBasePath))
            {
                if (_originalUriBasePath.StartsWith("/") &&
                    _invocation.GetProperty("Platform") == "Linux")
                {
                    _originalUriBasePath = "file:/" + _originalUriBasePath;
                }

                if (Uri.TryCreate(_originalUriBasePath, UriKind.Absolute, out Uri uri))
                {
                    run.OriginalUriBaseIds = new Dictionary <string, ArtifactLocation>
                    {
                        { FileLocationUriBaseId, new ArtifactLocation {
                              Uri = uri
                          } }
                    };
                }
            }

            PersistResults(output, _results, run);
        }
Esempio n. 2
0
        /// <summary>
        /// Interface implementation for converting a stream in Fortify FPR format to a stream in
        /// SARIF format.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="input">Stream in Fortify FPR format.</param>
        /// <param name="output">Stream in SARIF format.</param>
        /// <param name="dataToInsert">Optionally emitted properties that should be written to log.</param>
        public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            _invocation = new Invocation();
            _invocation.ToolExecutionNotifications = new List <Notification>();
            _invocation.ExecutionSuccessful        = true;
            _results.Clear();
            _files.Clear();
            _rules.Clear();
            _ruleIdToIndexMap.Clear();
            _cweIds.Clear();
            _tflToNodeIdDictionary.Clear();
            _tflToSnippetIdDictionary.Clear();
            _locationToSnippetIdDictionary.Clear();
            _resultToSnippetIdDictionary.Clear();
            _resultToReplacementDefinitionDictionary.Clear();
            _nodeIdToLocationDictionary.Clear();
            _nodeIdToActionTypeDictionary.Clear();
            _snippetIdToRegionsDictionary.Clear();

            ParseFprFile(input);
            AddMessagesToResults();
            AddSnippetsToResults();
            AddNodeLocationsToThreadFlowLocations();
            AddSnippetsToThreadFlowLocations();

            var run = new Run()
            {
                AutomationDetails = new RunAutomationDetails
                {
                    Guid = _runId,
                    Id   = _automationId + "/"
                },
                Artifacts = _files.OrderBy(d => d.Value.Item2)
                            .Select(p => p.Value)
                            .Select(t => t.Item1)
                            .ToList() as IList <Artifact>,
                Tool = new Tool
                {
                    Driver = new ToolComponent
                    {
                        Name  = ToolName,
                        Rules = _rules,
                        SupportedTaxonomies = new List <ToolComponentReference>
                        {
                            new ToolComponentReference
                            {
                                Name  = "CWE",
                                Index = 0,
                                Guid  = "2B841697-D0DE-45DD-9F19-1EEE1312429"
                            }
                        }
                    }
                },
                Taxonomies = new List <ToolComponent>
                {
                    CweToolComponent
                },
                Invocations = new[] { _invocation },
            };

            if (_cweIds.Count > 0)
            {
                run.Taxonomies[0].Taxa = _cweIds.Select(c => new ReportingDescriptor {
                    Id = c
                }).ToList();
            }

            if (!string.IsNullOrWhiteSpace(_originalUriBasePath))
            {
                if (_originalUriBasePath.StartsWith("/") &&
                    _invocation.GetProperty("Platform") == "Linux")
                {
                    _originalUriBasePath = "file:/" + _originalUriBasePath;
                }

                if (Uri.TryCreate(_originalUriBasePath, UriKind.Absolute, out Uri uri))
                {
                    run.OriginalUriBaseIds = new Dictionary <string, ArtifactLocation>
                    {
                        { FileLocationUriBaseId, new ArtifactLocation {
                              Uri = uri
                          } }
                    };
                }
            }

            PersistResults(output, _results, run);
        }