Ejemplo n.º 1
0
        public void FileData_ComputeHashes()
        {
            string filePath     = Path.GetTempFileName();
            string fileContents = Guid.NewGuid().ToString();
            Uri    uri          = new Uri(filePath);

            try
            {
                File.WriteAllText(filePath, fileContents);
                FileData fileData = FileData.Create(uri, OptionallyEmittedData.Hashes);
                fileData.FileLocation.Should().Be(null);
                HashData hashes = HashUtilities.ComputeHashes(filePath);
                fileData.MimeType.Should().Be(MimeType.Binary);
                fileData.Contents.Should().BeNull();
                fileData.Hashes.Count.Should().Be(3);

                foreach (Hash hash in fileData.Hashes)
                {
                    switch (hash.Algorithm)
                    {
                    case "md5": { hash.Value.Should().Be(hashes.MD5); break; }

                    case "sha-1": { hash.Value.Should().Be(hashes.Sha1); break; }

                    case "sha-256": { hash.Value.Should().Be(hashes.Sha256); break; }

                    default: { true.Should().BeFalse(); break; /* unexpected algorithm kind */ }
                    }
                }
            }
            finally
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Ejemplo n.º 2
0
        public static FileData Create(
            Uri uri,
            OptionallyEmittedData dataToInsert = OptionallyEmittedData.None,
            string mimeType        = null,
            Encoding encoding      = null,
            IFileSystem fileSystem = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            mimeType   = mimeType ?? SarifWriters.MimeType.DetermineFromFileExtension(uri);
            fileSystem = fileSystem ?? new FileSystem();

            var fileData = new FileData()
            {
                MimeType = mimeType
            };

            // Attempt to persist file contents and/or compute file hash and persist
            // this information to the log file. In the event that there is some issue
            // accessing the file, for example, due to ACLs applied to a directory,
            // we currently swallow these exceptions without populating any requested
            // data or putting a notification in the log file that a problem
            // occurred. Something to discuss moving forward.
            try
            {
                if (!uri.IsAbsoluteUri || !uri.IsFile || !fileSystem.FileExists(uri.LocalPath))
                {
                    return(fileData);
                }

                string filePath = uri.LocalPath;

                if (dataToInsert.Includes(OptionallyEmittedData.BinaryFiles) &&
                    SarifWriters.MimeType.IsBinaryMimeType(mimeType))
                {
                    fileData.Contents = GetEncodedFileContents(fileSystem, filePath, mimeType, encoding);
                }

                if (dataToInsert.Includes(OptionallyEmittedData.TextFiles) &&
                    SarifWriters.MimeType.IsTextualMimeType(mimeType))
                {
                    fileData.Contents = GetEncodedFileContents(fileSystem, filePath, mimeType, encoding);
                }

                if (dataToInsert.Includes(OptionallyEmittedData.Hashes))
                {
                    HashData hashes = HashUtilities.ComputeHashes(filePath);
                    fileData.Hashes = new List <Hash>
                    {
                        new Hash()
                        {
                            Value     = hashes.MD5,
                            Algorithm = "md5",
                        },
                        new Hash()
                        {
                            Value     = hashes.Sha1,
                            Algorithm = "sha-1",
                        },
                        new Hash()
                        {
                            Value     = hashes.Sha256,
                            Algorithm = "sha-256",
                        },
                    };
                }
            }
            catch (Exception e) when(e is IOException || e is UnauthorizedAccessException)
            {
            }

            return(fileData);
        }
Ejemplo n.º 3
0
        public static Artifact Create(
            Uri uri,
            OptionallyEmittedData dataToInsert = OptionallyEmittedData.None,
            Encoding encoding      = null,
            HashData hashData      = null,
            IFileSystem fileSystem = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            fileSystem = fileSystem ?? new FileSystem();

            var artifact = new Artifact()
            {
                Encoding = encoding?.WebName,
            };

            string mimeType = SarifWriters.MimeType.DetermineFromFileExtension(uri);

            // Attempt to persist file contents and/or compute file hash and persist
            // this information to the log file. In the event that there is some issue
            // accessing the file, for example, due to ACLs applied to a directory,
            // we currently swallow these exceptions without populating any requested
            // data or putting a notification in the log file that a problem
            // occurred. Something to discuss moving forward.
            try
            {
                bool workTodo = dataToInsert.HasFlag(OptionallyEmittedData.Hashes) ||
                                dataToInsert.HasFlag(OptionallyEmittedData.TextFiles) ||
                                dataToInsert.HasFlag(OptionallyEmittedData.BinaryFiles);

                if (!workTodo ||
                    !uri.IsAbsoluteUri ||
                    !uri.IsFile ||
                    !fileSystem.FileExists(uri.LocalPath))
                {
                    return(artifact);
                }

                string filePath = uri.LocalPath;

                if (dataToInsert.HasFlag(OptionallyEmittedData.BinaryFiles) &&
                    SarifWriters.MimeType.IsBinaryMimeType(mimeType))
                {
                    artifact.Contents = GetEncodedFileContents(fileSystem, filePath, mimeType, encoding);
                }

                if (dataToInsert.HasFlag(OptionallyEmittedData.TextFiles) &&
                    SarifWriters.MimeType.IsTextualMimeType(mimeType))
                {
                    artifact.Contents = GetEncodedFileContents(fileSystem, filePath, mimeType, encoding);
                }

                if (dataToInsert.HasFlag(OptionallyEmittedData.Hashes))
                {
                    HashData hashes = hashData ?? HashUtilities.ComputeHashes(filePath);
                    artifact.Hashes = new Dictionary <string, string>
                    {
                        { "md5", hashes.MD5 },
                        { "sha-1", hashes.Sha1 },
                        { "sha-256", hashes.Sha256 },
                    };
                }
            }
            catch (Exception e) when(e is IOException || e is UnauthorizedAccessException)
            {
            }

            return(artifact);
        }