Skip to content

Commit 8224320

Browse files
authored
Escape caller in user agent header (microsoft#5998)
## Change Escape the caller string in the user agent header using UTF-8 `%` encoding. If we don't, invalid characters lead to failures to set the headers and a broken flow.
1 parent f19d806 commit 8224320

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/AppInstallerCLITests/HttpClientHelper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <winget/Certificates.h>
1010
#include <winget/HttpClientHelper.h>
1111
#include <CertificateResources.h>
12+
#include <winget/JsonUtil.h>
1213

1314
using namespace AppInstaller::Http;
1415
using namespace AppInstaller::Runtime;
@@ -79,3 +80,12 @@ TEST_CASE("HttpClientHelper_PinningConfiguration", "[RestSource]")
7980

8081
REQUIRE_THROWS_HR(helper.HandleGet(L"https://github.com"), APPINSTALLER_CLI_ERROR_PINNED_CERTIFICATE_MISMATCH);
8182
}
83+
84+
TEST_CASE("HttpClientHelper_CallerCharacters", "[RestSource]")
85+
{
86+
HttpClientHelper::HttpRequestHeaders headers;
87+
headers.emplace(web::http::header_names::user_agent, AppInstaller::JSON::GetUtilityString(AppInstaller::Runtime::GetUserAgent("\xe6\xb5\x8b\xe8\xaf\x95")));
88+
89+
HttpClientHelper helper;
90+
REQUIRE_THROWS_HR(helper.HandleGet(L"https://github.com", headers), APPINSTALLER_CLI_ERROR_RESTAPI_UNSUPPORTED_MIME_TYPE);
91+
}

src/AppInstallerCommonCore/Runtime.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,11 @@ namespace AppInstaller::Runtime
572572

573573
Utility::LocIndString GetUserAgent(std::string_view caller)
574574
{
575+
auto escapedCaller = winrt::Windows::Foundation::Uri::EscapeComponent(Utility::ConvertToUTF16(caller));
576+
575577
std::ostringstream strstr;
576578
strstr <<
577-
caller <<
579+
Utility::ConvertToUTF8(escapedCaller) <<
578580
" WindowsPackageManager/" << GetClientVersion() <<
579581
" DesktopAppInstaller/" << GetPackageVersion();
580582
return Utility::LocIndString{ strstr.str() };

0 commit comments

Comments
 (0)