|
@@ -113,12 +113,17 @@ public class RunJar {
|
|
throws IOException {
|
|
throws IOException {
|
|
try (JarInputStream jar = new JarInputStream(inputStream)) {
|
|
try (JarInputStream jar = new JarInputStream(inputStream)) {
|
|
int numOfFailedLastModifiedSet = 0;
|
|
int numOfFailedLastModifiedSet = 0;
|
|
|
|
+ String targetDirPath = toDir.getCanonicalPath() + File.separator;
|
|
for (JarEntry entry = jar.getNextJarEntry();
|
|
for (JarEntry entry = jar.getNextJarEntry();
|
|
entry != null;
|
|
entry != null;
|
|
entry = jar.getNextJarEntry()) {
|
|
entry = jar.getNextJarEntry()) {
|
|
if (!entry.isDirectory() &&
|
|
if (!entry.isDirectory() &&
|
|
unpackRegex.matcher(entry.getName()).matches()) {
|
|
unpackRegex.matcher(entry.getName()).matches()) {
|
|
File file = new File(toDir, entry.getName());
|
|
File file = new File(toDir, entry.getName());
|
|
|
|
+ if (!file.getCanonicalPath().startsWith(targetDirPath)) {
|
|
|
|
+ throw new IOException("expanding " + entry.getName()
|
|
|
|
+ + " would create file outside of " + toDir);
|
|
|
|
+ }
|
|
ensureDirectory(file.getParentFile());
|
|
ensureDirectory(file.getParentFile());
|
|
try (OutputStream out = new FileOutputStream(file)) {
|
|
try (OutputStream out = new FileOutputStream(file)) {
|
|
IOUtils.copyBytes(jar, out, BUFFER_SIZE);
|
|
IOUtils.copyBytes(jar, out, BUFFER_SIZE);
|
|
@@ -178,6 +183,7 @@ public class RunJar {
|
|
throws IOException {
|
|
throws IOException {
|
|
try (JarFile jar = new JarFile(jarFile)) {
|
|
try (JarFile jar = new JarFile(jarFile)) {
|
|
int numOfFailedLastModifiedSet = 0;
|
|
int numOfFailedLastModifiedSet = 0;
|
|
|
|
+ String targetDirPath = toDir.getCanonicalPath() + File.separator;
|
|
Enumeration<JarEntry> entries = jar.entries();
|
|
Enumeration<JarEntry> entries = jar.entries();
|
|
while (entries.hasMoreElements()) {
|
|
while (entries.hasMoreElements()) {
|
|
final JarEntry entry = entries.nextElement();
|
|
final JarEntry entry = entries.nextElement();
|
|
@@ -185,6 +191,10 @@ public class RunJar {
|
|
unpackRegex.matcher(entry.getName()).matches()) {
|
|
unpackRegex.matcher(entry.getName()).matches()) {
|
|
try (InputStream in = jar.getInputStream(entry)) {
|
|
try (InputStream in = jar.getInputStream(entry)) {
|
|
File file = new File(toDir, entry.getName());
|
|
File file = new File(toDir, entry.getName());
|
|
|
|
+ if (!file.getCanonicalPath().startsWith(targetDirPath)) {
|
|
|
|
+ throw new IOException("expanding " + entry.getName()
|
|
|
|
+ + " would create file outside of " + toDir);
|
|
|
|
+ }
|
|
ensureDirectory(file.getParentFile());
|
|
ensureDirectory(file.getParentFile());
|
|
try (OutputStream out = new FileOutputStream(file)) {
|
|
try (OutputStream out = new FileOutputStream(file)) {
|
|
IOUtils.copyBytes(in, out, BUFFER_SIZE);
|
|
IOUtils.copyBytes(in, out, BUFFER_SIZE);
|