Skip to content

Commit

Permalink
HCMPRE-804-Removed sql injection in like queries
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhang-eGov committed Oct 14, 2024
1 parent e68c5fb commit e2b660c
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private String getQueryForIndividual(IndividualSearch searchObject, Integer limi
}
if (searchObject.getIndividualName() != null) {
query = query + "AND givenname LIKE :individualName ";
paramsMap.put("individualName", "%"+searchObject.getIndividualName()+"%");
paramsMap.put("individualName", "%"+searchObject.getIndividualName().replace("%", "\\%").replace("_", "\\_")+"%");
}
if (searchObject.getGender() != null) {
query = query + "AND gender =:gender ";
Expand All @@ -238,7 +238,9 @@ private String getQueryForIndividual(IndividualSearch searchObject, Integer limi
paramsMap.put("dateOfBirth", searchObject.getDateOfBirth());
}
if (searchObject.getSocialCategory() != null) {
query = query + "AND additionaldetails->'fields' @> '[{\"key\": \"SOCIAL_CATEGORY\", \"value\":" + "\"" + searchObject.getSocialCategory() + "\"}]' ";
query += " AND additionaldetails->'fields' @> :socialCategory ";
String socialCategoryJson = "[{\"key\": \"SOCIAL_CATEGORY\", \"value\": \"" + searchObject.getSocialCategory() + "\"}]";
paramsMap.put("socialCategory", socialCategoryJson);
}
if (searchObject.getCreatedFrom() != null) {

Expand All @@ -264,14 +266,11 @@ private String getQueryForIndividual(IndividualSearch searchObject, Integer limi
query = query + "AND lastModifiedTime>=:lastModifiedTime ";
}
if (searchObject.getRoleCodes() != null && !searchObject.getRoleCodes().isEmpty()) {
query = query + "AND roles @> '[";
for (int i = 0; i < searchObject.getRoleCodes().size(); i++) {
query = query + "{\"code\": \"" + searchObject.getRoleCodes().get(i) + "\"}";
if (i != searchObject.getRoleCodes().size() - 1) {
query = query + ",";
}
}
query = query + "]' ";
query = query + "AND roles @> :roleCodesJson ";
String roleCodesJson = searchObject.getRoleCodes().stream()
.map(code -> "{\"code\": \"" + code + "\"}")
.collect(Collectors.joining(",", "[", "]"));
paramsMap.put("roleCodesJson", roleCodesJson);
}

if (searchObject.getUsername() != null) {
Expand Down

0 comments on commit e2b660c

Please sign in to comment.