Commit da9ae363 authored by Gayane Petrosyan's avatar Gayane Petrosyan Committed by Commit Bot

Trial and group names can have '.' which should be escaped properly.

When trial and group names are outputed to string special characters are
escaped. However '.' and '*' are not escaped.

Bug: 694675
Change-Id: I52583ee8f05f0dd49c6a4b28b03881054014625a
Reviewed-on: https://chromium-review.googlesource.com/988267
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554866}
parent fc8bb6dd
......@@ -21,12 +21,6 @@
namespace variations {
namespace {
std::string UnescapeValue(const std::string& value) {
return net::UnescapeURLComponent(
value, net::UnescapeRule::PATH_SEPARATORS |
net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS);
}
void AssociateParamsFromExperiment(
const std::string& study_name,
const FieldTrialTestingExperiment& experiment,
......@@ -83,10 +77,30 @@ const FieldTrialTestingExperiment& ChooseExperiment(
} // namespace
std::string UnescapeValue(const std::string& value) {
return net::UnescapeURLComponent(
value, net::UnescapeRule::PATH_SEPARATORS |
net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS);
}
std::string EscapeValue(const std::string& value) {
// This needs to be the inverse of UnescapeValue in the anonymous namespace
// above.
return net::EscapeQueryParamValue(value, true /* use_plus */);
std::string net_escaped_str =
net::EscapeQueryParamValue(value, true /* use_plus */);
// net doesn't escape '.' and '*' but UnescapeValue() covers those cases.
std::string escaped_str;
escaped_str.reserve(net_escaped_str.length());
for (const char ch : net_escaped_str) {
if (ch == '.')
escaped_str.append("%2E");
else if (ch == '*')
escaped_str.append("%2A");
else
escaped_str.push_back(ch);
}
return escaped_str;
}
bool AssociateParamsFromString(const std::string& varations_string) {
......
......@@ -15,6 +15,9 @@ namespace variations {
struct FieldTrialTestingConfig;
// Unescapes special characters from the given string.
std::string UnescapeValue(const std::string& value);
// Escapes the trial name, or parameter name, or parameter value in a way that
// makes it usable within variations::switches::kForceFieldTrialParams.
std::string EscapeValue(const std::string& value);
......
......@@ -177,4 +177,15 @@ TEST_F(FieldTrialUtilTest, AssociateForcingFlagsFromFieldTrialConfig) {
EXPECT_EQ("ForcedGroup3", base::FieldTrialList::FindFullName("TestTrial3"));
}
TEST_F(FieldTrialUtilTest, TestEscapeValue) {
std::string str = "trail.:/,*";
std::string escaped_str = EscapeValue(str);
EXPECT_EQ(escaped_str.find('.'), std::string::npos);
EXPECT_EQ(escaped_str.find(':'), std::string::npos);
EXPECT_EQ(escaped_str.find('/'), std::string::npos);
EXPECT_EQ(escaped_str.find(','), std::string::npos);
EXPECT_EQ(escaped_str.find('*'), std::string::npos);
EXPECT_EQ(str, UnescapeValue(escaped_str));
}
} // namespace variations
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