2020-04-26 16:27:57 -03:00
/*
* Copyright ( c ) 2020 , Nicholas Hollett < niax @ niax . co . uk >
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-04-26 16:27:57 -03:00
*/
# include "ClientConnection.h"
# include "Launcher.h"
# include <AK/HashMap.h>
# include <AK/URL.h>
2020-05-06 12:40:06 -03:00
# include <LaunchServer/LaunchClientEndpoint.h>
2020-04-26 16:27:57 -03:00
namespace LaunchServer {
static HashMap < int , RefPtr < ClientConnection > > s_connections ;
2020-07-06 08:23:39 -03:00
ClientConnection : : ClientConnection ( NonnullRefPtr < Core : : LocalSocket > client_socket , int client_id )
2020-09-12 06:44:00 -03:00
: IPC : : ClientConnection < LaunchClientEndpoint , LaunchServerEndpoint > ( * this , move ( client_socket ) , client_id )
2020-04-26 16:27:57 -03:00
{
s_connections . set ( client_id , * this ) ;
}
ClientConnection : : ~ ClientConnection ( )
{
}
void ClientConnection : : die ( )
{
s_connections . remove ( client_id ( ) ) ;
}
2021-05-03 10:52:56 -03:00
Messages : : LaunchServer : : OpenUrlResponse ClientConnection : : open_url ( URL const & url , String const & handler_name )
2020-04-26 16:27:57 -03:00
{
2021-01-03 08:03:13 -03:00
if ( ! m_allowlist . is_empty ( ) ) {
2021-01-03 07:39:33 -03:00
bool allowed = false ;
2021-05-02 14:54:34 -03:00
auto request_url_without_fragment = url ;
2021-05-02 10:41:01 -03:00
request_url_without_fragment . set_fragment ( { } ) ;
2021-01-03 08:03:13 -03:00
for ( auto & allowed_handler : m_allowlist ) {
2021-05-02 14:54:34 -03:00
if ( allowed_handler . handler_name = = handler_name
2021-05-02 10:41:01 -03:00
& & ( allowed_handler . any_url | | allowed_handler . urls . contains_slow ( request_url_without_fragment ) ) ) {
2021-01-03 07:39:33 -03:00
allowed = true ;
break ;
}
}
if ( ! allowed ) {
// You are not on the list, go home!
2021-05-02 14:54:34 -03:00
did_misbehave ( String : : formatted ( " Client requested a combination of handler/URL that was not on the list: '{}' with '{}' " , handler_name , url ) . characters ( ) ) ;
2021-05-01 23:39:36 -03:00
return nullptr ;
2021-01-03 07:39:33 -03:00
}
}
2021-05-02 14:54:34 -03:00
return Launcher : : the ( ) . open_url ( url , handler_name ) ;
2020-04-26 16:27:57 -03:00
}
2020-05-12 13:43:55 -03:00
2021-05-03 10:52:56 -03:00
Messages : : LaunchServer : : GetHandlersForUrlResponse ClientConnection : : get_handlers_for_url ( URL const & url )
2020-05-12 13:43:55 -03:00
{
2021-05-01 23:39:36 -03:00
return Launcher : : the ( ) . handlers_for_url ( url ) ;
2020-05-12 13:43:55 -03:00
}
2021-05-03 10:52:56 -03:00
Messages : : LaunchServer : : GetHandlersWithDetailsForUrlResponse ClientConnection : : get_handlers_with_details_for_url ( URL const & url )
2020-07-13 21:55:09 -03:00
{
2021-05-01 23:39:36 -03:00
return Launcher : : the ( ) . handlers_with_details_for_url ( url ) ;
2020-07-13 21:55:09 -03:00
}
2021-05-02 14:54:34 -03:00
void ClientConnection : : add_allowed_url ( URL const & url )
2021-01-03 08:03:13 -03:00
{
if ( m_allowlist_is_sealed ) {
did_misbehave ( " Got request to add more allowed handlers after list was sealed " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 08:03:13 -03:00
}
2021-05-02 14:54:34 -03:00
if ( ! url . is_valid ( ) ) {
2021-01-03 08:03:13 -03:00
did_misbehave ( " Got request to allow invalid URL " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 08:03:13 -03:00
}
2021-05-02 14:54:34 -03:00
m_allowlist . empend ( String ( ) , false , Vector < URL > { url } ) ;
2021-01-03 08:03:13 -03:00
}
2021-05-02 14:54:34 -03:00
void ClientConnection : : add_allowed_handler_with_any_url ( String const & handler_name )
2021-01-03 07:39:33 -03:00
{
2021-01-03 08:03:13 -03:00
if ( m_allowlist_is_sealed ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got request to add more allowed handlers after list was sealed " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
if ( handler_name . is_empty ( ) ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got request to allow empty handler name " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
m_allowlist . empend ( handler_name , true , Vector < URL > ( ) ) ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
void ClientConnection : : add_allowed_handler_with_only_specific_urls ( String const & handler_name , Vector < URL > const & urls )
2021-01-03 07:39:33 -03:00
{
2021-01-03 08:03:13 -03:00
if ( m_allowlist_is_sealed ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got request to add more allowed handlers after list was sealed " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
if ( handler_name . is_empty ( ) ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got request to allow empty handler name " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
if ( urls . is_empty ( ) ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got request to allow empty URL list " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
m_allowlist . empend ( handler_name , false , urls ) ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 14:54:34 -03:00
void ClientConnection : : seal_allowlist ( )
2021-01-03 07:39:33 -03:00
{
2021-01-03 08:03:13 -03:00
if ( m_allowlist_is_sealed ) {
2021-01-03 07:39:33 -03:00
did_misbehave ( " Got more than one request to seal the allowed handlers list " ) ;
2021-05-02 00:20:28 -03:00
return ;
2021-01-03 07:39:33 -03:00
}
2021-05-02 03:16:42 -03:00
m_allowlist_is_sealed = true ;
2021-01-03 07:39:33 -03:00
}
2020-04-26 16:27:57 -03:00
}