Esempio n. 1
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            bool computeTargetsHash,
            IEnumerable <string> invocationTokensToRedact)
        {
            var run = new Run();

            if (analysisTargets != null)
            {
                run.Files = new Dictionary <string, IList <FileData> >();

                foreach (string target in analysisTargets)
                {
                    var fileReference = new FileData();

                    if (computeTargetsHash)
                    {
                        string md5, sha1, sha256;

                        HashUtilities.ComputeHashes(target, out md5, out sha1, out sha256);
                        fileReference.Hashes = new List <Hash>(new Hash[]
                        {
                            new Hash()
                            {
                                Value     = md5,
                                Algorithm = AlgorithmKind.MD5,
                            },
                            new Hash()
                            {
                                Value     = sha1,
                                Algorithm = AlgorithmKind.Sha1,
                            },
                            new Hash()
                            {
                                Value     = sha256,
                                Algorithm = AlgorithmKind.Sha256,
                            },
                        });
                    }
                    run.Files.Add(new Uri(target).ToString(), new List <FileData> {
                        fileReference
                    });
                }
            }

            string invocation = Environment.CommandLine;

            if (invocationTokensToRedact != null)
            {
                foreach (string tokenToRedact in invocationTokensToRedact)
                {
                    invocation = invocation.Replace(tokenToRedact, SarifConstants.RemovedMarker);
                }
            }
            run.Invocation = invocation;
            run.StartTime  = DateTime.UtcNow;
            return(run);
        }
Esempio n. 2
0
        private static Run CreateRun(
            IEnumerable <string> analysisTargets,
            bool computeTargetsHash,
            bool logEnvironment,
            IEnumerable <string> invocationTokensToRedact)
        {
            var run = new Run();

            if (analysisTargets != null)
            {
                run.Files = new Dictionary <string, IList <FileData> >();

                foreach (string target in analysisTargets)
                {
                    var fileReference = new FileData();

                    if (computeTargetsHash)
                    {
                        string md5, sha1, sha256;

                        HashUtilities.ComputeHashes(target, out md5, out sha1, out sha256);
                        fileReference.Hashes = new HashSet <Hash>
                        {
                            new Hash()
                            {
                                Value     = md5,
                                Algorithm = AlgorithmKind.MD5,
                            },
                            new Hash()
                            {
                                Value     = sha1,
                                Algorithm = AlgorithmKind.Sha1,
                            },
                            new Hash()
                            {
                                Value     = sha256,
                                Algorithm = AlgorithmKind.Sha256,
                            },
                        };
                    }
                    run.Files.Add(new Uri(target).ToString(), new List <FileData> {
                        fileReference
                    });
                }
            }


            run.Invocation = Invocation.Create(logEnvironment);

            // TODO we should actually redact across the complete log file context
            // by a dedicated rewriting visitor or some other approach.
            if (invocationTokensToRedact != null)
            {
                run.Invocation.Parameters       = Redact(run.Invocation.Parameters, invocationTokensToRedact);
                run.Invocation.Machine          = Redact(run.Invocation.Machine, invocationTokensToRedact);
                run.Invocation.Account          = Redact(run.Invocation.Account, invocationTokensToRedact);
                run.Invocation.Parameters       = Redact(run.Invocation.Parameters, invocationTokensToRedact);
                run.Invocation.WorkingDirectory = Redact(run.Invocation.WorkingDirectory, invocationTokensToRedact);

                if (run.Invocation.EnvironmentVariables != null)
                {
                    string[] keys = run.Invocation.EnvironmentVariables.Keys.ToArray();

                    foreach (string key in keys)
                    {
                        string value = run.Invocation.EnvironmentVariables[key];
                        run.Invocation.EnvironmentVariables[key] = Redact(value, invocationTokensToRedact);
                    }
                }
            }
            return(run);
        }