Skip to content

Commit ee9cdae

Browse files
committed
Escape the jar URL to allow symbols such as #.
1 parent d5d3569 commit ee9cdae

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

agent-common/src/com/thoughtworks/go/agent/common/util/JarUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ private static void prepareRefferedJars(String jarFileName, String absolutePath,
9898
depsDir.mkdirs();
9999
String entryBaseName = entryName.replaceAll(".*/", "");
100100
File extractedJar = new File(depsDir, entryBaseName);
101+
String escapedJarURL = new File(absolutePath).toURI().toURL().toExternalForm();
101102
InputStream jarStream = null;
102103
FileOutputStream fileOutputStream = null;
103104
try {
104-
URL jarUrl = new URL("jar:file:" + absolutePath + "!/" + entryName);
105+
URL jarUrl = new URL("jar:" + escapedJarURL + "!/" + entryName);
105106
URLConnection conn = jarUrl.openConnection();
106107
conn.setUseCaches(false);
107108
jarStream = conn.getInputStream();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.thoughtworks.go.agent.common.util;
17+
18+
import com.thoughtworks.go.agent.common.util.JarUtil;
19+
20+
import org.junit.Test;
21+
import org.junit.Before;
22+
import org.junit.After;
23+
import static org.junit.Assert.*;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
28+
import org.apache.commons.io.FileUtils;
29+
30+
public class JarUtilTest {
31+
32+
private static final String PATH_WITH_HASHES = "#hashes#in#path/";
33+
34+
@Before
35+
public void setUp() throws IOException {
36+
FileUtils.copyFile(new File("testdata/test-agent.jar"), new File(PATH_WITH_HASHES + "test-agent.jar"));
37+
}
38+
39+
@After
40+
public void tearDown() throws IOException {
41+
FileUtils.deleteQuietly(new File(PATH_WITH_HASHES + "test-agent.jar"));
42+
FileUtils.deleteDirectory(new File(PATH_WITH_HASHES));
43+
}
44+
45+
@Test
46+
public void shouldNotThrowMalformedUrlException() throws Exception {
47+
String absolutePath = new File(PATH_WITH_HASHES + "test-agent.jar").getAbsolutePath();
48+
49+
try {
50+
Object agent_launcher = JarUtil.objectFromJar(absolutePath, "Go-Agent-Bootstrap-Class");
51+
} catch (Exception e) {
52+
fail();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)