Skip to content

Commit 57c2a02

Browse files
Correctly parse and strip quotes in prepareAdditionalParams (AST-96181)
1 parent 7e23ab8 commit 57c2a02

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

src/main/wrapper/CxWrapper.ts

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,23 +407,62 @@ export class CxWrapper {
407407
return new ExecutionService().executeCommands(this.config.pathToExecutable, commands, CxConstants.MASK_TYPE);
408408
}
409409

410-
prepareAdditionalParams(additionalParameters: string): string[] {
411-
const params: string[] = [];
412410

411+
/**
412+
* Splits additional CLI parameters into an array of tokens,
413+
* correctly handling quoted values and key=value pairs.
414+
*
415+
* @param additionalParameters - A single string containing extra parameters
416+
* @returns Array of cleaned tokens ready to pass to the CLI
417+
*/
418+
private prepareAdditionalParams(additionalParameters: string): string[] {
413419
if (!additionalParameters) {
414-
return params;
415-
}
420+
return [];
421+
}
422+
423+
// Trim whitespace and remove surrounding quotes if present
424+
let trimmed = additionalParameters.trim();
425+
if (
426+
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
427+
(trimmed.startsWith("'") && trimmed.endsWith("'"))
428+
) {
429+
trimmed = trimmed.slice(1, -1);
430+
}
431+
432+
// Regex matches sequences without whitespace or quoted segments
433+
const regex = /(?:[^\s'"]+|'[^']*'|"[^"]*")+/g;
434+
const rawTokens = trimmed.match(regex) || [];
435+
436+
// Process tokens: remove quotes and handle key=value syntax
437+
return rawTokens.map(token => {
438+
// Remove surrounding quotes
439+
if (
440+
(token.startsWith('"') && token.endsWith('"')) ||
441+
(token.startsWith("'") && token.endsWith("'"))
442+
) {
443+
token = token.slice(1, -1);
444+
}
416445

417-
const paramList = additionalParameters.match(/(?:[^\s"]+|"[^"]*")+/g);
418-
logger.info("Additional parameters refined: " + paramList)
419-
if (paramList) {
420-
paramList.forEach((element) => {
421-
params.push(element);
422-
});
423-
}
424-
return params;
446+
// If token contains '=', split and clean value
447+
const eqIndex = token.indexOf('=');
448+
if (eqIndex !== -1) {
449+
const key = token.substring(0, eqIndex);
450+
let value = token.substring(eqIndex + 1);
451+
if (
452+
(value.startsWith('"') && value.endsWith('"')) ||
453+
(value.startsWith("'") && value.endsWith("'"))
454+
) {
455+
value = value.slice(1, -1);
456+
}
457+
return `${key}=${value}`;
458+
}
459+
460+
return token;
461+
});
425462
}
426463

464+
465+
427466
getIndexOfBflNode(bflNodes: CxBFL[], resultNodes: any[]): number {
428467
const bflNodeNotFound = -1;
429468

src/tests/ScanTest.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ describe("ScanCreate cases", () => {
6161

6262
})
6363

64+
it('prepareAdditionalParams splits complex parameters correctly', async () => {
65+
// Create a wrapper instance to access the method
66+
const wrapper: any = await cxWrapperFactory.createWrapper(cxScanConfig);
67+
68+
// The raw string exactly as it will be passed from YAML
69+
const raw = "--gradle-parameters='-Prepository.proxy.url=123 -Prepository.proxy.username=123 -Prepository.proxy.password=123' --log-level Debug";
70+
71+
// Invoke the method under test
72+
const tokens: string[] = wrapper.prepareAdditionalParams(raw);
73+
74+
// Verify that the output matches the expected tokens after the fix
75+
expect(tokens).toEqual([
76+
"--gradle-parameters=-Prepository.proxy.url=123 -Prepository.proxy.username=123 -Prepository.proxy.password=123",
77+
"--log-level",
78+
"Debug"
79+
]);
80+
});
81+
82+
83+
84+
6485
it('ScanCreate Successful case no wait mode', async () => {
6586
const params = new Map();
6687
params.set(CxParamType.PROJECT_NAME, "ast-cli-javascript-integration-nowait");

0 commit comments

Comments
 (0)