Added exceptions when accessing InnerPaths outside a FolderPack's root dir

This commit is contained in:
Vhati 2013-08-28 16:36:52 -04:00
parent 52e57f0e42
commit 7c9bb71cf3

View file

@ -353,7 +353,7 @@ public class FTLDat {
@Override @Override
public void add( String innerPath, InputStream is ) throws IOException { public void add( String innerPath, InputStream is ) throws IOException {
File dstFile = getFile( innerPath ); File dstFile = getFile( innerPath );
if ( dstFile.exists() ) throw new IOException( "File already exists" ); if ( dstFile.exists() ) throw new IOException( "InnerPath already exists: "+ innerPath );
dstFile.getParentFile().mkdirs(); dstFile.getParentFile().mkdirs();
@ -418,7 +418,16 @@ public class FTLDat {
*/ */
public File getFile( String innerPath ) { public File getFile( String innerPath ) {
if ( innerPath.indexOf("\\") != -1 ) throw new IllegalArgumentException( "InnerPath contains backslashes: "+ innerPath ); if ( innerPath.indexOf("\\") != -1 ) throw new IllegalArgumentException( "InnerPath contains backslashes: "+ innerPath );
return new File( rootDir, innerPath ); File tmpFile = new File( rootDir, innerPath )
// Check if the file is inside rootDir.
File parentDir = tmpFile.getParentFile();
while( parentDir != null ) {
if ( parentDir.equals( rootDir ) ) return tmpFile;
parentDir = parentDir.getParentFile();
}
throw new IllegalArgumentException( String.format( "InnerPath \"%s\" is outside the FolderPack at \"%s\".", innerPath, rootDir ) );
} }
} }