LCOV - code coverage report
Current view: top level - source/ps - ModIo.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 6 33.3 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #ifndef INCLUDED_MODIO
      24             : #define INCLUDED_MODIO
      25             : 
      26             : #include "lib/code_annotation.h"
      27             : #include "lib/external_libraries/curl.h"
      28             : #include "lib/os_path.h"
      29             : 
      30             : #include <cstddef>
      31             : #include <map>
      32             : #include <sodium.h>
      33             : #include <string>
      34             : #include <vector>
      35             : 
      36             : namespace Script { class Interface; }
      37             : 
      38             : // TODO: Allocate instance of the below two using sodium_malloc?
      39           0 : struct PKStruct
      40             : {
      41             :     unsigned char sig_alg[2] = {}; // == "Ed"
      42             :     unsigned char keynum[8] = {}; // should match the keynum in the sigstruct, else this is the wrong key
      43             :     unsigned char pk[crypto_sign_PUBLICKEYBYTES] = {};
      44             : };
      45             : 
      46          35 : struct SigStruct
      47             : {
      48             :     unsigned char sig_alg[2] = {}; // "ED" (since we only support the hashed mode)
      49             :     unsigned char keynum[8] = {}; // should match the keynum in the PKStruct
      50             :     unsigned char sig[crypto_sign_BYTES] = {};
      51             : };
      52             : 
      53          70 : struct ModIoModData
      54             : {
      55             :     std::map<std::string, std::string> properties;
      56             :     std::vector<std::string> dependencies;
      57             :     SigStruct sig;
      58             : };
      59             : 
      60             : enum class DownloadProgressStatus {
      61             :     NONE, // Default state
      62             :     GAMEID, // The game ID is being downloaded
      63             :     READY, // The game ID has been downloaded
      64             :     LISTING, // The mod list is being downloaded
      65             :     LISTED, // The mod list has been downloaded
      66             :     DOWNLOADING, // A mod file is being downloaded
      67             :     SUCCESS, // A mod file has been downloaded
      68             : 
      69             :     FAILED_GAMEID, // Game ID couldn't be retrieved
      70             :     FAILED_LISTING, // Mod list couldn't be retrieved
      71             :     FAILED_DOWNLOADING, // File couldn't be retrieved
      72             :     FAILED_FILECHECK // The file is corrupted
      73             : };
      74             : 
      75           0 : struct DownloadProgressData
      76             : {
      77             :     DownloadProgressStatus status;
      78             :     double progress;
      79             :     std::string error;
      80             : };
      81             : 
      82             : struct DownloadCallbackData;
      83             : 
      84             : /**
      85             :  * mod.io API interfacing code.
      86             :  *
      87             :  * Overview
      88             :  *
      89             :  * This class interfaces with a remote API provider that returns a list of mod files.
      90             :  * These can then be downloaded after some cursory checking of well-formedness of the returned
      91             :  * metadata.
      92             :  * Downloaded files are checked for well formedness by validating that they fit the size and hash
      93             :  * indicated by the API, then we check if the file is actually signed by a trusted key, and only
      94             :  * if all of that is success the file is actually possible to be loaded as a mod.
      95             :  *
      96             :  * Security considerations
      97             :  *
      98             :  * This both distrusts the loaded JS mods, and the API as much as possible.
      99             :  * We do not want a malicious mod to use this to download arbitrary files, nor do we want the API
     100             :  * to make us download something we have not verified.
     101             :  * Therefore we only allow mods to download one of the mods returned by this class (using indices).
     102             :  *
     103             :  * This (mostly) necessitates parsing the API responses here, as opposed to in JS.
     104             :  * One could alternatively parse the responses in a locked down JS context, but that would require
     105             :  * storing that code in here, or making sure nobody can overwrite it. Also this would possibly make
     106             :  * some of the needed accesses for downloading and verifying files a bit more complicated.
     107             :  *
     108             :  * Everything downloaded from the API has its signature verified against our public key.
     109             :  * This is a requirement, as otherwise a compromise of the API would result in users installing
     110             :  * possibly malicious files.
     111             :  * So a compromised API can just serve old files that we signed, so in that case there would need
     112             :  * to be an issue in that old file that was missed.
     113             :  *
     114             :  * To limit the extend to how old those files could be the signing key should be rotated
     115             :  * regularly (e.g. every release). To allow old versions of the engine to still use the API
     116             :  * files can be signed by both the old and the new key for some amount of time, that however
     117             :  * only makes sense in case a mod is compatible with both engine versions.
     118             :  *
     119             :  * Note that this does not prevent all possible attacks a package manager/update system should
     120             :  * defend against. This is intentionally not an update system since proper package managers already
     121             :  * exist. However there is some possible overlap in attack vectors and these should be evalutated
     122             :  * whether they apply and to what extend we can fix that on our side (or how to get the API provider
     123             :  * to help us do so). For a list of some possible issues see:
     124             :  * https://github.com/theupdateframework/specification/blob/master/tuf-spec.md
     125             :  *
     126             :  * The mod.io settings are also locked down such that only mods that have been authorized by us
     127             :  * show up in API queries. This is both done so that all required information (dependencies)
     128             :  * are stored for the files, and that only mods that have been checked for being ok are actually
     129             :  * shown to users.
     130             :  */
     131             : class ModIo
     132             : {
     133             :     NONCOPYABLE(ModIo);
     134             : public:
     135             :     ModIo();
     136             :     ~ModIo();
     137             : 
     138             :     // Async requests
     139             :     void StartGetGameId();
     140             :     void StartListMods();
     141             :     void StartDownloadMod(std::uint32_t idx);
     142             : 
     143             :     /**
     144             :      * Advance the current async request and perform final steps if the download is complete.
     145             :      *
     146             :      * @param scriptInterface used for parsing the data and possibly install the mod.
     147             :      * @return true if the download is complete (successful or not), false otherwise.
     148             :      */
     149             :     bool AdvanceRequest(const Script::Interface& scriptInterface);
     150             : 
     151             :     /**
     152             :      * Cancel the current async request and clean things up
     153             :      */
     154             :     void CancelRequest();
     155             : 
     156             :     const std::vector<ModIoModData>& GetMods() const
     157             :     {
     158           0 :         return m_ModData;
     159             :     }
     160             :     const DownloadProgressData& GetDownloadProgress() const
     161             :     {
     162           0 :         return m_DownloadProgressData;
     163             :     }
     164             : 
     165             : private:
     166             :     static size_t ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp);
     167             :     static size_t DownloadCallback(void* buffer, size_t size, size_t nmemb, void* userp);
     168             :     static int DownloadProgressCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
     169             : 
     170             :     CURLMcode SetupRequest(const std::string& url, bool fileDownload);
     171             :     void TearDownRequest();
     172             : 
     173             :     bool ParseGameId(const Script::Interface& scriptInterface, std::string& err);
     174             :     bool ParseMods(const Script::Interface& scriptInterface, std::string& err);
     175             : 
     176             :     void DeleteDownloadedFile();
     177             :     bool VerifyDownloadedFile(std::string& err);
     178             : 
     179             :     // Utility methods for parsing mod.io responses and metadata
     180             :     static bool ParseGameIdResponse(const Script::Interface& scriptInterface, const std::string& responseData, int& id, std::string& err);
     181             :     static bool ParseModsResponse(const Script::Interface& scriptInterface, const std::string& responseData, std::vector<ModIoModData>& modData, const PKStruct& pk, std::string& err);
     182             :     static bool ParseSignature(const std::vector<std::string>& minisigs, SigStruct& sig, const PKStruct& pk, std::string& err);
     183             : 
     184             :     // Url parts
     185             :     std::string m_BaseUrl;
     186             :     std::string m_GamesRequest;
     187             :     std::string m_GameId;
     188             : 
     189             :     // Query parameters
     190             :     std::string m_ApiKey;
     191             :     std::string m_IdQuery;
     192             : 
     193             :     CURL* m_Curl;
     194             :     CURLM* m_CurlMulti;
     195             :     curl_slist* m_Headers;
     196             :     char m_ErrorBuffer[CURL_ERROR_SIZE];
     197             :     std::string m_ResponseData;
     198             : 
     199             :     // Current mod download
     200             :     int m_DownloadModID;
     201             :     OsPath m_DownloadFilePath;
     202             :     DownloadCallbackData* m_CallbackData;
     203             :     DownloadProgressData m_DownloadProgressData;
     204             : 
     205             :     PKStruct m_pk;
     206             : 
     207             :     std::vector<ModIoModData> m_ModData;
     208             : 
     209             :     friend class TestModIo;
     210             : };
     211             : 
     212             : extern ModIo* g_ModIo;
     213             : 
     214             : #endif // INCLUDED_MODIO

Generated by: LCOV version 1.16