/indra/newview/llcommandhandler.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 237 lines · 158 code · 26 blank · 53 comment · 16 complexity · a458b6fb948dc1c1931ffae07b5908e4 MD5 · raw file

  1. /**
  2. * @file llcommandhandler.cpp
  3. * @brief Central registry for text-driven "commands", most of
  4. * which manipulate user interface. For example, the command
  5. * "agent (uuid) about" will open the UI for an avatar's profile.
  6. *
  7. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "llviewerprecompiledheaders.h"
  29. #include "llcommandhandler.h"
  30. #include "llnotificationsutil.h"
  31. #include "llcommanddispatcherlistener.h"
  32. #include "stringize.h"
  33. // system includes
  34. #include <boost/tokenizer.hpp>
  35. #define THROTTLE_PERIOD 5 // required seconds between throttled commands
  36. static LLCommandDispatcherListener sCommandDispatcherListener;
  37. //---------------------------------------------------------------------------
  38. // Underlying registry for command handlers, not directly accessible.
  39. //---------------------------------------------------------------------------
  40. struct LLCommandHandlerInfo
  41. {
  42. LLCommandHandler::EUntrustedAccess mUntrustedBrowserAccess;
  43. LLCommandHandler* mHandler; // safe, all of these are static objects
  44. };
  45. class LLCommandHandlerRegistry
  46. {
  47. public:
  48. static LLCommandHandlerRegistry& instance();
  49. void add(const char* cmd,
  50. LLCommandHandler::EUntrustedAccess untrusted_access,
  51. LLCommandHandler* handler);
  52. bool dispatch(const std::string& cmd,
  53. const LLSD& params,
  54. const LLSD& query_map,
  55. LLMediaCtrl* web,
  56. const std::string& nav_type,
  57. bool trusted_browser);
  58. private:
  59. friend LLSD LLCommandDispatcher::enumerate();
  60. std::map<std::string, LLCommandHandlerInfo> mMap;
  61. };
  62. // static
  63. LLCommandHandlerRegistry& LLCommandHandlerRegistry::instance()
  64. {
  65. // Force this to be initialized on first call, because we're going
  66. // to be adding items to the std::map before main() and we can't
  67. // rely on a global being initialized in the right order.
  68. static LLCommandHandlerRegistry instance;
  69. return instance;
  70. }
  71. void LLCommandHandlerRegistry::add(const char* cmd,
  72. LLCommandHandler::EUntrustedAccess untrusted_access,
  73. LLCommandHandler* handler)
  74. {
  75. LLCommandHandlerInfo info;
  76. info.mUntrustedBrowserAccess = untrusted_access;
  77. info.mHandler = handler;
  78. mMap[cmd] = info;
  79. }
  80. bool LLCommandHandlerRegistry::dispatch(const std::string& cmd,
  81. const LLSD& params,
  82. const LLSD& query_map,
  83. LLMediaCtrl* web,
  84. const std::string& nav_type,
  85. bool trusted_browser)
  86. {
  87. static bool slurl_blocked = false;
  88. static bool slurl_throttled = false;
  89. static F64 last_throttle_time = 0.0;
  90. F64 cur_time = 0.0;
  91. std::map<std::string, LLCommandHandlerInfo>::iterator it = mMap.find(cmd);
  92. if (it == mMap.end()) return false;
  93. const LLCommandHandlerInfo& info = it->second;
  94. if (!trusted_browser)
  95. {
  96. switch (info.mUntrustedBrowserAccess)
  97. {
  98. case LLCommandHandler::UNTRUSTED_ALLOW:
  99. // fall through and let the command be handled
  100. break;
  101. case LLCommandHandler::UNTRUSTED_BLOCK:
  102. // block request from external browser, but report as
  103. // "handled" because it was well formatted.
  104. LL_WARNS_ONCE("SLURL") << "Blocked SLURL command from untrusted browser" << LL_ENDL;
  105. if (! slurl_blocked)
  106. {
  107. LLNotificationsUtil::add("BlockedSLURL");
  108. slurl_blocked = true;
  109. }
  110. return true;
  111. case LLCommandHandler::UNTRUSTED_THROTTLE:
  112. // if users actually click on a link, we don't need to throttle it
  113. // (throttling mechanism is used to prevent an avalanche of clicks via
  114. // javascript
  115. if ( nav_type == "clicked" )
  116. {
  117. break;
  118. }
  119. cur_time = LLTimer::getElapsedSeconds();
  120. if (cur_time < last_throttle_time + THROTTLE_PERIOD)
  121. {
  122. // block request from external browser if it happened
  123. // within THROTTLE_PERIOD seconds of the last command
  124. LL_WARNS_ONCE("SLURL") << "Throttled SLURL command from untrusted browser" << LL_ENDL;
  125. if (! slurl_throttled)
  126. {
  127. LLNotificationsUtil::add("ThrottledSLURL");
  128. slurl_throttled = true;
  129. }
  130. return true;
  131. }
  132. last_throttle_time = cur_time;
  133. break;
  134. }
  135. }
  136. if (!info.mHandler) return false;
  137. return info.mHandler->handle(params, query_map, web);
  138. }
  139. //---------------------------------------------------------------------------
  140. // Automatic registration of commands, runs before main()
  141. //---------------------------------------------------------------------------
  142. LLCommandHandler::LLCommandHandler(const char* cmd,
  143. EUntrustedAccess untrusted_access)
  144. {
  145. LLCommandHandlerRegistry::instance().add(cmd, untrusted_access, this);
  146. }
  147. LLCommandHandler::~LLCommandHandler()
  148. {
  149. // Don't care about unregistering these, all the handlers
  150. // should be static objects.
  151. }
  152. //---------------------------------------------------------------------------
  153. // Public interface
  154. //---------------------------------------------------------------------------
  155. // static
  156. bool LLCommandDispatcher::dispatch(const std::string& cmd,
  157. const LLSD& params,
  158. const LLSD& query_map,
  159. LLMediaCtrl* web,
  160. const std::string& nav_type,
  161. bool trusted_browser)
  162. {
  163. return LLCommandHandlerRegistry::instance().dispatch(
  164. cmd, params, query_map, web, nav_type, trusted_browser);
  165. }
  166. static std::string lookup(LLCommandHandler::EUntrustedAccess value);
  167. LLSD LLCommandDispatcher::enumerate()
  168. {
  169. LLSD response;
  170. LLCommandHandlerRegistry& registry(LLCommandHandlerRegistry::instance());
  171. for (std::map<std::string, LLCommandHandlerInfo>::const_iterator chi(registry.mMap.begin()),
  172. chend(registry.mMap.end());
  173. chi != chend; ++chi)
  174. {
  175. LLSD info;
  176. info["untrusted"] = chi->second.mUntrustedBrowserAccess;
  177. info["untrusted_str"] = lookup(chi->second.mUntrustedBrowserAccess);
  178. response[chi->first] = info;
  179. }
  180. return response;
  181. }
  182. /*------------------------------ lookup stuff ------------------------------*/
  183. struct symbol_info
  184. {
  185. const char* name;
  186. LLCommandHandler::EUntrustedAccess value;
  187. };
  188. #define ent(SYMBOL) \
  189. { \
  190. #SYMBOL + 28, /* skip "LLCommandHandler::UNTRUSTED_" prefix */ \
  191. SYMBOL \
  192. }
  193. symbol_info symbols[] =
  194. {
  195. ent(LLCommandHandler::UNTRUSTED_ALLOW), // allow commands from untrusted browsers
  196. ent(LLCommandHandler::UNTRUSTED_BLOCK), // ignore commands from untrusted browsers
  197. ent(LLCommandHandler::UNTRUSTED_THROTTLE) // allow untrusted, but only a few per min.
  198. };
  199. #undef ent
  200. static std::string lookup(LLCommandHandler::EUntrustedAccess value)
  201. {
  202. for (symbol_info *sii(symbols), *siend(symbols + (sizeof(symbols)/sizeof(symbols[0])));
  203. sii != siend; ++sii)
  204. {
  205. if (sii->value == value)
  206. {
  207. return sii->name;
  208. }
  209. }
  210. return STRINGIZE("UNTRUSTED_" << value);
  211. }