Skip to content

Commit

Permalink
PIN-4557-fix: Fix escaping for mongodb regex filters
Browse files Browse the repository at this point in the history
  • Loading branch information
galales committed Feb 14, 2024
1 parent 912869a commit 0b5c259
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ object ReadModelCatalogQueries extends ReadModelQuery {
Some(Filters.or(certifiedFilter, declaredFilter, verifiedFilter))
}
val nameFilter =
if (exactMatchOnName) name.map(n => safeRegex("data.name", s"^$n$$", "i"))
else name.map(safeRegex("data.name", _, "i"))
if (exactMatchOnName) name.map(n => Filters.regex("data.name", s"^${escape(n)}$$", "i"))
else name.map(n => Filters.regex("data.name", escape(n), "i"))

val modeFilter = mode.map(_.toString).map(Filters.eq("data.mode", _))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package it.pagopa.interop.catalogprocess.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)
}

0 comments on commit 0b5c259

Please sign in to comment.