Ejemplo n.º 1
0
        public void TryReconstructAbsoluteUri_WhenInputResolvesIndirectly_ReturnsTrueWithResolvedUri()
        {
            var artifactLocation = new ArtifactLocation
            {
                Uri       = new Uri("Sarif/CopyrightNotice.txt", UriKind.Relative),
                UriBaseId = SourceRootBaseId
            };

            var originalUriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                [ProjectRootBaseId] = new ArtifactLocation
                {
                    Uri = new Uri("file://c:/code/sarif-sdk/", UriKind.Absolute)
                },

                [SourceRootBaseId] = new ArtifactLocation
                {
                    Uri       = new Uri("src/", UriKind.Relative),
                    UriBaseId = ProjectRootBaseId
                }
            };

            bool wasResolved = artifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri);

            wasResolved.Should().BeTrue();
            resolvedUri.Should().Be(new Uri("file://c:/code/sarif-sdk/src/Sarif/CopyrightNotice.txt", UriKind.Absolute));
        }
Ejemplo n.º 2
0
        public void TryReconstructAbsoluteUri_WhenChainedUriIsAbsent_ReturnsFalse()
        {
            var artifactLocation = new ArtifactLocation
            {
                Uri       = new Uri("Sarif/CopyrightNotice.txt", UriKind.Relative),
                UriBaseId = SourceRootBaseId
            };

            var originalUriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                [ProjectRootBaseId] = new ArtifactLocation
                {
                    Description = new Message
                    {
                        Text = "The root of the project."
                    }
                    // But Uri is absent.
                },

                [SourceRootBaseId] = new ArtifactLocation
                {
                    Uri       = new Uri("src/", UriKind.Relative),
                    UriBaseId = ProjectRootBaseId // But originalUriBaseIds[ProjectRootBaseId] is absent.
                }
            };

            bool wasResolved = artifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri);

            wasResolved.Should().BeFalse();
        }
Ejemplo n.º 3
0
        private bool AnalyzeArtifactLocation(ArtifactLocation artifactLocation)
        {
            // No artifactLocation / no artifacts, so we should look for the snippet.
            if (artifactLocation == null || this.artifacts == null)
            {
                return(true);
            }

            // Checking if we can reconstruct uri from artifactLocation
            // If we can't, we still need to validate, since neither originalUriBaseIds
            // nor artifactLocation.UriBaseId is required.
            artifactLocation.TryReconstructAbsoluteUri(this.originalUriBaseIds, out Uri resolvedUri);

            foreach (Artifact artifact in this.artifacts)
            {
                // Content/text doesn't exist, continue to next
                if (string.IsNullOrEmpty(artifact.Contents?.Text))
                {
                    continue;
                }


                // if mimetype exists and is binary, continue to next
                if (!string.IsNullOrEmpty(artifact.MimeType) && MimeType.IsBinaryMimeType(artifact.MimeType))
                {
                    continue;
                }

                // Checking if we can reconstruct uri from artifact
                // If we can't, we still need to validate, since originalUriBaseIds aren't
                // required nether artifactLocation.UriBaseId.
                artifact.Location.TryReconstructAbsoluteUri(this.originalUriBaseIds, out Uri artifactUri);

                if (resolvedUri != null && artifactUri != null)
                {
                    if (artifactUri.AbsolutePath.Equals(resolvedUri.AbsolutePath))
                    {
                        return(false);
                    }
                }
                else
                {
                    // Couldn't generate the absoluteUris, so let's compare everything.
                    if (this.artifacts.Any(a => a.Location?.Uri.OriginalString == artifactLocation.Uri.OriginalString &&
                                           a.Location?.UriBaseId == artifactLocation.UriBaseId))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        public static string ExtractSnippet(this Location location, Run run, FileRegionsCache fileRegionsCache)
        {
            PhysicalLocation physicalLocation = location.PhysicalLocation;
            ArtifactLocation artifactLocation = location.PhysicalLocation?.ArtifactLocation;
            Region           region           = location.PhysicalLocation?.Region;
            Uri uri = location.PhysicalLocation?.ArtifactLocation?.Uri;

            if (uri == null || region == null || region.IsBinaryRegion || physicalLocation == null)
            {
                return(string.Empty);
            }

            if (region.Snippet != null)
            {
                return(region.Snippet.Text);
            }

            if (artifactLocation.Uri == null && artifactLocation.Index >= 0)
            {
                // Uri is not stored at result level, but we have an index to go look in run.Artifacts
                // we must pick the ArtifactLocation details from run.artifacts array
                Artifact artifactFromRun = run.Artifacts[artifactLocation.Index];
                artifactLocation = artifactFromRun.Location;
            }

            // If we can resolve a file location to a newly constructed
            // absolute URI, we will prefer that
            if (!artifactLocation.TryReconstructAbsoluteUri(run.OriginalUriBaseIds, out Uri resolvedUri))
            {
                resolvedUri = artifactLocation.Uri;
            }

            if (!resolvedUri.IsAbsoluteUri)
            {
                return(string.Empty);
            }

            fileRegionsCache ??= new FileRegionsCache();
            Region expandedRegion = fileRegionsCache.PopulateTextRegionProperties(region, resolvedUri, populateSnippet: true);

            return(expandedRegion.Snippet != null ? expandedRegion.Snippet.Text : string.Empty);
        }
Ejemplo n.º 5
0
        public override Artifact VisitArtifact(Artifact node)
        {
            ArtifactLocation fileLocation = node.Location;

            if (fileLocation != null)
            {
                bool workToDo = false;
                bool overwriteExistingData = _dataToInsert.HasFlag(OptionallyEmittedData.OverwriteExistingData);

                workToDo |= (node.Hashes == null || overwriteExistingData) && _dataToInsert.HasFlag(OptionallyEmittedData.Hashes);
                workToDo |= (node.Contents?.Text == null || overwriteExistingData) && _dataToInsert.HasFlag(OptionallyEmittedData.TextFiles);
                workToDo |= (node.Contents?.Binary == null || overwriteExistingData) && _dataToInsert.HasFlag(OptionallyEmittedData.BinaryFiles);

                if (workToDo)
                {
                    if (fileLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri uri))
                    {
                        Encoding encoding = null;

                        string encodingText = node.Encoding ?? _run.DefaultEncoding;

                        if (!string.IsNullOrWhiteSpace(encodingText))
                        {
                            try
                            {
                                encoding = Encoding.GetEncoding(encodingText);
                            }
                            catch (ArgumentException) { }
                        }

                        int length = node.Length;
                        node               = Artifact.Create(uri, _dataToInsert, encoding: encoding);
                        node.Length        = length;
                        fileLocation.Index = -1;
                        node.Location      = fileLocation;
                    }
                }
            }

            return(base.VisitArtifact(node));
        }
Ejemplo n.º 6
0
        public void TryReconstructAbsoluteUri_WhenInputUriResolvesDirectly_ReturnsTrueWithResolvedUri()
        {
            var artifactLocation = new ArtifactLocation
            {
                Uri       = new Uri("README.md", UriKind.Relative),
                UriBaseId = ProjectRootBaseId
            };

            var originalUriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                [ProjectRootBaseId] = new ArtifactLocation
                {
                    Uri = new Uri("file://c:/code/sarif-sdk/", UriKind.Absolute)
                }
            };

            bool wasResolved = artifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri);

            wasResolved.Should().BeTrue();
            resolvedUri.Should().Be(new Uri("file://c:/code/sarif-sdk/README.md", UriKind.Absolute));
        }
Ejemplo n.º 7
0
        public void TryReconstructAbsoluteUri_WhenChainedUriBaseIdIsAbsent_ReturnsFalse()
        {
            var artifactLocation = new ArtifactLocation
            {
                Uri       = new Uri("Sarif/CopyrightNotice.txt", UriKind.Relative),
                UriBaseId = "SOURCE_ROOT"
            };

            var originalUriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                ["SOURCE_ROOT"] = new ArtifactLocation
                {
                    Uri       = new Uri("src/", UriKind.Relative),
                    UriBaseId = "PROJECT_ROOT" // But originalUriBaseIds["PROJECT_ROOT"] is absent.
                }
            };

            bool wasResolved = artifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri);

            wasResolved.Should().BeFalse();
        }
Ejemplo n.º 8
0
        public void TryReconstructAbsoluteUri_WhenBaseUriDoesNotEndWithSlash_EnsuresSlashIsPresent()
        {
            var artifactLocation = new ArtifactLocation
            {
                Uri       = new Uri("src/Sarif/CopyrightNotice.txt", UriKind.Relative),
                UriBaseId = ProjectRootBaseId
            };

            var originalUriBaseIds = new Dictionary <string, ArtifactLocation>
            {
                [ProjectRootBaseId] = new ArtifactLocation
                {
                    // It's invalid SARIF for a base URI not to end with a slash, but we shouldn't
                    // fail to resolve a URI just because some tool didn't do that.
                    Uri       = new Uri("file://c:/code/sarif-sdk"),
                    UriBaseId = ProjectRootBaseId
                }
            };

            bool wasResolved = artifactLocation.TryReconstructAbsoluteUri(originalUriBaseIds, out Uri resolvedUri);

            wasResolved.Should().BeTrue();
            resolvedUri.Should().Be(new Uri("file://c:/code/sarif-sdk/src/Sarif/CopyrightNotice.txt", UriKind.Absolute));
        }
Ejemplo n.º 9
0
        public override PhysicalLocation VisitPhysicalLocation(PhysicalLocation node)
        {
            if (node.Region == null || node.Region.IsBinaryRegion)
            {
                goto Exit;
            }

            bool insertRegionSnippets      = _dataToInsert.HasFlag(OptionallyEmittedData.RegionSnippets);
            bool overwriteExistingData     = _dataToInsert.HasFlag(OptionallyEmittedData.OverwriteExistingData);
            bool insertContextCodeSnippets = _dataToInsert.HasFlag(OptionallyEmittedData.ContextRegionSnippets);
            bool populateRegionProperties  = _dataToInsert.HasFlag(OptionallyEmittedData.ComprehensiveRegionProperties);

            if (insertRegionSnippets || populateRegionProperties || insertContextCodeSnippets)
            {
                Region           expandedRegion;
                ArtifactLocation artifactLocation = node.ArtifactLocation;

                _fileRegionsCache = _fileRegionsCache ?? new FileRegionsCache(_run);

                if (artifactLocation.Uri == null && artifactLocation.Index >= 0)
                {
                    // Uri is not stored at result level, but we have an index to go look in run.Artifacts
                    // we must pick the ArtifactLocation details from run.artifacts array
                    Artifact artifactFromRun = _run.Artifacts[artifactLocation.Index];
                    artifactLocation = artifactFromRun.Location;
                }

                // If we can resolve a file location to a newly constructed
                // absolute URI, we will prefer that
                if (!artifactLocation.TryReconstructAbsoluteUri(_run.OriginalUriBaseIds, out Uri resolvedUri))
                {
                    resolvedUri = artifactLocation.Uri;
                }

                if (!resolvedUri.IsAbsoluteUri)
                {
                    goto Exit;
                }

                expandedRegion = _fileRegionsCache.PopulateTextRegionProperties(node.Region, resolvedUri, populateSnippet: insertRegionSnippets);

                ArtifactContent originalSnippet = node.Region.Snippet;

                if (populateRegionProperties)
                {
                    node.Region = expandedRegion;
                }

                if (originalSnippet == null || overwriteExistingData)
                {
                    node.Region.Snippet = expandedRegion.Snippet;
                }
                else
                {
                    node.Region.Snippet = originalSnippet;
                }

                if (insertContextCodeSnippets && (node.ContextRegion == null || overwriteExistingData))
                {
                    node.ContextRegion = _fileRegionsCache.ConstructMultilineContextSnippet(expandedRegion, resolvedUri);
                }
            }

Exit:
            return(base.VisitPhysicalLocation(node));
        }