Skip to content

Commit

Permalink
Improved: Rename 2 UtilValidate class methods for clarity (OFBIZ-13160)
Browse files Browse the repository at this point in the history
Daniel Watford proposed to rename and better describe the 2 UtilValidate class
methods: isUrl and urlInString.
I respectively suggested isUrlInString and
isUrlInStringAndDoesNotStartByComponentProtocol.

Daniel also provided an UtilValidateTests class.

Thanks: Daniel
  • Loading branch information
JacquesLeRoux committed Oct 24, 2024
1 parent 3eb3fc9 commit efb43da
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static Class<?> getScriptClassFromLocation(String location) throws Genera
Class<?> scriptClass = PARSED_SCRIPTS.get(location);
if (scriptClass == null) {
URL scriptUrl = FlexibleLocation.resolveLocation(location);
if (scriptUrl == null || UtilValidate.urlInString(scriptUrl.toString())) {
if (scriptUrl == null || UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(scriptUrl.toString())) {
throw new GeneralException("Script not found at location [" + location + "]");
}
scriptClass = parseClass(scriptUrl.openStream(), location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static CompiledScript compileScriptFile(String filePath) throws ScriptEx
try {
Compilable compilableEngine = (Compilable) engine;
URL scriptUrl = FlexibleLocation.resolveLocation(filePath);
if (scriptUrl == null || UtilValidate.urlInString(scriptUrl.toString())) {
if (scriptUrl == null || UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(scriptUrl.toString())) {
throw new ScriptException("Script not found at location [" + filePath + "]");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(scriptUrl.openStream(), StandardCharsets.UTF_8));
Expand Down Expand Up @@ -357,7 +357,7 @@ public static Object executeScript(String filePath, String functionName, ScriptC
}
engine.setContext(scriptContext);
URL scriptUrl = FlexibleLocation.resolveLocation(filePath);
if (scriptUrl == null || UtilValidate.urlInString(scriptUrl.toString())) {
if (scriptUrl == null || UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(scriptUrl.toString())) {
throw new ScriptException("Script not found at location [" + filePath + "]");
}
try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public static Map<String, Object> canonicalizeParameterMap(Map<String, Object> p
// if the string contains only an URL beginning by http or ftp => no change to keep special chars
if (UtilValidate.isValidUrl(s) && (s.indexOf("://") == 4 || s.indexOf("://") == 3)) {
params = params + s + " ";
} else if (UtilValidate.isUrl(s) && !s.isEmpty()) {
} else if (UtilValidate.isUrlInString(s) && !s.isEmpty()) {
// if the string contains not only an URL => concatenate possible canonicalized before and after, w/o changing the URL
String url = extractUrls(s).get(0); // There should be only 1 URL in a block, makes no sense else
int start = s.indexOf(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,23 +621,23 @@ public static boolean isEmailList(String s) {
}

/**
* isUrl returns true if the string contains ://
* isUrlInString returns true if the string is empty or contains ://
* @param s String to validate Note: this does not handle "component://" specific to OFBiz
* @return true if s contains ://
* @return true if s is empty or contains ://
*/
public static boolean isUrl(String s) {
public static boolean isUrlInString(String s) {
if (isEmpty(s)) {
return DEFAULT_EMPTY_OK;
}
return s.indexOf("://") != -1;
}

/**
* urlInString returns true if the string contains :// and does not start with "component://"
* isUrlInStringAndDoesNotStartByComponentProtocol returns true if the string is non-empty, contains :// but does not start with "component://"
* @param s String to validate
* @return true if s contains :// and does not start with "component://"
* @return true if s is non-empty, contains :// and does not start with "component://"
*/
public static boolean urlInString(String s) {
public static boolean isUrlInStringAndDoesNotStartByComponentProtocol(String s) {
if (isEmpty(s) || s.startsWith("component://")) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.base.util;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class UtilValidateTests {

@Test
public void testUrlValidations() throws Exception {
assertFalse(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(""));
assertFalse(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol("non-url-string"));
assertFalse(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol("component://foo/bar"));
assertTrue(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(" component://foo/bar"));
assertTrue(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol("http://foo/bar"));
assertTrue(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol("https://foo/bar"));
assertFalse(UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol("component://foo/bar?param=http://moo/far"));

assertTrue(UtilValidate.isUrlInString(""));
assertFalse(UtilValidate.isUrlInString("non-url-string"));
assertTrue(UtilValidate.isUrlInString("component://foo/bar"));
assertTrue(UtilValidate.isUrlInString(" component://foo/bar"));
assertTrue(UtilValidate.isUrlInString("http://foo/bar"));
assertTrue(UtilValidate.isUrlInString("https://foo/bar"));
assertTrue(UtilValidate.isUrlInString("component://foo/bar?param=http://moo/far"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterCha
if (queryString != null) {
queryString = URLDecoder.decode(queryString, "UTF-8");
// wt=javabin allows Solr tests, see https://cwiki.apache.org/confluence/display/solr/javabin
if (UtilValidate.isUrl(queryString)
if (UtilValidate.isUrlInString(queryString)
|| !SecuredUpload.isValidText(queryString, Collections.emptyList())
&& !(queryString.contains("JavaScriptEnabled=Y")
|| queryString.contains("wt=javabin"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static Map<String, Object> entityImport(DispatchContext dctx, Map<String,
// #############################
// FM Template
// #############################
if (UtilValidate.urlInString(fulltext)
if (UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(fulltext)
&& !"true".equals(EntityUtilProperties.getPropertyValue("security", "security.datafile.loadurls.enable", "false", delegator))) {
Debug.logError("For security reason HTTP URLs are not accepted, see OFBIZ-12304", MODULE);
Debug.logInfo("Rather load your data from a file or set SystemProperty security.datafile.loadurls.enable = true", MODULE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static Map<String, ModelScreen> getScreensFromLocation(String resourceNam
long startTime = System.currentTimeMillis();
URL screenFileUrl = null;
screenFileUrl = FlexibleLocation.resolveLocation(resourceName);
if (screenFileUrl == null || UtilValidate.urlInString(screenFileUrl.toString())) {
if (screenFileUrl == null || UtilValidate.isUrlInStringAndDoesNotStartByComponentProtocol(screenFileUrl.toString())) {
throw new IllegalArgumentException("Could not resolve location to URL: " + resourceName);
}
Document screenFileDoc = UtilXml.readXmlDocument(screenFileUrl, true, true);
Expand Down

0 comments on commit efb43da

Please sign in to comment.