Skip to content

Commit

Permalink
feat: Warn if UNC paths used with file API calls when Butler runs on …
Browse files Browse the repository at this point in the history
…non-Windows OS

Implements ptarmiganlabs#522
  • Loading branch information
mountaindude committed Aug 7, 2022
1 parent bb71a6a commit 843f781
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/api/disk_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const apiFileCopy = {
},
},
403: {
description: 'No approved fromDir/toDir for file copy.',
description:
'No approved fromDir/toDir for file copy, or UNC path used when Butler is running on non-Windows operating system',
type: 'object',
properties: {
statusCode: { type: 'number' },
Expand Down
40 changes: 39 additions & 1 deletion src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { IncomingWebhook } = require('ms-teams-webhook');
const si = require('systeminformation');
const os = require('os');
const crypto = require('crypto');
const isUncPath = require('is-unc-path');

// Add dependencies
const { Command, Option } = require('commander');
Expand Down Expand Up @@ -301,6 +302,21 @@ if (config.has('Butler.fileCopyApprovedDirectories') && config.get('Butler.fileC
config.get('Butler.fileCopyApprovedDirectories').forEach((element) => {
logger.verbose(`fileCopy directories from config file: ${JSON.stringify(element, null, 2)}`);

// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(element.fromDirectory) === true) {
logger.warn(
`FILE COPY CONFIG: UNC paths won't work on non-Windows OSs ("${element.fromDirectory}"). OS is "${os.platform()}".`
);
}
if (isUncPath(element.toDirectory) === true) {
logger.warn(
`FILE COPY CONFIG: UNC paths won't work on non-Windows OSs ("${element.toDirectory}"). OS is "${os.platform()}".`
);
}
}

const newDirCombo = {
fromDir: upath.normalizeSafe(element.fromDirectory),
toDir: upath.normalizeSafe(element.toDirectory),
Expand All @@ -319,6 +335,21 @@ if (config.has('Butler.fileMoveApprovedDirectories') && config.get('Butler.fileM
config.get('Butler.fileMoveApprovedDirectories').forEach((element) => {
logger.verbose(`fileMove directories from config file: ${JSON.stringify(element, null, 2)}`);

// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(element.fromDirectory) === true) {
logger.warn(
`FILE MOVE CONFIG: UNC paths won't work on non-Windows OSs ("${element.fromDirectory}"). OS is "${os.platform()}".`
);
}
if (isUncPath(element.toDirectory) === true) {
logger.warn(
`FILE MOVE CONFIG: UNC paths won't work on non-Windows OSs ("${element.toDirectory}"). OS is "${os.platform()}".`
);
}
}

const newDirCombo = {
fromDir: upath.normalizeSafe(element.fromDirectory),
toDir: upath.normalizeSafe(element.toDirectory),
Expand All @@ -337,8 +368,15 @@ if (config.has('Butler.fileDeleteApprovedDirectories') && config.get('Butler.fil
config.get('Butler.fileDeleteApprovedDirectories').forEach((element) => {
logger.verbose(`fileDelete directory from config file: ${element}`);

const deleteDir = upath.normalizeSafe(element);
// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(element) === true) {
logger.warn(`FILE DELETE CONFIG: UNC paths won't work on non-Windows OSs ("${element}"). OS is "${os.platform()}".`);
}
}

const deleteDir = upath.normalizeSafe(element);
logger.info(`Adding normalized fileDelete directory ${deleteDir}`);

fileDeleteDirectories.push(deleteDir);
Expand Down
33 changes: 33 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"handlebars": "^4.7.7",
"http-errors": "^2.0.0",
"influx": "^5.9.3",
"is-unc-path": "^1.0.0",
"js-yaml": "^4.1.0",
"jshint": "^2.13.4",
"lodash": "^4.17.21",
Expand Down
79 changes: 79 additions & 0 deletions src/routes/disk_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const httpErrors = require('http-errors');
const fs = require('fs-extra');
const upath = require('upath');
const mkdirp = require('mkdirp');
const isUncPath = require('is-unc-path');
const os = require('os');

// Load global variables and functions
const globals = require('../globals');
Expand Down Expand Up @@ -33,6 +35,35 @@ async function handlerFileCopy(request, reply) {
preserveTimestamp = true;
}

// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so, then return error
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(request.body.fromFile) === true) {
globals.logger.warn(
`FILE COPY FROM: UNC paths not supported work on non-Windows OSs ("${
request.body.fromFile
}"). OS is "${os.platform()}".`
);
reply.send(
httpErrors(
400,
`UNC paths not supported for file copy operations when running Butler on non-Windows OS. Path: ${request.body.fromFile}`
)
);
}
if (isUncPath(request.body.toFile) === true) {
globals.logger.warn(
`FILE COPY TO: UNC paths not supported on non-Windows OSs ("${request.body.toFile}"). OS is "${os.platform()}".`
);
reply.send(
httpErrors(
400,
`UNC paths not supported for file copy operations when running Butler on non-Windows OS. Path: ${request.body.toFile}`
)
);
}
}

// Make sure that
// 1. fromFile is in a valid source directory (or subdirectory thereof),
// 2. toFile is in a valid associated destination directory (or subdirectory thereof)
Expand Down Expand Up @@ -115,6 +146,34 @@ async function handlerFileMove(request, reply) {
if (request.body.overwrite === 'true' || request.body.overwrite === true) {
overwrite = true;
}
// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so, then return error
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(request.body.fromFile) === true) {
globals.logger.warn(
`FILE MOVE FROM: UNC paths not supported work on non-Windows OSs ("${
request.body.fromFile
}"). OS is "${os.platform()}".`
);
reply.send(
httpErrors(
400,
`UNC paths not supported for file move operations when running Butler on non-Windows OS. Path: ${request.body.fromFile}`
)
);
}
if (isUncPath(request.body.toFile) === true) {
globals.logger.warn(
`FILE MOVE TO: UNC paths not supported on non-Windows OSs ("${request.body.toFile}"). OS is "${os.platform()}".`
);
reply.send(
httpErrors(
400,
`UNC paths not supported for file move operations when running Butler on non-Windows OS. Path: ${request.body.toFile}`
)
);
}
}

// Make sure that
// 1. fromFile is in a valid source directory (or subdirectory thereof),
Expand Down Expand Up @@ -172,6 +231,26 @@ async function handlerFileDelete(request, reply) {
// Required parameter is missing
reply.send(httpErrors(400, 'Required parameter missing'));
} else {
// Check if Butler is running on Linux-ish host and UNC path(s) are specified
// Warn if so, then return error
if (os.platform().toLowerCase() !== 'windows') {
if (isUncPath(request.body.deleteFile) === true) {
globals.logger.warn(
`FILE COPY FROM: UNC paths not supported work on non-Windows OSs ("${
request.body.deleteFile
}"). OS is "${os.platform()}".`
);
reply.send(
httpErrors(
400,
`UNC paths not supported for file copy operations when running Butler on non-Windows OS. Path: ${request.body.deleteFile}`
)
);

return;
}
}

// Make sure that
// 1. file exists
// 2. file is in a valid directoryv (or subdirectory thereof),
Expand Down

0 comments on commit 843f781

Please sign in to comment.