Skip to content

Commit

Permalink
LDEV-3349 avoid re-encoding url params with +
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Dec 2, 2024
1 parent e25ff47 commit aa9db36
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/commons/net/HTTPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private static String getProtocol(URL url) {
}

public static String escapeQSValue(String str, boolean encodeOnlyWhenNecessary) {
if (encodeOnlyWhenNecessary && !ReqRspUtil.needEncoding(str, false)) return str;
if (encodeOnlyWhenNecessary && !ReqRspUtil.needEncoding(str)) return str;
PageContextImpl pc = (PageContextImpl) ThreadLocalPageContext.get();
if (pc != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ public static void setProxy(String host, HttpClientBuilder builder, HttpUriReque
}

public static void addCookie(CookieStore cookieStore, String domain, String name, String value, String path, String charset) {
if (ReqRspUtil.needEncoding(name, false)) name = ReqRspUtil.encode(name, charset);
if (ReqRspUtil.needEncoding(value, false)) value = ReqRspUtil.encode(value, charset);
if (ReqRspUtil.needEncoding(name)) name = ReqRspUtil.encode(name, charset);
if (ReqRspUtil.needEncoding(value)) value = ReqRspUtil.encode(value, charset);
BasicClientCookie cookie = new BasicClientCookie(name, value);
if (!StringUtil.isEmpty(domain, true)) cookie.setDomain(domain);
if (!StringUtil.isEmpty(path, true)) cookie.setPath(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static String call(PageContext pc, String str, String encoding, boolean f

public static String invoke(String str, String encoding, boolean force) throws PageException {

if (!force && !ReqRspUtil.needEncoding(str, false)) return str;
if (!force && !ReqRspUtil.needEncoding(str)) return str;

try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static String call(PageContext pc, String str, String encoding, boolean f
}

public static String invoke(String str, String encoding, boolean force) throws PageException {
if (!force && !ReqRspUtil.needEncoding(str, false)) return str;
if (!force && !ReqRspUtil.needEncoding(str)) return str;

try {
String enc = lucee.commons.net.URLEncoder.encode(str, encoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private static void releasePageContext(PageContext pc, PageContext oldPC) {

private static String urlenc(String str, Charset charset) throws PageException {
try {
if (!ReqRspUtil.needEncoding(str, false)) return str;
if (!ReqRspUtil.needEncoding(str)) return str;
return URLEncoder.encode(str, charset);
}
catch (UnsupportedEncodingException uee) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/net/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Pair<String, String>[] cloneParameters(PageContext pc, HttpServlet
while (e.hasMoreElements()) {
name = (String) e.nextElement();
values = req instanceof HTTPServletRequestWrap ? ((HTTPServletRequestWrap) req).getParameterValues(pc, name) : req.getParameterValues(name);
if (values == null && ReqRspUtil.needEncoding(name, false)) values = req.getParameterValues(ReqRspUtil.encode(name, ReqRspUtil.getCharacterEncoding(null, req)));
if (values == null && ReqRspUtil.needEncoding(name)) values = req.getParameterValues(ReqRspUtil.encode(name, ReqRspUtil.getCharacterEncoding(null, req)));
if (values == null) {
if (pc != null && ReqRspUtil.identical(pc.getHttpServletRequest(), req)) {
values = HTTPServletRequestWrap.getParameterValues(pc, name);
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/lucee/runtime/net/http/ReqRspUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import lucee.commons.io.CharsetUtil;
import lucee.commons.io.IOUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.lang.ExceptionUtil;
import lucee.commons.lang.Pair;
import lucee.commons.lang.StringUtil;
Expand All @@ -74,6 +75,12 @@

public final class ReqRspUtil {

private static boolean urlEncodeAllowPlus;

static {
urlEncodeAllowPlus = Caster.toBooleanValue(SystemUtil.getSystemPropOrEnvVar("lucee.url.encodeAllowPlus", "true"), true);
}

private static Map<String, String> rootPathes = new ReferenceMap<String, String>(HARD, SOFT);

public static String get(Pair<String, Object>[] items, String name) {
Expand Down Expand Up @@ -292,6 +299,10 @@ public static String encode(String str, Charset charset) {
}
}

public static boolean needEncoding(String str) {
return needEncoding(str, urlEncodeAllowPlus);
}

public static boolean needEncoding(String str, boolean allowPlus) {
if (StringUtil.isEmpty(str, false)) return false;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/tag/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ private static String headerValue(String value) {
}

private static String urlenc(String str, String charset, boolean checkIfNeeded) throws UnsupportedEncodingException {
if (checkIfNeeded && !ReqRspUtil.needEncoding(str, false)) return str;
if (checkIfNeeded && !ReqRspUtil.needEncoding(str)) return str;
return URLEncoder.encode(str, CharsetUtil.toCharset(charset));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public String enc(String str, boolean encode) {
}

public String enc(String str) {
if (ReqRspUtil.needEncoding(str, false)) return enc(str, true);
if (ReqRspUtil.needEncoding(str)) return enc(str, true);
return enc(str, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static String[] getParameterValues(URLItem[][] itemsArr, String[] encodin
for (int x = 0; x < itemsArr.length; x++) {
items = itemsArr[x];
encoding = encodings[x];
if (ReqRspUtil.needEncoding(name, false)) encName = ReqRspUtil.encode(name, encoding);
if (ReqRspUtil.needEncoding(name)) encName = ReqRspUtil.encode(name, encoding);
else encName = null;
for (int i = 0; i < items.length; i++) {
n = items[i].getName();
Expand Down

0 comments on commit aa9db36

Please sign in to comment.