Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check endpoint to validate WOPI configuration from integrators #4353

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ constexpr const char FORKIT_URI[] = "/coolws/forkit";

constexpr const char CAPABILITIES_END_POINT[] = "/hosting/capabilities";

constexpr const char CHECK_END_POINT[] = "/hosting/check";

/// The file suffix used to mark the file slated for uploading.
constexpr const char TO_UPLOAD_SUFFIX[] = ".upload";
/// The file suffix used to mark the file being uploaded.
Expand Down
100 changes: 100 additions & 0 deletions wsd/COOLWSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,9 @@ class ClientRequestDispatcher final : public SimpleSocketHandler
requestDetails.isGet("/hosting/discovery/"))
handleWopiDiscoveryRequest(requestDetails, socket);

else if (requestDetails.isPost(CHECK_END_POINT))
handleCheckRequest(request, socket, message);

else if (requestDetails.isGet(CAPABILITIES_END_POINT))
handleCapabilitiesRequest(request, socket);

Expand Down Expand Up @@ -3274,6 +3277,103 @@ class ClientRequestDispatcher final : public SimpleSocketHandler
LOG_INF("Sent capabilities.json successfully.");
}

void handleCheckRequest(const Poco::Net::HTTPRequest& request,
const std::shared_ptr<StreamSocket>& socket,
Poco::MemoryInputStream& message)
{
assert(socket && "Must have a valid socket");

LOG_DBG("Check request: " << request.getURI());

bool validWopiConfig = false;
std::string connectMessage;
bool validConnectBack = false;

ConvertToPartHandler handler;
HTMLForm form(request, message, handler);

std::string url = (form.has("url") ? form.get("url") : "");
Poco::URI remoteServerURI(url);

// Validate allowed WOPI host
std::string addressToCheck = remoteServerURI.getHost();
LOG_DBG("Checking remote host: " << addressToCheck);
try
{
if (!StorageBase::allowedWopiHost(addressToCheck) && !net::isLocalhost(addressToCheck))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not assume that localhost or 127.0.0.x are route-able and reachable locally - this is often not the case in containers - and the main problem we have with setups.

{
// FIXME: does not automatically detect that 127.0.0.1 matches localhost in coolwsd.xml
const auto hostAddresses(Poco::Net::DNS::resolve(addressToCheck));
for (auto &address : hostAddresses.addresses())
{
LOG_DBG("Checking resolved remote host: " << address.toString());
if (StorageBase::allowedWopiHost(address.toString()))
{
validWopiConfig = true;
break;
}
LOG_DBG("Invalid resolved remote host: " << address.toString());
}
} else {
validWopiConfig = true;
}

}
catch (const Poco::Exception& exc)
{
LOG_ERR("Poco::Net::DNS::resolve(\"" << addressToCheck << "\") failed: " << exc.displayText());
// We can't find out the hostname, and it already failed the IP check
validWopiConfig = false;
}

// Validate connecting back to WOPI host
try
{
std::shared_ptr<http::Session> httpSession(
StorageBase::getHttpSession(remoteServerURI));
http::Request wopiRequest(remoteServerURI.getPathAndQuery());

const std::shared_ptr<const http::Response> httpResponse =
httpSession->syncRequest(wopiRequest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync is synchronous - this will block the server and the main thread until that request completes. We have to do async requests.


unsigned int statusCode = httpResponse->statusLine().statusCode();

if (statusCode == Poco::Net::HTTPResponse::HTTP_OK)
{
validConnectBack = true;
connectMessage = "OK";

std::string body = httpResponse->getBody();
}
else
{
connectMessage = "Collabora received unexpected status code from the WOPI host: " +
std::to_string(statusCode);
}
}
catch (...)
{
connectMessage = "Failed to fetch";
}


Poco::JSON::Object::Ptr check = new Poco::JSON::Object;
check->set("status", validWopiConfig && validConnectBack);
check->set("status_config", validWopiConfig);
check->set("status_connect", validConnectBack);
check->set("status_connect_message", connectMessage);
check->set("message", "Validated configuration and connectivity");
std::ostringstream ostrJSON;
check->stringify(ostrJSON);

http::Response httpResponse(http::StatusLine(200));
httpResponse.set("Last-Modified", Util::getHttpTimeNow());
httpResponse.setBody(ostrJSON.str(), "application/json");
httpResponse.set("X-Content-Type-Options", "nosniff");
socket->sendAndShutdown(httpResponse);
LOG_INF("Sent check results successfully.");
}

static void handleClipboardRequest(const Poco::Net::HTTPRequest& request,
Poco::MemoryInputStream& message,
SocketDisposition &disposition,
Expand Down
2 changes: 2 additions & 0 deletions wsd/RequestDetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ RequestDetails::RequestDetails(Poco::Net::HTTPRequest &request, const std::strin
const std::string &method = request.getMethod();
_isGet = method == "GET";
_isHead = method == "HEAD";
_isPost = method == "POST";
auto it = request.find("ProxyPrefix");
_isProxy = it != request.end();
if (_isProxy)
Expand All @@ -96,6 +97,7 @@ RequestDetails::RequestDetails(Poco::Net::HTTPRequest &request, const std::strin
RequestDetails::RequestDetails(const std::string &mobileURI)
: _isGet(true)
, _isHead(false)
, _isPost(false)
, _isProxy(false)
, _isWebSocket(false)
{
Expand Down
5 changes: 5 additions & 0 deletions wsd/RequestDetails.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class RequestDetails

bool _isGet : 1;
bool _isHead : 1;
bool _isPost : 1;
bool _isProxy : 1;
bool _isWebSocket : 1;
bool _isMobile : 1;
Expand Down Expand Up @@ -183,6 +184,10 @@ class RequestDetails
{
return _isGet && _uriString == path;
}
bool isPost(const char *path) const
{
return _isPost && _uriString == path;
}
bool isGetOrHead(const char *path) const
{
return (_isGet || _isHead) && _uriString == path;
Expand Down