Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ModIo.h
Go to the documentation of this file.
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"
28#include "lib/os_path.h"
29
30#include <cstddef>
31#include <map>
32#include <sodium.h>
33#include <string>
34#include <vector>
35
36namespace Script { class Interface; }
37
38// TODO: Allocate instance of the below two using sodium_malloc?
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
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
54{
55 std::map<std::string, std::string> properties;
56 std::vector<std::string> dependencies;
58};
59
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
76{
78 double progress;
79 std::string error;
80};
81
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 */
131class ModIo
132{
134public:
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 return m_ModData;
159 }
161 {
163 }
164
165private:
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
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;
195 curl_slist* m_Headers;
196 char m_ErrorBuffer[CURL_ERROR_SIZE];
197 std::string m_ResponseData;
198
199 // Current mod download
204
206
207 std::vector<ModIoModData> m_ModData;
208
209 friend class TestModIo;
210};
211
212extern ModIo* g_ModIo;
213
214#endif // INCLUDED_MODIO
DownloadProgressStatus
Definition: ModIo.h:60
ModIo * g_ModIo
Definition: ModIo.cpp:64
mod.io API interfacing code.
Definition: ModIo.h:132
std::string m_ResponseData
Definition: ModIo.h:197
std::string m_ApiKey
Definition: ModIo.h:190
void DeleteDownloadedFile()
Definition: ModIo.cpp:548
std::vector< ModIoModData > m_ModData
Definition: ModIo.h:207
const DownloadProgressData & GetDownloadProgress() const
Definition: ModIo.h:160
bool ParseMods(const Script::Interface &scriptInterface, std::string &err)
Definition: ModIo.cpp:541
std::string m_IdQuery
Definition: ModIo.h:191
CURLMcode SetupRequest(const std::string &url, bool fileDownload)
Definition: ModIo.cpp:218
int m_DownloadModID
Definition: ModIo.h:200
static bool ParseModsResponse(const Script::Interface &scriptInterface, const std::string &responseData, std::vector< ModIoModData > &modData, const PKStruct &pk, std::string &err)
Parses the current content of m_ResponseData into m_ModData.
Definition: ModIo.cpp:697
std::string m_GameId
Definition: ModIo.h:187
void TearDownRequest()
Definition: ModIo.cpp:254
DownloadProgressData m_DownloadProgressData
Definition: ModIo.h:203
bool VerifyDownloadedFile(std::string &err)
Definition: ModIo.cpp:557
friend class TestModIo
Definition: ModIo.h:209
void StartGetGameId()
Definition: ModIo.cpp:276
PKStruct m_pk
Definition: ModIo.h:205
~ModIo()
Definition: ModIo.cpp:158
const std::vector< ModIoModData > & GetMods() const
Definition: ModIo.h:156
void StartListMods()
Definition: ModIo.cpp:300
CURL * m_Curl
Definition: ModIo.h:193
curl_slist * m_Headers
Definition: ModIo.h:195
ModIo()
Definition: ModIo.cpp:95
static size_t ReceiveCallback(void *buffer, size_t size, size_t nmemb, void *userp)
Definition: ModIo.cpp:173
std::string m_GamesRequest
Definition: ModIo.h:186
static int DownloadProgressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition: ModIo.cpp:204
char m_ErrorBuffer[CURL_ERROR_SIZE]
Definition: ModIo.h:196
DownloadCallbackData * m_CallbackData
Definition: ModIo.h:202
void CancelRequest()
Cancel the current async request and clean things up.
Definition: ModIo.cpp:391
std::string m_BaseUrl
Definition: ModIo.h:185
OsPath m_DownloadFilePath
Definition: ModIo.h:201
static bool ParseSignature(const std::vector< std::string > &minisigs, SigStruct &sig, const PKStruct &pk, std::string &err)
Parse signatures to find one that matches the public key, and has a valid global signature.
Definition: ModIo.cpp:825
void StartDownloadMod(std::uint32_t idx)
Definition: ModIo.cpp:330
static size_t DownloadCallback(void *buffer, size_t size, size_t nmemb, void *userp)
Definition: ModIo.cpp:182
bool AdvanceRequest(const Script::Interface &scriptInterface)
Advance the current async request and perform final steps if the download is complete.
Definition: ModIo.cpp:415
bool ParseGameId(const Script::Interface &scriptInterface, std::string &err)
Definition: ModIo.cpp:529
CURLM * m_CurlMulti
Definition: ModIo.h:194
NONCOPYABLE(ModIo)
static bool ParseGameIdResponse(const Script::Interface &scriptInterface, const std::string &responseData, int &id, std::string &err)
Parses the current content of m_ResponseData to extract m_GameId.
Definition: ModIo.cpp:631
Definition: path.h:84
Definition: Interface.h:93
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
Definition: ModIo.cpp:67
Definition: ModIo.h:76
std::string error
Definition: ModIo.h:79
DownloadProgressStatus status
Definition: ModIo.h:77
double progress
Definition: ModIo.h:78
Definition: ModIo.h:54
std::map< std::string, std::string > properties
Definition: ModIo.h:55
std::vector< std::string > dependencies
Definition: ModIo.h:56
SigStruct sig
Definition: ModIo.h:57
Definition: ModIo.h:40
unsigned char keynum[8]
Definition: ModIo.h:42
unsigned char sig_alg[2]
Definition: ModIo.h:41
unsigned char pk[crypto_sign_PUBLICKEYBYTES]
Definition: ModIo.h:43
Definition: ModIo.h:47
unsigned char sig_alg[2]
Definition: ModIo.h:48
unsigned char keynum[8]
Definition: ModIo.h:49
unsigned char sig[crypto_sign_BYTES]
Definition: ModIo.h:50