This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new c6640ac3195 Escape WSDL values written into generated Java source
(#3814)
c6640ac3195 is described below
commit c6640ac31956f1b4c63b708a0a7067ba967ea966
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Wed Dec 2 14:16:28 2026 +0000
Escape WSDL values written into generated Java source (#3814)
---
.../org/apache/cxf/common/util/PackageUtils.java | 5 +-
.../apache/cxf/common/util/PackageUtilsTest.java | 5 +
.../apache/cxf/tools/common/VelocityGenerator.java | 2 +
.../cxf/tools/common/model/JAnnotationElement.java | 5 +-
.../cxf/tools/common/model/JavaInterface.java | 11 +-
.../apache/cxf/tools/util/JavaSourceEscaper.java | 101 ++++++++
.../cxf/tools/common/model/JAnnotationTest.java | 11 +-
.../cxf/tools/util/JavaSourceEscaperTest.java | 60 +++++
.../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 4 +-
.../tools/wsdlto/frontend/jaxws/template/client.vm | 2 +-
.../tools/wsdlto/frontend/jaxws/template/fault.vm | 2 +-
.../tools/wsdlto/frontend/jaxws/template/impl.vm | 8 +-
.../tools/wsdlto/frontend/jaxws/template/server.vm | 2 +-
.../wsdlto/frontend/jaxws/template/service.vm | 30 +--
.../cxf/tools/wsdlto/jaxws/CodeGenBugTest.java | 37 +++
.../codegen_injection/hello_world.wsdl | 269 +++++++++++++++++++++
16 files changed, 520 insertions(+), 34 deletions(-)
diff --git a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
index a63e3ac66cf..7659fd9fc41 100644
--- a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
@@ -156,8 +156,9 @@ public final class PackageUtils {
char c = token.charAt(i);
if (i == 0 && !Character.isJavaIdentifierStart(c)) {
- // prefix an '_' if the first char is illegal
- newToken.append('_').append(c);
+ // prefix an '_' if the first char is illegal, and replace the
char
+ // itself too if it cannot appear in an identifier at all
+ newToken.append('_').append(Character.isJavaIdentifierPart(c)
? c : '_');
} else if (!Character.isJavaIdentifierPart(c)) {
// replace the char with an '_' if it is illegal
newToken.append('_');
diff --git
a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
index 0388dac114d..49aab604c1d 100644
--- a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
@@ -145,6 +145,11 @@ public class PackageUtilsTest {
PackageUtils.getPackageNameByNameSpaceURI("http://www.Case.cxf.apache.org"));
assertEquals("org.apache.cxf._case",
PackageUtils.getPackageNameByNameSpaceURI("https://www.Case.cxf.apache.org"));
+ assertEquals("org.apache.cxf._1_0",
+
PackageUtils.getPackageNameByNameSpaceURI("http://cxf.apache.org/1.0"));
+ // a leading character that is not valid anywhere in an identifier
must not survive
+ assertEquals("org.apache.cxf.__a.__b.__",
+
PackageUtils.getPackageNameByNameSpaceURI("http://cxf.apache.org/\"a/;b/{"));
}
@Test
diff --git
a/tools/common/src/main/java/org/apache/cxf/tools/common/VelocityGenerator.java
b/tools/common/src/main/java/org/apache/cxf/tools/common/VelocityGenerator.java
index afc1741520e..4f60ca74eff 100644
---
a/tools/common/src/main/java/org/apache/cxf/tools/common/VelocityGenerator.java
+++
b/tools/common/src/main/java/org/apache/cxf/tools/common/VelocityGenerator.java
@@ -34,6 +34,7 @@ import org.apache.cxf.common.i18n.Message;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.helpers.FileUtils;
import org.apache.cxf.tools.util.FileWriterUtil;
+import org.apache.cxf.tools.util.JavaSourceEscaper;
import org.apache.cxf.version.Version;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
@@ -105,6 +106,7 @@ public final class VelocityGenerator {
try (VelocityWriter writer = new VelocityWriter(outputs)) {
ctx.put("out", writer);
+ ctx.put("esc", JavaSourceEscaper.INSTANCE);
tmpl.merge(ctx, writer);
} catch (Exception e) {
Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS", LOG);
diff --git
a/tools/common/src/main/java/org/apache/cxf/tools/common/model/JAnnotationElement.java
b/tools/common/src/main/java/org/apache/cxf/tools/common/model/JAnnotationElement.java
index 4d9d64d469d..52f74bec880 100644
---
a/tools/common/src/main/java/org/apache/cxf/tools/common/model/JAnnotationElement.java
+++
b/tools/common/src/main/java/org/apache/cxf/tools/common/model/JAnnotationElement.java
@@ -24,6 +24,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import org.apache.cxf.tools.util.JavaSourceEscaper;
+
public final class JAnnotationElement {
private String name;
private Object value;
@@ -145,7 +147,8 @@ public final class JAnnotationElement {
private void getStringValue(final StringBuilder sb, final Object obj) {
sb.append('"');
- sb.append(obj);
+ // values typically come from the WSDL, so they must stay inside the
literal
+ sb.append(JavaSourceEscaper.escapeStringLiteral(obj.toString()));
sb.append('"');
}
diff --git
a/tools/common/src/main/java/org/apache/cxf/tools/common/model/JavaInterface.java
b/tools/common/src/main/java/org/apache/cxf/tools/common/model/JavaInterface.java
index a0880d689eb..16a4829afd6 100644
---
a/tools/common/src/main/java/org/apache/cxf/tools/common/model/JavaInterface.java
+++
b/tools/common/src/main/java/org/apache/cxf/tools/common/model/JavaInterface.java
@@ -33,6 +33,7 @@ import org.w3c.dom.Element;
import jakarta.jws.soap.SOAPBinding;
import org.apache.cxf.tools.common.ToolException;
+import org.apache.cxf.tools.util.JavaSourceEscaper;
public class JavaInterface implements JavaAnnotatable {
@@ -103,14 +104,12 @@ public class JavaInterface implements JavaAnnotatable {
/**
* Make sure documentation text taken from a WSDL or WADL cannot terminate
- * the enclosing comment block. Unicode escapes are neutralized as well
since
- * the compiler translates them before lexing.
+ * the enclosing comment block.
+ *
+ * @see JavaSourceEscaper#escapeJavaDocContent(String)
*/
public static String escapeJavaDocContent(String s) {
- if (s == null || s.indexOf("\\u") == -1 && s.indexOf("*/") == -1) {
- return s;
- }
- return s.replace("\\u", "\u").replace("*/", "*/");
+ return JavaSourceEscaper.escapeJavaDocContent(s);
}
public void setWebServiceName(String wsn) {
diff --git
a/tools/common/src/main/java/org/apache/cxf/tools/util/JavaSourceEscaper.java
b/tools/common/src/main/java/org/apache/cxf/tools/util/JavaSourceEscaper.java
new file mode 100644
index 00000000000..0e3599d02e0
--- /dev/null
+++
b/tools/common/src/main/java/org/apache/cxf/tools/util/JavaSourceEscaper.java
@@ -0,0 +1,101 @@
+/**
+ * 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.cxf.tools.util;
+
+/**
+ * Escaping for values taken from a WSDL, WADL or other contract and written
+ * into generated Java source. Such values must never be able to change the
+ * structure of the generated code.
+ * <p>
+ * The shared {@link #INSTANCE} is available to every Velocity template as
+ * {@code $esc}, e.g. {@code new QName("$esc.literal($service.Namespace)",
...)}.
+ */
+public final class JavaSourceEscaper {
+ public static final JavaSourceEscaper INSTANCE = new JavaSourceEscaper();
+
+ private JavaSourceEscaper() {
+ }
+
+ /**
+ * Make sure documentation text cannot terminate the enclosing comment
block.
+ * Unicode escapes are neutralized as well since the compiler translates
them
+ * before lexing.
+ */
+ public static String escapeJavaDocContent(String s) {
+ if (s == null || s.indexOf("\\u") == -1 && s.indexOf("*/") == -1) {
+ return s;
+ }
+ return s.replace("\\u", "\u").replace("*/", "*/");
+ }
+
+ /**
+ * Escape a value so that it can be embedded between double quotes in a
Java
+ * string literal. Backslashes are escaped first, so a backslash-u
sequence in
+ * the input can no longer form a unicode escape (the compiler translates
those
+ * before lexing). Line terminators and other control characters, which
are not
+ * allowed or not visible inside a literal, become escape sequences.
+ */
+ public static String escapeStringLiteral(String s) {
+ if (s == null) {
+ return null;
+ }
+ StringBuilder sb = null;
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ final String replacement;
+ switch (c) {
+ case '\\':
+ replacement = "\\\\";
+ break;
+ case '"':
+ replacement = "\\\"";
+ break;
+ case '\n':
+ replacement = "\\n";
+ break;
+ case '\r':
+ replacement = "\\r";
+ break;
+ case '\t':
+ replacement = "\\t";
+ break;
+ default:
+ replacement = c < 0x20 || c == 0x7f ? String.format("\\%03o",
(int)c) : null;
+ }
+ if (replacement != null) {
+ if (sb == null) {
+ sb = new StringBuilder(s.length() + 16).append(s, 0, i);
+ }
+ sb.append(replacement);
+ } else if (sb != null) {
+ sb.append(c);
+ }
+ }
+ return sb == null ? s : sb.toString();
+ }
+
+ /**
+ * Template helper: {@code "$esc.literal($value)"}. A null value renders
as an
+ * empty string.
+ */
+ public String literal(Object value) {
+ return value == null ? "" : escapeStringLiteral(value.toString());
+ }
+}
diff --git
a/tools/common/src/test/java/org/apache/cxf/tools/common/model/JAnnotationTest.java
b/tools/common/src/test/java/org/apache/cxf/tools/common/model/JAnnotationTest.java
index eb41d0f4c32..0811405ac8a 100644
---
a/tools/common/src/test/java/org/apache/cxf/tools/common/model/JAnnotationTest.java
+++
b/tools/common/src/test/java/org/apache/cxf/tools/common/model/JAnnotationTest.java
@@ -107,4 +107,13 @@ public class JAnnotationTest {
annotation.addElement(new JAnnotationElement("mode", Mode.OUT));
assertEquals("@WebParam(header = false, mode = WebParam.Mode.OUT)",
annotation.toString());
}
-}
\ No newline at end of file
+
+ @Test
+ public void testStringValueCannotBreakOutOfLiteral() {
+ JAnnotation annotation = new JAnnotation(WebService.class);
+ annotation.addElement(new JAnnotationElement("targetNamespace",
"http://x/\") int PWNED = 1; //"));
+ annotation.addElement(new JAnnotationElement("name", "a\\u0022b"));
+ assertEquals("@WebService(targetNamespace = \"http://x/\\\") int PWNED
= 1; //\", "
+ + "name = \"a\\\\u0022b\")", annotation.toString());
+ }
+}
diff --git
a/tools/common/src/test/java/org/apache/cxf/tools/util/JavaSourceEscaperTest.java
b/tools/common/src/test/java/org/apache/cxf/tools/util/JavaSourceEscaperTest.java
new file mode 100644
index 00000000000..454798b4dc4
--- /dev/null
+++
b/tools/common/src/test/java/org/apache/cxf/tools/util/JavaSourceEscaperTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.cxf.tools.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+public class JavaSourceEscaperTest {
+
+ @Test
+ public void testStringLiteralUnchanged() {
+ assertNull(JavaSourceEscaper.escapeStringLiteral(null));
+ String url =
"http://localhost:9000/SoapContext/SoapPort?a=1&b='c'#\u00e9";
+ assertSame(url, JavaSourceEscaper.escapeStringLiteral(url));
+ }
+
+ @Test
+ public void testStringLiteralEscaped() {
+ assertEquals("http://x\\\"; Runtime.getRuntime(); String z=\\\"",
+ JavaSourceEscaper.escapeStringLiteral("http://x\";
Runtime.getRuntime(); String z=\""));
+ // a backslash-u sequence must not survive as a unicode escape
+ assertEquals("http://x\\\\u0022",
JavaSourceEscaper.escapeStringLiteral("http://x\\u0022"));
+ assertEquals("a\\\\\\nb\\r\\tc\\000d\\177",
+
JavaSourceEscaper.escapeStringLiteral("a\\\nb\r\tc\u0000d\u007f"));
+ }
+
+ @Test
+ public void testTemplateHelper() {
+ assertEquals("", JavaSourceEscaper.INSTANCE.literal(null));
+ assertEquals("a\\\"b", JavaSourceEscaper.INSTANCE.literal(new
StringBuilder("a\"b")));
+ }
+
+ @Test
+ public void testJavaDocContent() {
+ assertNull(JavaSourceEscaper.escapeJavaDocContent(null));
+ assertEquals("a */ b".replace("*/", "*/"),
JavaSourceEscaper.escapeJavaDocContent("a */ b"));
+ assertEquals("\u002a/",
JavaSourceEscaper.escapeJavaDocContent("\\u002a/"));
+ assertSame("plain", JavaSourceEscaper.escapeJavaDocContent("plain"));
+ }
+}
diff --git
a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
index e7a55a22827..572584bd060 100644
---
a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
+++
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
@@ -103,7 +103,7 @@ import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.jaxrs.utils.schemas.SchemaHandler;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.staxutils.StaxUtils;
-import org.apache.cxf.tools.common.model.JavaInterface;
+import org.apache.cxf.tools.util.JavaSourceEscaper;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.constants.Constants;
@@ -874,7 +874,7 @@ public class SourceGenerator {
private String getDocText(Element el) {
Element doc = DOMUtils.getFirstChildWithName(el, getWadlNamespace(),
"doc");
if (doc != null) {
- return
JavaInterface.escapeJavaDocContent(DOMUtils.getContent(doc));
+ return
JavaSourceEscaper.escapeJavaDocContent(DOMUtils.getContent(doc));
}
return null;
}
diff --git
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/client.vm
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/client.vm
index 64b580ea4ed..a997072f601 100644
---
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/client.vm
+++
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/client.vm
@@ -54,7 +54,7 @@ public final class ${clientClassName} {
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
#end
- private static final QName SERVICE_NAME = new QName("$service.Namespace",
"$service.ServiceName");
+ private static final QName SERVICE_NAME = new
QName("$esc.literal($service.Namespace)", "$esc.literal($service.ServiceName)");
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
diff --git
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/fault.vm
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/fault.vm
index c989eef5e0f..1aaaf1b2929 100644
---
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/fault.vm
+++
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/fault.vm
@@ -39,7 +39,7 @@ $expClass.classJavaDoc
*/
#foreach ($field in $expClass.Fields)
-@WebFault(name = "$field.Name", targetNamespace = "$field.TargetNamespace")
+@WebFault(name = "$esc.literal($field.Name)", targetNamespace =
"$esc.literal($field.TargetNamespace)")
#end
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end, comments =
"$fullversion")
diff --git
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/impl.vm
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/impl.vm
index e28e6fa88f4..24a7aa5cb57 100644
---
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/impl.vm
+++
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/impl.vm
@@ -41,16 +41,16 @@ import ${import};
@jakarta.jws.WebService(
#if ($service.ServiceName != "")
- serviceName = "$service.ServiceName",
+ serviceName = "$esc.literal($service.ServiceName)",
#end
#if ($port != "")
- portName = "$port",
+ portName = "$esc.literal($port)",
#end
#if ($service.Namespace != "")
- targetNamespace = "$service.Namespace",
+ targetNamespace = "$esc.literal($service.Namespace)",
#end
#if ($intf.Location != "")
- wsdlLocation = "$intf.Location",
+ wsdlLocation = "$esc.literal($intf.Location)",
#end
endpointInterface = "$intf.PackageName.$intf.Name")
diff --git
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/server.vm
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/server.vm
index 3be2581bf4f..3b678b5ed42 100644
---
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/server.vm
+++
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/server.vm
@@ -42,7 +42,7 @@ public class ${serverClassName}{
protected ${serverClassName}() throws java.lang.Exception {
System.out.println("Starting Server");
Object implementor = new ${impl}();
- String address = "${address}";
+ String address = "$esc.literal($address)";
Endpoint.publish(address, implementor);
}
diff --git
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/service.vm
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/service.vm
index 8b7149b213d..88c267df5c9 100644
---
a/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/service.vm
+++
b/tools/wsdlto/frontend/jaxws/src/main/java/org/apache/cxf/tools/wsdlto/frontend/jaxws/template/service.vm
@@ -53,11 +53,11 @@ $service.classJavaDoc
* Generated source version: $version
*
*/
-@WebServiceClient(name = "${service.ServiceName}",
+@WebServiceClient(name = "$esc.literal($service.ServiceName)",
#if ($wsdlLocation != "")
- wsdlLocation = "$wsdlLocation",
+ wsdlLocation = "$esc.literal($wsdlLocation)",
#end
- targetNamespace = "$service.Namespace")
+ targetNamespace = "$esc.literal($service.Namespace)")
#foreach ($annotation in $service.Annotations)
$annotation
#end
@@ -74,12 +74,12 @@ public class ${service.Name} extends ${serviceSuperclass} {
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
#end
- public static final QName SERVICE = new QName("$service.Namespace",
"$service.ServiceName");
+ public static final QName SERVICE = new
QName("$esc.literal($service.Namespace)", "$esc.literal($service.ServiceName)");
#foreach ($port in ${service.Ports})
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
#end
- public static final QName $port.Name = new QName("$service.Namespace",
"$port.PortName");
+ public static final QName $port.Name = new
QName("$esc.literal($service.Namespace)", "$esc.literal($port.PortName)");
#end
static {
#if ($wsdlLocation == "")
@@ -87,10 +87,10 @@ public class ${service.Name} extends ${serviceSuperclass} {
#elseif ($wsdlLocation == "null")
WSDL_LOCATION = null;
#elseif ($wsdlLocation.startsWith("classpath:"))
- URL url =
${service.Name}.class.getClassLoader().getResource("$wsdlLocation.substring(10)");
+ URL url =
${service.Name}.class.getClassLoader().getResource("$esc.literal($wsdlLocation.substring(10))");
#if ($wsdlLocation.startsWith("classpath:/"))
if (url == null) {
- url =
${service.Name}.class.getClassLoader().getResource("$wsdlLocation.substring(11)");
+ url =
${service.Name}.class.getClassLoader().getResource("$esc.literal($wsdlLocation.substring(11))");
}
#end
if (url == null) {
@@ -100,13 +100,13 @@ public class ${service.Name} extends ${serviceSuperclass}
{
java.util.logging.Logger.getLogger(${service.Name}.class.getName())
#end
.log(java.util.logging.Level.INFO,
- "Can not initialize the default wsdl from {0}",
"$wsdlLocation");
+ "Can not initialize the default wsdl from {0}",
"$esc.literal($wsdlLocation)");
}
WSDL_LOCATION = url;
#elseif ($useGetResource)
- URL url = ${service.Name}.class.getResource("$wsdlLocation");
+ URL url =
${service.Name}.class.getResource("$esc.literal($wsdlLocation)");
if (url == null) {
- url =
${service.Name}.class.getClassLoader().getResource("$wsdlLocation");
+ url =
${service.Name}.class.getClassLoader().getResource("$esc.literal($wsdlLocation)");
}
if (url == null) {
#if ($serviceTarget == "cxf")
@@ -115,13 +115,13 @@ public class ${service.Name} extends ${serviceSuperclass}
{
java.util.logging.Logger.getLogger(${service.Name}.class.getName())
#end
.log(java.util.logging.Level.INFO,
- "Can not initialize the default wsdl from {0}",
"$wsdlLocation");
+ "Can not initialize the default wsdl from {0}",
"$esc.literal($wsdlLocation)");
}
WSDL_LOCATION = url;
#else
URL url = null;
try {
- url = URI.create("$wsdlLocation").toURL();
+ url = URI.create("$esc.literal($wsdlLocation)").toURL();
} catch (MalformedURLException e) {
#if ($serviceTarget == "cxf")
LogUtils.getL7dLogger(${service.Name}.class)
@@ -129,7 +129,7 @@ public class ${service.Name} extends ${serviceSuperclass} {
java.util.logging.Logger.getLogger(${service.Name}.class.getName())
#end
.log(java.util.logging.Level.INFO,
- "Can not initialize the default wsdl from {0}",
"$wsdlLocation");
+ "Can not initialize the default wsdl from {0}",
"$esc.literal($wsdlLocation)");
}
WSDL_LOCATION = url;
#end
@@ -234,7 +234,7 @@ $port.javaDoc
* @return
* returns ${port.InterfaceClass}
*/
- @WebEndpoint(name = "${port.PortName}")
+ @WebEndpoint(name = "$esc.literal($port.PortName)")
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
#end
@@ -252,7 +252,7 @@ $port.javaDoc
* @return
* returns ${port.InterfaceClass}
*/
- @WebEndpoint(name = "${port.PortName}")
+ @WebEndpoint(name = "$esc.literal($port.PortName)")
#if ($markGenerated == "true")
@Generated(value = "org.apache.cxf.tools.wsdlto.WSDLToJava"#if
($suppressGeneratedDate != "true"), date = "$currentdate"#end)
#end
diff --git
a/tools/wsdlto/test/src/test/java/org/apache/cxf/tools/wsdlto/jaxws/CodeGenBugTest.java
b/tools/wsdlto/test/src/test/java/org/apache/cxf/tools/wsdlto/jaxws/CodeGenBugTest.java
index 9e968bbc5c0..ffa006414be 100644
---
a/tools/wsdlto/test/src/test/java/org/apache/cxf/tools/wsdlto/jaxws/CodeGenBugTest.java
+++
b/tools/wsdlto/test/src/test/java/org/apache/cxf/tools/wsdlto/jaxws/CodeGenBugTest.java
@@ -28,6 +28,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
import javax.xml.namespace.QName;
@@ -487,6 +489,41 @@ public class CodeGenBugTest extends AbstractCodeGenTest {
assertNotNull(clz);
}
+ @Test
+ public void testWsdlValuesCannotBreakOutOfGeneratedSource() throws
Exception {
+ // the targetNamespace, service and port names, soapAction,
wsaw:Action and
+ // soap:address values in this WSDL all try to close a Java string
literal
+ env.put(ToolConstants.CFG_ALL, ToolConstants.CFG_ALL);
+ // -validate would reject the service and port names, but it is off by
default
+ env.remove(ToolConstants.CFG_VALIDATE_WSDL);
+ env.put(ToolConstants.CFG_WSDLURL,
getLocation("/wsdl2java_wsdl/codegen_injection/hello_world.wsdl"));
+ processor.setContext(env);
+ // fails with "Failed to compile generated code" if any value escapes
its literal
+ processor.execute();
+
+ StringBuilder sources = new StringBuilder();
+ try (Stream<Path> files = Files.walk(output.toPath())) {
+ for (Path p : (Iterable<Path>)files.filter(f ->
f.toString().endsWith(".java"))::iterator) {
+ sources.append(new String(Files.readAllBytes(p),
StandardCharsets.UTF_8));
+ }
+ }
+ String all = sources.toString();
+ String payload = "\\\"; int PWNED = 1; String z = \\\"";
+ assertTrue(all.contains("String address = \"http://localhost:9000/x" +
payload + "\";"));
+ assertTrue(all.contains(
+ "String address = \"http://localhost:9100/\\\\u0022; int PWNED =
1; String z = \\\\u0022\";"));
+ assertTrue(all.contains("String address =
\"http://localhost:9101/a\\\\\\nb\";"));
+ assertTrue(all.contains("new
QName(\"http://cxf.apache.org/w2j/hello_world_soap_http/" + payload
+ + "\", \"SOAPService" + payload + "\")"));
+ assertTrue(all.contains("@WebEndpoint(name = \"SoapPort" + payload +
"\")"));
+ assertTrue(all.contains("@WebService(targetNamespace =
\"http://cxf.apache.org/w2j/hello_world_soap_http/"
+ + payload + "\", name = \"Greeter\")"));
+ assertTrue(all.contains("@WebMethod(action = \"urn:\\\\u0022) int
PWNED(); @Deprecated(\\\\u0022\")"));
+ assertTrue(all.contains("@Action(input = \"urn:" + payload + "\")"));
+ // no quote from the WSDL is left unescaped anywhere
+ assertFalse(Pattern.compile("[^\\\\]\"; int
PWNED").matcher(all).find());
+ }
+
// CXF-492
@Test
public void testDefaultNsMap() throws Exception {
diff --git
a/tools/wsdlto/test/src/test/resources/wsdl2java_wsdl/codegen_injection/hello_world.wsdl
b/tools/wsdlto/test/src/test/resources/wsdl2java_wsdl/codegen_injection/hello_world.wsdl
new file mode 100644
index 00000000000..839d6539ec0
--- /dev/null
+++
b/tools/wsdlto/test/src/test/resources/wsdl2java_wsdl/codegen_injection/hello_world.wsdl
@@ -0,0 +1,269 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://cxf.apache.org/w2j/hello_world_soap_http/"; int PWNED =
1; String z = ""
xmlns:x1="http://cxf.apache.org/w2j/hello_world_soap_http/types"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://cxf.apache.org/w2j/hello_world_soap_http/"; int
PWNED = 1; String z = "" name="HelloWorld">
+ <wsdl:types>
+ <schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:x1="http://cxf.apache.org/w2j/hello_world_soap_http/types"
targetNamespace="http://cxf.apache.org/w2j/hello_world_soap_http/types"
elementFormDefault="qualified">
+ <element name="sayHi">
+ <complexType/>
+ </element>
+ <element name="sayHiResponse">
+ <complexType>
+ <sequence>
+ <element name="responseType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="greetMe">
+ <complexType>
+ <sequence>
+ <element name="requestType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="greetMeResponse">
+ <complexType>
+ <sequence>
+ <element name="responseType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="greetMeSometime">
+ <complexType>
+ <sequence>
+ <element name="requestType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="greetMeSometimeResponse">
+ <complexType>
+ <sequence>
+ <element name="responseType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="greetMeOneWay">
+ <complexType>
+ <sequence>
+ <element name="requestType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="testDocLitFault">
+ <complexType>
+ <sequence>
+ <element name="faultType" type="string"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="testDocLitFaultResponse">
+ <complexType>
+ <sequence/>
+ </complexType>
+ </element>
+ <complexType name="ErrorCode">
+ <sequence>
+ <element name="minor" type="short"/>
+ <element name="major" type="short"/>
+ </sequence>
+ </complexType>
+ <element name="NoSuchCodeLit">
+ <complexType>
+ <sequence>
+ <element name="code" type="x1:ErrorCode"/>
+ </sequence>
+ </complexType>
+ </element>
+ <element name="BadRecordLit" type="string"/>
+ <complexType name="BadRecord">
+ <sequence>
+ <element name="reason" type="string"/>
+ <element name="code" type="short"/>
+ </sequence>
+ </complexType>
+ <complexType name="addNumbers">
+ <sequence>
+ <element name="arg0" type="int"/>
+ <element name="arg1" type="int"/>
+ </sequence>
+ </complexType>
+ <element name="addNumbers" type="x1:addNumbers"/>
+ <complexType name="addNumbersResponse">
+ <sequence>
+ <element name="return" type="int"/>
+ </sequence>
+ </complexType>
+ <element name="addNumbersResponse" type="x1:addNumbersResponse"/>
+ <element name="BareDocument" type="string"/>
+ <element name="BareDocumentResponse">
+ <complexType>
+ <sequence>
+ <element name="company" type="string"/>
+ </sequence>
+ <attribute name="id" type="int"/>
+ </complexType>
+ </element>
+ </schema>
+ </wsdl:types>
+ <wsdl:message name="sayHiRequest">
+ <wsdl:part name="in" element="x1:sayHi"/>
+ </wsdl:message>
+ <wsdl:message name="sayHiResponse">
+ <wsdl:part name="out" element="x1:sayHiResponse"/>
+ </wsdl:message>
+ <wsdl:message name="greetMeRequest">
+ <wsdl:part name="in" element="x1:greetMe"/>
+ </wsdl:message>
+ <wsdl:message name="greetMeResponse">
+ <wsdl:part name="out" element="x1:greetMeResponse"/>
+ </wsdl:message>
+ <wsdl:message name="greetMeSometimeRequest">
+ <wsdl:part name="in" element="x1:greetMeSometime"/>
+ </wsdl:message>
+ <wsdl:message name="greetMeSometimeResponse">
+ <wsdl:part name="out" element="x1:greetMeSometimeResponse"/>
+ </wsdl:message>
+ <wsdl:message name="greetMeOneWayRequest">
+ <wsdl:part name="in" element="x1:greetMeOneWay"/>
+ </wsdl:message>
+ <wsdl:message name="testDocLitFaultRequest">
+ <wsdl:part name="in" element="x1:testDocLitFault"/>
+ </wsdl:message>
+ <wsdl:message name="testDocLitFaultResponse">
+ <wsdl:part name="out" element="x1:testDocLitFaultResponse"/>
+ </wsdl:message>
+ <wsdl:message name="NoSuchCodeLitFault">
+ <wsdl:part name="NoSuchCodeLit" element="x1:NoSuchCodeLit"/>
+ </wsdl:message>
+ <wsdl:message name="BadRecordLitFault">
+ <wsdl:part name="BadRecordLit" element="x1:BadRecordLit"/>
+ </wsdl:message>
+ <wsdl:message name="testDocLitBareRequest">
+ <wsdl:part name="in" element="x1:BareDocument"/>
+ </wsdl:message>
+ <wsdl:message name="testDocLitBareResponse">
+ <wsdl:part name="out" element="x1:BareDocumentResponse"/>
+ </wsdl:message>
+ <wsdl:portType name="Greeter">
+ <wsdl:documentation>porttype documentation</wsdl:documentation>
+ <wsdl:operation name="sayHi">
+ <wsdl:documentation>porttype op documentation</wsdl:documentation>
+ <wsdl:input name="sayHiRequest"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" wsaw:Action="urn:";
int PWNED = 1; String z = "" message="tns:sayHiRequest"/>
+ <wsdl:output name="sayHiResponse" message="tns:sayHiResponse"/>
+ </wsdl:operation>
+ <wsdl:operation name="greetMe">
+ <wsdl:input name="greetMeRequest" message="tns:greetMeRequest"/>
+ <wsdl:output name="greetMeResponse" message="tns:greetMeResponse"/>
+ </wsdl:operation>
+ <wsdl:operation name="greetMeSometime">
+ <wsdl:input name="greetMeSometimeRequest"
message="tns:greetMeSometimeRequest"/>
+ <wsdl:output name="greetMeSometimeResponse"
message="tns:greetMeSometimeResponse"/>
+ </wsdl:operation>
+ <wsdl:operation name="greetMeOneWay">
+ <wsdl:input name="greetMeOneWayRequest"
message="tns:greetMeOneWayRequest"/>
+ </wsdl:operation>
+ <wsdl:operation name="testDocLitFault">
+ <wsdl:input name="testDocLitFaultRequest"
message="tns:testDocLitFaultRequest"/>
+ <wsdl:output name="testDocLitFaultResponse"
message="tns:testDocLitFaultResponse"/>
+ <wsdl:fault name="NoSuchCodeLitFault"
message="tns:NoSuchCodeLitFault"/>
+ <wsdl:fault name="BadRecordLitFault"
message="tns:BadRecordLitFault"/>
+ </wsdl:operation>
+ <wsdl:operation name="testDocLitBare">
+ <wsdl:input name="testDocLitBareRequest"
message="tns:testDocLitBareRequest"/>
+ <wsdl:output name="testDocLitBareResponse"
message="tns:testDocLitBareResponse"/>
+ </wsdl:operation>
+ </wsdl:portType>
+ <wsdl:binding name="Greeter_SOAPBinding" type="tns:Greeter">
+ <soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
+ <wsdl:operation name="sayHi">
+ <soap:operation style="document"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output>
+ <soap:body use="literal"/>
+ </wsdl:output>
+ </wsdl:operation>
+ <wsdl:operation name="greetMe">
+ <soap:operation style="document"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output>
+ <soap:body use="literal"/>
+ </wsdl:output>
+ </wsdl:operation>
+ <wsdl:operation name="greetMeSometime">
+ <soap:operation style="document"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output>
+ <soap:body use="literal"/>
+ </wsdl:output>
+ </wsdl:operation>
+ <wsdl:operation name="greetMeOneWay">
+ <soap:operation style="document"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ </wsdl:operation>
+ <wsdl:operation name="testDocLitFault">
+ <soap:operation style="document"/>
+ <wsdl:input>
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output>
+ <soap:body use="literal"/>
+ </wsdl:output>
+ <wsdl:fault name="NoSuchCodeLitFault">
+ <soap:fault name="NoSuchCodeLitFault" use="literal"/>
+ </wsdl:fault>
+ <wsdl:fault name="BadRecordLitFault">
+ <soap:fault name="BadRecordLitFault" use="literal"/>
+ </wsdl:fault>
+ </wsdl:operation>
+ <wsdl:operation name="testDocLitBare">
+ <soap:operation style="document" soapAction="urn:\u0022) int
PWNED(); @Deprecated(\u0022"/>
+ <wsdl:input name="testDocLitBareRequest">
+ <soap:body use="literal"/>
+ </wsdl:input>
+ <wsdl:output name="testDocLitBareResponse">
+ <soap:body use="literal"/>
+ </wsdl:output>
+ </wsdl:operation>
+ </wsdl:binding>
+ <wsdl:service name="SOAPService"; int PWNED = 1; String z = "">
+ <wsdl:port name="SoapPort"; int PWNED = 1; String z = ""
binding="tns:Greeter_SOAPBinding">
+ <soap:address location="http://localhost:9000/x"; int PWNED =
1; String z = ""/>
+ <wswa:UsingAddressing
xmlns:wswa="http://www.w3.org/2005/02/addressing/wsdl"/>
+ </wsdl:port>
+ </wsdl:service>
+ <wsdl:service name="SOAPService_Test1">
+ <wsdl:documentation>service documentation</wsdl:documentation>
+ <wsdl:port name="SoapPort_Test1" binding="tns:Greeter_SOAPBinding">
+ <wsdl:documentation>port1 documentation</wsdl:documentation>
+ <soap:address location="http://localhost:9100/\u0022; int PWNED =
1; String z = \u0022"/>
+ </wsdl:port>
+ <wsdl:port name="SoapPort_Test2" binding="tns:Greeter_SOAPBinding">
+ <wsdl:documentation>port2 documentation</wsdl:documentation>
+ <soap:address location="http://localhost:9101/a\ b"/>
+ </wsdl:port>
+ </wsdl:service>
+</wsdl:definitions>