Skip to content

Commit

Permalink
Merge pull request #7582 from rgacogne/auth40-remotebackend
Browse files Browse the repository at this point in the history
auth-4.0.x: remotebackend: http connector - Properly escape parameters
  • Loading branch information
rgacogne committed Mar 18, 2019
2 parents 6cbd567 + 3c2f3a3 commit 1e9825d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
89 changes: 50 additions & 39 deletions modules/remotebackend/httpconnector.cc
Expand Up @@ -35,7 +35,22 @@
#endif

HTTPConnector::HTTPConnector(std::map<std::string,std::string> options) {

if (options.find("url") == options.end()) {
throw PDNSException("Cannot find 'url' option in the remote backend HTTP connector's parameters");
}

this->d_url = options.find("url")->second;

try {
YaHTTP::URL url(d_url);
d_host = url.host;
d_port = url.port;
}
catch(const std::exception& e) {
throw PDNSException("Error parsing the 'url' option provided to the remote backend HTTP connector: " + std::string(e.what()));
}

if (options.find("url-suffix") != options.end()) {
this->d_url_suffix = options.find("url-suffix")->second;
} else {
Expand Down Expand Up @@ -71,7 +86,7 @@ HTTPConnector::~HTTPConnector() {
void HTTPConnector::addUrlComponent(const Json &parameters, const string& element, std::stringstream& ss) {
std::string sparam;
if (parameters[element] != Json())
ss << "/" << asString(parameters[element]);
ss << "/" << YaHTTP::Utility::encodeURL(asString(parameters[element]), false);
}

std::string HTTPConnector::buildMemberListArgs(std::string prefix, const Json& args) {
Expand All @@ -81,9 +96,9 @@ std::string HTTPConnector::buildMemberListArgs(std::string prefix, const Json& a
if (pair.second.is_bool()) {
stream << (pair.second.bool_value()?"1":"0");
} else if (pair.second.is_null()) {
stream << prefix << "[" << pair.first << "]=";
stream << prefix << "[" << YaHTTP::Utility::encodeURL(pair.first, false) << "]=";
} else {
stream << prefix << "[" << pair.first << "]=" << this->asString(pair.second);
stream << prefix << "[" << YaHTTP::Utility::encodeURL(pair.first, false) << "]=" << YaHTTP::Utility::encodeURL(this->asString(pair.second), false);
}
stream << "&";
}
Expand Down Expand Up @@ -334,45 +349,41 @@ int HTTPConnector::send_message(const Json& input) {
delete this->d_socket;
this->d_socket = NULL;

if (req.url.protocol == "unix") {
// connect using unix socket
} else {
// connect using tcp
struct addrinfo *gAddr, *gAddrPtr, hints;
std::string sPort = std::to_string(req.url.port);
memset(&hints,0,sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 6; // tcp
if ((ec = getaddrinfo(req.url.host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) {
// try to connect to each address.
gAddrPtr = gAddr;
// connect using tcp
struct addrinfo *gAddr, *gAddrPtr, hints;
std::string sPort = std::to_string(d_port);
memset(&hints,0,sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 6; // tcp
if ((ec = getaddrinfo(d_host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) {
// try to connect to each address.
gAddrPtr = gAddr;

while(gAddrPtr) {
try {
d_socket = new Socket(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol);
d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen);
d_socket->connect(d_addr);
d_socket->setNonBlocking();
d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
rv = 1;
} catch (NetworkError& ne) {
L<<Logger::Error<<"While writing to HTTP endpoint "<<d_addr.toStringWithPort()<<": "<<ne.what()<<std::endl;
} catch (...) {
L<<Logger::Error<<"While writing to HTTP endpoint "<<d_addr.toStringWithPort()<<": exception caught"<<std::endl;
}

if (rv > -1) break;
delete d_socket;
d_socket = NULL;
gAddrPtr = gAddrPtr->ai_next;

while(gAddrPtr) {
try {
d_socket = new Socket(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol);
d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen);
d_socket->connect(d_addr);
d_socket->setNonBlocking();
d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
rv = 1;
} catch (NetworkError& ne) {
L<<Logger::Error<<"While writing to HTTP endpoint "<<d_addr.toStringWithPort()<<": "<<ne.what()<<std::endl;
} catch (...) {
L<<Logger::Error<<"While writing to HTTP endpoint "<<d_addr.toStringWithPort()<<": exception caught"<<std::endl;
}
freeaddrinfo(gAddr);
} else {
L<<Logger::Error<<"Unable to resolve " << req.url.host << ": " << gai_strerror(ec) << std::endl;

if (rv > -1) break;
delete d_socket;
d_socket = NULL;
gAddrPtr = gAddrPtr->ai_next;

}
freeaddrinfo(gAddr);
} else {
L<<Logger::Error<<"Unable to resolve " << d_host << ": " << gai_strerror(ec) << std::endl;
}

return rv;
Expand Down
2 changes: 2 additions & 0 deletions modules/remotebackend/remotebackend.hh
Expand Up @@ -105,6 +105,8 @@ class HTTPConnector: public Connector {
std::string buildMemberListArgs(std::string prefix, const Json& args);
Socket* d_socket;
ComboAddress d_addr;
std::string d_host;
uint16_t d_port;
};

#ifdef REMOTEBACKEND_ZEROMQ
Expand Down

0 comments on commit 1e9825d

Please sign in to comment.