53
53
import java .nio .file .FileVisitResult ;
54
54
import java .nio .file .Files ;
55
55
import java .nio .file .Path ;
56
+ import java .nio .file .Paths ;
56
57
import java .nio .file .SimpleFileVisitor ;
57
58
import java .nio .file .StandardCopyOption ;
58
59
import java .nio .file .attribute .BasicFileAttributes ;
@@ -218,25 +219,25 @@ void execute(Terminal terminal, String pluginId, boolean isBatch, Environment en
218
219
throw new UserException (ExitCodes .USAGE , "plugin id is required" );
219
220
}
220
221
221
- Path pluginZip = download (terminal , pluginId , env .tmpFile ());
222
+ Path pluginZip = download (terminal , pluginId , env .tmpFile (), env . pluginsFile () );
222
223
Path extractedZip = unzip (pluginZip , env .pluginsFile ());
223
224
install (terminal , isBatch , extractedZip , env );
224
225
}
225
226
226
227
/** Downloads the plugin and returns the file it was downloaded to. */
227
- private Path download (Terminal terminal , String pluginId , Path tmpDir ) throws Exception {
228
+ private Path download (Terminal terminal , String pluginId , Path tmpDir , Path pluginsDir ) throws Exception {
228
229
if (OFFICIAL_PLUGINS .contains (pluginId )) {
229
230
final String url = getElasticUrl (terminal , getStagingHash (), Version .CURRENT , pluginId , Platforms .PLATFORM_NAME );
230
231
terminal .println ("-> Downloading " + pluginId + " from elastic" );
231
- return downloadZipAndChecksum (terminal , url , tmpDir , false );
232
+ return downloadZipAndChecksum (terminal , url , tmpDir , pluginsDir , false );
232
233
}
233
234
234
235
// now try as maven coordinates, a valid URL would only have a colon and slash
235
236
String [] coordinates = pluginId .split (":" );
236
237
if (coordinates .length == 3 && pluginId .contains ("/" ) == false && pluginId .startsWith ("file:" ) == false ) {
237
238
String mavenUrl = getMavenUrl (terminal , coordinates , Platforms .PLATFORM_NAME );
238
239
terminal .println ("-> Downloading " + pluginId + " from maven central" );
239
- return downloadZipAndChecksum (terminal , mavenUrl , tmpDir , true );
240
+ return downloadZipAndChecksum (terminal , mavenUrl , tmpDir , pluginsDir , true );
240
241
}
241
242
242
243
// fall back to plain old URL
@@ -250,7 +251,7 @@ private Path download(Terminal terminal, String pluginId, Path tmpDir) throws Ex
250
251
throw new UserException (ExitCodes .USAGE , msg );
251
252
}
252
253
terminal .println ("-> Downloading " + URLDecoder .decode (pluginId , "UTF-8" ));
253
- return downloadZip (terminal , pluginId , tmpDir );
254
+ return downloadZip (terminal , pluginId , tmpDir , pluginsDir );
254
255
}
255
256
256
257
// pkg private so tests can override
@@ -324,9 +325,17 @@ private List<String> checkMisspelledPlugin(String pluginId) {
324
325
/** Downloads a zip from the url, into a temp file under the given temp dir. */
325
326
// pkg private for tests
326
327
@ SuppressForbidden (reason = "We use getInputStream to download plugins" )
327
- Path downloadZip (Terminal terminal , String urlString , Path tmpDir ) throws IOException {
328
+ Path downloadZip (Terminal terminal , String urlString , Path tmpDir , Path pluginsDir ) throws IOException {
328
329
terminal .println (VERBOSE , "Retrieving zip from " + urlString );
329
330
URL url = new URL (urlString );
331
+ if (url .getProtocol ().equals ("file" )) {
332
+ Path pluginsFile = Paths .get (url .getFile ());
333
+ if (pluginsFile .startsWith (pluginsDir )) {
334
+ throw new IllegalStateException ("Installation failed! " +
335
+ "Make sure the plugins directory [" + pluginsDir + "] can not contain the plugin distribution [" +
336
+ pluginsFile + "]; move the distribution to an alternate location!" );
337
+ }
338
+ }
330
339
Path zip = Files .createTempFile (tmpDir , null , ".zip" );
331
340
URLConnection urlConnection = url .openConnection ();
332
341
urlConnection .addRequestProperty ("User-Agent" , "elasticsearch-plugin-installer" );
@@ -375,8 +384,9 @@ public void onProgress(int percent) {
375
384
/** Downloads a zip from the url, as well as a SHA512 (or SHA1) checksum, and checks the checksum. */
376
385
// pkg private for tests
377
386
@ SuppressForbidden (reason = "We use openStream to download plugins" )
378
- private Path downloadZipAndChecksum (Terminal terminal , String urlString , Path tmpDir , boolean allowSha1 ) throws Exception {
379
- Path zip = downloadZip (terminal , urlString , tmpDir );
387
+ private Path downloadZipAndChecksum (Terminal terminal , String urlString , Path tmpDir , Path pluginsDir , boolean allowSha1 )
388
+ throws Exception {
389
+ Path zip = downloadZip (terminal , urlString , tmpDir , pluginsDir );
380
390
pathsToDeleteOnShutdown .add (zip );
381
391
String checksumUrlString = urlString + ".sha512" ;
382
392
URL checksumUrl = openUrl (checksumUrlString );
0 commit comments