Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refuse to open overly long URLs #70

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion engine/system/sys_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class find_c {
std::filesystem::directory_iterator iter;
};

std::string GetWineHostVersion();

// ==========
// Interfaces
// ==========
Expand All @@ -75,7 +77,7 @@ class sys_IMain {
virtual char* ClipboardPaste() = 0;
virtual bool SetWorkDir(std::filesystem::path const& newCwd = {}) = 0;
virtual void SpawnProcess(std::filesystem::path cmdName, const char* argList) = 0;
virtual void OpenURL(const char* url) = 0;
virtual std::optional<std::string> OpenURL(const char* url) = 0;
virtual void Error(const char* fmt, ...) = 0;
virtual void Exit(const char* msg = NULL) = 0;
virtual void Restart() = 0;
Expand Down
2 changes: 1 addition & 1 deletion engine/system/win/sys_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class sys_main_c: public sys_IMain {
char* ClipboardPaste();
bool SetWorkDir(std::filesystem::path const& newCwd = {});
void SpawnProcess(std::filesystem::path cmdName, const char* argList);
void OpenURL(const char* url);
std::optional<std::string> OpenURL(const char* url); // return value has failure reason
void Error(const char* fmt, ...);
void Exit(const char* msg = NULL);
void Restart();
Expand Down
3 changes: 2 additions & 1 deletion engine/system/win/sys_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <CoreFoundation/CFBundle.h>
#include <ApplicationServices/ApplicationServices.h>

void PlatformOpenURL(const char* textUrl)
const char* PlatformOpenURL(const char* textUrl)
{
std::string_view urlView = textUrl;
CFURLRef url = CFURLCreateWithBytes(nullptr, (const UInt8*)urlView.data(), urlView.size(), kCFStringEncodingUTF8, nullptr);
LSOpenCFURLRef(url, nullptr);
CFRelease(url);
return nullptr;
}
43 changes: 39 additions & 4 deletions engine/system/win/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,23 +434,58 @@ void sys_main_c::SpawnProcess(std::filesystem::path cmdName, const char* argList
#endif
}

std::string GetWineHostVersion()
{
#ifdef _WIN32
using WineHostVersionFun = void(const char** /*sysname*/, const char** /*release*/);
HMODULE mod = GetModuleHandleA("ntdll.dll");
if (!mod)
return "";
auto ptr = GetProcAddress(mod, "wine_get_host_version");
if (!ptr)
return "";
auto fun = (WineHostVersionFun*)ptr;
const char* sysname{};
const char* release{};
fun(&sysname, &release);
return sysname ? sysname : "";
#else
return "";
#endif
}

#if _WIN32 || __linux__
void PlatformOpenURL(const char* url)
const char* PlatformOpenURL(const char* url)
{
#ifdef _WIN32
const std::string wineHost = GetWineHostVersion();
/*
Wine has some loosely determined maximum length on how long of an URL
can be, so we pick a "safe" maximum and refuse to open anything longer.
*/
if ((wineHost == "Linux" || wineHost == "Darwin") && strlen(url) > 1500)
return AllocString("Did not open URL, length likely too long for the OS.");
ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWDEFAULT);
return nullptr;
#else
#warning LV: URL opening not implemented on this OS.
// TODO(LV): Implement URL opening for other OSes.
return AllocString("URL opening not implemented on this OS.");
#endif
}
#else
void PlatformOpenURL(const char* url);
const char* PlatformOpenURL(const char* url);
#endif

void sys_main_c::OpenURL(const char* url)
std::optional<std::string> sys_main_c::OpenURL(const char* url)
{
PlatformOpenURL(url);
if (auto err = PlatformOpenURL(url))
{
std::string ret = err;
FreeString(err);
return ret;
}
return {};
}

// ==============================
Expand Down
21 changes: 0 additions & 21 deletions engine/system/win/sys_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,6 @@ void sys_IVideo::FreeHandle(sys_IVideo* hnd)
delete (sys_video_c*)hnd;
}


static std::string GetWineHostVersion()
{
#ifdef _WIN32
using WineHostVersionFun = void (const char** /*sysname*/, const char** /*release*/);
HMODULE mod = GetModuleHandleA("ntdll.dll");
if (!mod)
return "";
auto ptr = GetProcAddress(mod, "wine_get_host_version");
if (!ptr)
return "";
auto fun = (WineHostVersionFun*)ptr;
const char* sysname{};
const char* release{};
fun(&sysname, &release);
return sysname ? sysname : "";
#else
return "";
#endif
}

sys_video_c::sys_video_c(sys_IMain* sysHnd)
: sys((sys_main_c*)sysHnd)
{
Expand Down
7 changes: 5 additions & 2 deletions ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
** ConPrintTable(table[, noRecurse])
** ConExecute("<cmd>")
** SpawnProcess("<cmdName>"[, "<args>"])
** OpenURL("<url>")
** err = OpenURL("<url>")
** SetProfiling(isEnabled)
** Restart()
** Exit(["<message>"])
Expand Down Expand Up @@ -1906,7 +1906,10 @@ static int l_OpenURL(lua_State* L)
int n = lua_gettop(L);
ui->LAssert(L, n >= 1, "Usage: OpenURL(url)");
ui->LAssert(L, lua_isstring(L, 1), "OpenURL() argument 1: expected string, got %s", luaL_typename(L, 1));
ui->sys->OpenURL(lua_tostring(L, 1));
if (auto errMsg = ui->sys->OpenURL(lua_tostring(L, 1))) {
lua_pushstring(L, errMsg->c_str());
return 1;
}
return 0;
}

Expand Down