Commit 5044d2a4 authored by Sean Kau's avatar Sean Kau Committed by Commit Bot

Reject printers with invalid uri when parsing JSON.

Previous behavior was to accept the uri and CUPS would reject it when we
try to use it.  Received a report of this happening in TT.
This will help to surface the root cause for users.

Bug: 844521
Change-Id: I77a20232a7317ee40c7c3c0fa534d73d9aad2b4b
Reviewed-on: https://chromium-review.googlesource.com/1066392
Commit-Queue: Sean Kau <skau@chromium.org>
Reviewed-by: default avatarXiaoqian Dai <xdai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#560912}
parent c95deeda
......@@ -34,6 +34,24 @@ const char kUUID[] = "uuid";
const char kPpdResource[] = "ppd_resource";
const char kGuid[] = "guid";
// Returns true if the uri was retrieved, is valid, and was set on |printer|.
// Returns false otherwise.
bool SetUri(const DictionaryValue& dict, Printer* printer) {
std::string uri;
if (!dict.GetString(kUri, &uri)) {
LOG(WARNING) << "Uri required";
return false;
}
if (!chromeos::ParseUri(uri).has_value()) {
LOG(WARNING) << "Uri is malformed";
return false;
}
printer->set_uri(uri);
return true;
}
// Populates the |printer| object with corresponding fields from |value|.
// Returns false if |value| is missing a required field.
bool DictionaryToPrinter(const DictionaryValue& value, Printer* printer) {
......@@ -46,11 +64,7 @@ bool DictionaryToPrinter(const DictionaryValue& value, Printer* printer) {
return false;
}
std::string uri;
if (value.GetString(kUri, &uri)) {
printer->set_uri(uri);
} else {
LOG(WARNING) << "Uri required";
if (!SetUri(value, printer)) {
return false;
}
......
......@@ -129,6 +129,19 @@ TEST(PrinterTranslatorTest, MissingEffectiveMakeModelFails) {
EXPECT_FALSE(printer);
}
TEST(PrinterTranslatorTest, InvalidUriFails) {
base::DictionaryValue preference;
preference.SetString("id", kHash);
preference.SetString("display_name", kName);
preference.SetString("ppd_resource.effective_model", kEffectiveMakeAndModel);
// uri with dangling colon
preference.SetString("uri", "ipp://hostname.tld:");
std::unique_ptr<Printer> printer = RecommendedPrinterToPrinter(preference);
EXPECT_FALSE(printer);
}
TEST(PrinterTranslatorTest, RecommendedPrinterMinimalSetup) {
base::DictionaryValue preference;
preference.SetString("id", kHash);
......
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