Skip to content

Commit 008d9bf

Browse files
lsowenjimevans
authored andcommitted
Added ability to set whitelisted IP addresses to IEDriverServer
The list of IP addresses is a comma-delimited list passed to the /whitelisted-ips command-line argument. Defaults to local loopback address only. Also improved logging to show executable version and rchitecture in driver log. Signed-off-by: Jim Evans <james.h.evans.jr@gmail.com>
1 parent 2ddbec8 commit 008d9bf

File tree

11 files changed

+93
-18
lines changed

11 files changed

+93
-18
lines changed

cpp/iedriver/IEServer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ IEServer::IEServer(int port,
2626
const std::string& log_level,
2727
const std::string& log_file,
2828
const std::string& version,
29-
const std::string& driver_implementation) : Server(port, host, log_level, log_file) {
29+
const std::string& driver_implementation,
30+
const std::string& acl) : Server(port, host, log_level, log_file, acl) {
3031
LOG(TRACE) << "Entering IEServer::IEServer";
31-
32+
LOG(INFO) << "Driver version: " << version;
3233
this->version_ = version;
3334
this->driver_implementation_ = driver_implementation;
3435
}

cpp/iedriver/IEServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class IEServer : public Server {
3030
const std::string& log_level,
3131
const std::string& log_file,
3232
const std::string& version,
33-
const std::string& driver_implementation);
33+
const std::string& driver_implementation,
34+
const std::string& acl);
3435
virtual ~IEServer(void);
3536

3637
protected:

cpp/iedriver/WebDriver.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ webdriver::Server* StartServer(int port,
2222
const std::wstring& log_level,
2323
const std::wstring& log_file,
2424
const std::wstring& version,
25-
const std::wstring& driver_engine) {
25+
const std::wstring& driver_engine,
26+
const std::wstring& whitelist) {
2627
LOG(TRACE) << "Entering StartServer";
2728
if (server == NULL) {
2829
LOG(DEBUG) << "Instantiating webdriver server";
@@ -32,12 +33,14 @@ webdriver::Server* StartServer(int port,
3233
std::string converted_log_file = webdriver::StringUtilities::ToString(log_file);
3334
std::string converted_version = webdriver::StringUtilities::ToString(version);
3435
std::string converted_engine = webdriver::StringUtilities::ToString(driver_engine);
36+
std::string converted_acl = webdriver::StringUtilities::ToString(whitelist);
3537
server = new webdriver::IEServer(port,
3638
converted_host,
3739
converted_log_level,
3840
converted_log_file,
3941
converted_version,
40-
converted_engine);
42+
converted_engine,
43+
converted_acl);
4144
if (!server->Start()) {
4245
LOG(TRACE) << "Starting of IEServer is failed";
4346
delete server;

cpp/iedriver/WebDriver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ EXPORT webdriver::Server* StartServer(int port,
3232
const std::wstring& log_level,
3333
const std::wstring& log_file,
3434
const std::wstring& version,
35-
const std::wstring& driver_engine);
35+
const std::wstring& driver_engine,
36+
const std::wstring& whitelist);
3637
EXPORT void StopServer(void);
3738

3839
#ifdef __cplusplus

cpp/iedriverserver/CHANGELOG

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ available via the project downloads page. Changes in "revision" field indicate
99
private releases checked into the prebuilts directory of the source tree, but
1010
not made generally available on the downloads page.
1111

12+
v2.48.0.4
13+
=========
14+
* Added ability to set whitelisted IP addresses to access IE driver server.
15+
The list of IP addresses is a comma-delimited list passed to the
16+
/whitelisted-ips command-line argument. Defaults to local loopback address
17+
only. Also improved logging to show executable version and architecture in
18+
driver log. Patch provided by lsowen.
19+
1220
v2.48.0.3
1321
=========
1422
* Updates to JavaScript automation atoms.

cpp/iedriverserver/IEDriverServer.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// by the .dll produced by the IEDriver project in this solution.
3030
// The definitions of these functions can be found in WebDriver.h
3131
// in that project.
32-
typedef void* (__cdecl *STARTSERVERPROC)(int, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&);
32+
typedef void* (__cdecl *STARTSERVERPROC)(int, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&, const std::wstring&);
3333
typedef void (__cdecl *STOPSERVERPROC)(void);
3434

3535
#define ERR_DLL_EXTRACT_FAIL 1
@@ -49,6 +49,7 @@ typedef void (__cdecl *STOPSERVERPROC)(void);
4949
#define SILENT_COMMAND_LINE_ARG L"silent"
5050
#define EXTRACTPATH_COMMAND_LINE_ARG L"extract-path"
5151
#define IMPLEMENTATION_COMMAND_LINE_ARG L"implementation"
52+
#define ACL_COMMAND_LINE_ARG L"whitelisted-ips"
5253
#define BOOLEAN_COMMAND_LINE_ARG_MISSING_VALUE L"value-not-specified"
5354

5455
bool ExtractResource(unsigned short resource_id,
@@ -157,11 +158,13 @@ std::wstring GetExecutableVersion() {
157158
return static_cast<wchar_t*>(value);
158159
}
159160

161+
160162
void ShowUsage(void) {
161163
std::wcout << L"Launches the WebDriver server for the Internet Explorer driver" << std::endl
162164
<< std::endl
163165
<< L"IEDriverServer [/port=<port>] [/host=<host>] [/log-level=<level>]" << std::endl
164166
<< L" [/log-file=<file>] [/extract-path=<path>] [/silent]" << std::endl
167+
<< L" [/whitelisted-ips=<whitelisted-ips>]" << std::endl
165168
<< std::endl
166169
<< L" /port=<port> Specifies the port on which the server will listen for" << std::endl
167170
<< L" commands. Defaults to 5555 if not specified." << std::endl
@@ -182,7 +185,10 @@ void ShowUsage(void) {
182185
<< L" Specifies the full path to the directory used to extract" << std::endl
183186
<< L" supporting files used by the server. Defaults to the TEMP" << std::endl
184187
<< L" directory if not specified." << std::endl
185-
<< L" /silent Suppresses diagnostic output when the server is started." << std::endl;
188+
<< L" /silent Suppresses diagnostic output when the server is started." << std::endl
189+
<< L" /whitelisted-ips=<whitelisted-ips>" << std::endl
190+
<< L" Comma-separated whitelist of remote IPv4 addresses which" << std::endl
191+
<< L" are allowed to connect to the WebDriver server." << std::endl;
186192
}
187193

188194
int _tmain(int argc, _TCHAR* argv[]) {
@@ -241,8 +247,10 @@ int _tmain(int argc, _TCHAR* argv[]) {
241247
bool silent = args.GetValue(SILENT_COMMAND_LINE_ARG,
242248
BOOLEAN_COMMAND_LINE_ARG_MISSING_VALUE).size() == 0;
243249
std::wstring executable_version = GetExecutableVersion();
250+
std::wstring executable_architecture = GetProcessArchitectureDescription();
244251
std::wstring implementation = args.GetValue(IMPLEMENTATION_COMMAND_LINE_ARG,
245252
L"");
253+
std::wstring whitelist = args.GetValue(ACL_COMMAND_LINE_ARG, L"");
246254

247255
// coerce log level and implementation to uppercase, making the values
248256
// case-insensitive, to match expected values.
@@ -255,23 +263,26 @@ int _tmain(int argc, _TCHAR* argv[]) {
255263
implementation.begin(),
256264
toupper);
257265

266+
258267
void* server_value = start_server_ex_proc(port,
259268
host_address,
260269
log_level,
261270
log_file,
262-
executable_version,
263-
implementation);
271+
executable_version + L" (" + executable_architecture + L")",
272+
implementation,
273+
whitelist);
264274
if (server_value == NULL) {
265275
std::wcout << L"Failed to start the server with: "
266276
<< L"port = '" << port << L"', "
267277
<< L"host = '" << host_address << L"', "
268278
<< L"log level = '" << log_level << L"', "
269-
<< L"log file = '" << log_file << L"'.";
279+
<< L"log file = '" << log_file << L"', "
280+
<< L"whitelisted ips = '" << whitelist << L"'.";
270281
return ERR_SERVER_START;
271282
}
272283
if (!silent) {
273284
std::wcout << L"Started InternetExplorerDriver server"
274-
<< L" (" << GetProcessArchitectureDescription() << L")"
285+
<< L" (" << executable_architecture << L")"
275286
<< std::endl;
276287
std::wcout << executable_version
277288
<< std::endl;
@@ -301,6 +312,14 @@ int _tmain(int argc, _TCHAR* argv[]) {
301312
<< extraction_path_arg
302313
<< std::endl;
303314
}
315+
if (whitelist.size() > 0) {
316+
std::wcout << L"IP addresses allowed to connect are "
317+
<< whitelist
318+
<< std::endl;
319+
} else {
320+
std::wcout << L"Only local connections are allowed"
321+
<< std::endl;
322+
}
304323
}
305324

306325
// Create the shutdown event and wait for it to be signaled.

cpp/iedriverserver/IEDriverServer.rc

0 Bytes
Binary file not shown.
4.5 KB
Binary file not shown.
5 KB
Binary file not shown.

cpp/webdriver-server/server.cc

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "logging.h"
2424

2525
#define SERVER_DEFAULT_PAGE "<html><head><title>WebDriver</title></head><body><p id='main'>This is the initial start page for the WebDriver server.</p></body></html>"
26+
#define SERVER_DEFAULT_WHITELIST "127.0.0.1"
27+
#define SERVER_DEFAULT_BLACKLIST "-0.0.0.0/0"
2628
#define HTML_CONTENT_TYPE "text/html"
2729
#define JSON_CONTENT_TYPE "application/json"
2830

@@ -44,18 +46,26 @@ inline int wd_snprintf(char* str, size_t size, const char* format, ...) {
4446
namespace webdriver {
4547

4648
Server::Server(const int port) {
47-
this->Initialize(port, "", "", "");
49+
this->Initialize(port, "", "", "", SERVER_DEFAULT_WHITELIST);
4850
}
4951

5052
Server::Server(const int port, const std::string& host) {
51-
this->Initialize(port, host, "", "");
53+
this->Initialize(port, host, "", "", SERVER_DEFAULT_WHITELIST);
5254
}
5355

5456
Server::Server(const int port,
5557
const std::string& host,
5658
const std::string& log_level,
5759
const std::string& log_file) {
58-
this->Initialize(port, host, log_level, log_file);
60+
this->Initialize(port, host, log_level, log_file, SERVER_DEFAULT_WHITELIST);
61+
}
62+
63+
Server::Server(const int port,
64+
const std::string& host,
65+
const std::string& log_level,
66+
const std::string& log_file,
67+
const std::string& acl) {
68+
this->Initialize(port, host, log_level, log_file, acl);
5969
}
6070

6171
Server::~Server(void) {
@@ -69,16 +79,36 @@ Server::~Server(void) {
6979
void Server::Initialize(const int port,
7080
const std::string& host,
7181
const std::string& log_level,
72-
const std::string& log_file) {
82+
const std::string& log_file,
83+
const std::string& acl) {
7384
LOG::Level(log_level);
7485
LOG::File(log_file);
7586
LOG(INFO) << "Starting WebDriver server on port: '"
7687
<< port << "' on host: '" << host << "'";
7788
this->port_ = port;
7889
this->host_ = host;
90+
if (acl.size() > 0) {
91+
this->ProcessWhitelist(acl);
92+
} else {
93+
this->whitelist_.push_back(SERVER_DEFAULT_WHITELIST);
94+
}
7995
this->PopulateCommandRepository();
8096
}
8197

98+
void Server::ProcessWhitelist(const std::string& whitelist) {
99+
std::string input_copy = whitelist;
100+
while (input_copy.size() > 0) {
101+
size_t delimiter_pos = input_copy.find(",");
102+
std::string token = input_copy.substr(0, delimiter_pos);
103+
if (delimiter_pos == std::string::npos) {
104+
input_copy = "";
105+
} else {
106+
input_copy = input_copy.substr(delimiter_pos + 1);
107+
}
108+
this->whitelist_.push_back(token);
109+
}
110+
}
111+
82112
int Server::OnNewHttpRequest(struct mg_connection* conn) {
83113
mg_context* context = mg_get_context(conn);
84114
Server* current_server = reinterpret_cast<Server*>(mg_get_user_data(context));
@@ -109,7 +139,12 @@ bool Server::Start() {
109139
this->host_.c_str(),
110140
this->port_);
111141

112-
std::string acl = "-0.0.0.0/0,+127.0.0.1";
142+
std::string acl = SERVER_DEFAULT_BLACKLIST;
143+
for (std::vector<std::string>::const_iterator it = this->whitelist_.begin();
144+
it < this->whitelist_.end();
145+
++it) {
146+
acl.append(",+").append(*it);
147+
}
113148
LOG(DEBUG) << "Civetweb ACL is " << acl;
114149

115150
const char* options[] = { "listening_ports", listening_ports_buffer,

0 commit comments

Comments
 (0)