-
Notifications
You must be signed in to change notification settings - Fork 731
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
|
@@ -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)) | ||
{ | ||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.