Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib,report: improve signal name validation #27137

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/internal/process/report.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const { convertToValidSignal } = require('internal/util');
const {
ERR_INVALID_ARG_TYPE,
ERR_SYNTHETIC
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { validateSignalName, validateString } = require('internal/validators');
const nr = internalBinding('report');
const report = {
writeReport(file, err) {
Expand Down Expand Up @@ -47,8 +46,7 @@ const report = {
return nr.getSignal();
},
set signal(sig) {
validateString(sig, 'signal');
convertToValidSignal(sig); // Validate that the signal is recognized.
validateSignalName(sig, 'signal');
removeSignalHandler();
addSignalHandler(sig);
nr.setSignal(sig);
Expand Down
21 changes: 19 additions & 2 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE
ERR_OUT_OF_RANGE,
ERR_UNKNOWN_SIGNAL
}
} = require('internal/errors');
const {
isArrayBufferView
} = require('internal/util/types');
const { signals } = internalBinding('constants').os;

function isInt32(value) {
return value === (value | 0);
Expand Down Expand Up @@ -110,6 +112,20 @@ function validateNumber(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}

function validateSignalName(signal, name = 'signal') {
if (typeof signal !== 'string')
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);

if (signals[signal] === undefined) {
if (signals[signal.toUpperCase()] !== undefined) {
throw new ERR_UNKNOWN_SIGNAL(signal +
' (signals must use all capital letters)');
}

throw new ERR_UNKNOWN_SIGNAL(signal);
}
}

// TODO(BridgeAR): We have multiple validation functions that call
// `require('internal/utils').toBuf()` before validating for array buffer views.
// Those should likely also be consolidated in here.
Expand All @@ -130,5 +146,6 @@ module.exports = {
validateInt32,
validateUint32,
validateString,
validateNumber
validateNumber,
validateSignalName
};
11 changes: 10 additions & 1 deletion test/report/test-report-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ if (!common.isWindows) {
}, { code: 'ERR_INVALID_ARG_TYPE' });
common.expectsError(() => {
process.report.signal = 'foo';
}, { code: 'ERR_UNKNOWN_SIGNAL' });
}, {
code: 'ERR_UNKNOWN_SIGNAL',
message: 'Unknown signal: foo'
});
common.expectsError(() => {
process.report.signal = 'sigusr1';
}, {
code: 'ERR_UNKNOWN_SIGNAL',
message: 'Unknown signal: sigusr1 (signals must use all capital letters)'
});
assert.strictEqual(process.report.signal, 'SIGUSR2');
process.report.signal = 'SIGUSR1';
assert.strictEqual(process.report.signal, 'SIGUSR1');
Expand Down