private AnalyzeContext CreateGuidMatchingSkimmer(
            string scanTargetExtension,
            ref SearchDefinition definition,
            out SearchSkimmer skimmer,
            string allowFileExtension  = null,
            string denyFileExtension   = null,
            ValidatorsCache validators = null)
        {
            MatchExpression expression =
                CreateGuidDetectingMatchExpression(
                    denyFileExtension: denyFileExtension,
                    allowFileExtension: allowFileExtension);

            definition ??= CreateDefaultSearchDefinition(expression);

            var logger = new TestLogger();

            var context = new AnalyzeContext
            {
                TargetUri    = new Uri($"file:///c:/{definition.Name}.{definition.FileNameAllowRegex}.{scanTargetExtension}"),
                FileContents = definition.Id,
                Logger       = logger
            };

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(x => x.FileReadAllText(context.TargetUri.LocalPath)).Returns(definition.Id);

            skimmer = CreateSkimmer(
                definition,
                validators: validators,
                fileSystem: mockFileSystem.Object);

            return(context);
        }
예제 #2
0
        public int Run(ValidateOptions options)
        {
            try
            {
                IEnumerable <FileProcessingData> filesData = GetFilesToProcess(options);

                if (!ValidateOptions(options, filesData))
                {
                    return(FAILURE);
                }

                IEnumerable <string> validatorAssemblyPaths =
                    GetValidatorPaths(options.SearchDefinitionsPaths);

                var validators = new ValidatorsCache(validatorAssemblyPaths);

                foreach (FileProcessingData fileData in filesData)
                {
                    SarifLog sarifLog = fileData.SarifLog;
                    new ValidatingVisitor(validators).VisitSarifLog(sarifLog);

                    WriteSarifFile(FileSystem,
                                   fileData.SarifLog,
                                   fileData.OutputFilePath,
                                   options.Minify);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(FAILURE);
            }

            return(SUCCESS);
        }
 public SearchSkimmer(IRegex engine, ValidatorsCache validators, FileRegionsCache fileRegionsCache, SearchDefinition definition, IFileSystem fileSystem = null)
     : this(
         engine,
         validators,
         fileRegionsCache,
         definition.Id,
         definition.Name,
         definition.Description,
         definition.MatchExpressions,
         fileSystem)
 {
 }
        public SearchSkimmer(
            IRegex engine,
            ValidatorsCache validators,
            FileRegionsCache fileRegionsCache,
            string id,
            string name,
            string description,
            IList <MatchExpression> matchExpressions,
            IFileSystem fileSystem = null)
        {
            _id               = id;
            _name             = name;
            _engine           = engine;
            _validators       = validators;
            _fileRegionsCache = fileRegionsCache;
            _fullDescription  = new MultiformatMessageString {
                Text = description
            };
            _fileSystem = fileSystem ?? FileSystem.Instance;

            _messageStrings = new Dictionary <string, MultiformatMessageString>
            {
                { nameof(SdkResources.NotApplicable_InvalidMetadata), new MultiformatMessageString()
                  {
                      Text = SdkResources.NotApplicable_InvalidMetadata,
                  } },
            };

            foreach (MatchExpression matchExpression in matchExpressions)
            {
                string matchExpressionMessage = matchExpression.Message;
                matchExpression.ArgumentNameToIndexMap = GenerateIndicesForNamedArguments(ref matchExpressionMessage);

                string messageId = matchExpression.SubId ?? "Default";
                if (!_messageStrings.TryGetValue(messageId, out MultiformatMessageString mfString))
                {
                    _messageStrings[messageId] = new MultiformatMessageString
                    {
                        Text = matchExpressionMessage,
                    };
                }
            }

            _matchExpressions = matchExpressions;
        }
        private SearchSkimmer CreateSkimmer(
            SearchDefinition definition,
            IRegex engine = null,
            ValidatorsCache validators        = null,
            FileRegionsCache fileRegionsCache = null,
            IFileSystem fileSystem            = null)
        {
            var definitions = new SearchDefinitions
            {
                Definitions = new List <SearchDefinition>(new[] { definition }),
            };

            definitions = AnalyzeCommand.PushInheritedData(definitions, sharedStrings: null);

            return(new SearchSkimmer(
                       engine: engine ?? RE2Regex.Instance,
                       validators: validators,
                       fileRegionsCache: fileRegionsCache ?? new FileRegionsCache(),
                       definition: definitions.Definitions[0],
                       fileSystem: fileSystem));
        }
        public void SearchSkimmer_ValidatorResultsAreProperlyPrioritized()
        {
            string validatorAssemblyPath = $@"c:\{Guid.NewGuid()}.dll";
            string scanTargetExtension   = Guid.NewGuid().ToString();

            var mockFileSystem = new Mock <IFileSystem>();

            mockFileSystem.Setup(x => x.FileExists(validatorAssemblyPath)).Returns(true);
            mockFileSystem.Setup(x => x.AssemblyLoadFrom(validatorAssemblyPath)).Returns(this.GetType().Assembly);

            var validators = new ValidatorsCache(
                new string[] { validatorAssemblyPath },
                fileSystem: mockFileSystem.Object);

            MatchExpression expression =
                CreateGuidDetectingMatchExpression(
                    allowFileExtension: scanTargetExtension);

            expression.ContentsRegex = "TestRule";

            SearchDefinition definition = CreateDefaultSearchDefinition(expression);

            // This Id will match us up with the TestRuleValidator type.
            definition.Id = "TestRule";

            AnalyzeContext context =
                CreateGuidMatchingSkimmer(
                    scanTargetExtension: scanTargetExtension,
                    ref definition,
                    out SearchSkimmer skimmer,
                    validators: validators);

            skimmer.Analyze(context);

            //((TestLogger)context.Logger).Results.Should().BeNull();
        }
예제 #7
0
        public static ISet <Skimmer <AnalyzeContext> > CreateSkimmersFromDefinitionsFiles(
            IFileSystem fileSystem,
            IEnumerable <string> searchDefinitionsPaths,
            IRegex engine = null)
        {
            engine ??= RE2Regex.Instance;

            var validators = new ValidatorsCache();
            FileRegionsCache fileRegionsCache = FileRegionsCache.Instance;

            var skimmers = new HashSet <Skimmer <AnalyzeContext> >();

            // TODO exception handling for bad search definitions files
            foreach (string searchDefinitionsPath in searchDefinitionsPaths)
            {
                string searchDefinitionsText =
                    fileSystem.FileReadAllText(searchDefinitionsPath);

                SearchDefinitions definitions =
                    JsonConvert.DeserializeObject <SearchDefinitions>(searchDefinitionsText);

                // This would skip files that does not look like rules.
                if (definitions == null || definitions.Definitions == null)
                {
                    continue;
                }

                string validatorPath        = null;
                string definitionsDirectory = Path.GetDirectoryName(searchDefinitionsPath);

                if (!string.IsNullOrEmpty(definitions.ValidatorsAssemblyName))
                {
                    // TODO File.Exists check? Logging if not locatable?
                    validatorPath = Path.Combine(definitionsDirectory, definitions.ValidatorsAssemblyName);
                    validators.ValidatorPaths.Add(validatorPath);
                }
                else
                {
                    // If no explicit name of a validator binary was provided,
                    // we look for one that lives alongside the definitions file.
                    validatorPath = Path.GetFileNameWithoutExtension(searchDefinitionsPath) + ".dll";
                    validatorPath = Path.Combine(definitionsDirectory, validatorPath);

                    if (File.Exists(validatorPath))
                    {
                        validators.ValidatorPaths.Add(validatorPath);
                    }
                }

                Dictionary <string, string> sharedStrings = null;
                if (!string.IsNullOrEmpty(definitions.SharedStringsFileName))
                {
                    string sharedStringsFullPath = Path.Combine(definitionsDirectory, definitions.SharedStringsFileName);
                    sharedStrings = LoadSharedStrings(sharedStringsFullPath, fileSystem);
                }

                definitions = PushInheritedData(definitions, sharedStrings);

                foreach (SearchDefinition definition in definitions.Definitions)
                {
                    Skimmer <AnalyzeContext> skimmer = skimmers.FirstOrDefault(skimmer => skimmer.Id == definition.Id);

                    if (skimmer != null)
                    {
                        skimmers.Remove(skimmer);
                    }

                    skimmers.Add(
                        new SearchSkimmer(
                            engine: engine,
                            validators: validators,
                            fileRegionsCache: fileRegionsCache,
                            definition));

                    const string singleSpace = " ";

                    // Send no-op match operations through engine in order to drive caching of all regexes.
                    if (definition.FileNameAllowRegex != null)
                    {
                        engine.Match(singleSpace, definition.FileNameAllowRegex, RegexDefaults.DefaultOptionsCaseSensitive);
                    }

                    foreach (MatchExpression matchExpression in definition.MatchExpressions)
                    {
                        if (!string.IsNullOrEmpty(matchExpression.FileNameAllowRegex))
                        {
                            engine.Match(singleSpace, matchExpression.FileNameAllowRegex, RegexDefaults.DefaultOptionsCaseSensitive);
                        }

                        if (!string.IsNullOrEmpty(matchExpression.ContentsRegex))
                        {
                            engine.Match(singleSpace, matchExpression.ContentsRegex, RegexDefaults.DefaultOptionsCaseSensitive);
                        }
                    }
                }
            }

            return(skimmers);
        }