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

[backend] handle multilines with cmd #2002

Merged
merged 3 commits into from
Jan 2, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@
return result.toString();
}

private static String formatMultilineCommand(String command) {
String[] lines = command.split("\n");
StringBuilder formattedCommand = new StringBuilder();

Check warning on line 78 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L77-L78

Added lines #L77 - L78 were not covered by tests

for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String trimmedLine = line.trim();

Check warning on line 82 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L81-L82

Added lines #L81 - L82 were not covered by tests
if (trimmedLine.isEmpty()) {
continue;

Check warning on line 84 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L84

Added line #L84 was not covered by tests
}
formattedCommand.append(trimmedLine);

Check warning on line 86 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L86

Added line #L86 was not covered by tests

boolean isLastLine = (i == lines.length - 1);
boolean isAfterParentheses = trimmedLine.endsWith("(");

Check warning on line 89 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L89

Added line #L89 was not covered by tests
boolean isBeforeParentheses = !isLastLine && lines[i + 1].trim().startsWith(")");

if (!isAfterParentheses && !isBeforeParentheses && !isLastLine) {
formattedCommand.append(" & ");

Check warning on line 93 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L93

Added line #L93 was not covered by tests
} else {
formattedCommand.append(" ");

Check warning on line 95 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L95

Added line #L95 was not covered by tests
}
}

return formattedCommand.toString();

Check warning on line 99 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L99

Added line #L99 was not covered by tests
}

private String processAndEncodeCommand(
String command,
String executor,
Expand All @@ -83,7 +109,7 @@

if (executor.equals("cmd")) {
computedCommand = replaceCmdVariables(computedCommand);
computedCommand = computedCommand.trim().replace("\n", " & ");
computedCommand = formatMultilineCommand(computedCommand);

Check warning on line 112 in openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/inject/service/ExecutableInjectService.java#L112

Added line #L112 was not covered by tests
}

if (obfuscator.equals("base64")) {
Expand Down
Loading