Skip to content

Commit

Permalink
Only trust X-Forwarded-Proto and X-Forwarded-For if we're behind a re…
Browse files Browse the repository at this point in the history
…verse proxy
  • Loading branch information
RockinRoel committed Oct 23, 2019
1 parent 6c87163 commit 8799e42
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/http/ProxyReply.C
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ void ProxyReply::assembleRequestHeaders()
std::string forwardedFor;
std::string forwardedProto = request_.urlScheme;
std::string forwardedPort;
const Wt::Configuration& wtConfiguration
= connection()->server()->controller()->configuration();
for (Request::HeaderList::const_iterator it = request_.headers.begin();
it != request_.headers.end(); ++it) {
if (it->name.iequals("Connection") || it->name.iequals("Keep-Alive") ||
Expand All @@ -267,20 +269,22 @@ void ProxyReply::assembleRequestHeaders()
"requests to a child process. Maybe someone is trying to spoof this "
"header?");
} else if (it->name.iequals("X-Forwarded-For") ||
it->name.iequals("Client-IP")) {
const Wt::Configuration& wtConfiguration
= connection()->server()->controller()->configuration();
it->name.iequals("Client-IP")) {
if (wtConfiguration.behindReverseProxy()) {
forwardedFor = it->value.str() + ", ";
forwardedFor = it->value.str() + ", ";
}
} else if (it->name.iequals("Upgrade")) {
if (it->value.iequals("websocket")) {
establishWebSockets = true;
establishWebSockets = true;
}
} else if (it->name.iequals("X-Forwarded-Proto")) {
forwardedProto = it->value.str();
if (wtConfiguration.behindReverseProxy()) {
forwardedProto = it->value.str();
}
} else if(it->name.iequals("X-Forwarded-Port")) {
forwardedPort = it->value.str();
if (wtConfiguration.behindReverseProxy()) {
forwardedPort = it->value.str();
}
} else if (it->name.length() > 0) {
os << it->name << ": " << it->value << "\r\n";
}
Expand All @@ -293,10 +297,10 @@ void ProxyReply::assembleRequestHeaders()
}
os << "X-Forwarded-For: " << forwardedFor << request_.remoteIP << "\r\n";
os << "X-Forwarded-Proto: " << forwardedProto << "\r\n";
if(forwardedPort.size() > 0)
os << "X-Forwarded-Port: " << forwardedPort << "\r\n";
if(!forwardedPort.empty())
os << "X-Forwarded-Port: " << forwardedPort << "\r\n";
else
os << "X-Forwarded-Port: " << request_.port << "\r\n";
os << "X-Forwarded-Port: " << request_.port << "\r\n";
// Forward SSL Certificate to session only for first request
if (request_.sslInfo() && fwCertificates_) {
appendSSLInfo(request_.sslInfo(), os);
Expand Down

0 comments on commit 8799e42

Please sign in to comment.