1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_proxy_client_socket.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <fuzzer/FuzzedDataProvider.h>
11
12 #include <memory>
13 #include <string>
14
15 #include "base/check_op.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "net/base/address_list.h"
18 #include "net/base/auth.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/network_anonymization_key.h"
21 #include "net/base/test_completion_callback.h"
22 #include "net/http/http_auth_cache.h"
23 #include "net/http/http_auth_handler_basic.h"
24 #include "net/http/http_auth_handler_digest.h"
25 #include "net/http/http_auth_handler_factory.h"
26 #include "net/http/http_auth_preferences.h"
27 #include "net/http/http_auth_scheme.h"
28 #include "net/log/net_log.h"
29 #include "net/log/test_net_log.h"
30 #include "net/socket/fuzzed_socket.h"
31 #include "net/socket/next_proto.h"
32 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
33
34 // Fuzzer for HttpProxyClientSocket only tests establishing a connection when
35 // using the proxy as a tunnel.
36 //
37 // |data| is used to create a FuzzedSocket to fuzz reads and writes, see that
38 // class for details.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)39 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
40 FuzzedDataProvider data_provider(data, size);
41 // Including an observer; even though the recorded results aren't currently
42 // used, it'll ensure the netlogging code is fuzzed as well.
43 net::RecordingNetLogObserver net_log_observer;
44
45 net::TestCompletionCallback callback;
46 auto fuzzed_socket =
47 std::make_unique<net::FuzzedSocket>(&data_provider, net::NetLog::Get());
48 CHECK_EQ(net::OK, fuzzed_socket->Connect(callback.callback()));
49
50 // Create auth handler supporting basic and digest schemes. Other schemes can
51 // make system calls, which doesn't seem like a great idea.
52 net::HttpAuthCache auth_cache(
53 false /* key_server_entries_by_network_anonymization_key */);
54 net::HttpAuthPreferences http_auth_preferences;
55 http_auth_preferences.set_allowed_schemes(
56 std::set<std::string>{net::kBasicAuthScheme, net::kDigestAuthScheme});
57 net::HttpAuthHandlerRegistryFactory auth_handler_factory(
58 &http_auth_preferences);
59
60 scoped_refptr<net::HttpAuthController> auth_controller(
61 base::MakeRefCounted<net::HttpAuthController>(
62 net::HttpAuth::AUTH_PROXY, GURL("http://proxy:42/"),
63 net::NetworkAnonymizationKey(), &auth_cache, &auth_handler_factory,
64 nullptr));
65 // Determine if the HttpProxyClientSocket should be told the underlying socket
66 // is HTTPS.
67 net::HttpProxyClientSocket socket(
68 std::move(fuzzed_socket), "Bond/007", net::HostPortPair("foo", 80),
69 net::ProxyServer(net::ProxyServer::SCHEME_HTTP,
70 net::HostPortPair("proxy", 42)),
71 auth_controller.get(), nullptr /* proxy_delegate */,
72 TRAFFIC_ANNOTATION_FOR_TESTS);
73 int result = socket.Connect(callback.callback());
74 result = callback.GetResult(result);
75
76 // Repeatedly try to log in with the same credentials.
77 while (result == net::ERR_PROXY_AUTH_REQUESTED) {
78 if (!auth_controller->HaveAuth()) {
79 auth_controller->ResetAuth(net::AuthCredentials(u"user", u"pass"));
80 }
81 result = socket.RestartWithAuth(callback.callback());
82 result = callback.GetResult(result);
83 }
84
85 return 0;
86 }
87