Skip to content

Commit 7f37f4d

Browse files
committed
Minor code enhancement for settable parameters to work in all scenarios
1 parent d74c01d commit 7f37f4d

File tree

6 files changed

+222
-39
lines changed

6 files changed

+222
-39
lines changed

lib/oracledb.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,31 +324,33 @@ async function _verifyOptions(options, inCreatePool) {
324324

325325
// machine must be a string
326326
if (options.machine !== undefined) {
327-
errors.assertPropValue(typeof options.machine === 'string', "machine");
327+
nodbUtil.assertParamPropNetworkName(options, 1, "machine");
328328
outOptions.machine = options.machine;
329329
}
330330

331331
// osUser must be a string
332332
if (options.osUser !== undefined) {
333-
errors.assertPropValue(typeof options.osUser === 'string', "osUser");
333+
nodbUtil.assertParamPropNetworkName(options, 1, "osUser");
334334
outOptions.osUser = options.osUser;
335335
}
336336

337337
// driverName must be a string
338338
if (options.driverName !== undefined) {
339-
errors.assertPropValue(typeof options.driverName === 'string', "driverName");
339+
errors.assertParamPropValue(typeof options.driverName === 'string',
340+
1, "driverName");
340341
outOptions.driverName = options.driverName;
341342
}
342343

343344
// program must be a string
344345
if (options.program !== undefined) {
345-
errors.assertPropValue(typeof options.program === 'string', "program");
346+
nodbUtil.assertParamPropNetworkName(options, 1, "program");
346347
outOptions.program = options.program;
347348
}
348349

349350
// terminal must be a string
350351
if (options.terminal !== undefined) {
351-
errors.assertPropValue(typeof options.terminal === 'string', "terminal");
352+
errors.assertParamPropValue(typeof options.terminal === 'string',
353+
1, "terminal");
352354
outOptions.terminal = options.terminal;
353355
}
354356

@@ -1411,6 +1413,8 @@ module.exports = {
14111413

14121414
set machine(value) {
14131415
errors.assertPropValue(typeof value === 'string', "machine");
1416+
const sanitizedValue = nodbUtil.sanitize(value);
1417+
errors.assertPropValue(value == sanitizedValue, "machine");
14141418
settings.machine = value;
14151419
},
14161420

@@ -1421,6 +1425,8 @@ module.exports = {
14211425

14221426
set osUser(value) {
14231427
errors.assertPropValue(typeof value === 'string', "osUser");
1428+
const sanitizedValue = nodbUtil.sanitize(value);
1429+
errors.assertPropValue(value == sanitizedValue, "osUser");
14241430
settings.osUser = value;
14251431
},
14261432

@@ -1480,6 +1486,8 @@ module.exports = {
14801486

14811487
set program(value) {
14821488
errors.assertPropValue(typeof value === 'string', "program");
1489+
const sanitizedValue = nodbUtil.sanitize(value);
1490+
errors.assertPropValue(value == sanitizedValue, "program");
14831491
settings.program = value;
14841492
},
14851493

lib/thin/sqlnet/navNodes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ class NavDescription extends Description {
778778
this.connectData = "(SERVICE_NAME=)";
779779
}
780780

781-
let pgmName = "\"'" + cInfo.program + "'\"";
781+
let pgmName = cInfo.program;
782782
if (cs.program) {
783783
pgmName = cs.program;
784784
}

lib/thin/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@
2929
const constants = require('../constants');
3030
const errors = require('../errors.js');
3131
const os = require('os');
32+
const nodbUtil = require('../util.js');
3233

3334
//---------------------------------------------------------------------------
3435
// populateClientInfo()
3536
//
3637
// Populates client process information
3738
//---------------------------------------------------------------------------
3839
function populateClientInfo() {
39-
this.program = process.argv0;
40+
this.program = nodbUtil.sanitize(process.argv0);
4041
this.terminal = "unknown";
4142
this.pid = process.pid.toString();
4243
try {
43-
this.userName = os.userInfo().username;
44+
this.userName = nodbUtil.sanitize(os.userInfo().username);
4445
} catch {
4546
this.userName = "unknown";
4647
}
47-
this.hostName = os.hostname();
48+
this.hostName = nodbUtil.sanitize(os.hostname());
4849
}
4950
// Initialize client data on startup.
5051
const CLIENT_INFO = new populateClientInfo();

lib/util.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ const types = require('./types.js');
3434
const constants = require('./constants.js');
3535
const traceHandler = require('./traceHandler.js');
3636

37+
// set of valid network characters
38+
const validNetworkCharacterSet = new Set(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
39+
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
40+
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
41+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
42+
'4', '5', '6', '7', '8', '9', '0', '<', '>', '/', '\\', ',', '.', ':', ';', '\'',
43+
'"', '-', '_', '$', '+', '*', '#', '&', '!', '%', '?', '@']);
44+
3745
// node-oracledb version number
3846
let packageJSON;
3947
try {
@@ -55,6 +63,17 @@ const BUILD_FILE = 'oracledb.node';
5563
// Staging directory used by maintainers building the npm package
5664
const STAGING_DIR = 'package/Staging';
5765

66+
//-----------------------------------------------------------------------------
67+
// assertParamPropNetworkName()
68+
//
69+
// Asserts input vaue and sanitized value passes specified condition
70+
// -----------------------------------------------------------------------------
71+
function assertParamPropNetworkName(obj, parameterNum, propName) {
72+
errors.assertParamPropString(obj, parameterNum, propName);
73+
const sanitizedValue = sanitize(obj[propName]);
74+
errors.assertParamPropValue(obj[propName] === sanitizedValue, parameterNum, propName);
75+
}
76+
5877
// getInstallURL returns a string with installation URL
5978
function getInstallURL() {
6079
return ('Node-oracledb installation instructions: https://node-oracledb.readthedocs.io/en/latest/user_guide/installation.html');
@@ -501,6 +520,42 @@ function makeDate(useLocal, year, month, day, hour, minute,
501520
fseconds) - offset * 60000);
502521
}
503522

523+
//---------------------------------------------------------------------------
524+
// sanitize()
525+
//
526+
// this function replaces invalid characters in a string with characters
527+
// guaranteed to be in the Network Character Set.
528+
//---------------------------------------------------------------------------
529+
function sanitize(text) {
530+
let value = text.split('');
531+
532+
// if first character is single/double quote
533+
if ((value[0] === '\'' || value[0] === '"')) {
534+
value = value.splice(1);
535+
}
536+
537+
// if last character is single/double quote
538+
if ((value[value.length - 1] === '\'' || value[value.length - 1] === '"')) {
539+
value.pop();
540+
}
541+
542+
// look for invalid characters, and replace them with '?'
543+
// in case of default values and throw an error
544+
// for user provided values
545+
for (let i = 0; i < value.length; i++) {
546+
if (!validNetworkCharacterSet.has(value[i])) {
547+
value[i] = '?';
548+
}
549+
}
550+
551+
// if last character is a backslash
552+
if (value[value.length - 1] === '\\') {
553+
value[value.length - 1] = '?';
554+
}
555+
556+
return value.join('');
557+
}
558+
504559
// define exports
505560
module.exports = {
506561
BINARY_FILE,
@@ -509,6 +564,7 @@ module.exports = {
509564
RELEASE_DIR,
510565
STAGING_DIR,
511566
addTypeProperties,
567+
assertParamPropNetworkName,
512568
callbackify,
513569
denormalizePrivateKey,
514570
getInstallURL,
@@ -525,6 +581,7 @@ module.exports = {
525581
isXid,
526582
normalizeXid,
527583
makeDate,
584+
sanitize,
528585
verifySodaDoc,
529586
wrapFn,
530587
wrapFns

test/connection.js

Lines changed: 115 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,11 @@ describe('1. connection.js', function() {
609609
if (!dbConfig.test.DBA_PRIVILEGE || !oracledb.thin) return;
610610

611611
// Set the original parameters back
612-
oracledb.driverName = origDriverName;
613-
oracledb.program = origProgramName;
614-
oracledb.terminal = origTerminalName;
615-
oracledb.machine = origMachineName;
616-
oracledb.osUser = origUserName;
612+
oracledb.driverName = origDriverName ?? "";
613+
oracledb.program = origProgramName ?? "";
614+
oracledb.terminal = origTerminalName ?? "";
615+
oracledb.machine = origMachineName ?? "";
616+
oracledb.osUser = origUserName ?? "";
617617
});
618618

619619
it('1.18.1 negative - Check parameter value type', async function() {
@@ -627,61 +627,61 @@ describe('1. connection.js', function() {
627627
dbaConfig.driverName = null;
628628
await assert.rejects(
629629
async () => await oracledb.getConnection(dbaConfig),
630-
/NJS-004:/
630+
/NJS-007:/
631631
);
632632

633633
dbaConfig.driverName = 1;
634634
await assert.rejects(
635635
async () => await oracledb.getConnection(dbaConfig),
636-
/NJS-004:/
636+
/NJS-007:/
637637
);
638638

639639
dbaConfig.machine = null;
640640
await assert.rejects(
641641
async () => await oracledb.getConnection(dbaConfig),
642-
/NJS-004:/
642+
/NJS-007:/
643643
);
644644

645645
dbaConfig.machine = 1;
646646
await assert.rejects(
647647
async () => await oracledb.getConnection(dbaConfig),
648-
/NJS-004:/
648+
/NJS-007:/
649649
);
650650

651651
dbaConfig.terminal = null;
652652
await assert.rejects(
653653
async () => await oracledb.getConnection(dbaConfig),
654-
/NJS-004:/
654+
/NJS-007:/
655655
);
656656

657657
dbaConfig.terminal = 1;
658658
await assert.rejects(
659659
async () => await oracledb.getConnection(dbaConfig),
660-
/NJS-004:/
660+
/NJS-007:/
661661
);
662662

663663
dbaConfig.program = null;
664664
await assert.rejects(
665665
async () => await oracledb.getConnection(dbaConfig),
666-
/NJS-004:/
666+
/NJS-007:/
667667
);
668668

669669
dbaConfig.program = 1;
670670
await assert.rejects(
671671
async () => await oracledb.getConnection(dbaConfig),
672-
/NJS-004:/
672+
/NJS-007:/
673673
);
674674

675675
dbaConfig.osUser = null;
676676
await assert.rejects(
677677
async () => await oracledb.getConnection(dbaConfig),
678-
/NJS-004:/
678+
/NJS-007:/
679679
);
680680

681681
dbaConfig.osUser = 1;
682682
await assert.rejects(
683683
async () => await oracledb.getConnection(dbaConfig),
684-
/NJS-004:/
684+
/NJS-007:/
685685
);
686686
});
687687

@@ -780,6 +780,106 @@ describe('1. connection.js', function() {
780780
// above change in config won't effect already existing connection
781781
res = await conn.execute(sqlDriverName);
782782
assert.deepStrictEqual(res.rows[0][0], 'mydriver');
783+
784+
// new connection with parameters value having invalid special character
785+
// We check for parameters i.e. program, machine and osUser
786+
dbaConfig.program = 'pg(m4';
787+
await assert.rejects(
788+
async () => await oracledb.getConnection(dbaConfig),
789+
/NJS-007:/
790+
);
791+
dbaConfig.program = 'pgm)4';
792+
await assert.rejects(
793+
async () => await oracledb.getConnection(dbaConfig),
794+
/NJS-007:/
795+
);
796+
dbaConfig.program = 'pgm4=';
797+
await assert.rejects(
798+
async () => await oracledb.getConnection(dbaConfig),
799+
/NJS-007:/
800+
);
801+
dbaConfig.program = 'pgm4\\';
802+
await assert.rejects(
803+
async () => await oracledb.getConnection(dbaConfig),
804+
/NJS-007:/
805+
);
806+
dbaConfig.program = '"pgm4';
807+
await assert.rejects(
808+
async () => await oracledb.getConnection(dbaConfig),
809+
/NJS-007:/
810+
);
811+
dbaConfig.program = 'pgm4"';
812+
await assert.rejects(
813+
async () => await oracledb.getConnection(dbaConfig),
814+
/NJS-007:/
815+
);
816+
dbaConfig.osUser = '(myuser4';
817+
await assert.rejects(
818+
async () => await oracledb.getConnection(dbaConfig),
819+
/NJS-007:/
820+
);
821+
dbaConfig.osUser = 'myus)er4';
822+
await assert.rejects(
823+
async () => await oracledb.getConnection(dbaConfig),
824+
/NJS-007:/
825+
);
826+
dbaConfig.osUser = 'myus=er4';
827+
await assert.rejects(
828+
async () => await oracledb.getConnection(dbaConfig),
829+
/NJS-007:/
830+
);
831+
dbaConfig.osUser = 'myuser4\\';
832+
await assert.rejects(
833+
async () => await oracledb.getConnection(dbaConfig),
834+
/NJS-007:/
835+
);
836+
dbaConfig.osUser = '"myuser4';
837+
await assert.rejects(
838+
async () => await oracledb.getConnection(dbaConfig),
839+
/NJS-007:/
840+
);
841+
dbaConfig.osUser = 'myuser4"';
842+
await assert.rejects(
843+
async () => await oracledb.getConnection(dbaConfig),
844+
/NJS-007:/
845+
);
846+
dbaConfig.machine = '(machine4';
847+
await assert.rejects(
848+
async () => await oracledb.getConnection(dbaConfig),
849+
/NJS-007:/
850+
);
851+
dbaConfig.machine = ')machine4';
852+
await assert.rejects(
853+
async () => await oracledb.getConnection(dbaConfig),
854+
/NJS-007:/
855+
);
856+
dbaConfig.machine = 'machine4\\';
857+
await assert.rejects(
858+
async () => await oracledb.getConnection(dbaConfig),
859+
/NJS-007:/
860+
);
861+
dbaConfig.machine = 'machine4=';
862+
await assert.rejects(
863+
async () => await oracledb.getConnection(dbaConfig),
864+
/NJS-007:/
865+
);
866+
dbaConfig.machine = '"machine4';
867+
await assert.rejects(
868+
async () => await oracledb.getConnection(dbaConfig),
869+
/NJS-007:/
870+
);
871+
dbaConfig.machine = 'machine4"';
872+
await assert.rejects(
873+
async () => await oracledb.getConnection(dbaConfig),
874+
/NJS-007:/
875+
);
876+
res = await conn3.execute(sqlDriverName);
877+
assert.deepStrictEqual(res.rows[0][0], 'mydriver3');
878+
879+
// above change in config won't effect already existing connection
880+
res = await conn.execute(sqlDriverName);
881+
assert.deepStrictEqual(res.rows[0][0], 'mydriver');
882+
783883
await conn1.close();
784884
await conn.close();
785885
await conn2.close();

0 commit comments

Comments
 (0)