|
49 | 49 | public class EndpointReferenceUtilsTest { |
50 | 50 | private static final String NAMESPACE = "urn:test:endpoint:reference:utils"; |
51 | 51 | private static final String MEMORY_SYSTEM_ID = "memory:/schema.xsd"; |
| 52 | + private static final String ALLOWED_SCHEMES_PROPERTY = "org.apache.cxf.resource.uriresolver.allowedSchemes"; |
52 | 53 |
|
53 | 54 | @Rule |
54 | 55 | public TemporaryFolder tempFolder = new TemporaryFolder(); |
@@ -125,6 +126,62 @@ public void testGetSchemaDoesNotResolveDtdFromImportedSchema() throws Exception |
125 | 126 | } |
126 | 127 | } |
127 | 128 |
|
| 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 | + |
128 | 185 | private File writeSchemaWithExternalDtd(String name, String namespace, int port) throws Exception { |
129 | 186 | String schemaText = |
130 | 187 | "<?xml version='1.0'?>" |
@@ -167,19 +224,25 @@ private static final class LocalHttpProbeServer implements AutoCloseable { |
167 | 224 | private LocalHttpProbeServer() throws Exception { |
168 | 225 | this.serverSocket = new ServerSocket(0); |
169 | 226 | this.serverSocket.setSoTimeout(750); |
170 | | - this.thread = new Thread(this::acceptOneConnection, "EndpointReferenceUtilsTest-HttpProbe"); |
| 227 | + this.thread = new Thread(this::acceptConnections, "EndpointReferenceUtilsTest-HttpProbe"); |
171 | 228 | this.thread.setDaemon(true); |
172 | 229 | this.thread.start(); |
173 | 230 | } |
174 | 231 |
|
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 | + } |
183 | 246 | } |
184 | 247 | } |
185 | 248 |
|
|
0 commit comments