diff --git a/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java b/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
index fc6c46933..97cde97a2 100644
--- a/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
+++ b/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
@@ -37,6 +37,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
+import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Properties;
@@ -61,9 +62,7 @@ public OMNode lookup(String key) {
log.debug("==> Repository fetch of resource with key : " + key);
}
- URL url = SynapseConfigUtils.getURLFromPath(root + key, properties.get(
- SynapseConstants.SYNAPSE_HOME) != null ?
- properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
+ URL url = resolveWithinRoot(key);
if (url == null) {
return null;
}
@@ -147,9 +146,7 @@ public RegistryEntry getRegistryEntry(String key) {
if (log.isDebugEnabled()) {
log.debug("Perform RegistryEntry lookup for key : " + key);
}
- URL url = SynapseConfigUtils.getURLFromPath(root + key, properties.get(
- SynapseConstants.SYNAPSE_HOME) != null ?
- properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
+ URL url = resolveWithinRoot(key);
if (url == null) {
return null;
}
@@ -234,9 +231,7 @@ public RegistryEntry[] getChildren(RegistryEntry entry) {
entryImpl.setKey("");
entry = entryImpl;
}
- url = SynapseConfigUtils.getURLFromPath(root + entry.getKey(), properties.get(
- SynapseConstants.SYNAPSE_HOME) != null ?
- properties.get(SynapseConstants.SYNAPSE_HOME).toString() : "");
+ url = resolveWithinRoot(entry.getKey());
if (url == null) {
return null;
}
@@ -323,4 +318,55 @@ private void handleException(String msg) {
throw new SynapseException(msg);
}
+ /**
+ * Resolve {@code key} against the configured {@link #root} and, for a {@code file} root,
+ * verify that the resolved location does not escape it. The check runs on the
+ * canonicalized, decoded target path rather than on the raw key string, since the key may
+ * arrive percent-encoded, backslash-separated, or otherwise disguised in a way a literal
+ * "../" string match would miss -- while the file: URL handler that will actually open the
+ * resource decodes and normalizes it anyway.
+ */
+ private URL resolveWithinRoot(String key) {
+ Object synapseHomeProp = properties.get(SynapseConstants.SYNAPSE_HOME);
+ String synapseHome = synapseHomeProp != null ? synapseHomeProp.toString() : null;
+ URL url = SynapseConfigUtils.getURLFromPath(root + key, synapseHome);
+ if (url == null) {
+ return null;
+ }
+ if (root != null && !root.isEmpty() && "file".equals(url.getProtocol())) {
+ assertWithinRoot(url, key);
+ }
+ return url;
+ }
+
+ private void assertWithinRoot(URL url, String key) {
+ URL rootUrl;
+ try {
+ rootUrl = new URL(root);
+ } catch (MalformedURLException e) {
+ // root itself isn't a well-formed URL (an unusual configuration already tolerated
+ // by init()); there is nothing reliable to contain against here.
+ return;
+ }
+ try {
+ File target = new File(decodePath(url.getPath())).getCanonicalFile();
+ File rootDir = new File(decodePath(rootUrl.getPath())).getCanonicalFile();
+ String rootPath = rootDir.getPath();
+ String targetPath = target.getPath();
+ if (!targetPath.equals(rootPath) && !targetPath.startsWith(rootPath + File.separator)) {
+ throw new SynapseException(
+ "Registry key '" + key + "' resolves outside the configured registry root");
+ }
+ } catch (IOException e) {
+ throw new SynapseException(
+ "Unable to validate registry key '" + key + "' against the configured root", e);
+ }
+ }
+
+ private static String decodePath(String rawPath) throws UnsupportedEncodingException {
+ // URLDecoder is meant for application/x-www-form-urlencoded content, where '+' means
+ // space; escape any literal '+' first so a path legitimately containing one survives.
+ return URLDecoder.decode(rawPath.replace("+", "%2B"), "UTF-8");
+ }
+
}
diff --git a/modules/core/src/test/java/org/apache/synapse/registry/url/SimpleURLRegistryTest.java b/modules/core/src/test/java/org/apache/synapse/registry/url/SimpleURLRegistryTest.java
index b7e469978..d0c010f18 100644
--- a/modules/core/src/test/java/org/apache/synapse/registry/url/SimpleURLRegistryTest.java
+++ b/modules/core/src/test/java/org/apache/synapse/registry/url/SimpleURLRegistryTest.java
@@ -25,6 +25,7 @@
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
+import org.apache.synapse.SynapseException;
import org.apache.synapse.config.Entry;
import org.apache.synapse.registry.Registry;
import org.custommonkey.xmlunit.XMLAssert;
@@ -131,6 +132,62 @@ public void testXPathEvaluationOnRegistryResource() throws Exception {
assertEquals("ValueOne", ((OMText) node).getText());
}
+ public void testKeyResolutionStaysWithinConfiguredRoot() throws Exception {
+ String rootDir = "target/f013registry/root";
+ String outsideFile = "target/f013registry/outside.xml";
+ String insideFile = rootDir + "/inside.xml";
+ String outsideContent = "";
+ String insideContent = "";
+
+ new File(rootDir, "sub").mkdirs();
+ writeFile(outsideFile, outsideContent);
+ writeFile(insideFile, insideContent);
+
+ try {
+ Registry reg = new SimpleURLRegistry();
+ Properties props = new Properties();
+ props.put("root", "file:" + rootDir + "/");
+ reg.init(props);
+
+ // control: a plain in-root key keeps resolving.
+ OMNode inRoot = (OMNode) reg.lookup("inside.xml");
+ assertNotNull(inRoot);
+ XMLAssert.assertXMLEqual(insideContent, inRoot.toString());
+
+ // control: an in-root key with a harmless internal ".." that still nets to a
+ // location inside the root must keep resolving -- the fix must not over-block.
+ OMNode viaDotSegment = (OMNode) reg.lookup("sub/../inside.xml");
+ assertNotNull(viaDotSegment);
+ XMLAssert.assertXMLEqual(insideContent, viaDotSegment.toString());
+
+ // every one of these must be refused: none may ever return the out-of-root
+ // resource's content, whether by returning null or by throwing. A literal
+ // "../" string check is not enough -- the file: URL handler that actually
+ // opens the resource decodes percent-escapes and treats '\' as a separator on
+ // Windows, so the containment check must be enforced on the resolved,
+ // canonicalized target, not on the raw key string.
+ String[] escapes = {
+ "../outside.xml",
+ "..%2foutside.xml",
+ "..%2Foutside.xml",
+ "..%5coutside.xml",
+ ".%2e%2foutside.xml",
+ "%2e%2e%2foutside.xml",
+ };
+ for (String key : escapes) {
+ Object result;
+ try {
+ result = reg.lookup(key);
+ } catch (SynapseException refused) {
+ continue;
+ }
+ assertNull("key '" + key + "' must not resolve to the out-of-root resource", result);
+ }
+ } finally {
+ FileUtils.deleteQuietly(new File("target/f013registry"));
+ }
+ }
+
@Override
public void tearDown() throws Exception {
FileUtils.deleteQuietly(new File(FILE));
@@ -143,4 +200,11 @@ private void writeToFile(String content) throws Exception {
out.flush();
out.close();
}
+
+ private void writeFile(String path, String content) throws Exception {
+ BufferedWriter out = new BufferedWriter(new FileWriter(new File(path)));
+ out.write(content);
+ out.flush();
+ out.close();
+ }
}