Skip to content

Commit

Permalink
docker, processLoad fixed potential security issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Feb 14, 2021
1 parent 2e92938 commit 07daa05
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
64 changes: 48 additions & 16 deletions lib/docker.js
Expand Up @@ -109,6 +109,9 @@ function dockerContainers(all, callback) {
callback = all;
all = false;
}
if (typeof all !== 'boolean' && all !== undefined) {
all = false;
}

all = all || false;
let result = [];
Expand Down Expand Up @@ -185,16 +188,20 @@ function dockerContainers(all, callback) {
// container inspect (for one container)

function dockerContainerInspect(containerID, payload) {
containerID = containerID || '';
return new Promise((resolve) => {
process.nextTick(() => {
if (containerID) {
containerID = containerID || '';
if (typeof containerID !== 'string') {
resolve();
}
const containerIdSanitized = (util.isPrototypePolluted() ? '' : util.sanitizeShellString(containerID, true)).trim();
if (containerIdSanitized) {

if (!_docker_socket) {
_docker_socket = new DockerSocket();
}

_docker_socket.getInspect(containerID.trim(), data => {
_docker_socket.getInspect(containerIdSanitized.trim(), data => {
try {
resolve({
id: payload.Id,
Expand Down Expand Up @@ -325,19 +332,39 @@ function docker_calcBlockIO(blkio_stats) {
function dockerContainerStats(containerIDs, callback) {

let containerArray = [];
// fallback - if only callback is given
if (util.isFunction(containerIDs) && !callback) {
callback = containerIDs;
containerArray = ['*'];
} else {
containerIDs = containerIDs || '*';
containerIDs = containerIDs.trim().toLowerCase().replace(/,+/g, '|');
containerArray = containerIDs.split('|');
}

return new Promise((resolve) => {
process.nextTick(() => {

// fallback - if only callback is given
if (util.isFunction(containerIDs) && !callback) {
callback = containerIDs;
containerArray = ['*'];
} else {
containerIDs = containerIDs || '*';
if (typeof containerIDs !== 'string') {
if (callback) { callback([]); }
return resolve([]);
}
let containerIDsSanitized = '';
containerIDsSanitized.__proto__.toLowerCase = util.stringToLower;
containerIDsSanitized.__proto__.replace = util.stringReplace;
containerIDsSanitized.__proto__.trim = util.stringTrim;

const s = (util.isPrototypePolluted() ? '' : util.sanitizeShellString(containerIDs, true)).trim();
for (let i = 0; i <= 2000; i++) {
if (!(s[i] === undefined)) {
s[i].__proto__.toLowerCase = util.stringToLower;
const sl = s[i].toLowerCase();
if (sl && sl[0] && !sl[1]) {
containerIDsSanitized = containerIDsSanitized + sl[0];
}
}
}

containerIDsSanitized = containerIDsSanitized.trim().toLowerCase().replace(/,+/g, '|');
containerArray = containerIDs.split('|');
}

const result = [];

const workload = [];
Expand Down Expand Up @@ -444,17 +471,22 @@ exports.dockerContainerStats = dockerContainerStats;
// container processes (for one container)

function dockerContainerProcesses(containerID, callback) {
containerID = containerID || '';
let result = [];
return new Promise((resolve) => {
process.nextTick(() => {
if (containerID) {
containerID = containerID || '';
if (typeof containerID !== 'string') {
resolve(result);
}
const containerIdSanitized = (util.isPrototypePolluted() ? '' : util.sanitizeShellString(containerID, true)).trim();

if (containerIdSanitized) {

if (!_docker_socket) {
_docker_socket = new DockerSocket();
}

_docker_socket.getProcesses(containerID, data => {
_docker_socket.getProcesses(containerIdSanitized, data => {
/**
* @namespace
* @property {Array} Titles
Expand Down
4 changes: 2 additions & 2 deletions lib/internet.js
Expand Up @@ -40,7 +40,7 @@ function inetChecksite(url, callback) {
status: 404,
ms: null
};
if (typeof url !== "string") {
if (typeof url !== 'string') {
if (callback) { callback(result); }
return resolve(result);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ function inetLatency(host, callback) {

return new Promise((resolve) => {
process.nextTick(() => {
if (typeof host !== "string") {
if (typeof host !== 'string') {
if (callback) { callback(null); }
return resolve(null);
}
Expand Down
28 changes: 19 additions & 9 deletions lib/network.js
Expand Up @@ -973,19 +973,29 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e
function networkStats(ifaces, callback) {

let ifacesArray = [];
// fallback - if only callback is given
if (util.isFunction(ifaces) && !callback) {
callback = ifaces;
ifacesArray = [getDefaultNetworkInterface()];
} else {
ifaces = ifaces || getDefaultNetworkInterface();
ifaces = ifaces.trim().toLowerCase().replace(/,+/g, '|');
ifacesArray = ifaces.split('|');
}

return new Promise((resolve) => {
process.nextTick(() => {

// fallback - if only callback is given
if (util.isFunction(ifaces) && !callback) {
callback = ifaces;
ifacesArray = [getDefaultNetworkInterface()];
} else {
if (typeof ifaces !== 'string' && ifaces !== undefined) {
if (callback) { callback([]); }
return resolve([]);
}
ifaces = ifaces || getDefaultNetworkInterface();

ifaces.__proto__.toLowerCase = util.stringToLower;
ifaces.__proto__.replace = util.stringReplace;
ifaces.__proto__.trim = util.stringTrim;

ifaces = ifaces.trim().toLowerCase().replace(/,+/g, '|');
ifacesArray = ifaces.split('|');
}

const result = [];

const workload = [];
Expand Down
9 changes: 8 additions & 1 deletion lib/processes.js
Expand Up @@ -99,7 +99,7 @@ function services(srv, callback) {

return new Promise((resolve) => {
process.nextTick(() => {
if (typeof srv !== "string") {
if (typeof srv !== 'string') {
if (callback) { callback([]); }
return resolve([]);
}
Expand Down Expand Up @@ -892,6 +892,13 @@ function processLoad(proc, callback) {
return new Promise((resolve) => {
process.nextTick(() => {

proc = proc || '';

if (typeof proc !== 'string') {
if (callback) { callback([]); }
return resolve([]);
}

let processesString = '';
processesString.__proto__.toLowerCase = util.stringToLower;
processesString.__proto__.replace = util.stringReplace;
Expand Down

0 comments on commit 07daa05

Please sign in to comment.