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

fix: not apply identifiable token filter to uid length < 4[DHIS2-17098-39] #19334

Open
wants to merge 1 commit into
base: 2.39
Choose a base branch
from
Open
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
Expand Up @@ -61,6 +61,9 @@ public Criterion getHibernateCriterion(QueryPath queryPath) {
@Override
public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPath queryPath) {
String value = caseSensitive ? getValue(String.class) : getValue(String.class).toLowerCase();
if (skipUidToken(value, queryPath)) {
return null;
}

return builder.equal(
builder.function(
Expand All @@ -75,4 +78,8 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa
public boolean test(Object value) {
return TokenUtils.test(value, getValue(String.class), caseSensitive, matchMode);
}

private boolean skipUidToken(String value, QueryPath query) {
return "uid".equals(query.getProperty().getFieldName()) && value.length() < 4;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
package org.hisp.dhis.query;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.hisp.dhis.query.operators.MatchMode;
import org.hisp.dhis.query.operators.NotTokenOperator;
import org.hisp.dhis.query.operators.TokenOperator;
import org.hisp.dhis.query.planner.QueryPath;
import org.hisp.dhis.schema.Property;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -43,6 +47,16 @@
*/
class TokenOperatorTest {

@Test
@DisplayName("Uid Token filter must have at least 4 characters. Otherwise return null.")
void testUidLength() {
TokenOperator operator = new TokenOperator("ABC", false, MatchMode.ANYWHERE);
Property property = new Property();
property.setFieldName("uid");
QueryPath queryPath = new QueryPath(property, true);
assertNull(operator.getPredicate(null, null, queryPath));
}

@Test
void nullValue() {
for (MatchMode mode : MatchMode.values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2004-2023, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.webapi.controller;

import static org.hisp.dhis.web.WebClientUtils.assertStatus;
import static org.junit.jupiter.api.Assertions.assertEquals;

import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.jsontree.JsonResponse;
import org.hisp.dhis.web.HttpStatus;
import org.hisp.dhis.webapi.DhisControllerIntegrationTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@Slf4j
class CrudControllerIntegrationTest extends DhisControllerIntegrationTest {

@Test
@DisplayName("Should not apply token filter for UID if value has length < 4")
void testIdentifiableTokenFilterLength() {
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits/",
"{'name':'My Unit 1', 'shortName':'OU1', 'openingDate': '2020-01-01'}"));
String ou2 =
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits/",
"{'name':'My Unit 2', 'shortName':'OU2', 'openingDate': '2020-01-01'}"));

JsonResponse response =
GET("/organisationUnits?filter=identifiable:token:" + ou2.substring(0, 3)).content();
assertEquals(0, response.getArray("organisationUnits").size());

response = GET("/organisationUnits?filter=identifiable:token:" + ou2.substring(0, 4)).content();
assertEquals(1, response.getArray("organisationUnits").size());
assertEquals(ou2, response.getArray("organisationUnits").getObject(0).getString("id").string());
}
}