Skip to content

Commit 177078e

Browse files
committed
Improved CEF handling in the event of missing files
Added file verification for local CEF files
1 parent 6b9314c commit 177078e

File tree

8 files changed

+70
-29
lines changed

8 files changed

+70
-29
lines changed

MTA10/core/CWebCore.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,14 @@ bool CWebCore::StaticFetchBlacklistProgress ( double dDownloadNow, double dDownl
789789
//////////////////////////////////////////////////////////////
790790
// CefApp implementation //
791791
//////////////////////////////////////////////////////////////
792+
CefRefPtr<CefResourceHandler> CCefApp::HandleError ( const SString& strError, unsigned int uiError )
793+
{
794+
auto stream = CefStreamReader::CreateForData ( (void*)strError.c_str(), strError.length());
795+
return new CefStreamResourceHandler (
796+
uiError, strError, "text/plain", CefResponse::HeaderMap(), stream);
797+
}
798+
799+
792800
void CCefApp::OnRegisterCustomSchemes ( CefRefPtr < CefSchemeRegistrar > registrar )
793801
{
794802
// Register custom MTA scheme (has to be called in all proceseses)
@@ -830,7 +838,7 @@ CefRefPtr<CefResourceHandler> CCefApp::Create ( CefRefPtr<CefBrowser> browser, C
830838
request->SetURL ( "http://mta/local/" + resourceName + resourcePath );
831839
return Create ( browser, frame, "http", request );
832840
}
833-
return nullptr;
841+
return HandleError ("404 - Not found", 404);
834842
}
835843

836844
// Redirect mtalocal://* to http://mta/local/*, call recursively
@@ -847,13 +855,13 @@ CefRefPtr<CefResourceHandler> CCefApp::Create ( CefRefPtr<CefBrowser> browser, C
847855
SString path = UTF16ToMbUTF8 ( urlParts.path.str ).substr ( 1 ); // Remove slash at the front
848856
size_t slashPos = path.find ( '/' );
849857
if ( slashPos == std::string::npos )
850-
return nullptr;
858+
return HandleError ( "404 - Not found", 404 );
851859

852860
SString resourceName = path.substr ( 0, slashPos );
853861
SString resourcePath = path.substr ( slashPos + 1 );
854862

855863
if ( resourcePath.empty () )
856-
return nullptr;
864+
return HandleError ( "404 - Not found", 404 );
857865

858866
// Get mime type from extension
859867
CefString mimeType;
@@ -932,17 +940,21 @@ CefRefPtr<CefResourceHandler> CCefApp::Create ( CefRefPtr<CefBrowser> browser, C
932940

933941
// Calculate absolute path
934942
if ( !pWebView->GetFullPathFromLocal ( path ) )
935-
return nullptr;
943+
return HandleError ( "404 - Not found", 404 );
944+
945+
// Verify local files
946+
if ( !pWebView->VerifyFile ( path ) )
947+
return HandleError ( "403 - Access Denied", 403 );
936948

937949
// Finally, load the file stream
938950
auto stream = CefStreamReader::CreateForFile ( path );
939951
if ( stream.get () )
940952
return new CefStreamResourceHandler ( mimeType, stream );
953+
return HandleError ( "404 - Not found", 404 );
941954
}
942-
943-
return nullptr;
944955
}
945956

946957
// Return null if there is no matching scheme
958+
// This falls back to letting CEF handle the request
947959
return nullptr;
948960
}

MTA10/core/CWebCore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ class CWebCore : public CWebCoreInterface
119119
class CCefApp : public CefApp, public CefSchemeHandlerFactory
120120
{
121121
public:
122+
// Error Handler
123+
static CefRefPtr<CefResourceHandler> CCefApp::HandleError ( const SString& strError, unsigned int uiError );
124+
122125
virtual void OnRegisterCustomSchemes ( CefRefPtr<CefSchemeRegistrar> registrar ) override;
123126

124127
// CefSchemeHandlerFactory methods

MTA10/core/CWebView.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,13 @@ void CWebView::HandleAjaxRequest ( const SString& strURL, CAjaxResourceHandler *
340340
{
341341
auto func = std::bind ( &CWebBrowserEventsInterface::Events_OnAjaxRequest, m_pEventsInterface, pHandler, strURL );
342342
g_pCore->GetWebCore ()->AddEventToEventQueue ( func, this, "AjaxResourceRequest" );
343-
}
343+
}
344344

345+
bool CWebView::VerifyFile ( const SString& strPath )
346+
{
347+
return m_pEventsInterface->Events_OnResourceFileCheck ( strPath );
348+
}
349+
345350

346351
////////////////////////////////////////////////////////////////////
347352
// //

MTA10/core/CWebView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CWebView : public CWebViewInterface, private CefClient, private CefRenderH
7474
void GetSourceCode ( const std::function<void( const std::string& code )>& callback );
7575

7676
bool GetFullPathFromLocal ( SString& strPath );
77+
bool VerifyFile ( const SString& strPath );
7778

7879
virtual bool RegisterAjaxHandler ( const SString& strURL ) override;
7980
virtual bool UnregisterAjaxHandler ( const SString& strURL ) override;

MTA10/mods/deathmatch/logic/CResourceManager.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,34 @@ class CResourceManager
3636
{
3737

3838
public:
39-
CResourceManager ( void );
40-
~CResourceManager ( void );
39+
CResourceManager ( void );
40+
~CResourceManager ( void );
4141

42-
CResource* Add ( unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity, const SString& strMinServerReq, const SString& strMinClientReq, bool bEnableOOP );
43-
CResource* GetResource ( const char* szResourceName );
44-
CResource* GetResourceFromNetID ( unsigned short usNetID );
45-
CResource* GetResourceFromScriptID ( uint uiScriptID );
46-
CResource* GetResourceFromLuaState ( struct lua_State* luaVM );
47-
SString GetResourceName ( struct lua_State* luaVM );
48-
bool RemoveResource ( unsigned short usID );
49-
void Remove ( CResource* pResource );
50-
bool Exists ( CResource* pResource );
51-
void StopAll ( void );
42+
CResource* Add ( unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity, const SString& strMinServerReq, const SString& strMinClientReq, bool bEnableOOP );
43+
CResource* GetResource ( const char* szResourceName );
44+
CResource* GetResourceFromNetID ( unsigned short usNetID );
45+
CResource* GetResourceFromScriptID ( uint uiScriptID );
46+
CResource* GetResourceFromLuaState ( struct lua_State* luaVM );
47+
SString GetResourceName ( struct lua_State* luaVM );
48+
49+
bool RemoveResource ( unsigned short usID );
50+
void Remove ( CResource* pResource );
51+
bool Exists ( CResource* pResource );
52+
void StopAll ( void );
5253

53-
void OnDownloadGroupFinished ( void );
54-
void UpdatePendingDownloads ( void );
54+
void OnDownloadGroupFinished ( void );
55+
void UpdatePendingDownloads ( void );
5556

56-
void OnAddResourceFile ( CDownloadableResource* pResourceFile );
57-
void OnRemoveResourceFile ( CDownloadableResource* pResourceFile );
58-
void OnDownloadedResourceFile ( const SString& strFilename );
59-
bool IsResourceFile ( const SString& strFilename );
60-
void FileModifedByScript ( const SString& strFilename );
61-
void ValidateResourceFile ( const SString& strFilename, const CBuffer& fileData );
57+
void OnAddResourceFile ( CDownloadableResource* pResourceFile );
58+
void OnRemoveResourceFile ( CDownloadableResource* pResourceFile );
59+
void OnDownloadedResourceFile ( const SString& strFilename );
60+
bool IsResourceFile ( const SString& strFilename );
61+
void FileModifedByScript ( const SString& strFilename );
62+
void ValidateResourceFile ( const SString& strFilename, const CBuffer& fileData );
63+
inline CDownloadableResource* GetDownloadableResourceFile ( const SString& strFilename ) { return MapFindRef ( m_ResourceFileMap, strFilename ); }
6264

63-
static bool ParseResourcePathInput ( std::string strInput, CResource* &pResource, std::string &strPath, std::string &strMetaPath );
64-
static bool ParseResourcePathInput ( std::string strInput, CResource* &pResource, std::string &strPath );
65+
static bool ParseResourcePathInput ( std::string strInput, CResource* &pResource, std::string &strPath, std::string &strMetaPath );
66+
static bool ParseResourcePathInput ( std::string strInput, CResource* &pResource, std::string &strPath );
6567

6668
private:
6769

MTA10/mods/shared_logic/CClientWebBrowser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ bool CClientWebBrowser::Events_OnResourcePathCheck ( SString& strURL )
242242
return false;
243243
}
244244

245+
bool CClientWebBrowser::Events_OnResourceFileCheck ( const SString& strPath )
246+
{
247+
// If no resource is set, we do not require to verify the file
248+
if ( !m_pResource )
249+
return true;
250+
251+
auto pFile = g_pClientGame->GetResourceManager ()->GetDownloadableResourceFile ( strPath.ToLower() );
252+
253+
// If we did not download this file, it has been script or user generated, nothing to verify for us
254+
if ( pFile == nullptr )
255+
return true;
256+
257+
pFile->GenerateClientChecksum ();
258+
return pFile->DoesClientAndServerChecksumMatch ();
259+
}
260+
245261
void CClientWebBrowser::Events_OnResourceBlocked ( const SString& strURL, const SString& strDomain, unsigned char reason )
246262
{
247263
CLuaArguments Arguments;

MTA10/mods/shared_logic/CClientWebBrowser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class CClientWebBrowser : public CClientTexture, public CWebBrowserEventsInterfa
7171
void Events_OnTooltip ( const SString& strTooltip ) override;
7272
void Events_OnInputFocusChanged ( bool bGainedFocus ) override;
7373
bool Events_OnResourcePathCheck ( SString& strURL ) override;
74+
bool Events_OnResourceFileCheck ( const SString& strURL ) override;
7475
void Events_OnResourceBlocked ( const SString& strURL, const SString& strDomain, unsigned char reason ) override;
7576
void Events_OnAjaxRequest ( CAjaxResourceHandlerInterface* pHandler, const SString& strURL ) override;
7677

MTA10/sdk/core/CWebBrowserEventsInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CWebBrowserEventsInterface
2626
virtual void Events_OnTooltip ( const SString& strTooltip ) = 0;
2727
virtual void Events_OnInputFocusChanged ( bool bGainedFocus ) = 0;
2828
virtual bool Events_OnResourcePathCheck ( SString& strURL ) = 0;
29+
virtual bool Events_OnResourceFileCheck ( const SString& strURL ) = 0;
2930
virtual void Events_OnResourceBlocked ( const SString& strURL, const SString& strDomain, unsigned char reason ) = 0;
3031
virtual void Events_OnAjaxRequest ( CAjaxResourceHandlerInterface* pHandler, const SString& strURL ) = 0;
3132
};

0 commit comments

Comments
 (0)