Skip to content

Commit

Permalink
Use escapeQuotes over escapeJson for values passed to CEL policy …
Browse files Browse the repository at this point in the history
…script sources

Continuation of #337

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Sep 29, 2023
1 parent 4524636 commit f215cd0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.dependencytrack.model.PolicyCondition;
import org.json.JSONObject;

import static org.apache.commons.lang3.StringEscapeUtils.escapeJson;
import static org.dependencytrack.policy.cel.compat.CelPolicyScriptSourceBuilder.escapeQuotes;

public class ComponentHashCelPolicyScriptSourceBuilder implements CelPolicyScriptSourceBuilder {

Expand All @@ -26,7 +26,7 @@ public String apply(final PolicyCondition policyCondition) {
if (policyCondition.getOperator().equals(PolicyCondition.Operator.IS)) {
return """
component.%s == "%s"
""".formatted(fieldName, escapeJson(hash.getValue()));
""".formatted(fieldName, escapeQuotes(hash.getValue()));
} else {
LOGGER.warn("Policy operator %s is not allowed with this policy".formatted(policyCondition.getOperator().toString()));
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.StringEscapeUtils.escapeJson;
import static org.dependencytrack.policy.cel.compat.CelPolicyScriptSourceBuilder.escapeQuotes;

public class CoordinatesCelPolicyScriptSourceBuilder implements CelPolicyScriptSourceBuilder {

Expand Down Expand Up @@ -52,7 +52,7 @@ private static String evaluateScript(final String conditionGroupPart, final Stri
.build();
return """
component.group.matches("%s") && component.name.matches("%s") && component.matches_range("%s")
""".formatted(escapeJson(group), escapeJson(name), conditionVers.toString());
""".formatted(escapeQuotes(group), escapeQuotes(name), conditionVers.toString());
}

io.github.nscuro.versatile.Comparator versionComparator = switch (versionOperatorMatcher.group(1)) {
Expand All @@ -76,7 +76,7 @@ private static String evaluateScript(final String conditionGroupPart, final Stri

return """
component.group.matches("%s") && component.name.matches("%s") && component.matches_range("%s")
""".formatted(escapeJson(group), escapeJson(name), conditionVers.toString());
""".formatted(escapeQuotes(group), escapeQuotes(name), conditionVers.toString());
}

private static String replace(String conditionString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.dependencytrack.model.PolicyCondition;

import static org.apache.commons.lang3.StringEscapeUtils.escapeJson;
import static org.dependencytrack.policy.cel.compat.CelPolicyScriptSourceBuilder.escapeQuotes;

public class LicenseCelPolicyScriptSourceBuilder implements CelPolicyScriptSourceBuilder {

Expand All @@ -37,7 +37,7 @@ public String apply(final PolicyCondition policyCondition) {
""";
}
} else {
final String escapedLicenseUuid = escapeJson(policyCondition.getValue());
final String escapedLicenseUuid = escapeQuotes(policyCondition.getValue());
if (policyCondition.getOperator() == PolicyCondition.Operator.IS) {
return """
component.resolved_license.uuid == "%s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import org.dependencytrack.model.PolicyCondition;

import static org.apache.commons.lang3.StringEscapeUtils.escapeJson;
import static org.dependencytrack.policy.cel.compat.CelPolicyScriptSourceBuilder.escapeQuotes;

public class LicenseGroupCelPolicyScriptSourceBuilder implements CelPolicyScriptSourceBuilder {

@Override
public String apply(final PolicyCondition policyCondition) {
final String scriptSrc = """
component.resolved_license.groups.exists(group, group.uuid == "%s")
""".formatted(escapeJson(policyCondition.getValue()));
""".formatted(escapeQuotes(policyCondition.getValue()));

if (policyCondition.getOperator() == PolicyCondition.Operator.IS) {
return scriptSrc;
Expand Down

0 comments on commit f215cd0

Please sign in to comment.