Skip to content

Correctly parse and strip quotes in prepareAdditionalParams (AST-96181) #90

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 51 additions & 12 deletions src/main/wrapper/CxWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,23 +407,62 @@ export class CxWrapper {
return new ExecutionService().executeCommands(this.config.pathToExecutable, commands, CxConstants.MASK_TYPE);
}

prepareAdditionalParams(additionalParameters: string): string[] {
const params: string[] = [];

/**
* Splits additional CLI parameters into an array of tokens,
* correctly handling quoted values and key=value pairs.
*
* @param additionalParameters - A single string containing extra parameters
* @returns Array of cleaned tokens ready to pass to the CLI
*/
private prepareAdditionalParams(additionalParameters: string): string[] {
if (!additionalParameters) {
return params;
}
return [];
}

// Trim whitespace and remove surrounding quotes if present
let trimmed = additionalParameters.trim();
if (
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
(trimmed.startsWith("'") && trimmed.endsWith("'"))
) {
trimmed = trimmed.slice(1, -1);
}

// Regex matches sequences without whitespace or quoted segments
const regex = /(?:[^\s'"]+|'[^']*'|"[^"]*")+/g;
const rawTokens = trimmed.match(regex) || [];

// Process tokens: remove quotes and handle key=value syntax
return rawTokens.map(token => {
// Remove surrounding quotes
if (
(token.startsWith('"') && token.endsWith('"')) ||
(token.startsWith("'") && token.endsWith("'"))
) {
token = token.slice(1, -1);
}

const paramList = additionalParameters.match(/(?:[^\s"]+|"[^"]*")+/g);
logger.info("Additional parameters refined: " + paramList)
if (paramList) {
paramList.forEach((element) => {
params.push(element);
});
}
return params;
// If token contains '=', split and clean value
const eqIndex = token.indexOf('=');
if (eqIndex !== -1) {
const key = token.substring(0, eqIndex);
let value = token.substring(eqIndex + 1);
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
return `${key}=${value}`;
}

return token;
});
}



getIndexOfBflNode(bflNodes: CxBFL[], resultNodes: any[]): number {
const bflNodeNotFound = -1;

Expand Down
21 changes: 21 additions & 0 deletions src/tests/ScanTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ describe("ScanCreate cases", () => {

})

it('prepareAdditionalParams splits complex parameters correctly', async () => {
// Create a wrapper instance to access the method
const wrapper: any = await cxWrapperFactory.createWrapper(cxScanConfig);

// The raw string exactly as it will be passed from YAML
const raw = "--gradle-parameters='-Prepository.proxy.url=123 -Prepository.proxy.username=123 -Prepository.proxy.password=123' --log-level Debug";

// Invoke the method under test
const tokens: string[] = wrapper.prepareAdditionalParams(raw);

// Verify that the output matches the expected tokens after the fix
expect(tokens).toEqual([
"--gradle-parameters=-Prepository.proxy.url=123 -Prepository.proxy.username=123 -Prepository.proxy.password=123",
"--log-level",
"Debug"
]);
});




it('ScanCreate Successful case no wait mode', async () => {
const params = new Map();
params.set(CxParamType.PROJECT_NAME, "ast-cli-javascript-integration-nowait");
Expand Down
Loading