From b6b6e8be62a0b898001b7755b6b0886b9f4e5eb2 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Wed, 19 Apr 2017 15:52:30 -0500 Subject: [PATCH] [plugin installer] Look for forward or back slashes (#11332) * [plugin installer] Look for forward or back slashes when checking for directory * [plugin installer] Test more file paths for directory check --- src/cli_plugin/install/__tests__/zip.js | 18 +++++++++++++++++- src/cli_plugin/install/zip.js | 8 ++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/cli_plugin/install/__tests__/zip.js b/src/cli_plugin/install/__tests__/zip.js index 5f7f73a65012d..28477703729e5 100644 --- a/src/cli_plugin/install/__tests__/zip.js +++ b/src/cli_plugin/install/__tests__/zip.js @@ -3,7 +3,7 @@ import rimraf from 'rimraf'; import path from 'path'; import os from 'os'; import glob from 'glob'; -import { analyzeArchive, extractArchive } from '../zip'; +import { analyzeArchive, extractArchive, _isDirectory } from '../zip'; describe('kibana cli', function () { @@ -64,4 +64,20 @@ describe('kibana cli', function () { }); }); + describe('_isDirectory', () => { + it('should check for a forward slash', () => { + expect(_isDirectory('/foo/bar/')).to.be(true); + }); + + it('should check for a backslash', () => { + expect(_isDirectory('\\foo\\bar\\')).to.be(true); + }); + + it('should return false for files', () => { + expect(_isDirectory('foo.txt')).to.be(false); + expect(_isDirectory('\\path\\to\\foo.txt')).to.be(false); + expect(_isDirectory('/path/to/foo.txt')).to.be(false); + }); + }); + }); diff --git a/src/cli_plugin/install/zip.js b/src/cli_plugin/install/zip.js index a22bf03877f17..a9445748dc205 100644 --- a/src/cli_plugin/install/zip.js +++ b/src/cli_plugin/install/zip.js @@ -65,6 +65,11 @@ export function analyzeArchive(archive) { }); } +const isDirectoryRegex = /(\/|\\)$/; +export function _isDirectory(filename) { + return isDirectoryRegex.test(filename); +} + export function extractArchive(archive, targetDir, extractPath) { return new Promise((resolve, reject) => { yauzl.open(archive, { lazyEntries: true }, function (err, zipfile) { @@ -87,8 +92,7 @@ export function extractArchive(archive, targetDir, extractPath) { fileName = path.join(targetDir, fileName); } - if (/\/$/.test(fileName)) { - // directory file names end with '/' + if (_isDirectory(fileName)) { mkdirp(fileName, function (err) { if (err) { return reject(err);