summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Jolly <olivier.jolly@pcedev.com>2006-03-25 20:31:08 +0000
committerOlivier Jolly <olivier.jolly@pcedev.com>2006-03-25 20:31:08 +0000
commit27cca741ee39476ed2858e8cbee2928f555d8179 (patch)
treec1fd3abcc13dae2262d9961c3d14200e9790cd1f
parent9b6f5082d584af8dbc1c45a977c595083aeccfed (diff)
downloadclasspath-27cca741ee39476ed2858e8cbee2928f555d8179.tar.gz
2006-03-25 Olivier Jolly <olivier.jolly@pcedev.com>
* java/net/URLClassLoader.java (FileURLLoader.getResource): Added test to validate all components of a resource path. (FileURLLoader.walkPathComponents): Helper which ensures that we are allowed to walk through every component of a resource path.
-rw-r--r--ChangeLog7
-rw-r--r--java/net/URLClassLoader.java70
2 files changed, 74 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index e84908b2d..d8016d45a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2006-03-25 Olivier Jolly <olivier.jolly@pcedev.com>
+
+ * java/net/URLClassLoader.java (FileURLLoader.getResource): Added test
+ to validate all components of a resource path.
+ (FileURLLoader.walkPathComponents): Helper which ensures that we are
+ allowed to walk through every component of a resource path.
+
2006-03-25 Michael Koch <konqueror@gmx.de>
* NEWS: Added item for CLDR 1.3 update.
diff --git a/java/net/URLClassLoader.java b/java/net/URLClassLoader.java
index ffa73dda6..677a2275a 100644
--- a/java/net/URLClassLoader.java
+++ b/java/net/URLClassLoader.java
@@ -538,9 +538,14 @@ public class URLClassLoader extends SecureClassLoader
{
try
{
- File file = new File(dir, name).getCanonicalFile();
- if (file.exists())
- return new FileResource(this, file);
+ // Make sure that all components in name are valid by walking through
+ // them
+ File file = walkPathComponents(name);
+
+ if (file == null)
+ return null;
+
+ return new FileResource(this, file);
}
catch (IOException e)
{
@@ -548,6 +553,65 @@ public class URLClassLoader extends SecureClassLoader
}
return null;
}
+
+ /**
+ * Walk all path tokens and check them for validity. At no moment, we are
+ * allowed to reach a directory located "above" the root directory, stored
+ * in "dir" property. We are also not allowed to enter a non existing
+ * directory or a non directory component (plain file, symbolic link, ...).
+ * An empty or null path is valid. Pathnames components are separated by
+ * <code>File.separatorChar</code>
+ *
+ * @param resourceFileName the name to be checked for validity.
+ * @return the canonical file pointed by the resourceFileName or null if the
+ * walking failed
+ * @throws IOException in case of issue when creating the canonical
+ * resulting file
+ * @see File#separatorChar
+ */
+ private File walkPathComponents(String resourceFileName) throws IOException
+ {
+ StringTokenizer stringTokenizer = new StringTokenizer(resourceFileName, File.separator);
+ File currentFile = dir;
+ int tokenCount = stringTokenizer.countTokens();
+
+ for (int i = 0; i < tokenCount - 1; i++)
+ {
+ String currentToken = stringTokenizer.nextToken();
+
+ // If we are at the root directory and trying to go up, the walking is
+ // finished with an error
+ if ("..".equals(currentToken) && currentFile.equals(dir))
+ return null;
+
+ currentFile = new File(currentFile, currentToken);
+
+ // If the current file doesn't exist or is not a directory, the walking is
+ // finished with an error
+ if (! (currentFile.exists() && currentFile.isDirectory()))
+ return null;
+
+ }
+
+ // Treat the last token differently, if it exists, because it does not need
+ // to be a directory
+ if (tokenCount > 0)
+ {
+ String currentToken = stringTokenizer.nextToken();
+
+ if ("..".equals(currentToken) && currentFile.equals(dir))
+ return null;
+
+ currentFile = new File(currentFile, currentToken);
+
+ // If the current file doesn't exist, the walking is
+ // finished with an error
+ if (! currentFile.exists())
+ return null;
+ }
+
+ return currentFile.getCanonicalFile();
+ }
}
static final class FileResource extends Resource