Commit 6c573ee6 authored by Nikita Podguzov's avatar Nikita Podguzov Committed by Commit Bot

Policy: Use JSON_ALLOW_TRAILING_COMMAS json parsing schema for policies

This change includes special handlers for ArcPolicy,
NativePrintersBulkConfiguration, DeviceNativePrinters,
External​Print​Servers policies and "Dictionary" data type policies for
extensions.
Now it's allowed to use commas after last objects in JSON lists and
dictionaries.

Bug: 843988
Change-Id: I3ced2d1cfb3e009287b95ee07ccc47595ae29edf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2041618Reviewed-by: default avatarSean Kau <skau@chromium.org>
Reviewed-by: default avatarBartosz Fabianowski <bartfab@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Commit-Queue: Nikita Podguzov <nikitapodguzov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#740251}
parent ca0673ac
......@@ -243,8 +243,9 @@ std::string GetFilteredJSONPolicies(
std::string app_policy_string;
app_policy_value->GetAsString(&app_policy_string);
std::unique_ptr<base::DictionaryValue> app_policy_dict =
base::DictionaryValue::From(
base::JSONReader::ReadDeprecated(app_policy_string));
base::DictionaryValue::From(base::JSONReader::ReadDeprecated(
app_policy_string,
base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS));
if (app_policy_dict) {
// Need a deep copy of all values here instead of doing a swap, because
// JSONReader::Read constructs a dictionary whose StringValues are
......
......@@ -71,8 +71,8 @@ base::Optional<EcryptfsMigrationAction> DecodeMigrationActionFromPolicy(
std::set<std::string> GetRequestedPackagesFromArcPolicy(
const std::string& arc_policy) {
std::unique_ptr<base::Value> dict =
base::JSONReader::ReadDeprecated(arc_policy);
std::unique_ptr<base::Value> dict = base::JSONReader::ReadDeprecated(
arc_policy, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (!dict || !dict->is_dict())
return {};
......
......@@ -53,8 +53,8 @@ std::unique_ptr<PrinterCache> ParsePrinters(std::unique_ptr<std::string> data) {
base::BlockingType::MAY_BLOCK);
std::unique_ptr<base::Value> json_blob =
base::JSONReader::ReadAndReturnErrorDeprecated(
*data, base::JSONParserOptions::JSON_PARSE_RFC, &error_code,
nullptr /* error_msg_out */, &error_line);
*data, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS,
&error_code, nullptr /* error_msg_out */, &error_line);
// It's not valid JSON. Give up.
if (!json_blob || !json_blob->is_list()) {
LOG(WARNING) << "Failed to parse printers policy (" << error_code
......
......@@ -54,7 +54,7 @@ TaskResults ParseData(int task_id, std::unique_ptr<std::string> data) {
base::BlockingType::MAY_BLOCK);
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
*data, base::JSONParserOptions::JSON_PARSE_RFC);
*data, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (value_with_error.error_code != base::JSONReader::JSON_NO_ERROR) {
LOG(WARNING) << "Failed to parse print servers policy ("
<< value_with_error.error_message << ") on line "
......
......@@ -169,7 +169,9 @@ std::unique_ptr<base::Value> PolicyConverter::ConvertValueToSchema(
std::string string_value;
if (value->GetAsString(&string_value)) {
std::unique_ptr<base::Value> decoded_value =
base::JSONReader::ReadDeprecated(string_value);
base::JSONReader::ReadDeprecated(
string_value,
base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (decoded_value)
return decoded_value;
}
......
......@@ -398,8 +398,8 @@ bool ComponentCloudPolicyStore::ParsePolicy(const std::string& data,
std::string json_reader_error_message;
std::unique_ptr<base::Value> json =
base::JSONReader::ReadAndReturnErrorDeprecated(
data, base::JSON_PARSE_RFC, nullptr /* error_code_out */,
&json_reader_error_message);
data, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS,
nullptr /* error_code_out */, &json_reader_error_message);
base::DictionaryValue* dict = nullptr;
if (!json) {
LOG(ERROR) << "Invalid JSON blob: " << json_reader_error_message;
......
......@@ -124,8 +124,8 @@ std::unique_ptr<base::Value> ConvertRegistryValue(const base::Value& value,
case base::Value::Type::DICTIONARY: {
// Dictionaries may be encoded as JSON strings.
if (value.GetAsString(&string_value)) {
std::unique_ptr<base::Value> result =
base::JSONReader::ReadDeprecated(string_value);
std::unique_ptr<base::Value> result = base::JSONReader::ReadDeprecated(
string_value, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
if (result && result->type() == schema.type())
return result;
}
......
......@@ -1433,10 +1433,10 @@ std::unique_ptr<base::Value> Schema::ParseToDictAndValidate(
const std::string& schema,
int validator_options,
std::string* error) {
base::JSONParserOptions json_options = base::JSON_ALLOW_TRAILING_COMMAS;
std::unique_ptr<base::Value> json =
base::JSONReader::ReadAndReturnErrorDeprecated(schema, json_options,
nullptr, error);
base::JSONReader::ReadAndReturnErrorDeprecated(
schema, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS, nullptr,
error);
if (!json)
return nullptr;
if (!json->is_dict()) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment