From 5cbd351f12d22d57115f2da4ad8126a6e1768dff Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Fri, 22 Jul 2022 20:05:03 -0400 Subject: [PATCH 1/5] Fix compiler issue for older versions of C++/WinRT (#10278) * Fix compiler issue for older versions of C++/WinRT In older versions of cppwinrt, the header generated for Windows.Foundation.Collections.h do not include the begin and end methods for `IIterable`. This just switches the logic for compatibility with older versions of cppwinrt. Note, it may also include a bug fix where a comma separator is not emitted between headers from the request header collection and headers from the content header collection. * Change files --- ...-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json | 7 ++++++ .../Networking/OriginPolicyHttpFilter.cpp | 25 +++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json diff --git a/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json b/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json new file mode 100644 index 00000000000..78ad6dd4a05 --- /dev/null +++ b/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fix compiler issue for older versions of C++/WinRT", + "packageName": "react-native-windows", + "email": "erozell@outlook.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp b/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp index 15d23ec1b5b..65079ac5d45 100644 --- a/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp +++ b/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp @@ -651,21 +651,26 @@ ResponseOperation OriginPolicyHttpFilter::SendPreflightAsync(HttpRequestMessage preflightRequest.Headers().Insert(L"Access-Control-Request-Method", coRequest.Method().ToString()); auto headerNames = wstring{}; - auto headerItr = coRequest.Headers().begin(); - if (headerItr != coRequest.Headers().end()) { - headerNames += (*headerItr).Key(); + auto writeSeparator = false; + for (const auto &header : coRequest.Headers()) { + if (writeSeparator) { + headerNames += L", "; + } else { + writeSeparator = true; + } - while (++headerItr != coRequest.Headers().end()) - headerNames += L", " + (*headerItr).Key(); + headerNames += header.Key(); } if (coRequest.Content()) { - headerItr = coRequest.Content().Headers().begin(); - if (headerItr != coRequest.Content().Headers().end()) { - headerNames += (*headerItr).Key(); + for (const auto &header : coRequest.Content().Headers()) { + if (writeSeparator) { + headerNames += L", "; + } else { + writeSeparator = true; + } - while (++headerItr != coRequest.Content().Headers().end()) - headerNames += L", " + (*headerItr).Key(); + headerNames += header.Key(); } } From d70fb4bc4cfcdb215ad28844276ba5abe17dae37 Mon Sep 17 00:00:00 2001 From: "Julio C. Rocha" Date: Tue, 20 Dec 2022 22:46:56 -0800 Subject: [PATCH 2/5] Remove change file --- ...ative-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json diff --git a/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json b/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json deleted file mode 100644 index 78ad6dd4a05..00000000000 --- a/change/react-native-windows-4559f97f-bb39-4fe8-a1a9-c3e950ae966e.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "prerelease", - "comment": "Fix compiler issue for older versions of C++/WinRT", - "packageName": "react-native-windows", - "email": "erozell@outlook.com", - "dependentChangeType": "patch" -} From 073c29316b1e006144f3eecacdfa11aff33ca527 Mon Sep 17 00:00:00 2001 From: "Julio C. Rocha" Date: Tue, 20 Dec 2022 22:58:47 -0800 Subject: [PATCH 3/5] Change files --- ...ative-windows-129c1704-256e-40c9-9ba0-5e88391a0656.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/react-native-windows-129c1704-256e-40c9-9ba0-5e88391a0656.json diff --git a/change/react-native-windows-129c1704-256e-40c9-9ba0-5e88391a0656.json b/change/react-native-windows-129c1704-256e-40c9-9ba0-5e88391a0656.json new file mode 100644 index 00000000000..cfafdf51c9e --- /dev/null +++ b/change/react-native-windows-129c1704-256e-40c9-9ba0-5e88391a0656.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "Fix compiler issue for older versions of C++/WinRT (#10278)", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +} From 21ea08581d641ee8b9acf2607045e6d1b3b0a26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar=20Rocha?= Date: Wed, 21 Dec 2022 14:34:04 -0800 Subject: [PATCH 4/5] Test Access-Control-Request-Headers (#11031) * Add test ValidatePreflightResponseMainAndContentHeadersSucceeds * Allow resetting static origin --- ...-02bc19fb-606a-49b9-b555-145c15e79849.json | 7 +++ .../OriginPolicyHttpFilterTest.cpp | 52 +++++++++++++++++++ .../Networking/OriginPolicyHttpFilter.cpp | 2 + 3 files changed, 61 insertions(+) create mode 100644 change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json diff --git a/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json b/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json new file mode 100644 index 00000000000..13cb99c9391 --- /dev/null +++ b/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Test Access-Control-Request-Headers", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Desktop.UnitTests/OriginPolicyHttpFilterTest.cpp b/vnext/Desktop.UnitTests/OriginPolicyHttpFilterTest.cpp index a31afb0ff05..7bf22675485 100644 --- a/vnext/Desktop.UnitTests/OriginPolicyHttpFilterTest.cpp +++ b/vnext/Desktop.UnitTests/OriginPolicyHttpFilterTest.cpp @@ -4,19 +4,28 @@ #include #include +#include +#include "WinRTNetworkingMocks.h" // Windows API #include #include using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace winrt::Windows::Web::Http; using Microsoft::React::Networking::OriginPolicyHttpFilter; +using Microsoft::React::Networking::RequestArgs; +using Microsoft::React::Networking::ResponseOperation; using winrt::Windows::Foundation::Uri; namespace Microsoft::React::Test { TEST_CLASS (OriginPolicyHttpFilterTest) { + TEST_CLASS_INITIALIZE(Initialize) { + winrt::uninit_apartment(); + } + // TEMP tests to see if Uri has comparison capabilities TEST_METHOD(UrlsHaveSameOrigin) { // clang-format off @@ -242,6 +251,49 @@ TEST_CLASS (OriginPolicyHttpFilterTest) { Assert::AreEqual(2, static_cast(response.Headers().Size())); } } + + TEST_METHOD(ValidatePreflightResponseMainAndContentHeadersSucceeds) { + auto mockFilter = winrt::make(); + mockFilter.as()->Mocks.SendRequestAsync = + [](HttpRequestMessage const &request) -> ResponseOperation { + HttpResponseMessage response{}; + + response.StatusCode(HttpStatusCode::Ok); + response.Headers().Insert(L"Access-Control-Allow-Origin", L"*"); + // Return allowed headers as requested by client + response.Headers().Insert( + L"Access-Control-Allow-Headers", request.Headers().Lookup(L"Access-Control-Request-Headers")); + + co_return response; + }; + + auto reqArgs = winrt::make(); + auto request = HttpRequestMessage(HttpMethod::Get(), Uri{L"http://somehost"}); + request.Properties().Insert(L"RequestArgs", reqArgs); + request.Headers().TryAppendWithoutValidation(L"Authorization", L"Bearer abc"); + // Should implicitly set Conent-Length and Content-Type + request.Content(HttpStringContent{L"PreflightContent"}); + + auto filter = winrt::make(mockFilter); + auto opFilter = filter.as(); + + OriginPolicyHttpFilter::SetStaticOrigin("http://somehost"); + try { + auto sendOp = opFilter->SendPreflightAsync(request); + sendOp.get(); + + auto response = sendOp.GetResults(); + opFilter->ValidatePreflightResponse(request, response); + + OriginPolicyHttpFilter::SetStaticOrigin({}); + Assert::AreEqual( + L"Authorization, Content-Length, Content-Type", + response.Headers().Lookup(L"Access-Control-Allow-Headers").c_str()); + } catch (const winrt::hresult_error &e) { + OriginPolicyHttpFilter::SetStaticOrigin({}); + Assert::Fail(e.message().c_str()); + } + } }; } // namespace Microsoft::React::Test diff --git a/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp b/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp index 15d23ec1b5b..c0185a6936a 100644 --- a/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp +++ b/vnext/Shared/Networking/OriginPolicyHttpFilter.cpp @@ -113,6 +113,8 @@ bool OriginPolicyHttpFilter::ConstWcharComparer::operator()(const wchar_t *a, co /*static*/ void OriginPolicyHttpFilter::SetStaticOrigin(std::string &&url) { if (!url.empty()) s_origin = Uri{to_hstring(url)}; + else + s_origin = nullptr; } /*static*/ bool OriginPolicyHttpFilter::IsSameOrigin(Uri const &u1, Uri const &u2) noexcept { From be46b12e51e788ba52b3c3da89b1b0405dfb77bf Mon Sep 17 00:00:00 2001 From: "Julio C. Rocha" Date: Wed, 21 Dec 2022 15:06:03 -0800 Subject: [PATCH 5/5] Remove change file --- ...ative-windows-02bc19fb-606a-49b9-b555-145c15e79849.json | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json diff --git a/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json b/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json deleted file mode 100644 index 13cb99c9391..00000000000 --- a/change/react-native-windows-02bc19fb-606a-49b9-b555-145c15e79849.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "prerelease", - "comment": "Test Access-Control-Request-Headers", - "packageName": "react-native-windows", - "email": "julio.rocha@microsoft.com", - "dependentChangeType": "patch" -}