From b10960db37a5bb6c9ab37c2e9d6c82bf352774be Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 25 Jan 2022 10:28:26 +0100 Subject: [PATCH] SONAR-15825 Escape special characters in like sql query for portfolio projects (cherry picked from commit 52785af21a65810243bdf6e7512406cf0d80d714) --- .../org/sonar/db/component/ComponentDao.java | 3 ++- .../sonar/db/component/ComponentMapper.xml | 10 ++++++++-- .../sonar/db/component/ComponentDaoTest.java | 20 +++++++++++++++++++ .../server/view/index/ViewIndexerTest.java | 5 +++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java index 7a1b2b8ecaa..e93cfef0026 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java @@ -249,7 +249,8 @@ public class ComponentDao implements Dao { } public List selectProjectsFromView(DbSession session, String viewUuid, String projectViewUuid) { - return mapper(session).selectProjectsFromView("%." + viewUuid + ".%", projectViewUuid); + String escapedViewUuid = viewUuid.replace("_", "\\_").replace("%", "\\%"); + return mapper(session).selectProjectsFromView("%." + escapedViewUuid + ".%", projectViewUuid); } /** diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml index 87d255caefb..14b4a7db29f 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml @@ -424,14 +424,20 @@ and p.scope = 'PRJ' and p.qualifier in ('VW', 'APP') - diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java index baae16d1a85..dd2fa7838bf 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java @@ -994,6 +994,26 @@ public class ComponentDaoTest { assertThat(underTest.selectProjectsFromView(dbSession, "Unknown", "Unknown")).isEmpty(); } + @Test + public void select_projects_from_view_should_escape_like_sensitive_characters() { + ComponentDto project1 = db.components().insertPrivateProject(); + ComponentDto project2 = db.components().insertPrivateProject(); + ComponentDto project3 = db.components().insertPrivateProject(); + + ComponentDto view = db.components().insertPrivatePortfolio(); + + //subview with uuid containing special character ( '_' ) for 'like' SQL clause + ComponentDto subView1 = db.components().insertComponent(newSubView(view, "A_C", "A_C-key")); + db.components().insertComponent(newProjectCopy(project1, subView1)); + db.components().insertComponent(newProjectCopy(project2, subView1)); + + ComponentDto subView2 = db.components().insertComponent(newSubView(view, "ABC", "ABC-key")); + db.components().insertComponent(newProjectCopy(project3, subView2)); + + assertThat(underTest.selectProjectsFromView(dbSession, subView1.uuid(), view.uuid())).containsExactlyInAnyOrder(project1.uuid(), project2.uuid()); + assertThat(underTest.selectProjectsFromView(dbSession, subView2.uuid(), view.uuid())).containsExactlyInAnyOrder(project3.uuid()); + } + @Test public void select_projects() { ComponentDto provisionedProject = db.components().insertPrivateProject(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java index 4ec942a8b28..83a88f571d2 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java @@ -57,6 +57,11 @@ public class ViewIndexerTest { private final DbSession dbSession = db.getSession(); private final ViewIndexer underTest = new ViewIndexer(dbClient, es.client()); + @Test + public void getIndexTypes() { + assertThat(underTest.getIndexTypes()).containsExactly(TYPE_VIEW); + } + @Test public void index_nothing() { underTest.indexOnStartup(emptySet()); -- 2.39.5