This is an automated email from the ASF dual-hosted git repository.

ashishvijaywargiya pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 231b3cb3cd Improved: bind EntityFunction literal values as SQL 
parameters instead of inlining them (#1729)
231b3cb3cd is described below

commit 231b3cb3cd3aec32362b2f2c794f6222349f80af
Author: Ashish Vijaywargiya <[email protected]>
AuthorDate: Wed Aug 26 19:11:40 2026 +0530

    Improved: bind EntityFunction literal values as SQL parameters instead of 
inlining them (#1729)
    
    EntityFunction.addSqlValue built the SQL for a wrapped literal value
    (used by ignore-case comparisons such as UPPER(...)) by always passing a
    null ModelField, even when the caller had already identified the field
    via setModelField. This caused the value to be inlined into the
    generated SQL text instead of bound as a JDBC parameter, unlike the
    field-bound path used everywhere else in the same class.
    
    Use the ModelField that's already available instead of discarding it, so
    the value is bound the same way as any other condition value.
    
    Added coverage for performFind's ignore-case search and for
    createUserLogin's ignore-case duplicate check, both of which build this
    kind of condition, verifying the value is now compared literally.
    
    Thank you Krishna Uprit for your help in reviewing the fix.
---
 .../apache/ofbiz/common/test/UserLoginTests.groovy | 25 ++++++++
 .../apache/ofbiz/common/test/PerformFindTests.java | 25 ++++++++
 .../ofbiz/entity/condition/EntityFunction.java     |  2 +-
 .../entity/condition/EntityFunctionTests.java      | 71 ++++++++++++++++++++++
 4 files changed, 122 insertions(+), 1 deletion(-)

diff --git 
a/framework/common/src/test/groovy/org/apache/ofbiz/common/test/UserLoginTests.groovy
 
b/framework/common/src/test/groovy/org/apache/ofbiz/common/test/UserLoginTests.groovy
index 6d715a6f3f..e453fc3356 100644
--- 
a/framework/common/src/test/groovy/org/apache/ofbiz/common/test/UserLoginTests.groovy
+++ 
b/framework/common/src/test/groovy/org/apache/ofbiz/common/test/UserLoginTests.groovy
@@ -47,4 +47,29 @@ class UserLoginTests implements JupiterTestHelper {
         assert createdUserLogin.enabled == 'Y'
     }
 
+    /*
+     * createUserLogin's duplicate-userLoginId check compares userLoginId 
ignoring case. A
+     * userLoginId containing quotes and parentheses should be compared 
literally, just like any
+     * other value, and match only an existing UserLogin with that exact id - 
so creating a new,
+     * not-yet-registered userLoginId built from such characters should still 
succeed.
+     */
+    @Test
+    void testCreateUserLoginIgnoreCaseDuplicateCheckComparesValueLiterally() {
+        String userLoginId = "\\') OR (1=1) OR ('"
+
+        Map serviceCtx = [
+                userLoginId: userLoginId,
+                enabled: 'Y',
+                currentPassword: 'ofbiz',
+                currentPasswordVerify: 'ofbiz'
+        ]
+        Map serviceResult = dispatcher.runSync('createUserLogin', serviceCtx)
+        assert ServiceUtil.isSuccess(serviceResult): serviceResult.errorMessage
+
+        GenericValue createdUserLogin = from('UserLogin')
+                .where('userLoginId', userLoginId)
+                .queryOne()
+        assert createdUserLogin
+    }
+
 }
diff --git 
a/framework/common/src/test/java/org/apache/ofbiz/common/test/PerformFindTests.java
 
b/framework/common/src/test/java/org/apache/ofbiz/common/test/PerformFindTests.java
index 957c9bcec4..e046dce97b 100644
--- 
a/framework/common/src/test/java/org/apache/ofbiz/common/test/PerformFindTests.java
+++ 
b/framework/common/src/test/java/org/apache/ofbiz/common/test/PerformFindTests.java
@@ -131,6 +131,7 @@ public class PerformFindTests implements JupiterTestHelper {
         performFindGroupOrAnd();
         performFindDateFindAndIgnoreCase();
         performFindFilterByDateWithDedicateDateField();
+        performFindIgnoreCaseComparesValueLiterally();
     }
 
     private void performFindConditionFieldEquals() throws Exception {
@@ -352,4 +353,28 @@ public class PerformFindTests implements JupiterTestHelper 
{
         foundElements = getCompleteList(result);
         assertEquals(4, foundElements.size(), "performFind search with 
filterDate Y and specific date field name");
     }
+
+    /*
+     * An ignore-case search value should be compared literally, like any 
other search value: a
+     * value containing quotes and parentheses must only ever match a row 
whose field equals that
+     * exact string, never a broader set of rows.
+     */
+    private void performFindIgnoreCaseComparesValueLiterally() throws 
Exception {
+        GenericValue userLogin = getUserLogin("system");
+        prepareData();
+
+        LocalDispatcher dispatcher = getDispatcher();
+        String valueWithSpecialCharacters = "\\') OR (1=1) OR ('";
+        Map<String, Object> inputFields = UtilMisc.toMap(
+                "testingTypeId", "PERFOMFINDTEST",
+                "testingName", valueWithSpecialCharacters,
+                "testingName_ic", "Y");
+        Map<String, Object> performFindMap = UtilMisc.toMap("userLogin", 
userLogin, "entityName", "Testing", "inputFields", inputFields);
+        Map<String, Object> result = dispatcher.runSync("performFind", 
performFindMap);
+        assertTrue(ServiceUtil.isSuccess(result));
+        List<GenericValue> foundElements = getCompleteList(result);
+        assertTrue(foundElements.isEmpty(),
+                "performFind ignore-case search should only match rows whose 
testingName equals the search value exactly, found: "
+                        + foundElements);
+    }
 }
diff --git 
a/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java
 
b/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java
index 188d43fe8f..15eceaccc0 100644
--- 
a/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java
+++ 
b/framework/entity/src/main/java/org/apache/ofbiz/entity/condition/EntityFunction.java
@@ -222,7 +222,7 @@ public abstract class EntityFunction<T extends 
Comparable<?>> extends EntityCond
         if (nested != null) {
             nested.addSqlValue(sql, tableAliases, modelEntity, 
entityConditionParams, includeTableNamePrefix, datasourceinfo);
         } else {
-            EntityConditionUtils.addValue(sql, null, value, 
entityConditionParams);
+            EntityConditionUtils.addValue(sql, field, value, 
entityConditionParams);
         }
         sql.append(')');
     }
diff --git 
a/framework/entity/src/test/java/org/apache/ofbiz/entity/condition/EntityFunctionTests.java
 
b/framework/entity/src/test/java/org/apache/ofbiz/entity/condition/EntityFunctionTests.java
new file mode 100644
index 0000000000..2195a72065
--- /dev/null
+++ 
b/framework/entity/src/test/java/org/apache/ofbiz/entity/condition/EntityFunctionTests.java
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.apache.ofbiz.entity.condition;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.ofbiz.entity.model.ModelField;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Regression tests for the SQL generated by an {@link EntityFunction} (e.g. 
UPPER/LOWER/TRIM)
+ * that wraps a literal Java value rather than a nested field, as happens for 
ignore-case
+ * comparisons such as those built by FindServices.createSingleCondition and by
+ * LoginServices.createUserLogin's duplicate-userLoginId check.
+ *
+ * A caller that already knows the ModelField being compared against (i.e. it 
called
+ * setModelField before addSqlValue) must get that value bound as a JDBC 
parameter, exactly
+ * like the nested-field case does, rather than inlined as a SQL string 
literal.
+ */
+public class EntityFunctionTests {
+
+    @Test
+    public void literalValueWithKnownFieldIsBoundAsParameter() {
+        ModelField field = ModelField.create(null, "userLoginId", "id-vlong", 
true);
+        EntityFunction<String> upper = EntityFunction.upper("' OR (1=1) OR '");
+        upper.setModelField(field);
+
+        StringBuilder sql = new StringBuilder();
+        List<EntityConditionParam> params = new ArrayList<>();
+        upper.addSqlValue(sql, null, params, false, null);
+
+        assertEquals("UPPER(?)", sql.toString());
+        assertEquals(1, params.size());
+        assertEquals(field, params.get(0).getModelField());
+        assertEquals("' OR (1=1) OR '", params.get(0).getFieldValue());
+    }
+
+    @Test
+    public void literalValueWithoutKnownFieldIsStillInlinedLikeBefore() {
+        // Preserve existing behavior when no caller ever identifies a 
ModelField for this
+        // EntityFunction: it still inlines, same as prior to the fix.
+        EntityFunction<String> upper = EntityFunction.upper("O'BRIEN");
+
+        StringBuilder sql = new StringBuilder();
+        List<EntityConditionParam> params = new ArrayList<>();
+        upper.addSqlValue(sql, null, params, false, null);
+
+        assertEquals("UPPER('O''BRIEN')", sql.toString());
+        assertFalse(sql.toString().contains("' OR"));
+    }
+}

Reply via email to