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 ecd27ce commit b174f73
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 @@ -143,8 +143,8 @@ object ReadModelPurposeQueries extends ReadModelQuery {
val eServicesIdsFilter = mapToVarArgs(eServicesIds.map(Filters.eq("data.eserviceId", _)))(Filters.or)
val consumersIdsFilter = mapToVarArgs(consumersIds.map(Filters.eq("data.consumerId", _)))(Filters.or)
val titleFilter =
if (exactMatchOnTitle) title.map(n => safeRegex("data.title", s"^$n$$", "i"))
else title.map(safeRegex("data.title", _, "i"))
if (exactMatchOnTitle) title.map(n => Filters.regex("data.title", s"^${escape(n)}$$", "i"))
else title.map(n => Filters.regex("data.title", escape(n), "i"))
mapToVarArgs(
eServicesIdsFilter.toList ++ consumersIdsFilter.toList ++ statesFilter.toList ++ titleFilter.toList // :+ permissionFilter
)(Filters.and).getOrElse(Filters.empty())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package it.pagopa.interop.purposeprocess.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 b174f73

Please sign in to comment.