Ejemplo n.º 1
0
        public void IndexDoc_Security_CannotAddPasswordHash()
        {
            var passwordHashFieldName = "PasswordHash";
            var passwordHashField     = new IndexField(passwordHashFieldName, "31275491872354956198543",
                                                       IndexingMode.NotAnalyzed, IndexStoringMode.No, IndexTermVector.No);
            var indexDoc = new IndexDocument {
                passwordHashField
            };

            Assert.IsFalse(indexDoc.Any(f => f.Name == passwordHashFieldName));
            Assert.IsNull(indexDoc.GetStringValue(passwordHashFieldName));

            Assert.IsFalse(indexDoc.Fields.ContainsKey(passwordHashFieldName));
        }
Ejemplo n.º 2
0
        public void IndexDoc_Security_CannotAddPasswordHash()
        {
            var passwordHashFieldName = "PasswordHash";
            var passwordHashField     = new IndexField(passwordHashFieldName, "31275491872354956198543",
                                                       IndexingMode.NotAnalyzed, IndexStoringMode.No, IndexTermVector.No);
            var indexDoc = new IndexDocument {
                passwordHashField
            };

            Assert.IsFalse(indexDoc.Any(f => f.Name == passwordHashFieldName));
            Assert.IsNull(indexDoc.GetStringValue(passwordHashFieldName));

            var indexDocAcc = new PrivateObject(indexDoc);
            var fields      = (Dictionary <string, IndexField>)indexDocAcc.GetFieldOrProperty("_fields");

            Assert.IsFalse(fields.ContainsKey(passwordHashFieldName));
        }
Ejemplo n.º 3
0
        public void IndexDoc_Security_CannotAddPassword()
        {
            var passwordFieldName = "Password";
            var passwordField     = new IndexField(passwordFieldName, "password123",
                                                   IndexingMode.NotAnalyzed, IndexStoringMode.No, IndexTermVector.No);
            var indexDoc = new IndexDocument {
                passwordField
            };

            Assert.IsFalse(indexDoc.Any(f => f.Name == passwordFieldName));
            Assert.IsNull(indexDoc.GetStringValue(passwordFieldName));

            var indexDocAcc = new ObjectAccessor(indexDoc);
            var fields      = (Dictionary <string, IndexField>)indexDocAcc.GetFieldOrProperty("_fields");

            Assert.IsFalse(fields.ContainsKey(passwordFieldName));
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public IndexDocument CompleteIndexDocument(Node node, IndexDocument baseDocument)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var textEtract = new StringBuilder(baseDocument.GetStringValue(IndexFieldName.AllText));

            var faultedFieldNames = new List <string>();

            if (node is IIndexableDocument ixnode)
            {
                foreach (var field in ixnode.GetIndexableFields())
                {
                    if (IndexDocument.ForbiddenFields.Contains(field.Name))
                    {
                        continue;
                    }
                    if (IndexDocument.PostponedFields.Contains(field.Name))
                    {
                        continue;
                    }
                    if (node.SavingState != ContentSavingState.Finalized && (field.IsBinaryField || SkippedMultistepFields.Contains(field.Name)))
                    {
                        continue;
                    }
                    if (!field.IsBinaryField)
                    {
                        continue;
                    }
                    if (!TextExtractor.TextExtractingWillBePotentiallySlow((BinaryData)((BinaryField)field).GetData()))
                    {
                        continue;
                    }

                    IEnumerable <IndexField> indexFields = null;
                    string extract = null;
                    try
                    {
                        indexFields = field.GetIndexFields(out extract);
                    }
                    catch (Exception)
                    {
                        faultedFieldNames.Add(field.Name);
                    }

                    if (!String.IsNullOrEmpty(extract)) // do not add extra line if extract is empty
                    {
                        try
                        {
                            textEtract.AppendLine(extract);
                        }
                        catch (OutOfMemoryException)
                        {
                            SnLog.WriteWarning("Out of memory error during indexing.",
                                               EventId.Indexing,
                                               properties: new Dictionary <string, object>
                            {
                                { "Path", node.Path },
                                { "Field", field.Name }
                            });
                        }
                    }

                    if (indexFields != null)
                    {
                        foreach (var indexField in indexFields)
                        {
                            baseDocument.Add(indexField);
                        }
                    }
                }
            }

            baseDocument.Add(new IndexField(IndexFieldName.AllText, textEtract.ToString(), IndexingMode.Analyzed, IndexStoringMode.No, IndexTermVector.Default));

            if (faultedFieldNames.Any())
            {
                if (!baseDocument.GetBooleanValue(IndexFieldName.IsFaulted))
                {
                    baseDocument.Add(new IndexField(IndexFieldName.IsFaulted, true, IndexingMode.NotAnalyzed, IndexStoringMode.Yes, IndexTermVector.Default));
                }
                foreach (var faultedFieldName in faultedFieldNames)
                {
                    baseDocument.Add(new IndexField(IndexFieldName.FaultedFieldName, faultedFieldName, IndexingMode.NotAnalyzed, IndexStoringMode.Yes, IndexTermVector.Default));
                }
            }

            return(baseDocument);
        }
Ejemplo n.º 5
0
        private void WriteTo(string rootPath, IndexDocument document)
        {
            if (!Directory.Exists(rootPath))
            {
                Directory.CreateDirectory(rootPath);
            }

            var nodeId      = document.GetIntegerValue(IndexFieldName.NodeId);
            var versionId   = document.GetIntegerValue(IndexFieldName.VersionId);
            var contentType = document.GetStringValue(IndexFieldName.Type);

            var contentFileBase = $"{rootPath}\\{nodeId}-{versionId}-{contentType}";
            var contentFile     = contentFileBase;
            var suffix          = 0;

            while (File.Exists(contentFile + ".txt"))
            {
                contentFile = contentFileBase + "-" + ++suffix;
            }
            contentFile = contentFile + ".txt";

            using (var writer = new StreamWriter(contentFile))
            {
                writer.WriteLine("{");

                foreach (var field in document)
                {
                    string value;
                    var    type = "";
                    switch (field.Type)
                    {
                    case IndexValueType.String:
                        value = field.StringValue == null ? "null" : $"\"{field.StringValue}\"";
                        break;

                    case IndexValueType.StringArray:
                        value = "[" + string.Join(", ", field.StringArrayValue.Select(s => $"\"{s}\"").ToArray()) + "]";
                        break;

                    case IndexValueType.DateTime:
                        value = $"\"{field.ValueAsString}\"";
                        break;

                    case IndexValueType.Long:
                    case IndexValueType.Float:
                    case IndexValueType.Double:
                        value = field.ValueAsString;
                        type  = " // " + field.Type.ToString().ToLowerInvariant();
                        break;

                    case IndexValueType.Bool:
                    case IndexValueType.Int:
                    default:
                        value = field.ValueAsString;
                        break;
                    }
                    writer.WriteLine("    {0}: {1},{2}", field.Name, value, type);
                }

                writer.WriteLine("}");
            }
        }