Commit d9268409 authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

[Nearby] Obfuscate text preview for URL and Phone

Obfuscation is needed as the text is shown as a preview on the remote
device before accepting the transfer.

Bug: 1085067
Change-Id: I100d369685c5aec2b7bcb97058df68f6c94c4c6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2368594
Commit-Queue: Richard Knoll <knollr@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800736}
parent f08cb837
......@@ -2,18 +2,89 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <utility>
#include "base/strings/strcat.h"
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include "url/gurl.h"
namespace {
constexpr size_t kMaxPreviewTextLength = 32;
// Tries to get a valid host name from the |text|. Returns nullopt otherwise.
base::Optional<std::string> GetHostFromText(const std::string& text) {
GURL url(text);
if (!url.is_valid() || !url.has_host())
return base::nullopt;
return url.host();
}
// Masks the given |number| depending on the string length:
// - length <= 4: Masks all characters
// - 4 < length <= 6 Masks the last 4 characters
// - 6 < length <= 10: Skips the first 2 and masks the following 4 characters
// - length > 10: Skips the first 2 and last 4 characters. Masks the rest of
// the string
// Note: We're assuming a formatted phone number and won't try to reformat to
// E164 like on Android as there's no easy way of determining the intended
// region for the phone number.
std::string MaskPhoneNumber(const std::string& number) {
constexpr int kMinMaskedDigits = 4;
constexpr int kMaxLeadingDigits = 2;
constexpr int kMaxTailingDigits = 4;
constexpr int kLengthWithNoLeadingDigits = kMinMaskedDigits;
constexpr int kLengthWithNoTailingDigits =
kMinMaskedDigits + kMaxLeadingDigits;
if (number.empty())
return number;
std::string result = number;
bool has_plus = false;
if (number[0] == '+') {
result = number.substr(1);
has_plus = true;
}
// First calculate how many digits we would mask having exactly
// kMinMaskedDigits digits masked.
int leading_digits = 0;
if (result.length() > kLengthWithNoLeadingDigits)
leading_digits = result.length() - kLengthWithNoLeadingDigits;
int tailing_digits = 0;
if (result.length() > kLengthWithNoTailingDigits)
tailing_digits = result.length() - kLengthWithNoTailingDigits;
// Now limit resulting numbers of digits to maximally allowed values.
leading_digits = std::min(kMaxLeadingDigits, leading_digits);
tailing_digits = std::min(kMaxTailingDigits, tailing_digits);
int masked_digits = result.length() - leading_digits - tailing_digits;
return base::StrCat({(has_plus ? "+" : ""), result.substr(0, leading_digits),
std::string(masked_digits, 'x'),
result.substr(result.length() - tailing_digits)});
}
std::string GetTextTitle(const std::string& text_body,
TextAttachment::Type type) {
// TODO(crbug.com/1085067): Improve title based on |type|.
constexpr size_t kMaxPreviewTextLength = 32;
switch (type) {
case TextAttachment::Type::kUrl: {
base::Optional<std::string> host = GetHostFromText(text_body);
if (host)
return *host;
break;
}
case TextAttachment::Type::kPhoneNumber:
return MaskPhoneNumber(text_body);
default:
break;
}
if (text_body.size() > kMaxPreviewTextLength)
return base::StrCat({text_body.substr(0, kMaxPreviewTextLength), "…"});
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include <string>
#include "testing/gtest/include/gtest/gtest.h"
namespace {
struct TextAttachmentTextTitleTestData {
TextAttachment::Type type;
std::string text_body;
std::string expected_text_title;
} kTextAttachmentTextTitleTestData[] = {
{TextAttachment::Type::kText, "Short text", "Short text"},
{TextAttachment::Type::kText, "Long text that should be truncated",
"Long text that should be truncat…"},
{TextAttachment::Type::kUrl,
"https://www.google.com/maps/search/restaurants/@/"
"data=!3m1!4b1?disco_ad=1234",
"www.google.com"},
{TextAttachment::Type::kUrl, "Invalid URL", "Invalid URL"},
{TextAttachment::Type::kPhoneNumber, "1234", "xxxx"},
{TextAttachment::Type::kPhoneNumber, "+1234", "+xxxx"},
{TextAttachment::Type::kPhoneNumber, "123456", "12xxxx"},
{TextAttachment::Type::kPhoneNumber, "12345678", "12xxxx78"},
{TextAttachment::Type::kPhoneNumber, "+447123456789", "+44xxxxxx6789"},
{TextAttachment::Type::kPhoneNumber,
"+1255555555555555555555555555555555555555555555555556789",
"+12xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6789"},
{TextAttachment::Type::kPhoneNumber, "+16196784004", "+16xxxxx4004"},
{TextAttachment::Type::kPhoneNumber, "+3841235782564", "+38xxxxxxx2564"},
{TextAttachment::Type::kPhoneNumber, "+12345678901", "+12xxxxx8901"},
{TextAttachment::Type::kPhoneNumber, "+1234567891", "+12xxxx7891"},
{TextAttachment::Type::kPhoneNumber, "+123456789", "+12xxxx789"},
{TextAttachment::Type::kPhoneNumber, "+12345678", "+12xxxx78"},
{TextAttachment::Type::kPhoneNumber, "+1234567", "+12xxxx7"},
{TextAttachment::Type::kPhoneNumber, "+123456", "+12xxxx"},
{TextAttachment::Type::kPhoneNumber, "+12345", "+1xxxx"},
{TextAttachment::Type::kPhoneNumber, "+1234", "+xxxx"},
{TextAttachment::Type::kPhoneNumber, "+123", "+xxx"},
{TextAttachment::Type::kPhoneNumber, "+12", "+xx"},
{TextAttachment::Type::kPhoneNumber, "+1", "+x"},
{TextAttachment::Type::kPhoneNumber,
"1255555555555555555555555555555555555555555555555556789",
"12xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6789"},
{TextAttachment::Type::kPhoneNumber, "12345678901", "12xxxxx8901"},
{TextAttachment::Type::kPhoneNumber, "1234567891", "12xxxx7891"},
{TextAttachment::Type::kPhoneNumber, "123456789", "12xxxx789"},
{TextAttachment::Type::kPhoneNumber, "12345678", "12xxxx78"},
{TextAttachment::Type::kPhoneNumber, "1234567", "12xxxx7"},
{TextAttachment::Type::kPhoneNumber, "123456", "12xxxx"},
{TextAttachment::Type::kPhoneNumber, "12345", "1xxxx"},
{TextAttachment::Type::kPhoneNumber, "1234", "xxxx"},
{TextAttachment::Type::kPhoneNumber, "123", "xxx"},
{TextAttachment::Type::kPhoneNumber, "12", "xx"},
{TextAttachment::Type::kPhoneNumber, "1", "x"},
{TextAttachment::Type::kPhoneNumber, "+", "+"},
};
using TextAttachmentTextTitleTest =
testing::TestWithParam<TextAttachmentTextTitleTestData>;
} // namespace
TEST_P(TextAttachmentTextTitleTest, TextTitleMatches) {
TextAttachment attachment(GetParam().type, GetParam().text_body);
EXPECT_EQ(GetParam().expected_text_title, attachment.text_title());
}
INSTANTIATE_TEST_CASE_P(TextAttachmentTextTitleTest,
TextAttachmentTextTitleTest,
testing::ValuesIn(kTextAttachmentTextTitleTestData));
......@@ -3776,6 +3776,7 @@ test("unit_tests") {
"../browser/nearby_sharing/nearby_sharing_service_impl_unittest.cc",
"../browser/nearby_sharing/paired_key_verification_runner_unittest.cc",
"../browser/nearby_sharing/payload_tracker_unittest.cc",
"../browser/nearby_sharing/text_attachment_unittest.cc",
"../browser/nearby_sharing/webrtc_signaling_messenger_unittest.cc",
"../browser/password_manager/generated_password_leak_detection_pref_unittest.cc",
"../browser/performance_manager/test_support/page_discarding_utils.cc",
......
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