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