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: fix crash if invalid SaaS token #235

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 26 additions & 8 deletions lib/nsolid.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,16 +684,10 @@ function updateConfig(config = {}) {
throw new ERR_INVALID_ARG_TYPE(`config.${key}`, 'string', value);

nsolidConfig[key] = value;
if (key === 'command') {
nsolidConfig.saas = undefined;
}
} else if (key === 'statsd' || key === 'grpc') {
// The statsd variable can now be set to null to stop the statsd agent.
nsolidConfig[key] = value;
}
} else if (key === 'saas' && !config.command) {
nsolidConfig.saas = config.saas;
nsolidConfig.command = parseSaasEnvVar(nsolidConfig.saas, 0);
} else if (key === 'otlp') {
const otlp = parseOTLPType(config.otlp);
if (otlp !== nsolidConfig.otlp) {
Expand All @@ -713,6 +707,25 @@ function updateConfig(config = {}) {
nsolidConfig.otlp = null;
}

if (nsolidConfig.saas) {
if (!config.command) {
nsolidConfig.command = undefined;
}

if (nsolidConfig.command) {
nsolidConfig.saas = undefined;
} else {
const url = parseSaasEnvVar(nsolidConfig.saas, 0);
if (!url) {
nsolidConfig.saas = undefined;
} else if (!nsolidConfig.grpc) {
nsolidConfig.command = url;
} else {
nsolidConfig.grpc = '' + config.grpc;
}
}
}

if (nsolidConfig.grpc && !nsolidConfig.saas) {
// Make sure it's a valid URL otherwise it might crash in the grpc++
// URLParser implementation
Expand Down Expand Up @@ -777,8 +790,13 @@ function initializeConfig(nsolidConfig) {
if (nsolidConfig.saas) {
if (nsolidConfig.command) {
nsolidConfig.saas = undefined;
} else if (!nsolidConfig.grpc) {
nsolidConfig.command = parseSaasEnvVar(nsolidConfig.saas, 0);
} else {
const url = parseSaasEnvVar(nsolidConfig.saas, 0);
if (!url) {
nsolidConfig.saas = undefined;
} else if (!nsolidConfig.grpc) {
nsolidConfig.command = url;
}
}
}

Expand Down
98 changes: 98 additions & 0 deletions test/parallel/test-nsolid-config-saas.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const saasToken =
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.example.org:9001';
const saasCommand = 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.example.org:9001';
const invalidSaasToken = 'invalid.example.org:9001';

if (process.config.variables.asan)
common.skip('incorrect leak reporting in curl.');
Expand Down Expand Up @@ -171,6 +172,85 @@
});
}

function execProc8() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child1' ],
{
env: {
NSOLID_SAAS: invalidSaasToken
}
});
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);
const config = JSON.parse(output);
assert.strictEqual(config.command, undefined);
assert.strictEqual(config.saas, undefined);
});
}

function execProc9() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child1' ],
{
env: {
NSOLID_SAAS: invalidSaasToken,
NSOLID_GRPC: PORT,
}
});
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);
const config = JSON.parse(output);
assert.strictEqual(config.command, undefined);
assert.strictEqual(config.grpc, `localhost:${PORT}`);
assert.strictEqual(config.saas, undefined);
});
}

function execProc10() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child5' ]);
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);

Check failure on line 228 in test/parallel/test-nsolid-config-saas.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:assert:90 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: null !== 0 at ChildProcess.<anonymous> (/home/runner/work/nsolid/nsolid/test/parallel/test-nsolid-config-saas.js:228:12) at ChildProcess.emit (node:events:518:28) at maybeClose (node:internal/child_process:1104:16) at ChildProcess._handle.onexit (node:internal/child_process:304:5) { generatedMessage: true, code: 'ERR_ASSERTION', actual: null, expected: 0, operator: 'strictEqual' } Node.js v22.11.0 Command: out/Release/nsolid --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/nsolid/nsolid/test/parallel/test-nsolid-config-saas.js
const config = JSON.parse(output);
assert.strictEqual(config.command, undefined);
assert.strictEqual(config.grpc, `localhost:${PORT}`);
assert.strictEqual(config.saas, undefined);
});
}

function execProc11() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child6' ]);
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);
const config = JSON.parse(output);
assert.strictEqual(config.command, undefined);
assert.strictEqual(config.grpc, `${PORT}`);
assert.strictEqual(config.saas, saasToken);
});
}


switch (process.argv[2]) {
case 'child1':
process.stdout.write(JSON.stringify(nsolid.config));
Expand Down Expand Up @@ -201,6 +281,20 @@
process.stdout.write(JSON.stringify(out));
}
break;
case 'child5':
nsolid.start({
grpc: PORT,
saas: invalidSaasToken
});
process.stdout.write(JSON.stringify(nsolid.config));
break;
case 'child6':
nsolid.start({
grpc: PORT,
saas: saasToken
});
process.stdout.write(JSON.stringify(nsolid.config));
break;
default:
execProc1();
execProc2();
Expand All @@ -209,4 +303,8 @@
execProc5();
execProc6();
execProc7();
execProc8();
execProc9();
execProc10();
execProc11();
}
Loading