Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIN-4557: Safer mongodb regex filters #96

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading