Skip to content

Commit 887e731

Browse files
committed
Whitelist add ability to allow/deny command for all ip
1 parent e1454e2 commit 887e731

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

inc/webdriver_access.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <string>
55
#include <vector>
6+
#include <list>
67
#include "base/file_util.h"
78

89
namespace webdriver {
@@ -14,7 +15,8 @@ struct AccessCommandTable
1415
};
1516

1617
struct AccessRule {
17-
long host_ip;
18+
long hostIp;
19+
bool isGeneralRule; //for all ip
1820
bool allowed;
1921
std::vector<AccessCommandTable> commandList;
2022
};
@@ -29,7 +31,7 @@ class AccessValidator
2931

3032
private:
3133
bool convertIpString(const char *str_ip, long *int_ip);
32-
std::vector<AccessRule> accessList;
34+
std::list<AccessRule> accessList;
3335
};
3436

3537
} // namespace webdriver

src/webdriver/webdriver_access.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ bool AccessValidator::isAllowed(const long &remote_ip, const std::string &url, c
1616
if (accessList.empty())
1717
return true;
1818
bool result = false;
19-
for (std::vector<AccessRule>::iterator it = accessList.begin(); it != accessList.end(); ++it)
19+
for (std::list<AccessRule>::iterator it = accessList.begin(); it != accessList.end(); ++it)
2020
{
2121
AccessRule host = *it;
22-
if (host.host_ip == remote_ip)
22+
if ((host.hostIp == remote_ip) || (host.isGeneralRule))
2323
{
2424
if (!host.allowed) {
2525
for (std::vector<AccessCommandTable>::iterator it = host.commandList.begin(); it != host.commandList.end(); ++it) {
@@ -68,13 +68,20 @@ void AccessValidator::setWhiteList(FilePath &xmlPath)
6868
pugi::xml_attribute atr = xnode.attribute("ip");
6969

7070
AccessRule rule;
71-
if (!convertIpString(atr.value(), &rule.host_ip)) {
72-
std::string error_descr = "WhiteList: "+ std::string(atr.value()) + " is not a valid ip address";
73-
GlobalLogger::Log(kWarningLogLevel, error_descr);
74-
continue;
71+
rule.isGeneralRule = false;
72+
rule.allowed = true;
73+
74+
if (!convertIpString(atr.value(), &rule.hostIp)) {
75+
if (!strcmp(atr.value(), "*")) {
76+
rule.isGeneralRule = true;
77+
} else {
78+
std::string error_descr = "WhiteList: "+ std::string(atr.value()) + " is not a valid ip address";
79+
GlobalLogger::Log(kWarningLogLevel, error_descr);
80+
continue;
81+
}
82+
7583
}
7684

77-
rule.allowed = true;
7885
pugi::xpath_query query_nodes("./deny");
7986
pugi::xpath_node_set deny_nodes = query_nodes.evaluate_node_set(xnode);
8087
if ( (NULL == query_nodes.result().error) &&
@@ -110,8 +117,8 @@ void AccessValidator::setWhiteList(FilePath &xmlPath)
110117
}
111118
}
112119
}
113-
114-
accessList.push_back(rule);
120+
// if we have wildcard put this rule in the end
121+
rule.isGeneralRule ? accessList.push_back(rule) : accessList.push_front(rule);
115122
}
116123
} else {
117124
std::string error_descr = "WhiteList: Cant evaluate XPath to node set: ";

src/webdriver/webdriver_server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ bool Server::ParseRequestInfo(const struct mg_request_info* const request_info,
435435
if (!accessValidor.isAllowed(request_info->remote_ip, uri, *method))
436436
{
437437
response->SetError(new Error(
438-
kUnknownCommand,
439-
"Command was restricted by whitelist"));
438+
kUnknownError,
439+
"Command is forbidden for this origin"));
440440
return false;
441441
}
442442

0 commit comments

Comments
 (0)