ClientConnection.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, Nicholas Hollett <niax@niax.co.uk>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClientConnection.h"
  7. #include "Launcher.h"
  8. #include <AK/HashMap.h>
  9. #include <AK/URL.h>
  10. #include <LaunchServer/LaunchClientEndpoint.h>
  11. namespace LaunchServer {
  12. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  13. ClientConnection::ClientConnection(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
  14. : IPC::ClientConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, move(client_socket), client_id)
  15. {
  16. s_connections.set(client_id, *this);
  17. }
  18. ClientConnection::~ClientConnection()
  19. {
  20. }
  21. void ClientConnection::die()
  22. {
  23. s_connections.remove(client_id());
  24. }
  25. Messages::LaunchServer::OpenUrlResponse ClientConnection::open_url(URL const& url, String const& handler_name)
  26. {
  27. if (!m_allowlist.is_empty()) {
  28. bool allowed = false;
  29. auto request_url_without_fragment = url;
  30. request_url_without_fragment.set_fragment({});
  31. for (auto& allowed_handler : m_allowlist) {
  32. if (allowed_handler.handler_name == handler_name
  33. && (allowed_handler.any_url || allowed_handler.urls.contains_slow(request_url_without_fragment))) {
  34. allowed = true;
  35. break;
  36. }
  37. }
  38. if (!allowed) {
  39. // You are not on the list, go home!
  40. did_misbehave(String::formatted("Client requested a combination of handler/URL that was not on the list: '{}' with '{}'", handler_name, url).characters());
  41. return nullptr;
  42. }
  43. }
  44. return Launcher::the().open_url(url, handler_name);
  45. }
  46. Messages::LaunchServer::GetHandlersForUrlResponse ClientConnection::get_handlers_for_url(URL const& url)
  47. {
  48. return Launcher::the().handlers_for_url(url);
  49. }
  50. Messages::LaunchServer::GetHandlersWithDetailsForUrlResponse ClientConnection::get_handlers_with_details_for_url(URL const& url)
  51. {
  52. return Launcher::the().handlers_with_details_for_url(url);
  53. }
  54. void ClientConnection::add_allowed_url(URL const& url)
  55. {
  56. if (m_allowlist_is_sealed) {
  57. did_misbehave("Got request to add more allowed handlers after list was sealed");
  58. return;
  59. }
  60. if (!url.is_valid()) {
  61. did_misbehave("Got request to allow invalid URL");
  62. return;
  63. }
  64. m_allowlist.empend(String(), false, Vector<URL> { url });
  65. }
  66. void ClientConnection::add_allowed_handler_with_any_url(String const& handler_name)
  67. {
  68. if (m_allowlist_is_sealed) {
  69. did_misbehave("Got request to add more allowed handlers after list was sealed");
  70. return;
  71. }
  72. if (handler_name.is_empty()) {
  73. did_misbehave("Got request to allow empty handler name");
  74. return;
  75. }
  76. m_allowlist.empend(handler_name, true, Vector<URL>());
  77. }
  78. void ClientConnection::add_allowed_handler_with_only_specific_urls(String const& handler_name, Vector<URL> const& urls)
  79. {
  80. if (m_allowlist_is_sealed) {
  81. did_misbehave("Got request to add more allowed handlers after list was sealed");
  82. return;
  83. }
  84. if (handler_name.is_empty()) {
  85. did_misbehave("Got request to allow empty handler name");
  86. return;
  87. }
  88. if (urls.is_empty()) {
  89. did_misbehave("Got request to allow empty URL list");
  90. return;
  91. }
  92. m_allowlist.empend(handler_name, false, urls);
  93. }
  94. void ClientConnection::seal_allowlist()
  95. {
  96. if (m_allowlist_is_sealed) {
  97. did_misbehave("Got more than one request to seal the allowed handlers list");
  98. return;
  99. }
  100. m_allowlist_is_sealed = true;
  101. }
  102. }