libreoffice-online/common/Session.cpp

284 lines
7.6 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include "Session.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <ftw.h>
#include <utime.h>
#include <cassert>
2015-03-12 09:18:35 -05:00
#include <cstring>
#include <fstream>
#include <iostream>
2015-04-30 07:58:13 -05:00
#include <iterator>
#include <map>
2015-03-12 09:18:35 -05:00
#include <memory>
#include <mutex>
#include <set>
#include <Poco/Exception.h>
#include <Poco/Path.h>
#include <Poco/String.h>
#include <Poco/URI.h>
#include "Common.hpp"
#include "IoUtil.hpp"
#include "Protocol.hpp"
#include "Log.hpp"
#include <TileCache.hpp>
#include "Util.hpp"
#include "Unit.hpp"
using namespace LOOLProtocol;
using Poco::Exception;
using std::size_t;
Session::Session(const std::shared_ptr<ProtocolHandlerInterface> &protocol,
const std::string& name, const std::string& id, bool readOnly) :
MessageHandlerInterface(protocol),
_id(id),
_name(name),
_disconnected(false),
_isActive(true),
_lastActivityTime(std::chrono::steady_clock::now()),
_isCloseFrame(false),
_isReadOnly(readOnly),
_docPassword(""),
_haveDocPassword(false),
_isDocPasswordProtected(false),
_watermarkOpacity(0.2)
{
}
Session::~Session()
{
}
bool Session::sendTextFrame(const char* buffer, const int length)
{
if (!_protocol)
{
LOG_TRC("ERR - missing protocol " << getName() << ": Send: [" << getAbbreviatedMessage(buffer, length) << "].");
return false;
}
LOG_TRC(getName() << ": Send: [" << getAbbreviatedMessage(buffer, length) << "].");
return _protocol->sendTextMessage(buffer, length) >= length;
}
bool Session::sendBinaryFrame(const char *buffer, int length)
{
if (!_protocol)
{
LOG_TRC("ERR - missing protocol " << getName() << ": Send: " << std::to_string(length) << " binary bytes.");
return false;
}
LOG_TRC(getName() << ": Send: " << std::to_string(length) << " binary bytes.");
return _protocol->sendBinaryMessage(buffer, length) >= length;
}
void Session::parseDocOptions(const StringVector& tokens, int& part, std::string& timestamp, std::string& doctemplate)
{
// First token is the "load" command itself.
size_t offset = 1;
if (tokens.size() > 2 && tokens[1].find("part=") == 0)
{
getTokenInteger(tokens[1], "part", part);
++offset;
}
for (size_t i = offset; i < tokens.size(); ++i)
{
std::string name;
std::string value;
if (!LOOLProtocol::parseNameValuePair(tokens[i], name, value))
{
LOG_WRN("Unexpected doc options token [" << tokens[i] << "]. Skipping.");
continue;
}
2018-01-15 06:21:49 -06:00
if (name == "url")
{
_docURL = value;
++offset;
}
else if (name == "jail")
{
_jailedFilePath = value;
++offset;
}
else if (name == "xjail")
{
_jailedFilePathAnonym = value;
++offset;
}
else if (name == "authorid")
{
Poco::URI::decode(value, _userId);
++offset;
}
else if (name == "xauthorid")
{
Poco::URI::decode(value, _userIdAnonym);
++offset;
}
else if (name == "author")
{
Poco::URI::decode(value, _userName);
++offset;
}
else if (name == "xauthor")
{
Poco::URI::decode(value, _userNameAnonym);
++offset;
}
else if (name == "authorextrainfo")
{
Poco::URI::decode(value, _userExtraInfo);
++offset;
}
else if (name == "readonly")
{
_isReadOnly = value != "0";
++offset;
}
else if (name == "password")
{
_docPassword = value;
_haveDocPassword = true;
++offset;
}
else if (name == "lang")
{
_lang = value;
++offset;
}
else if (name == "watermarkText")
{
Poco::URI::decode(value, _watermarkText);
++offset;
}
else if (name == "watermarkOpacity")
{
_watermarkOpacity = std::stod(value);
++offset;
}
else if (name == "timestamp")
{
timestamp = value;
++offset;
}
else if (name == "template")
{
doctemplate = value;
++offset;
}
}
Util::mapAnonymized(_userId, _userIdAnonym);
Util::mapAnonymized(_userName, _userNameAnonym);
Util::mapAnonymized(_jailedFilePath, _jailedFilePathAnonym);
if (tokens.size() > offset)
{
if (getTokenString(tokens[offset], "options", _docOptions))
{
if (tokens.size() > offset + 1)
_docOptions += tokens.cat(std::string(" "), offset + 1);
}
}
}
void Session::disconnect()
{
if (!_disconnected)
{
_disconnected = true;
shutdown();
}
}
void Session::shutdown(bool goingAway, const std::string& statusMessage)
{
LOG_TRC("Shutting down WS [" << getName() << "] " <<
(goingAway ? "going" : "normal") <<
" and reason [" << statusMessage << "].");
// See protocol.txt for this application-level close frame.
if (_protocol)
{
// skip the queue; FIXME: should we flush SessionClient's queue ?
std::string closeMsg = "close: " + statusMessage;
_protocol->sendTextMessage(closeMsg, closeMsg.size());
_protocol->shutdown(goingAway, statusMessage);
}
}
void Session::handleMessage(const std::vector<char> &data)
{
try
{
std::unique_ptr< std::vector<char> > replace;
Add an initial libfuzzer based fuzzer - target ClientSession::_handleInput(), since crashing there would bring down the whole loolwsd (not just a kit process), and it deals with input from untrusted users (browsers) - add a --enable-fuzzers configure switch to build with -fsanitize=fuzzer (compared to normal sanitizers build, this is the only special flag needed) - configuring other sanitizers is not done automatically, either use --with-sanitizer=... or the environment variables from LODE's sanitizer config - run the actual fuzzer like this: ./clientsession_fuzzer -max_len=16384 fuzzer/data/ - note that at least openSUSE Leap 15.1 sadly ships with a clang with libfuzzer static libs removed from the package, so you need a self-built clang to run the fuzzer (either manual build or one from LODE) - <https://chromium.googlesource.com/chromium/src/testing/libfuzzer/+/refs/heads/master/efficient_fuzzing.md#execution-speed> suggests that "You should aim for at least 1,000 exec/s from your fuzz target locally" (i.e. one run should not take more than 1 ms), so try this minimal approach first. The alternative would be to start from the existing loolwsd_fuzzer binary, then step by step cut it down to not fork(), not do any network traffic, etc -- till it's fast enough that the fuzzer can find interesting input - the various configurations start to be really complex (the matrix is just very large), so try to use Util::isFuzzing() for fuzzer-specific changes (this is what core.git does as well), and only resort to ifdefs for the Util::isFuzzing() itself Change-Id: I72dc1193b34c93eacb5d8e39cef42387d42bd72f Reviewed-on: https://gerrit.libreoffice.org/c/online/+/89226 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
2020-02-21 08:52:20 -06:00
if (!Util::isFuzzing() && UnitBase::get().filterSessionInput(this, &data[0], data.size(), replace))
{
if (!replace || replace->empty())
_handleInput(replace->data(), replace->size());
return;
}
if (!data.empty())
_handleInput(&data[0], data.size());
}
catch (const Exception& exc)
{
LOG_ERR("Session::handleInput: Exception while handling [" <<
getAbbreviatedMessage(data) <<
"] in " << getName() << ": " << exc.displayText() <<
(exc.nested() ? " (" + exc.nested()->displayText() + ")" : ""));
}
catch (const std::exception& exc)
{
LOG_ERR("Session::handleInput: Exception while handling [" <<
getAbbreviatedMessage(data) << "]: " << exc.what());
}
}
void Session::getIOStats(uint64_t &sent, uint64_t &recv)
{
_protocol->getIOStats(sent, recv);
}
void Session::dumpState(std::ostream& os)
{
os << "\t\tid: " << _id
<< "\n\t\tname: " << _name
<< "\n\t\tdisconnected: " << _disconnected
<< "\n\t\tisActive: " << _isActive
<< "\n\t\tisCloseFrame: " << _isCloseFrame
<< "\n\t\tisReadOnly: " << _isReadOnly
<< "\n\t\tdocURL: " << _docURL
<< "\n\t\tjailedFilePath: " << _jailedFilePath
<< "\n\t\tdocPwd: " << _docPassword
<< "\n\t\thaveDocPwd: " << _haveDocPassword
<< "\n\t\tisDocPwdProtected: " << _isDocPasswordProtected
<< "\n\t\tDocOptions: " << _docOptions
<< "\n\t\tuserId: " << _userId
<< "\n\t\tuserName: " << _userName
<< "\n\t\tlang: " << _lang
<< "\n";
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */