From 4d1c5ebdd2d566677c3716d909d06bfb3bd2c021 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 27 Sep 2021 13:36:08 +0300 Subject: [PATCH] Fix CefCommandLine character case requirements (fixes issue #1872) Switch names will now be converted to lowercase ASCII on all platforms. Switch values will retain the original case and UTF8 encoding. --- include/capi/cef_command_line_capi.h | 7 +++-- include/cef_command_line.h | 5 ++-- libcef/common/command_line_impl.cc | 6 ++-- patch/patch.cfg | 5 ++++ patch/patches/base_command_line_1872.patch | 17 +++++++++++ tests/ceftests/command_line_unittest.cc | 33 ++++++++++++++++------ 6 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 patch/patches/base_command_line_1872.patch diff --git a/include/capi/cef_command_line_capi.h b/include/capi/cef_command_line_capi.h index df70b2a02..fb7a276c8 100644 --- a/include/capi/cef_command_line_capi.h +++ b/include/capi/cef_command_line_capi.h @@ -33,7 +33,7 @@ // by hand. See the translator.README.txt file in the tools directory for // more information. // -// $hash=5af5bf1e877dd53f21f751d332a9e2166817324c$ +// $hash=3ecebd6b30bb8fb837e062eacd021c1a1ff3620a$ // #ifndef CEF_INCLUDE_CAPI_CEF_COMMAND_LINE_CAPI_H_ @@ -53,8 +53,9 @@ extern "C" { // optionally have a value specified using the '=' delimiter (e.g. // "-switch=value"). An argument of "--" will terminate switch parsing with all // subsequent tokens, regardless of prefix, being interpreted as non-switch -// arguments. Switch names are considered case-insensitive. This structure can -// be used before cef_initialize() is called. +// arguments. Switch names should be lowercase ASCII and will be converted to +// such if necessary. Switch values will retain the original case and UTF8 +// encoding. This structure can be used before cef_initialize() is called. /// typedef struct _cef_command_line_t { /// diff --git a/include/cef_command_line.h b/include/cef_command_line.h index dd5491cd2..45cc20067 100644 --- a/include/cef_command_line.h +++ b/include/cef_command_line.h @@ -49,8 +49,9 @@ // optionally have a value specified using the '=' delimiter (e.g. // "-switch=value"). An argument of "--" will terminate switch parsing with all // subsequent tokens, regardless of prefix, being interpreted as non-switch -// arguments. Switch names are considered case-insensitive. This class can be -// used before CefInitialize() is called. +// arguments. Switch names should be lowercase ASCII and will be converted to +// such if necessary. Switch values will retain the original case and UTF8 +// encoding. This class can be used before CefInitialize() is called. /// /*--cef(source=library,no_debugct_check)--*/ class CefCommandLine : public virtual CefBaseRefCounted { diff --git a/libcef/common/command_line_impl.cc b/libcef/common/command_line_impl.cc index ba63c2985..acb71aa5b 100644 --- a/libcef/common/command_line_impl.cc +++ b/libcef/common/command_line_impl.cc @@ -6,6 +6,7 @@ #include "base/files/file_path.h" #include "base/logging.h" +#include "base/strings/string_util.h" CefCommandLineImpl::CefCommandLineImpl(base::CommandLine* value, bool will_delete, @@ -90,12 +91,13 @@ bool CefCommandLineImpl::HasSwitches() { bool CefCommandLineImpl::HasSwitch(const CefString& name) { CEF_VALUE_VERIFY_RETURN(false, false); - return const_value().HasSwitch(name.ToString()); + return const_value().HasSwitch(base::ToLowerASCII(name.ToString())); } CefString CefCommandLineImpl::GetSwitchValue(const CefString& name) { CEF_VALUE_VERIFY_RETURN(false, CefString()); - return const_value().GetSwitchValueNative(name.ToString()); + return const_value().GetSwitchValueNative( + base::ToLowerASCII(name.ToString())); } void CefCommandLineImpl::GetSwitches(SwitchMap& switches) { diff --git a/patch/patch.cfg b/patch/patch.cfg index 44198e584..4fcf37b93 100644 --- a/patch/patch.cfg +++ b/patch/patch.cfg @@ -483,6 +483,11 @@ patches = [ # https://bitbucket.org/chromiumembedded/cef/issues/123 'name': 'print_preview_123', }, + { + # Store command-line switch names as lower-case ASCII on all platforms. + # https://bitbucket.org/chromiumembedded/cef/issues/1872 + 'name': 'base_command_line_1872', + }, { # Remove cef_sandbox dependency on boringssl MD5/SHA1 functions. # https://bitbucket.org/chromiumembedded/cef/issues/2743 diff --git a/patch/patches/base_command_line_1872.patch b/patch/patches/base_command_line_1872.patch new file mode 100644 index 000000000..900e2dd60 --- /dev/null +++ b/patch/patches/base_command_line_1872.patch @@ -0,0 +1,17 @@ +diff --git base/command_line.cc base/command_line.cc +index 3ef2e87e40687..f7b91a202a377 100644 +--- base/command_line.cc ++++ base/command_line.cc +@@ -333,11 +333,10 @@ void CommandLine::AppendSwitchPath(StringPiece switch_string, + + void CommandLine::AppendSwitchNative(StringPiece switch_string, + CommandLine::StringPieceType value) { +-#if defined(OS_WIN) + const std::string switch_key = ToLowerASCII(switch_string); ++#if defined(OS_WIN) + StringType combined_switch_string(UTF8ToWide(switch_key)); + #elif defined(OS_POSIX) || defined(OS_FUCHSIA) +- StringPiece switch_key = switch_string; + StringType combined_switch_string(switch_key); + #endif + size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); diff --git a/tests/ceftests/command_line_unittest.cc b/tests/ceftests/command_line_unittest.cc index abb71c488..978ff1f3b 100644 --- a/tests/ceftests/command_line_unittest.cc +++ b/tests/ceftests/command_line_unittest.cc @@ -18,9 +18,10 @@ void VerifyCommandLine(CefRefPtr command_line, EXPECT_TRUE(command_line->HasSwitch("switch1")); std::string switch1 = command_line->GetSwitchValue("switch1"); EXPECT_EQ("", switch1); - EXPECT_TRUE(command_line->HasSwitch("switch2")); - std::string switch2 = command_line->GetSwitchValue("switch2"); - EXPECT_EQ("val2", switch2); + // Switch names are converted to lowercase but values are left unchanged. + EXPECT_TRUE(command_line->HasSwitch("SWITCH2")); + std::string switch2 = command_line->GetSwitchValue("SWITCH2"); + EXPECT_EQ("VAL2", switch2); EXPECT_TRUE(command_line->HasSwitch("switch3")); std::string switch3 = command_line->GetSwitchValue("switch3"); EXPECT_EQ("val3", switch3); @@ -45,7 +46,7 @@ void VerifyCommandLine(CefRefPtr command_line, EXPECT_EQ("", val); } else if (name == "switch2") { has2 = true; - EXPECT_EQ("val2", val); + EXPECT_EQ("VAL2", val); } else if (name == "switch3") { has3 = true; EXPECT_EQ("val3", val); @@ -86,10 +87,10 @@ TEST(CommandLineTest, Init) { #if defined(OS_WIN) command_line->InitFromString( - "test.exe --switch1 -switch2=val2 /switch3=val3 " + "test.exe --switch1 -switch2=VAL2 /switch3=val3 " "-switch4=\"val 4\" arg1 \"arg 2\""); #else - const char* args[] = {"test.exe", "--switch1", "-switch2=val2", + const char* args[] = {"test.exe", "--switch1", "-switch2=VAL2", "-switch3=val3", "-switch4=val 4", "arg1", "arg 2"}; command_line->InitFromArgv(sizeof(args) / sizeof(char*), args); @@ -105,7 +106,7 @@ TEST(CommandLineTest, Manual) { command_line->SetProgram("test.exe"); command_line->AppendSwitch("switch1"); - command_line->AppendSwitchWithValue("switch2", "val2"); + command_line->AppendSwitchWithValue("switch2", "VAL2"); command_line->AppendSwitchWithValue("switch3", "val3"); command_line->AppendSwitchWithValue("switch4", "val 4"); command_line->AppendArgument("arg1"); @@ -121,7 +122,7 @@ TEST(CommandLineTest, IgnorePrefixes) { command_line->SetProgram("test.exe"); command_line->AppendSwitch("-switch1"); - command_line->AppendSwitchWithValue("--switch2", "val2"); + command_line->AppendSwitchWithValue("--switch2", "VAL2"); command_line->AppendSwitchWithValue("-switch3", "val3"); command_line->AppendSwitchWithValue("-switch4", "val 4"); @@ -133,3 +134,19 @@ TEST(CommandLineTest, IgnorePrefixes) { VerifyCommandLine(command_line, arg1, arg2); } + +// Test that command line switch names are converted to lowercase ASCII. +TEST(CommandLineTest, IgnoreCase) { + CefRefPtr command_line = CefCommandLine::CreateCommandLine(); + EXPECT_TRUE(command_line.get() != nullptr); + + command_line->SetProgram("test.exe"); + command_line->AppendSwitch("-Switch1"); + command_line->AppendSwitchWithValue("-SWITCH2", "VAL2"); + command_line->AppendSwitchWithValue("-switch3", "val3"); + command_line->AppendSwitchWithValue("-switch4", "val 4"); + command_line->AppendArgument("arg1"); + command_line->AppendArgument("arg 2"); + + VerifyCommandLine(command_line); +}