Skip to content

Commit

Permalink
Ensure empty YAML inputs yield checked exceptions
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 682755056
  • Loading branch information
TristonianJones authored and copybara-github committed Oct 6, 2024
1 parent d80531c commit 2f6f308
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 0 additions & 1 deletion policy/src/main/java/dev/cel/policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ java_library(
":value_string",
":yaml_helper",
"//common:compiler_common",
"//common:source_location",
"//common/internal",
"@maven//:com_google_guava_guava",
"@maven//:org_yaml_snakeyaml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ private CelPolicyConfig parseYaml(String source, String description)
throws CelPolicyValidationException {
Node node;
try {
node = parseYamlSource(source);
Node yamlNode =
parseYamlSource(source)
.orElseThrow(
() ->
new CelPolicyValidationException(
String.format("YAML document is malformed: %s", source)));
node = yamlNode;
} catch (RuntimeException e) {
throw new CelPolicyValidationException("YAML document is malformed: " + e.getMessage(), e);
}
Expand Down
9 changes: 8 additions & 1 deletion policy/src/main/java/dev/cel/policy/CelPolicyYamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ private static class ParserImpl implements PolicyParserContext<Node> {

private CelPolicy parseYaml() throws CelPolicyValidationException {
Node node;
String policySourceString = policySource.getContent().toString();
try {
node = YamlHelper.parseYamlSource(policySource.getContent().toString());
Node yamlNode =
YamlHelper.parseYamlSource(policySourceString)
.orElseThrow(
() ->
new CelPolicyValidationException(
String.format("YAML document is malformed: %s", policySourceString)));
node = yamlNode;
} catch (RuntimeException e) {
throw new CelPolicyValidationException("YAML document is malformed: " + e.getMessage(), e);
}
Expand Down
6 changes: 3 additions & 3 deletions policy/src/main/java/dev/cel/policy/YamlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Joiner;
import java.io.StringReader;
import java.util.List;
import java.util.Optional;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
Expand Down Expand Up @@ -68,10 +69,9 @@ public static boolean assertYamlType(
return false;
}

static Node parseYamlSource(String policyContent) {
static Optional<Node> parseYamlSource(String policyContent) {
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));

return yaml.compose(new StringReader(policyContent));
return Optional.ofNullable(yaml.compose(new StringReader(policyContent)));
}

static boolean assertRequiredFields(
Expand Down

0 comments on commit 2f6f308

Please sign in to comment.