Skip to content

Commit

Permalink
#1388 Fix parsing of tags with hash sign
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jun 28, 2020
1 parent dbed42d commit f9a6d69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 9 additions & 9 deletions thehive/app/org/thp/thehive/models/Tag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ case class Tag(
}

object Tag {
lazy val logger: Logger = Logger(getClass)
val namespacePredicateValueColour: Regex = "([^\".:=]+)[.:]([\".=]+)=\"([^\"]+)\"#(\\p{XDigit}{6})".r
val namespacePredicateValue: Regex = "([^\".:=]+)[.:]([^\".=]+)=\"?([^\"]+)\"?".r
val namespacePredicate: Regex = "([^\".:=]+)[.]([^\".=]+)".r
val PredicateValue: Regex = "([^\".:=]+)[=:]\"?([^\"]+)\"?".r
val predicate: Regex = "([^\".:=]+)".r
lazy val logger: Logger = Logger(getClass)
val tagColour: Regex = "(.*)#(\\p{XDigit}{6})".r
val namespacePredicateValue: Regex = "([^\".:=]+)[.:]([^\".=]+)=\"?([^\"]+)\"?".r
val namespacePredicate: Regex = "([^\".:=]+)[.]([^\".=]+)".r
val PredicateValue: Regex = "([^\".:=]+)[=:]\"?([^\"]+)\"?".r
val predicate: Regex = "([^\".:=]+)".r

def fromString(tagName: String, defaultNamespace: String, defaultColour: Int = 0): Tag = {
val (name, colour) = tagName.split('#') match {
case Array(n, c) => n -> Try(Integer.parseUnsignedInt(c, 16)).getOrElse(defaultColour)
case Array(n) => n -> defaultColour
val (name, colour) = tagName match {
case tagColour(n, c) => n -> Try(Integer.parseUnsignedInt(c, 16)).getOrElse(defaultColour)
case _ => tagName -> defaultColour
}
name match {
case namespacePredicateValue(namespace, predicate, value) if value.exists(_ != '=') =>
Expand Down
12 changes: 12 additions & 0 deletions thehive/test/org/thp/thehive/models/TagTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,17 @@ class TagTest extends PlaySpecification {
tag must beEqualTo(Tag(defaultNamespace, "ip", Some("8.8.8.8"), None, defaultColor))
tag.toString must beEqualTo("ip=\"8.8.8.8\"")
}

"be parsed with colour" in {
val tag = parseTag("ip:8.8.8.8#FF00FF")
tag must beEqualTo(Tag(defaultNamespace, "ip", Some("8.8.8.8"), None, 0xFF00FF))
tag.toString must beEqualTo("ip=\"8.8.8.8\"")
}

"be parsed with hash sign and colour" in {
val tag = parseTag("case:#42#FF00FF")
tag must beEqualTo(Tag(defaultNamespace, "case", Some("#42"), None, 0xFF00FF))
tag.toString must beEqualTo("case=\"#42\"")
}
}
}

0 comments on commit f9a6d69

Please sign in to comment.