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

Do not crash when converting YAML to JSON fails #4110

Merged
merged 7 commits into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion source/common/json/json_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,9 @@ FieldSharedPtr parseYamlNode(YAML::Node node) {
} // namespace

ObjectSharedPtr Factory::loadFromYamlString(const std::string& yaml) {
YAML::Node node;
try {
return parseYamlNode(YAML::Load(yaml));
node = YAML::Load(yaml);
} catch (YAML::ParserException& e) {
throw EnvoyException(e.what());
} catch (YAML::BadConversion& e) {
Expand All @@ -756,6 +757,12 @@ ObjectSharedPtr Factory::loadFromYamlString(const std::string& yaml) {
// the Envoy Exception text.
throw EnvoyException(fmt::format("Unexpected YAML exception: {}", +e.what()));
}

// A valid JSON can only be built from a Map or a Sequence.
if (node.Type() == YAML::NodeType::Map || node.Type() == YAML::NodeType::Sequence) {
return parseYamlNode(node);
}
throw EnvoyException(fmt::format("Unable to convert YAML as JSON: {}", yaml));
}

ObjectSharedPtr Factory::loadFromString(const std::string& json) {
Expand Down
13 changes: 13 additions & 0 deletions test/common/protobuf/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ TEST(UtilityTest, JsonConvertCamelSnake) {
.string_value());
}

TEST(UtilityTest, YamlLoadFromStringFail) {
envoy::config::bootstrap::v2::Bootstrap bootstrap;
// Verify loadFromYaml can parse valid YAML string.
MessageUtil::loadFromYaml("node: { id: node1 }", bootstrap);
// Verify loadFromYaml throws error when the input is an invalid YAML string.
EXPECT_THROW_WITH_MESSAGE(MessageUtil::loadFromYaml("not_a_yaml", bootstrap), EnvoyException,
"Unable to convert YAML as JSON: not_a_yaml");
// When wrongly inputted by a file path, loadFromYaml throws an error.
EXPECT_THROW_WITH_MESSAGE(MessageUtil::loadFromYaml("/home/configs/config.yaml", bootstrap),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this path is available in CI? Is it possible to do a more direct test case of the actual issue potentially not even loading from a file?

Copy link
Member Author

@dio dio Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simulates what is described in the issue #3990, that gave input to --config-yaml a file path string (while this config option expects a valid yaml string instead). #3990 (comment)

This doesn't try to load a file. The --config-yaml perceives this as a string (in this case the string value is a file path).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Got it.

EnvoyException,
"Unable to convert YAML as JSON: /home/configs/config.yaml");
}

TEST(DurationUtilTest, OutOfRange) {
{
ProtobufWkt::Duration duration;
Expand Down