-
Notifications
You must be signed in to change notification settings - Fork 5
fix: Preserve percent-encoding in HttpRequest targets and query construction #614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kinyoklion
wants to merge
4
commits into
main
Choose a base branch
from
rlamb/fix-http-request-encoded-target
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3f07c60
fix: Preserve percent-encoding in HttpRequest targets and appended paths
kinyoklion 84f0ac3
fix: Append FDv1 polling query parameters with the URL library
kinyoklion 5beaafe
test: Check path escapes that every supported Boost release preserves
kinyoklion 494b73b
docs: Drop the base64url example from the AppendUrl comment
kinyoklion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // The Beast backend sends HttpRequest::Path() verbatim as the request target. | ||
| // These tests put percent-encoded URLs on the wire and check what a real HTTP | ||
| // parser at the other end sees. | ||
| #ifndef LD_CURL_NETWORKING | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <launchdarkly/config/shared/builders/http_properties_builder.hpp> | ||
| #include <launchdarkly/config/shared/sdks.hpp> | ||
| #include <launchdarkly/network/asio_requester.hpp> | ||
|
|
||
| #include <boost/asio/io_context.hpp> | ||
| #include <boost/asio/ip/tcp.hpp> | ||
| #include <boost/beast/core.hpp> | ||
| #include <boost/beast/http.hpp> | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
|
|
||
| using launchdarkly::config::shared::ClientSDK; | ||
| using launchdarkly::config::shared::builders::HttpPropertiesBuilder; | ||
| using launchdarkly::config::shared::built::HttpProperties; | ||
| using launchdarkly::network::AsioRequester; | ||
| using launchdarkly::network::HttpMethod; | ||
| using launchdarkly::network::HttpRequest; | ||
| using launchdarkly::network::HttpResult; | ||
| using namespace std::chrono_literals; | ||
|
|
||
| namespace { | ||
|
|
||
| HttpProperties Props() { | ||
| return HttpPropertiesBuilder<ClientSDK>() | ||
| .ConnectTimeout(2s) | ||
| .ResponseTimeout(2s) | ||
| .Build(); | ||
| } | ||
|
|
||
| // Accepts one connection on an ephemeral loopback port, records the request | ||
| // as Beast parsed it, answers 200, and closes the socket. | ||
| class OneShotServer : public std::enable_shared_from_this<OneShotServer> { | ||
| public: | ||
| explicit OneShotServer(net::io_context& ioc) | ||
| : acceptor_(ioc, tcp::endpoint(net::ip::make_address("127.0.0.1"), 0)), | ||
| socket_(ioc) { | ||
| response_.result(http::status::ok); | ||
| response_.set(http::field::content_type, "application/json"); | ||
| response_.body() = "{}"; | ||
| response_.prepare_payload(); | ||
| } | ||
|
|
||
| unsigned short Port() const { return acceptor_.local_endpoint().port(); } | ||
|
|
||
| void Start() { | ||
| acceptor_.async_accept( | ||
| socket_, | ||
| [self = shared_from_this()](boost::system::error_code const& ec) { | ||
| boost::system::error_code ignored; | ||
| self->acceptor_.close(ignored); | ||
| if (ec) { | ||
| return; | ||
| } | ||
| http::async_read( | ||
| self->socket_, self->buffer_, self->request_, | ||
| [self](boost::system::error_code const& ec, std::size_t) { | ||
| if (ec) { | ||
| self->read_error_ = ec.message(); | ||
| self->Close(); | ||
| return; | ||
| } | ||
| self->seen_ = self->request_; | ||
| http::async_write( | ||
| self->socket_, self->response_, | ||
| [self](boost::system::error_code const&, | ||
| std::size_t) { self->Close(); }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| std::optional<http::request<http::string_body>> const& Seen() const { | ||
| return seen_; | ||
| } | ||
|
|
||
| std::optional<std::string> const& ReadError() const { return read_error_; } | ||
|
|
||
| private: | ||
| void Close() { | ||
| boost::system::error_code ignored; | ||
| socket_.shutdown(tcp::socket::shutdown_both, ignored); | ||
| socket_.close(ignored); | ||
| } | ||
|
|
||
| tcp::acceptor acceptor_; | ||
| tcp::socket socket_; | ||
| beast::flat_buffer buffer_; | ||
| http::request<http::string_body> request_; | ||
| http::response<http::string_body> response_; | ||
| std::optional<http::request<http::string_body>> seen_; | ||
| std::optional<std::string> read_error_; | ||
| }; | ||
|
|
||
| // Sends one GET for the given target through the asio requester and returns | ||
| // the request exactly as the server parsed it. | ||
| std::optional<http::request<http::string_body>> RoundTrip( | ||
| std::string const& target) { | ||
| net::io_context ioc; | ||
| auto server = std::make_shared<OneShotServer>(ioc); | ||
| server->Start(); | ||
|
|
||
| auto props = Props(); | ||
| AsioRequester requester(ioc.get_executor(), props.Tls()); | ||
| std::optional<HttpResult> result; | ||
| requester.Request(HttpRequest("http://127.0.0.1:" + | ||
| std::to_string(server->Port()) + target, | ||
| HttpMethod::kGet, props, std::nullopt), | ||
| [&](HttpResult res) { result = std::move(res); }); | ||
| ioc.run_for(5s); | ||
|
|
||
| EXPECT_FALSE(server->ReadError().has_value()) | ||
| << "server could not parse the request: " << *server->ReadError(); | ||
| EXPECT_TRUE(result.has_value()); | ||
| if (result) { | ||
| EXPECT_FALSE(result->IsError()) << *result; | ||
| EXPECT_EQ(200u, result->Status()); | ||
| } | ||
| return server->Seen(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // A server-supplied value that a URL builder percent-encoded, such as the | ||
| // FDv2 "basis" selector state. Decoding it again on the way out would end | ||
| // the request line at the CR LF and turn the remainder into a header. | ||
| TEST(AsioRequesterTest, PercentEncodedQueryReachesTheServerIntact) { | ||
| std::string const target = | ||
| "/sdk/poll/eval?basis=x%0D%0AX-Injected:%201&f=a%26b%20c%23d"; | ||
|
|
||
| auto seen = RoundTrip(target); | ||
|
|
||
| ASSERT_TRUE(seen.has_value()); | ||
| EXPECT_EQ(target, seen->target()); | ||
| EXPECT_EQ(seen->end(), seen->find("X-Injected")); | ||
| } | ||
|
|
||
| // Path normalization may decode escapes of characters that are legal in a | ||
| // path (older Boost releases include %2F), so only characters that cannot | ||
| // appear raw in a request target are checked here. | ||
| TEST(AsioRequesterTest, PercentEncodedPathReachesTheServerIntact) { | ||
| std::string const target = "/ld%20relay/p%23q/sdk/latest-all"; | ||
|
|
||
| auto seen = RoundTrip(target); | ||
|
|
||
| ASSERT_TRUE(seen.has_value()); | ||
| EXPECT_EQ(target, seen->target()); | ||
| } | ||
|
|
||
| #endif // LD_CURL_NETWORKING |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels error-prone to me. I mean, "characters not allowed in a path are percent-encoded" implies that if you put
%in there, it would be encoded as%25. But this also says "Percent-encoding in the URL and in the appended path is preserved". So, it'll encode things except for percent signs? I generally think it's a good idea for APIs to either take an encoded string or a raw string, and be clear about it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be that it accepts non-encoded parameters and then they are encoded. As that is already what happens when it makes its way into the curl path. At least from the computed URLs I could see. Now when things happen at different points in time I could see needed internally to know that something was already encoded earlier. There aren't really any cases where we should be using anything that actually needs encoded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So then can we simplify this by not having it try to detect and special case things that are already encoded?