Skip to content

Commit 367e906

Browse files
authored
Apply the allowed URL schemes to schema imports and WSDL import locat… (#3803)
* Apply the allowed URL schemes to schema imports and WSDL import locations * Review comments
1 parent 96973d0 commit 367e906

6 files changed

Lines changed: 215 additions & 24 deletions

File tree

‎core/src/main/java/org/apache/cxf/resource/URIResolver.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public static boolean isAllowedReference(String href, String base) {
528528
* whatever the base URI is (RFC 3986, section 5.2). A nested archive is written in the reference itself,
529529
* e.g. jar:http://host/a.jar!/a.xsd, and then both "jar" and "http" are checked.
530530
*/
531-
public static String getDisallowedScheme(String href, String base) {
531+
static String getDisallowedScheme(String href, String base) {
532532
if (href == null) {
533533
return null;
534534
}

‎core/src/main/java/org/apache/cxf/ws/addressing/EndpointReferenceUtils.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ public LSInput resolveResource(String type, String namespaceURI, String publicId
207207
if (systemId == null) {
208208
systemId = publicId;
209209
}
210+
if (systemId != null && !URIResolver.isAllowedReference(systemId, baseURI)) {
211+
// Return an empty input rather than null: for null, a SchemaFactory that does not
212+
// support ACCESS_EXTERNAL_SCHEMA (e.g. Apache Xerces) would fetch the schema itself
213+
LOG.warning("Not resolving Schema " + systemId + " as its URL scheme is not allowed");
214+
return createInput(systemId, new byte[0]);
215+
}
210216
if (systemId != null) {
211217
// Run inside doPrivileged so that sm.checkPermission() calls
212218
// inside the resolver chain (SecurityActions.fileExists) stop
@@ -600,8 +606,10 @@ private static Schema createSchema(ServiceInfo serviceInfo, Bus b) {
600606

601607

602608
} catch (Exception ex) {
603-
// Something not right with the schema from the wsdl.
604-
LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
609+
// Something not right with the schema from the wsdl. Callers set this null schema on
610+
// the readers and writers, so schema validation is not performed for the service.
611+
LOG.log(Level.SEVERE, "Could not build the schema for service " + serviceInfo.getName()
612+
+ ", so schema validation will NOT be performed for it", ex);
605613
for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
606614
String s = StaxUtils.toString(schemaInfo.getElement(), 4);
607615
LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s);

‎core/src/test/java/org/apache/cxf/ws/addressing/EndpointReferenceUtilsTest.java‎

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
public class EndpointReferenceUtilsTest {
5050
private static final String NAMESPACE = "urn:test:endpoint:reference:utils";
5151
private static final String MEMORY_SYSTEM_ID = "memory:/schema.xsd";
52+
private static final String ALLOWED_SCHEMES_PROPERTY = "org.apache.cxf.resource.uriresolver.allowedSchemes";
5253

5354
@Rule
5455
public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -125,6 +126,62 @@ public void testGetSchemaDoesNotResolveDtdFromImportedSchema() throws Exception
125126
}
126127
}
127128

129+
@Test
130+
public void testGetSchemaDoesNotFetchArchiveImportWithDisallowedInnerScheme() throws Exception {
131+
try (LocalHttpProbeServer probeServer = new LocalHttpProbeServer()) {
132+
// "jar" is an allowed scheme, but "ftp" is not, so the archive must not be fetched
133+
ServiceInfo serviceInfo = createServiceInfoWithImport(
134+
"jar:ftp://127.0.0.1:" + probeServer.getPort() + "/imported.jar!/imported.xsd");
135+
136+
EndpointReferenceUtils.getSchema(serviceInfo, null);
137+
138+
probeServer.awaitCompletion();
139+
assertFalse("Archive with a disallowed inner scheme should not be fetched",
140+
probeServer.wasConnected());
141+
}
142+
}
143+
144+
@Test
145+
public void testGetSchemaFetchesImportWithConfiguredAllowedScheme() throws Exception {
146+
// The fallback honours the configured allowed schemes, not a fixed rule
147+
String previous = System.getProperty(ALLOWED_SCHEMES_PROPERTY);
148+
System.setProperty(ALLOWED_SCHEMES_PROPERTY, "ftp");
149+
try (LocalHttpProbeServer probeServer = new LocalHttpProbeServer()) {
150+
ServiceInfo serviceInfo = createServiceInfoWithImport(
151+
"ftp://127.0.0.1:" + probeServer.getPort() + "/imported.xsd");
152+
153+
EndpointReferenceUtils.getSchema(serviceInfo, null);
154+
155+
probeServer.awaitCompletion();
156+
assertTrue("Import with a configured allowed scheme should be fetched", probeServer.wasConnected());
157+
} finally {
158+
if (previous == null) {
159+
System.clearProperty(ALLOWED_SCHEMES_PROPERTY);
160+
} else {
161+
System.setProperty(ALLOWED_SCHEMES_PROPERTY, previous);
162+
}
163+
}
164+
}
165+
166+
/**
167+
* Only the SchemaInfo element carries the import, so it is not loaded into the XmlSchemaCollection
168+
* and is resolved through the last-resort fallback of the SchemaLSResourceResolver.
169+
*/
170+
private ServiceInfo createServiceInfoWithImport(String schemaLocation) throws Exception {
171+
String schemaText =
172+
"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
173+
+ "targetNamespace='" + NAMESPACE + "' elementFormDefault='qualified'>"
174+
+ "<xsd:import namespace='urn:test:endpoint:reference:utils:imported' "
175+
+ "schemaLocation='" + schemaLocation + "'/>"
176+
+ "<xsd:element name='value' type='xsd:string'/>"
177+
+ "</xsd:schema>";
178+
Document doc = StaxUtils.read(new ByteArrayInputStream(schemaText.getBytes(StandardCharsets.UTF_8)));
179+
180+
ServiceInfo serviceInfo = createServiceInfo(MEMORY_SYSTEM_ID);
181+
serviceInfo.getSchemas().iterator().next().setElement(doc.getDocumentElement());
182+
return serviceInfo;
183+
}
184+
128185
private File writeSchemaWithExternalDtd(String name, String namespace, int port) throws Exception {
129186
String schemaText =
130187
"<?xml version='1.0'?>"
@@ -167,19 +224,25 @@ private static final class LocalHttpProbeServer implements AutoCloseable {
167224
private LocalHttpProbeServer() throws Exception {
168225
this.serverSocket = new ServerSocket(0);
169226
this.serverSocket.setSoTimeout(750);
170-
this.thread = new Thread(this::acceptOneConnection, "EndpointReferenceUtilsTest-HttpProbe");
227+
this.thread = new Thread(this::acceptConnections, "EndpointReferenceUtilsTest-HttpProbe");
171228
this.thread.setDaemon(true);
172229
this.thread.start();
173230
}
174231

175-
private void acceptOneConnection() {
176-
try (Socket socket = serverSocket.accept()) {
177-
connected.set(true);
178-
OutputStream out = socket.getOutputStream();
179-
out.write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n".getBytes(StandardCharsets.US_ASCII));
180-
out.flush();
181-
} catch (Exception ex) {
182-
// timeout/no connection is expected for this test
232+
// Answer every connection until none arrives within the accept timeout, so that a second
233+
// request cannot block the test
234+
private void acceptConnections() {
235+
while (true) {
236+
try (Socket socket = serverSocket.accept()) {
237+
connected.set(true);
238+
OutputStream out = socket.getOutputStream();
239+
out.write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"
240+
.getBytes(StandardCharsets.US_ASCII));
241+
out.flush();
242+
} catch (Exception ex) {
243+
// timeout/no connection is expected for this test
244+
return;
245+
}
183246
}
184247
}
185248

‎rt/wsdl/src/main/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocator.java‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.io.ByteArrayInputStream;
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
24-
import java.net.URI;
25-
import java.net.URISyntaxException;
2624

2725
import javax.wsdl.xml.WSDLLocator;
2826
import javax.xml.stream.XMLStreamException;
@@ -95,14 +93,12 @@ public String getBaseURI() {
9593
}
9694

9795
public InputSource getImportInputSource(String parentLocation, String importLocation) {
98-
// Do a check on the scheme to see if it's anything that could be a security risk
99-
try {
100-
URI url = new URI(importLocation);
101-
if (!(url.getScheme() == null || URIResolver.getAllowedSchemes().contains(url.getScheme()))) {
102-
throw new IllegalArgumentException("The " + url.getScheme() + " URI scheme is not allowed");
103-
}
104-
} catch (URISyntaxException e) {
105-
// Just continue here as we might still be able to load it from the filesystem
96+
// Do a check on the scheme to see if it's anything that could be a security risk. This also checks
97+
// the location of an archive (e.g. jar:ftp://...), and does not need the import location to parse
98+
// as a URI. A relative import is resolved against its parent, which has already been checked.
99+
if (importLocation != null && !URIResolver.isAllowedReference(importLocation, null)) {
100+
throw new IllegalArgumentException("Not resolving import " + importLocation
101+
+ " as its URL scheme is not allowed");
106102
}
107103

108104
InputSource src = parent.getImportInputSource(parentLocation, importLocation);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cxf.wsdl11;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
import javax.wsdl.xml.WSDLLocator;
28+
29+
import org.xml.sax.InputSource;
30+
31+
import org.junit.Test;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
35+
import static org.junit.Assert.fail;
36+
37+
public class AbstractWrapperWSDLLocatorTest {
38+
39+
@Test
40+
public void testImportWithDisallowedSchemeRejected() {
41+
assertRejected("ftp://localhost/a.xsd");
42+
}
43+
44+
@Test
45+
public void testImportOfArchiveWithDisallowedLocationRejected() {
46+
assertRejected("jar:ftp://localhost/a.jar!/a.xsd");
47+
}
48+
49+
@Test
50+
public void testImportWithDisallowedSchemeThatIsNotAValidUriRejected() {
51+
// Previously a location that did not parse as a URI skipped the check
52+
assertRejected("ftp://localhost/a b.xsd");
53+
}
54+
55+
@Test
56+
public void testAllowedImportsResolved() {
57+
RecordingLocator parent = new RecordingLocator();
58+
TestLocator locator = new TestLocator(parent);
59+
locator.getImportInputSource("http://localhost/a.wsdl", "http://localhost/b.xsd");
60+
locator.getImportInputSource("http://localhost/a.wsdl", "b.xsd");
61+
locator.getImportInputSource("bundle://1.0:1/a.wsdl", "b.xsd");
62+
locator.getImportInputSource("file:/tmp/a.wsdl", "jar:file:/tmp/a.jar!/b.xsd");
63+
assertEquals(4, parent.imports.size());
64+
}
65+
66+
private static void assertRejected(String importLocation) {
67+
RecordingLocator parent = new RecordingLocator();
68+
TestLocator locator = new TestLocator(parent);
69+
try {
70+
locator.getImportInputSource("http://localhost/a.wsdl", importLocation);
71+
fail("Expected the import of " + importLocation + " to be rejected");
72+
} catch (IllegalArgumentException ex) {
73+
assertEquals("Not resolving import " + importLocation + " as its URL scheme is not allowed",
74+
ex.getMessage());
75+
}
76+
assertTrue("Rejected import should not reach the parent locator", parent.imports.isEmpty());
77+
}
78+
79+
private static final class TestLocator extends AbstractWrapperWSDLLocator {
80+
TestLocator(WSDLLocator parent) {
81+
super("http://localhost/a.wsdl", parent);
82+
}
83+
84+
@Override
85+
public InputSource getInputSource() {
86+
return null;
87+
}
88+
89+
@Override
90+
public InputSource getInputSource(String parentLocation, String importLocation) {
91+
return null;
92+
}
93+
}
94+
95+
private static final class RecordingLocator implements WSDLLocator {
96+
private final List<String> imports = new ArrayList<>();
97+
98+
public InputSource getBaseInputSource() {
99+
return null;
100+
}
101+
102+
public InputSource getImportInputSource(String parentLocation, String importLocation) {
103+
imports.add(importLocation);
104+
InputSource source = new InputSource(
105+
new ByteArrayInputStream("<a/>".getBytes(StandardCharsets.UTF_8)));
106+
source.setSystemId(importLocation);
107+
return source;
108+
}
109+
110+
public String getBaseURI() {
111+
return null;
112+
}
113+
114+
public String getLatestImportURI() {
115+
return null;
116+
}
117+
118+
public void close() {
119+
// nothing to close
120+
}
121+
}
122+
}

‎tools/validator/src/test/java/org/apache/cxf/tools/validator/WSDLValidationTest.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ public void testIllegalWSDLImportProtocol() throws Exception {
214214
getLocation("/validator_wsdl/ftp_import.wsdl")};
215215
WSDLValidator.main(args);
216216

217-
assertTrue(getStdErr().indexOf("The ftp URI scheme is not allowed") != -1);
217+
assertTrue(getStdErr().indexOf(
218+
"Not resolving import ftp://localhost:12345 as its URL scheme is not allowed") != -1);
218219
}
219220

220221
@Test
@@ -223,7 +224,8 @@ public void testIllegalXSDImportProtocol() throws Exception {
223224
getLocation("/validator_wsdl/ftp_import2.wsdl")};
224225
WSDLValidator.main(args);
225226

226-
assertTrue(getStdErr().indexOf("The ftp URI scheme is not allowed") != -1);
227+
assertTrue(getStdErr().indexOf(
228+
"Not resolving import ftp://localhost:12345 as its URL scheme is not allowed") != -1);
227229
}
228230

229231
@Override

0 commit comments

Comments
 (0)