@@ -21,14 +21,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121#include " common/c_converter.h"
2222#include " common/c_content.h"
2323#include " lua_api/l_http.h"
24+ #include " cpp_api/s_security.h"
2425#include " httpfetch.h"
2526#include " settings.h"
2627#include " debug.h"
2728#include " log.h"
2829
29- #include < algorithm>
3030#include < iomanip>
31- #include < cctype>
3231
3332#define HTTP_API (name ) \
3433 lua_pushstring (L, #name); \
@@ -181,40 +180,8 @@ int ModApiHttp::l_request_http_api(lua_State *L)
181180{
182181 NO_MAP_LOCK_REQUIRED ;
183182
184- // We have to make sure that this function is being called directly by
185- // a mod, otherwise a malicious mod could override this function and
186- // steal its return value.
187- lua_Debug info;
188-
189- // Make sure there's only one item below this function on the stack...
190- if (lua_getstack (L, 2 , &info)) {
191- return 0 ;
192- }
193- FATAL_ERROR_IF (!lua_getstack (L, 1 , &info), " lua_getstack() failed" );
194- FATAL_ERROR_IF (!lua_getinfo (L, " S" , &info), " lua_getinfo() failed" );
195-
196- // ...and that that item is the main file scope.
197- if (strcmp (info.what , " main" ) != 0 ) {
198- return 0 ;
199- }
200-
201- // Mod must be listed in secure.http_mods or secure.trusted_mods
202- lua_rawgeti (L, LUA_REGISTRYINDEX , CUSTOM_RIDX_CURRENT_MOD_NAME );
203- if (!lua_isstring (L, -1 )) {
204- return 0 ;
205- }
206-
207- std::string mod_name = readParam<std::string>(L, -1 );
208- std::string http_mods = g_settings->get (" secure.http_mods" );
209- http_mods.erase (std::remove (http_mods.begin (), http_mods.end (), ' ' ), http_mods.end ());
210- std::vector<std::string> mod_list_http = str_split (http_mods, ' ,' );
211-
212- std::string trusted_mods = g_settings->get (" secure.trusted_mods" );
213- trusted_mods.erase (std::remove (trusted_mods.begin (), trusted_mods.end (), ' ' ), trusted_mods.end ());
214- std::vector<std::string> mod_list_trusted = str_split (trusted_mods, ' ,' );
215-
216- mod_list_http.insert (mod_list_http.end (), mod_list_trusted.begin (), mod_list_trusted.end ());
217- if (std::find (mod_list_http.begin (), mod_list_http.end (), mod_name) == mod_list_http.end ()) {
183+ if (!ScriptApiSecurity::checkWhitelisted (L, " secure.http_mods" ) &&
184+ !ScriptApiSecurity::checkWhitelisted (L, " secure.trusted_mods" )) {
218185 lua_pushnil (L);
219186 return 1 ;
220187 }
0 commit comments