Skip to content

Commit

Permalink
PIN-4557: Safer mongodb regex filters
Browse files Browse the repository at this point in the history
  • Loading branch information
galales committed Feb 13, 2024
1 parent 1fee9a5 commit 728f824
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package it.pagopa.interop.tenantprocess.common.readmodel

import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.model.Filters

trait ReadModelQuery {
def mapToVarArgs[A, B](l: Seq[A])(f: Seq[A] => B): Option[B] = Option.when(l.nonEmpty)(f(l))

def escape(str: String): String = str.replaceAll("([.*+?^${}()|\\[\\]\\\\])", "\\\\$1")
def safeRegex(fieldName: String, pattern: String, options: String): Bson =
Filters.regex(fieldName, escape(pattern), options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ object ReadModelTenantQueries extends ReadModelQuery {

private def listTenantsFilters(name: Option[String]): Bson = {
val nameFilter = name match {
case Some(n) if n.nonEmpty => List(Filters.regex("data.name", n, "i"))
case Some(n) if n.nonEmpty => List(safeRegex("data.name", n, "i"))
case _ => Nil
}
val withSelfcareIdFilter = Filters.exists("data.selfcareId", true)
Expand Down

0 comments on commit 728f824

Please sign in to comment.