From ce3b71f646a32d6b7504c8001435a5b3f6ff4f25 Mon Sep 17 00:00:00 2001 From: Rushaway Date: Thu, 3 Sep 2026 10:49:47 +0200 Subject: [PATCH 1/2] fix(game): harmonize SQL query formatting, truncation logging, and DataPack lifecycle Port of upstream sbpp/sourcebans-pp#1492, adapted to keep the fork's admin_name column/subquery in the bans and comms INSERT statements. - replace Escape + Format patterns with DB.Format/SQLiteDB.Format in main and comms - enforce placeholder policy: %!s for internal SQL identifiers/fragments, %s for runtime/user input - add query buffer truncation guards before query execution, standardized on LogError - allocate DataPack only after successful query formatting; add CleanupBanDataPack helper Co-Authored-By: Claude Sonnet 5 Co-authored-by: Rushaway --- game/addons/sourcemod/scripting/sbpp_comms.sp | 210 ++++++----- game/addons/sourcemod/scripting/sbpp_main.sp | 348 +++++++++++------- 2 files changed, 333 insertions(+), 225 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp_comms.sp b/game/addons/sourcemod/scripting/sbpp_comms.sp index eb19fae3c..957f3f561 100644 --- a/game/addons/sourcemod/scripting/sbpp_comms.sp +++ b/game/addons/sourcemod/scripting/sbpp_comms.sp @@ -353,22 +353,23 @@ public void VerifyBlock(int client) MarkClientAsGagged(client); } - char sClAuthYZEscaped[sizeof(g_sSteamIDs[]) * 2 + 1]; - g_hDatabase.Escape(g_sSteamIDs[client][8], sClAuthYZEscaped, sizeof(sClAuthYZEscaped)); - char Query[4096]; - FormatEx(Query, sizeof(Query), + if (g_hDatabase.Format(Query, sizeof(Query), "SELECT (c.ends - UNIX_TIMESTAMP()) AS remaining, \ c.length, c.type, c.created, c.reason, a.user, \ IF (a.immunity>=g.immunity, a.immunity, IFNULL(g.immunity,0)) AS immunity, \ c.aid, c.sid, a.authid \ - FROM %s_comms AS c \ - LEFT JOIN %s_admins AS a ON a.aid = c.aid \ - LEFT JOIN %s_srvgroups AS g ON g.name = a.srv_group \ + FROM %!s_comms AS c \ + LEFT JOIN %!s_admins AS a ON a.aid = c.aid \ + LEFT JOIN %!s_srvgroups AS g ON g.name = a.srv_group \ WHERE RemoveType IS NULL \ AND c.authid REGEXP '^STEAM_[0-9]:%s$' \ AND (length = '0' OR ends > UNIX_TIMESTAMP())", - DatabasePrefix, DatabasePrefix, DatabasePrefix, sClAuthYZEscaped); + DatabasePrefix, DatabasePrefix, DatabasePrefix, g_sSteamIDs[client][8]) >= sizeof(Query)) + { + LogError("VerifyBlock query truncated for %L", client); + return; + } #if defined LOG_QUERIES LogToFile(logQuery, "VerifyBlock for: %s. QUERY: %s", g_sSteamIDs[client], Query); #endif @@ -1557,18 +1558,20 @@ public void Query_UnBlockSelect(Database db, DBResultSet results, const char[] e newDataPack.WriteString(g_sName[target]); newDataPack.WriteString(targetAuth); - char unbanReason[sizeof(reason) * 2 + 1]; - db.Escape(reason, unbanReason, sizeof(unbanReason)); - char query[2048]; - Format(query, sizeof(query), - "UPDATE %s_comms \ + if (db.Format(query, sizeof(query), + "UPDATE %!s_comms \ SET RemovedBy = %d, \ RemoveType = 'U', \ RemovedOn = UNIX_TIMESTAMP(), \ ureason = '%s' \ WHERE bid = %d", - DatabasePrefix, iAID, unbanReason, bid); + DatabasePrefix, iAID, reason, bid) >= sizeof(query)) + { + LogError("Query_UnBlockSelect update query truncated"); + delete newDataPack; + continue; + } #if defined LOG_QUERIES LogToFile(logQuery, "Query_UnBlockSelect. QUERY: %s", query); #endif @@ -1713,12 +1716,6 @@ public void Query_ProcessQueue(Database db, DBResultSet results, const char[] er if (!results.FetchRow()) continue; - char sAuthEscaped[sizeof(auth) * 2 + 1]; - char banName[MAX_NAME_LENGTH * 2 + 1]; - char banReason[sizeof(reason) * 2 + 1]; - char sAdmAuthEscaped[sizeof(adminAuth) * 2 + 1]; - char sAdmAuthYZEscaped[sizeof(adminAuth) * 2 + 1]; - // if we get to here then there are rows in the queue pending processing //steam_id TEXT, time INTEGER, start_time INTEGER, reason TEXT, name TEXT, admin_id TEXT, admin_ip TEXT, type INTEGER int id = results.FetchInt(0); @@ -1731,23 +1728,20 @@ public void Query_ProcessQueue(Database db, DBResultSet results, const char[] er results.FetchString(7, adminIp, sizeof(adminIp)); int type = results.FetchInt(8); - if (DB_Connect()) { - db.Escape(auth, sAuthEscaped, sizeof(sAuthEscaped)); - db.Escape(name, banName, sizeof(banName)); - db.Escape(reason, banReason, sizeof(banReason)); - db.Escape(adminAuth, sAdmAuthEscaped, sizeof(sAdmAuthEscaped)); - db.Escape(adminAuth[8], sAdmAuthYZEscaped, sizeof(sAdmAuthYZEscaped)); - } - else + if (!DB_Connect()) continue; // all blocks should be entered into db! - FormatEx(query, sizeof(query), - "INSERT INTO %s_comms (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, type) \ + if (db.Format(query, sizeof(query), + "INSERT INTO %!s_comms (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, type) \ VALUES ('%s', '%s', %d, %d, %d, '%s', \ - IFNULL((SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '0'), \ - '%s', IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), %d, %d)", - DatabasePrefix, sAuthEscaped, banName, startTime, (startTime + (time * 60)), (time * 60), banReason, DatabasePrefix, sAdmAuthEscaped, sAdmAuthYZEscaped, adminIp, DatabasePrefix, sAdmAuthEscaped, sAdmAuthYZEscaped, serverID, type); + IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '0'), \ + '%s', IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), %d, %d)", + DatabasePrefix, auth, name, startTime, (startTime + (time * 60)), (time * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID, type) >= sizeof(query)) + { + LogError("Query_ProcessQueue insert query truncated"); + continue; + } #if defined LOG_QUERIES LogToFile(logQuery, "Query_ProcessQueue. QUERY: %s", query); #endif @@ -1761,10 +1755,14 @@ public void Query_AddBlockFromQueue(Database db, DBResultSet results, const char if (error[0] == '\0') { // The insert was successful so delete the record from the queue - FormatEx(query, sizeof(query), + if (SQLiteDB.Format(query, sizeof(query), "DELETE FROM queue2 \ WHERE id = %d", - data); + data) >= sizeof(query)) + { + LogError("Query_AddBlockFromQueue delete query truncated"); + return; + } #if defined LOG_QUERIES LogToFile(logQuery, "Query_AddBlockFromQueue. QUERY: %s", query); #endif @@ -2593,43 +2591,37 @@ stock void ProcessUnBlock(int client, int targetId = 0, int type, char[] sReason } } - // Pack everything into a data pack so we can retain it - DataPack dataPack = new DataPack(); - dataPack.WriteCell(GetClientUserId2(client)); - dataPack.WriteCell(g_iUserIDs[target]); - dataPack.WriteCell(type); - dataPack.WriteString(adminAuth); - dataPack.WriteString(targetAuth); - dataPack.WriteString(reason); - // Check current player status. If player has temporary punishment - don't get info from DB if (DB_Connect()) { - char sAdminAuthEscaped[sizeof(adminAuth) * 2 + 1]; - char sAdminAuthYZEscaped[sizeof(adminAuth) * 2 + 1]; - char sTargetAuthEscaped[sizeof(targetAuth) * 2 + 1]; - char sTargetAuthYZEscaped[sizeof(targetAuth) * 2 + 1]; - - g_hDatabase.Escape(adminAuth, sAdminAuthEscaped, sizeof(sAdminAuthEscaped)); - g_hDatabase.Escape(adminAuth[8], sAdminAuthYZEscaped, sizeof(sAdminAuthYZEscaped)); - g_hDatabase.Escape(targetAuth, sTargetAuthEscaped, sizeof(sTargetAuthEscaped)); - g_hDatabase.Escape(targetAuth[8], sTargetAuthYZEscaped, sizeof(sTargetAuthYZEscaped)); - char query[4096]; - Format(query, sizeof(query), + if (g_hDatabase.Format(query, sizeof(query), "SELECT c.bid, \ - IFNULL((SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '0') as iaid, \ + IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '0') as iaid, \ c.aid, \ IF (a.immunity>=g.immunity, a.immunity, IFNULL(g.immunity,0)) as immunity, \ c.type \ - FROM %s_comms AS c \ - LEFT JOIN %s_admins AS a ON a.aid = c.aid \ - LEFT JOIN %s_srvgroups AS g ON g.name = a.srv_group \ + FROM %!s_comms AS c \ + LEFT JOIN %!s_admins AS a ON a.aid = c.aid \ + LEFT JOIN %!s_srvgroups AS g ON g.name = a.srv_group \ WHERE RemoveType IS NULL \ AND (c.authid = '%s' OR c.authid REGEXP '^STEAM_[0-9]:%s$') \ AND (length = '0' OR ends > UNIX_TIMESTAMP()) \ - AND %s", - DatabasePrefix, sAdminAuthEscaped, sAdminAuthYZEscaped, DatabasePrefix, DatabasePrefix, DatabasePrefix, sTargetAuthEscaped, sTargetAuthYZEscaped, typeWHERE); + AND %!s", + DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, DatabasePrefix, DatabasePrefix, targetAuth, targetAuth[8], typeWHERE) >= sizeof(query)) + { + LogError("ProcessUnBlock select query truncated"); + return; + } + + // Pack everything into a data pack so we can retain it + DataPack dataPack = new DataPack(); + dataPack.WriteCell(GetClientUserId2(client)); + dataPack.WriteCell(g_iUserIDs[target]); + dataPack.WriteCell(type); + dataPack.WriteString(adminAuth); + dataPack.WriteString(targetAuth); + dataPack.WriteString(reason); #if defined LOG_QUERIES LogToFile(logQuery, "ProcessUnBlock. QUERY: %s", query); @@ -2639,6 +2631,14 @@ stock void ProcessUnBlock(int client, int targetId = 0, int type, char[] sReason } else { + DataPack dataPack = new DataPack(); + dataPack.WriteCell(GetClientUserId2(client)); + dataPack.WriteCell(g_iUserIDs[target]); + dataPack.WriteCell(type); + dataPack.WriteString(adminAuth); + dataPack.WriteString(targetAuth); + dataPack.WriteString(reason); + #if defined DEBUG PrintToServer("Calling TempUnBlock from ProcessUnBlock"); #endif @@ -2754,23 +2754,17 @@ stock void InsertTempBlock(int length, int type, const char[] name, const char[] { LogMessage("Saving punishment for %s into queue", auth); - char banName[MAX_NAME_LENGTH * 2 + 1]; - char banReason[256 * 2 + 1]; - char sAuthEscaped[MAX_AUTHID_LENGTH * 2 + 1]; - char sAdminAuthEscaped[MAX_AUTHID_LENGTH * 2 + 1]; char sQuery[4096], sQueryVal[2048]; char sQueryMute[2048], sQueryGag[2048]; - // escaping everything - SQLiteDB.Escape(name, banName, sizeof(banName)); - SQLiteDB.Escape(reason, banReason, sizeof(banReason)); - SQLiteDB.Escape(auth, sAuthEscaped, sizeof(sAuthEscaped)); - SQLiteDB.Escape(adminAuth, sAdminAuthEscaped, sizeof(sAdminAuthEscaped)); - // steam_id time start_time reason name admin_id admin_ip - FormatEx(sQueryVal, sizeof(sQueryVal), + if (SQLiteDB.Format(sQueryVal, sizeof(sQueryVal), "'%s', %d, %d, '%s', '%s', '%s', '%s'", - sAuthEscaped, length, GetTime(), banReason, banName, sAdminAuthEscaped, adminIp); + auth, length, GetTime(), reason, name, adminAuth, adminIp) >= sizeof(sQueryVal)) + { + LogError("InsertTempBlock values query truncated"); + return; + } switch (type) { @@ -2783,9 +2777,13 @@ stock void InsertTempBlock(int length, int type, const char[] name, const char[] } } - FormatEx(sQuery, sizeof(sQuery), + if (SQLiteDB.Format(sQuery, sizeof(sQuery), "INSERT INTO queue2 (steam_id, time, start_time, reason, name, admin_id, admin_ip, type) VALUES %s%s%s", - sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag); + sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery)) + { + LogError("InsertTempBlock insert query truncated"); + return; + } #if defined LOG_QUERIES LogToFile(logQuery, "InsertTempBlock. QUERY: %s", sQuery); @@ -3158,46 +3156,50 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, if (DB_Connect()) { // Accepts length in minutes, writes to db in seconds! In all over places in plugin - length is in minutes. - char banName[MAX_NAME_LENGTH * 2 + 1]; - char banReason[256 * 2 + 1]; - char sAuthidEscaped[MAX_AUTHID_LENGTH * 2 + 1]; - char sAdminAuthIdEscaped[MAX_AUTHID_LENGTH * 2 + 1]; - char sAdminAuthIdYZEscaped[MAX_AUTHID_LENGTH * 2 + 1]; char sQuery[4096], sQueryAdm[512], sQueryVal[1024]; char sQueryMute[1024], sQueryGag[1024]; sQueryMute[0] = 0; sQueryGag[0] = 0; - // escaping everything - g_hDatabase.Escape(sName, banName, sizeof(banName)); - g_hDatabase.Escape(reason, banReason, sizeof(banReason)); - g_hDatabase.Escape(targetAuth, sAuthidEscaped, sizeof(sAuthidEscaped)); - g_hDatabase.Escape(adminAuth, sAdminAuthIdEscaped, sizeof(sAdminAuthIdEscaped)); - g_hDatabase.Escape(adminAuth[8], sAdminAuthIdYZEscaped, sizeof(sAdminAuthIdYZEscaped)); - // bid authid name created ends lenght reason aid adminip sid removedBy removedType removedon type ureason - FormatEx(sQueryAdm, sizeof(sQueryAdm), - "IFNULL((SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), 0)", - DatabasePrefix, sAdminAuthIdEscaped, sAdminAuthIdYZEscaped); + if (g_hDatabase.Format(sQueryAdm, sizeof(sQueryAdm), + "IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), 0)", + DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdm)) + { + LogError("SavePunishment admin subquery truncated"); + return; + } char sQueryAdmName[512]; - FormatEx(sQueryAdmName, sizeof(sQueryAdmName), - "IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '')", - DatabasePrefix, sAdminAuthIdEscaped, sAdminAuthIdYZEscaped); + if (g_hDatabase.Format(sQueryAdmName, sizeof(sQueryAdmName), + "IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '')", + DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdmName)) + { + LogError("SavePunishment admin name subquery truncated"); + return; + } if (length >= 0) { // authid name, created, ends, length, reason, aid, adminIp, admin_name, sid - FormatEx(sQueryVal, sizeof(sQueryVal), - "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %s, '%s', %s, %d", - sAuthidEscaped, banName, length * 60, length * 60, banReason, sQueryAdm, adminIp, sQueryAdmName, serverID); + if (g_hDatabase.Format(sQueryVal, sizeof(sQueryVal), + "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %!s, '%s', %!s, %d", + targetAuth, sName, length * 60, length * 60, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal)) + { + LogError("SavePunishment values query truncated"); + return; + } } else // Session mutes { // authid name, created, ends, length, reason, aid, adminIp, admin_name, sid - FormatEx(sQueryVal, sizeof(sQueryVal), - "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %s, '%s', %s, %d", - sAuthidEscaped, banName, SESSION_MUTE_FALLBACK, -1, banReason, sQueryAdm, adminIp, sQueryAdmName, serverID); + if (g_hDatabase.Format(sQueryVal, sizeof(sQueryVal), + "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %!s, '%s', %!s, %d", + targetAuth, sName, SESSION_MUTE_FALLBACK, -1, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal)) + { + LogError("SavePunishment values query truncated"); + return; + } } switch (type) @@ -3214,9 +3216,13 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, } // litle magic - one query for all actions (mute, gag or silence) - FormatEx(sQuery, sizeof(sQuery), - "INSERT INTO %s_comms (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, type) VALUES %s%s%s", - DatabasePrefix, sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag); + if (g_hDatabase.Format(sQuery, sizeof(sQuery), + "INSERT INTO %!s_comms (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, type) VALUES %!s%!s%!s", + DatabasePrefix, sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery)) + { + LogError("SavePunishment insert query truncated"); + return; + } #if defined LOG_QUERIES LogToFile(logQuery, "SavePunishment. QUERY: %s", sQuery); diff --git a/game/addons/sourcemod/scripting/sbpp_main.sp b/game/addons/sourcemod/scripting/sbpp_main.sp index 75746aa6c..94f840f62 100644 --- a/game/addons/sourcemod/scripting/sbpp_main.sp +++ b/game/addons/sourcemod/scripting/sbpp_main.sp @@ -348,8 +348,12 @@ public void OnClientAuthorized(int client, const char[] auth) if (PlayerStatus[client]) return; - char Query[256]; - FormatEx(Query, sizeof(Query), "SELECT bid, ip FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, g_sSteamIDs[client][8], g_sPlayerIP[client]); + char Query[512]; + if (DB.Format(Query, sizeof(Query), "SELECT bid, ip FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, g_sSteamIDs[client][8], g_sPlayerIP[client]) >= sizeof(Query)) + { + LogError("OnClientAuthorized query truncated for %L", client); + return; + } #if defined DEBUG LogToFile(logFile, "Checking ban for: %s", g_sSteamIDs[client]); @@ -565,6 +569,14 @@ public Action CommandBanIp(int client, int args) strcopy(adminIp, sizeof(adminIp), g_sPlayerIP[client]); } + char sQuery[256]; + if (DB.Format(sQuery, sizeof(sQuery), "SELECT bid FROM %!s_bans WHERE type = 1 AND ip = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", + DatabasePrefix, arg) >= sizeof(sQuery)) + { + LogError("CommandBanIp query truncated"); + return Plugin_Handled; + } + // Pack everything into a data pack so we can retain it DataPack dataPack = new DataPack(); dataPack.WriteCell(client); @@ -576,12 +588,6 @@ public Action CommandBanIp(int client, int args) dataPack.WriteString(adminAuth); dataPack.WriteString(adminIp); - char sQuery[256], argEscaped[sizeof(arg) * 2 + 1]; - DB.Escape(arg, argEscaped, sizeof(argEscaped)); - - FormatEx(sQuery, sizeof(sQuery), "SELECT bid FROM %s_bans WHERE type = 1 AND ip = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", - DatabasePrefix, argEscaped); - DB.Query(SelectBanIpCallback, sQuery, dataPack, DBPrio_High); return Plugin_Handled; @@ -620,6 +626,23 @@ public Action CommandUnban(int client, int args) strcopy(adminAuth, sizeof(adminAuth), g_sSteamIDs[client]); } + char query[256]; + + if (strncmp(arg, "STEAM_", 6) == 0) + { + if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 0 AND authid = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query)) + { + LogError("CommandUnban query truncated (steam)"); + return Plugin_Handled; + } + } else { + if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 1 AND ip = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query)) + { + LogError("CommandUnban query truncated (ip)"); + return Plugin_Handled; + } + } + // Pack everything into a data pack so we can retain it DataPack dataPack = new DataPack(); dataPack.WriteCell(client); @@ -627,16 +650,6 @@ public Action CommandUnban(int client, int args) dataPack.WriteString(arg); // Steamid - IP dataPack.WriteString(adminAuth); // Admin SteamID - char query[256], argEscaped[sizeof(arg) * 2 + 1]; - DB.Escape(arg, argEscaped, sizeof(argEscaped)); - - if (strncmp(arg, "STEAM_", 6) == 0) - { - Format(query, sizeof(query), "SELECT bid FROM %s_bans WHERE (type = 0 AND authid = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, argEscaped); - } else { - Format(query, sizeof(query), "SELECT bid FROM %s_bans WHERE (type = 1 AND ip = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, argEscaped); - } - DB.Query(SelectUnbanCallback, query, dataPack); return Plugin_Handled; @@ -707,6 +720,14 @@ public Action CommandAddBan(int client, int args) strcopy(adminIp, sizeof(adminIp), g_sPlayerIP[client]); } + char sQuery[256]; + if (DB.Format(sQuery, sizeof sQuery, "SELECT bid FROM %!s_bans WHERE type = 0 AND authid = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", + DatabasePrefix, authid) >= sizeof(sQuery)) + { + LogError("CommandAddBan query truncated"); + return Plugin_Handled; + } + // Pack everything into a data pack so we can retain it DataPack dataPack = new DataPack(); dataPack.WriteCell(client); @@ -716,12 +737,6 @@ public Action CommandAddBan(int client, int args) dataPack.WriteString(adminAuth); dataPack.WriteString(adminIp); - char sQuery[256], authidEscaped[sizeof(authid) * 2 + 1]; - DB.Escape(authid, authidEscaped, sizeof(authidEscaped)); - - FormatEx(sQuery, sizeof sQuery, "SELECT bid FROM %s_bans WHERE type = 0 AND authid = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", - DatabasePrefix, authidEscaped); - DB.Query(SelectAddbanCallback, sQuery, dataPack, DBPrio_High); return Plugin_Handled; @@ -1201,6 +1216,8 @@ public void VerifyInsert(Database db, DBResultSet results, const char[] error, D dataPack.ReadString(Name, sizeof(Name)); ReasonPack.ReadString(Reason, sizeof(Reason)); + delete dataPack; + if (!time) { if (Reason[0] == '\0') @@ -1242,8 +1259,8 @@ public void VerifyInsert(Database db, DBResultSet results, const char[] error, D public void SelectBanIpCallback(Database db, DBResultSet results, const char[] error, DataPack dataPack) { int admin, minutes; - char adminAuth[MAX_AUTHID_LENGTH], adminIp[16], banReason[256], ip[16], reason[128], Query[1536]; - char targetName[MAX_NAME_LENGTH], sTEscapedName[MAX_NAME_LENGTH * 2 + 1], targetAuth[MAX_AUTHID_LENGTH]; + char adminAuth[MAX_AUTHID_LENGTH], adminIp[16], ip[16], reason[128], Query[2048]; + char targetName[MAX_NAME_LENGTH], targetAuth[MAX_AUTHID_LENGTH]; dataPack.Reset(); admin = dataPack.ReadCell(); @@ -1254,8 +1271,6 @@ public void SelectBanIpCallback(Database db, DBResultSet results, const char[] e dataPack.ReadString(targetAuth, sizeof(targetAuth)); dataPack.ReadString(adminAuth, sizeof(adminAuth)); dataPack.ReadString(adminIp, sizeof(adminIp)); - DB.Escape(reason, banReason, sizeof(banReason)); - DB.Escape(targetName, sTEscapedName, sizeof(sTEscapedName)); if (results == null) { @@ -1265,6 +1280,7 @@ public void SelectBanIpCallback(Database db, DBResultSet results, const char[] e else PrintToServer("%s%t", Prefix, "Ban Fail"); + delete dataPack; return; } if (results.RowCount) @@ -1274,21 +1290,32 @@ public void SelectBanIpCallback(Database db, DBResultSet results, const char[] e else PrintToServer("%s%t", Prefix, "Already Banned", ip); + delete dataPack; return; } if (serverID == -1) { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (type, ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ - (SELECT sid FROM %s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, ip, targetAuth, sTEscapedName, (minutes * 60), (minutes * 60), banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort); + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (type, ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", + DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + { + LogError("SelectBanIpCallback insert query truncated"); + delete dataPack; + return; + } } else { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (type, ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (type, ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, ip, targetAuth, sTEscapedName, (minutes * 60), (minutes * 60), banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID); + DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query)) + { + LogError("SelectBanIpCallback insert query truncated"); + delete dataPack; + return; + } } db.Query(InsertBanIpCallback, Query, dataPack, DBPrio_High); @@ -1361,7 +1388,7 @@ public void InsertBanIpCallback(Database db, DBResultSet results, const char[] e public void SelectUnbanCallback(Database db, DBResultSet results, const char[] error, DataPack dataPack) { int admin; - char arg[MAX_AUTHID_LENGTH], adminAuth[MAX_AUTHID_LENGTH], unbanReason[256]; + char arg[MAX_AUTHID_LENGTH], adminAuth[MAX_AUTHID_LENGTH]; char reason[128]; dataPack.Reset(); @@ -1370,8 +1397,6 @@ public void SelectUnbanCallback(Database db, DBResultSet results, const char[] e dataPack.ReadString(arg, sizeof(arg)); // SteamID - IP dataPack.ReadString(adminAuth, sizeof(adminAuth)); // Admin SteamID - db.Escape(reason, unbanReason, sizeof(unbanReason)); - // If error is not an empty string the query failed if (results == null) { @@ -1404,8 +1429,13 @@ public void SelectUnbanCallback(Database db, DBResultSet results, const char[] e int bid = results.FetchInt(0); char query[1024]; - Format(query, sizeof(query), "UPDATE %s_bans SET RemovedBy = (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), RemoveType = 'U', RemovedOn = UNIX_TIMESTAMP(), ureason = '%s' WHERE bid = %d", - DatabasePrefix, DatabasePrefix, adminAuth, adminAuth[8], unbanReason, bid); + if (db.Format(query, sizeof(query), "UPDATE %!s_bans SET RemovedBy = (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), RemoveType = 'U', RemovedOn = UNIX_TIMESTAMP(), ureason = '%s' WHERE bid = %d", + DatabasePrefix, DatabasePrefix, adminAuth, adminAuth[8], reason, bid) >= sizeof(query)) + { + LogError("SelectUnbanCallback update query truncated"); + delete dataPack; + return; + } db.Query(InsertUnbanCallback, query, dataPack); } @@ -1453,7 +1483,7 @@ public void InsertUnbanCallback(Database db, DBResultSet results, const char[] e public void SelectAddbanCallback(Database db, DBResultSet results, const char[] error, DataPack dataPack) { int admin, minutes; - char adminAuth[MAX_AUTHID_LENGTH], adminIp[16], authid[MAX_AUTHID_LENGTH], banReason[256], Query[1536]; + char adminAuth[MAX_AUTHID_LENGTH], adminIp[16], authid[MAX_AUTHID_LENGTH], Query[2048]; char reason[128]; dataPack.Reset(); @@ -1463,7 +1493,6 @@ public void SelectAddbanCallback(Database db, DBResultSet results, const char[] dataPack.ReadString(authid, sizeof(authid)); dataPack.ReadString(adminAuth, sizeof(adminAuth)); dataPack.ReadString(adminIp, sizeof(adminIp)); - db.Escape(reason, banReason, sizeof(banReason)); if (results == null) { @@ -1474,6 +1503,7 @@ public void SelectAddbanCallback(Database db, DBResultSet results, const char[] else PrintToServer("%s%t", Prefix, "Ban Fail"); + delete dataPack; return; } if (results.RowCount) @@ -1483,21 +1513,32 @@ public void SelectAddbanCallback(Database db, DBResultSet results, const char[] else PrintToServer("%s%t", Prefix, "Already Banned", authid); + delete dataPack; return; } if (serverID == -1) { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ - (SELECT sid FROM %s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, authid, (minutes * 60), (minutes * 60), banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort); + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", + DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + { + LogError("SelectAddbanCallback insert query truncated"); + delete dataPack; + return; + } } else { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, authid, (minutes * 60), (minutes * 60), banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID); + DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query)) + { + LogError("SelectAddbanCallback insert query truncated"); + delete dataPack; + return; + } } db.Query(InsertAddbanCallback, Query, dataPack, DBPrio_High); @@ -1552,9 +1593,7 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] char ip[16]; char adminAuth[MAX_AUTHID_LENGTH]; char adminIp[16]; - char query[1536]; - char banName[MAX_NAME_LENGTH]; - char banReason[256]; + char query[2048]; while (results.MoreRows) { // Oh noes! What happened?! @@ -1570,28 +1609,34 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] results.FetchString(5, ip, sizeof(ip)); results.FetchString(6, adminAuth, sizeof(adminAuth)); results.FetchString(7, adminIp, sizeof(adminIp)); - db.Escape(name, banName, sizeof(banName)); - db.Escape(reason, banReason, sizeof(banReason)); if (startTime + time * 60 > GetTime() || time == 0) { // This ban is still valid and should be entered into the db if (serverID == -1) { - FormatEx(query, sizeof(query), - "INSERT INTO %s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid) VALUES \ - ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ - (SELECT sid FROM %s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1))", - DatabasePrefix, ip, auth, banName, startTime, startTime + time * 60, time * 60, banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort); + if (db.Format(query, sizeof(query), + "INSERT INTO %!s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid) VALUES \ + ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1))", + DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(query)) + { + LogError("ProcessQueueCallback insert query truncated"); + continue; + } } else { - FormatEx(query, sizeof(query), - "INSERT INTO %s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid) VALUES \ - ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + if (db.Format(query, sizeof(query), + "INSERT INTO %!s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid) VALUES \ + ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d)", - DatabasePrefix, ip, auth, banName, startTime, startTime + time * 60, time * 60, banReason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID); + DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(query)) + { + LogError("ProcessQueueCallback insert query truncated"); + continue; + } } DataPack authPack = new DataPack(); authPack.WriteString(auth); @@ -1599,8 +1644,12 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] db.Query(AddedFromSQLiteCallback, query, authPack); } else { // The ban is no longer valid and should be deleted from the queue - FormatEx(query, sizeof(query), "DELETE FROM queue WHERE steam_id = '%s'", auth); - SQLiteDB.Query(ErrorCheckCallback, query); + if (db.Format(query, sizeof(query), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(query)) + { + LogError("ProcessQueueCallback delete query truncated"); + continue; + } + db.Query(ErrorCheckCallback, query); } } // We have finished processing the queue but should process again in ProcessQueueTime minutes @@ -1616,7 +1665,12 @@ public void AddedFromSQLiteCallback(Database db, DBResultSet results, const char if (results != null) { // The insert was successful so delete the record from the queue - FormatEx(buffer, sizeof(buffer), "DELETE FROM queue WHERE steam_id = '%s'", auth); + if (SQLiteDB.Format(buffer, sizeof(buffer), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(buffer)) + { + LogError("AddedFromSQLiteCallback delete query truncated"); + delete dataPack; + return; + } SQLiteDB.Query(ErrorCheckCallback, buffer); // They are added to main banlist, so remove the temp ban @@ -1642,7 +1696,6 @@ public void ServerInfoCallback(Database db, DBResultSet results, const char[] er { // get the game folder name used to determine the mod char desc[64], query[512], rcon[128]; - char descEscaped[sizeof(desc) * 2 + 1], rconEscaped[sizeof(rcon) * 2 + 1]; GetGameFolderName(desc, sizeof(desc)); Format(rcon, sizeof(rcon), ""); @@ -1655,9 +1708,11 @@ public void ServerInfoCallback(Database db, DBResultSet results, const char[] er } } - db.Escape(desc, descEscaped, sizeof(descEscaped)); - db.Escape(rcon, rconEscaped, sizeof(rconEscaped)); - FormatEx(query, sizeof(query), "INSERT INTO %s_servers (ip, port, rcon, modid) VALUES ('%s', '%s', '%s', (SELECT mid FROM %s_mods WHERE modfolder = '%s'))", DatabasePrefix, ServerIp, ServerPort, rconEscaped, DatabasePrefix, descEscaped); + if (db.Format(query, sizeof(query), "INSERT INTO %!s_servers (ip, port, rcon, modid) VALUES ('%s', '%s', '%s', (SELECT mid FROM %!s_mods WHERE modfolder = '%s'))", DatabasePrefix, ServerIp, ServerPort, rcon, DatabasePrefix, desc) >= sizeof(query)) + { + LogError("ServerInfoCallback insert query truncated"); + return; + } db.Query(ErrorCheckCallback, query); } } @@ -1692,7 +1747,7 @@ public void VerifyBan(Database db, DBResultSet results, const char[] error, int if (results.RowCount > 0) { - char buffer[40], Name[MAX_NAME_LENGTH], Query[512]; + char buffer[40], Query[512]; // Amending to ban record's IP field if (results.FetchRow()) @@ -1706,31 +1761,46 @@ public void VerifyBan(Database db, DBResultSet results, const char[] error, int { char sQuery[256]; - FormatEx(sQuery, sizeof sQuery, "UPDATE %s_bans SET `ip` = '%s' WHERE `bid` = '%d'", DatabasePrefix, clientIp, iBid); - - DB.Query(SQL_OnIPMend, sQuery, client); + if (DB.Format(sQuery, sizeof sQuery, "UPDATE %!s_bans SET `ip` = '%s' WHERE `bid` = '%d'", DatabasePrefix, clientIp, iBid) < sizeof(sQuery)) + { + DB.Query(SQL_OnIPMend, sQuery, client); + } + else + { + LogError("VerifyBan mend IP query truncated for %L", client); + } } } - DB.Escape(g_sName[client], Name, sizeof Name); - if (serverID == -1) { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_banlog (sid ,time ,name ,bid) VALUES \ - ((SELECT sid FROM %s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), UNIX_TIMESTAMP(), '%s', \ - (SELECT bid FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", - DatabasePrefix, DatabasePrefix, ServerIp, ServerPort, Name, DatabasePrefix, clientAuth[8], clientIp); + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_banlog (sid ,time ,name ,bid) VALUES \ + ((SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), UNIX_TIMESTAMP(), '%s', \ + (SELECT bid FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", + DatabasePrefix, DatabasePrefix, ServerIp, ServerPort, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query)) + { + db.Query(ErrorCheckCallback, Query, client, DBPrio_High); + } + else + { + LogError("VerifyBan banlog query truncated for %L", client); + } } else { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_banlog (sid ,time ,name ,bid) VALUES \ + if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_banlog (sid ,time ,name ,bid) VALUES \ (%d, UNIX_TIMESTAMP(), '%s', \ - (SELECT bid FROM %s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", - DatabasePrefix, serverID, Name, DatabasePrefix, clientAuth[8], clientIp); + (SELECT bid FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", + DatabasePrefix, serverID, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query)) + { + db.Query(ErrorCheckCallback, Query, client, DBPrio_High); + } + else + { + LogError("VerifyBan banlog query truncated for %L", client); + } } - db.Query(ErrorCheckCallback, Query, client, DBPrio_High); - FormatEx(buffer, sizeof(buffer), "banid 5 %s", clientAuth); ServerCommand(buffer); KickClient(client, "%t", "Banned Check Site", WebsiteAddress); @@ -2408,17 +2478,14 @@ public int Native_SBReportPlayer(Handle plugin, int numParams) GetNativeString(3, sReason, iReasonLen); - char sREscapedName[MAX_NAME_LENGTH * 2 + 1], sTEscapedName[MAX_NAME_LENGTH * 2 + 1]; - char[] sEscapedReason = new char[iReasonLen * 2 + 1]; - - DB.Escape(g_sName[iReporter], sREscapedName, sizeof sREscapedName); - DB.Escape(g_sName[iTarget], sTEscapedName, sizeof sTEscapedName); - DB.Escape(sReason, sEscapedReason, iReasonLen * 2 + 1); - - char[] sQuery = new char[512 + (iReasonLen * 2 + 1)]; - - Format(sQuery, 512 + (iReasonLen * 2 + 1), "INSERT INTO %s_submissions (`submitted`, `modid`, `SteamId`, `name`, `email`, `reason`, `ip`, `subname`, `sip`, `archiv`, `server`)" - ... "VALUES ('%d', 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, '%d')", DatabasePrefix, iTime, g_sSteamIDs[iTarget], sTEscapedName, g_sSteamIDs[iReporter], sEscapedReason, g_sPlayerIP[iReporter], sREscapedName, g_sPlayerIP[iTarget], (serverID != -1) ? serverID : 0); + int iQueryLen = 768 + (iReasonLen * 2 + 1); + char[] sQuery = new char[iQueryLen]; + if (DB.Format(sQuery, iQueryLen, "INSERT INTO %!s_submissions (`submitted`, `modid`, `SteamId`, `name`, `email`, `reason`, `ip`, `subname`, `sip`, `archiv`, `server`)" + ... "VALUES ('%d', 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, '%d')", DatabasePrefix, iTime, g_sSteamIDs[iTarget], g_sName[iTarget], g_sSteamIDs[iReporter], sReason, g_sPlayerIP[iReporter], g_sName[iReporter], g_sPlayerIP[iTarget], (serverID != -1) ? serverID : 0) >= iQueryLen) + { + ThrowNativeError(SP_ERROR_NATIVE, "Failed to build report query (buffer too small)"); + return 0; + } DataPack dataPack = new DataPack(); @@ -2434,7 +2501,10 @@ public int Native_SBReportPlayer(Handle plugin, int numParams) public void SQL_OnReportPlayer(Database db, DBResultSet results, const char[] error, DataPack dataPack) { if (results == null) + { LogToFile(logFile, "Failed to submit report: %s", error); + delete dataPack; + } else { dataPack.Reset(); @@ -2552,28 +2622,60 @@ public bool CreateBan(int client, int target, int time, const char[] reason) return true; } +stock void CleanupBanDataPack(DataPack pack) +{ + if (pack == null) + { + return; + } + + pack.Reset(); + pack.ReadCell(); // admin index + pack.ReadCell(); // target index + pack.ReadCell(); // admin userid + pack.ReadCell(); // target userid + pack.ReadCell(); // time + + DataPack reasonPack = view_as(pack.ReadCell()); + if (reasonPack != null) + delete reasonPack; + + delete pack; +} + stock void UTIL_InsertBan(int time, const char[] Name, const char[] Authid, const char[] Ip, const char[] Reason, const char[] AdminAuthid, const char[] AdminIp, DataPack dataPack) { + if (dataPack == null) + { + return; + } + //new Handle:dummy; //PruneBans(dummy); - char banName[MAX_NAME_LENGTH]; - char banReason[256]; - char Query[1536]; - DB.Escape(Name, banName, sizeof(banName)); - DB.Escape(Reason, banReason, sizeof(banReason)); + char Query[2048]; if (serverID == -1) { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ - (SELECT sid FROM %s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, Ip, Authid, banName, (time * 60), (time * 60), banReason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], DatabasePrefix, ServerIp, ServerPort); + if (DB.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", + DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + { + LogError("UTIL_InsertBan query truncated"); + CleanupBanDataPack(dataPack); + return; + } } else { - FormatEx(Query, sizeof(Query), "INSERT INTO %s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ - ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ - IFNULL((SELECT user FROM %s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ + if (DB.Format(Query, sizeof(Query), "INSERT INTO %!s_bans (ip, authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, country) VALUES \ + ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ + IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, Ip, Authid, banName, (time * 60), (time * 60), banReason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], serverID); + DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], serverID) >= sizeof(Query)) + { + LogError("UTIL_InsertBan query truncated"); + CleanupBanDataPack(dataPack); + return; + } } DB.Query(VerifyInsert, Query, dataPack, DBPrio_High); } @@ -2611,13 +2713,13 @@ stock void UTIL_InsertTempBan(int time, const char[] name, const char[] auth, co KickClient(client, "%t\n\n%t", "Banned Check Site", WebsiteAddress, "Kick Reason", admin, reason, length); } - char banName[MAX_NAME_LENGTH], banReason[256], query[512]; - - SQLiteDB.Escape(name, banName, sizeof(banName)); - SQLiteDB.Escape(reason, banReason, sizeof(banReason)); - - FormatEx(query, sizeof(query), "INSERT INTO queue VALUES ('%s', %i, %i, '%s', '%s', '%s', '%s', '%s')", - auth, time, GetTime(), banReason, banName, ip, adminAuth, adminIp); + char query[1024]; + if (SQLiteDB.Format(query, sizeof(query), "INSERT INTO queue VALUES ('%s', %i, %i, '%s', '%s', '%s', '%s', '%s')", + auth, time, GetTime(), reason, name, ip, adminAuth, adminIp) >= sizeof(query)) + { + LogError("UTIL_InsertTempBan query truncated"); + return; + } SQLiteDB.Query(ErrorCheckCallback, query); } From a6205b1154b7f1be0d70c6fcd80acff58b8de4af Mon Sep 17 00:00:00 2001 From: Rushaway Date: Thu, 3 Sep 2026 14:14:15 +0200 Subject: [PATCH 2/2] review fixes: make truncation guards actually fire and stop escaping prebuilt SQL fragments Two defects found while reviewing the port of sbpp/sourcebans-pp#1492. 1. Truncation guards were dead code. Database.Format / SQL_FormatQuery goes through InternalFormat -> atcprintf, which starts with llen = maxlen - 1 and returns (maxlen - llen - 1). The return value is therefore capped at maxlength - 1 and can never be >= sizeof(buffer), so every "if (db.Format(...) >= sizeof(buf))" check (and the inverted "< sizeof(buf)" variants in VerifyBan) could never trigger. All 34 guards in sbpp_main.sp and sbpp_comms.sp now compare against sizeof(buf) - 1 (and iQueryLen - 1 for the dynamically sized report query), which is the standard truncation test. 2. sbpp_comms.sp InsertTempBlock escaped its own SQL. The queue2 INSERT passed the prebuilt sQueryMute/sQueryGag value tuples through %s, so SQLiteDB.Format escaped the single quotes inside them and produced a syntactically invalid statement, silently dropping temp comm blocks queued while the main database was down. Switched to %!s%!s%!s to match the equivalent statement in SavePunishment. Co-Authored-By: Claude Sonnet 5 Co-authored-by: Rushaway --- game/addons/sourcemod/scripting/sbpp_comms.sp | 26 +++++------ game/addons/sourcemod/scripting/sbpp_main.sp | 44 +++++++++---------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/game/addons/sourcemod/scripting/sbpp_comms.sp b/game/addons/sourcemod/scripting/sbpp_comms.sp index 957f3f561..e93746041 100644 --- a/game/addons/sourcemod/scripting/sbpp_comms.sp +++ b/game/addons/sourcemod/scripting/sbpp_comms.sp @@ -365,7 +365,7 @@ public void VerifyBlock(int client) WHERE RemoveType IS NULL \ AND c.authid REGEXP '^STEAM_[0-9]:%s$' \ AND (length = '0' OR ends > UNIX_TIMESTAMP())", - DatabasePrefix, DatabasePrefix, DatabasePrefix, g_sSteamIDs[client][8]) >= sizeof(Query)) + DatabasePrefix, DatabasePrefix, DatabasePrefix, g_sSteamIDs[client][8]) >= sizeof(Query) - 1) { LogError("VerifyBlock query truncated for %L", client); return; @@ -1566,7 +1566,7 @@ public void Query_UnBlockSelect(Database db, DBResultSet results, const char[] e RemovedOn = UNIX_TIMESTAMP(), \ ureason = '%s' \ WHERE bid = %d", - DatabasePrefix, iAID, reason, bid) >= sizeof(query)) + DatabasePrefix, iAID, reason, bid) >= sizeof(query) - 1) { LogError("Query_UnBlockSelect update query truncated"); delete newDataPack; @@ -1737,7 +1737,7 @@ public void Query_ProcessQueue(Database db, DBResultSet results, const char[] er VALUES ('%s', '%s', %d, %d, %d, '%s', \ IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '0'), \ '%s', IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), %d, %d)", - DatabasePrefix, auth, name, startTime, (startTime + (time * 60)), (time * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID, type) >= sizeof(query)) + DatabasePrefix, auth, name, startTime, (startTime + (time * 60)), (time * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID, type) >= sizeof(query) - 1) { LogError("Query_ProcessQueue insert query truncated"); continue; @@ -1758,7 +1758,7 @@ public void Query_AddBlockFromQueue(Database db, DBResultSet results, const char if (SQLiteDB.Format(query, sizeof(query), "DELETE FROM queue2 \ WHERE id = %d", - data) >= sizeof(query)) + data) >= sizeof(query) - 1) { LogError("Query_AddBlockFromQueue delete query truncated"); return; @@ -2608,7 +2608,7 @@ stock void ProcessUnBlock(int client, int targetId = 0, int type, char[] sReason AND (c.authid = '%s' OR c.authid REGEXP '^STEAM_[0-9]:%s$') \ AND (length = '0' OR ends > UNIX_TIMESTAMP()) \ AND %!s", - DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, DatabasePrefix, DatabasePrefix, targetAuth, targetAuth[8], typeWHERE) >= sizeof(query)) + DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, DatabasePrefix, DatabasePrefix, targetAuth, targetAuth[8], typeWHERE) >= sizeof(query) - 1) { LogError("ProcessUnBlock select query truncated"); return; @@ -2760,7 +2760,7 @@ stock void InsertTempBlock(int length, int type, const char[] name, const char[] // steam_id time start_time reason name admin_id admin_ip if (SQLiteDB.Format(sQueryVal, sizeof(sQueryVal), "'%s', %d, %d, '%s', '%s', '%s', '%s'", - auth, length, GetTime(), reason, name, adminAuth, adminIp) >= sizeof(sQueryVal)) + auth, length, GetTime(), reason, name, adminAuth, adminIp) >= sizeof(sQueryVal) - 1) { LogError("InsertTempBlock values query truncated"); return; @@ -2778,8 +2778,8 @@ stock void InsertTempBlock(int length, int type, const char[] name, const char[] } if (SQLiteDB.Format(sQuery, sizeof(sQuery), - "INSERT INTO queue2 (steam_id, time, start_time, reason, name, admin_id, admin_ip, type) VALUES %s%s%s", - sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery)) + "INSERT INTO queue2 (steam_id, time, start_time, reason, name, admin_id, admin_ip, type) VALUES %!s%!s%!s", + sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery) - 1) { LogError("InsertTempBlock insert query truncated"); return; @@ -3164,7 +3164,7 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, // bid authid name created ends lenght reason aid adminip sid removedBy removedType removedon type ureason if (g_hDatabase.Format(sQueryAdm, sizeof(sQueryAdm), "IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), 0)", - DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdm)) + DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdm) - 1) { LogError("SavePunishment admin subquery truncated"); return; @@ -3173,7 +3173,7 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, char sQueryAdmName[512]; if (g_hDatabase.Format(sQueryAdmName, sizeof(sQueryAdmName), "IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '')", - DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdmName)) + DatabasePrefix, adminAuth, adminAuth[8]) >= sizeof(sQueryAdmName) - 1) { LogError("SavePunishment admin name subquery truncated"); return; @@ -3184,7 +3184,7 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, // authid name, created, ends, length, reason, aid, adminIp, admin_name, sid if (g_hDatabase.Format(sQueryVal, sizeof(sQueryVal), "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %!s, '%s', %!s, %d", - targetAuth, sName, length * 60, length * 60, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal)) + targetAuth, sName, length * 60, length * 60, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal) - 1) { LogError("SavePunishment values query truncated"); return; @@ -3195,7 +3195,7 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, // authid name, created, ends, length, reason, aid, adminIp, admin_name, sid if (g_hDatabase.Format(sQueryVal, sizeof(sQueryVal), "'%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', %!s, '%s', %!s, %d", - targetAuth, sName, SESSION_MUTE_FALLBACK, -1, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal)) + targetAuth, sName, SESSION_MUTE_FALLBACK, -1, reason, sQueryAdm, adminIp, sQueryAdmName, serverID) >= sizeof(sQueryVal) - 1) { LogError("SavePunishment values query truncated"); return; @@ -3218,7 +3218,7 @@ stock void SavePunishment(int admin = 0, int target, int type, int length = -1, // litle magic - one query for all actions (mute, gag or silence) if (g_hDatabase.Format(sQuery, sizeof(sQuery), "INSERT INTO %!s_comms (authid, name, created, ends, length, reason, aid, adminIp, admin_name, sid, type) VALUES %!s%!s%!s", - DatabasePrefix, sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery)) + DatabasePrefix, sQueryMute, type == TYPE_SILENCE ? ", " : "", sQueryGag) >= sizeof(sQuery) - 1) { LogError("SavePunishment insert query truncated"); return; diff --git a/game/addons/sourcemod/scripting/sbpp_main.sp b/game/addons/sourcemod/scripting/sbpp_main.sp index 94f840f62..c54412c6a 100644 --- a/game/addons/sourcemod/scripting/sbpp_main.sp +++ b/game/addons/sourcemod/scripting/sbpp_main.sp @@ -349,7 +349,7 @@ public void OnClientAuthorized(int client, const char[] auth) return; char Query[512]; - if (DB.Format(Query, sizeof(Query), "SELECT bid, ip FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, g_sSteamIDs[client][8], g_sPlayerIP[client]) >= sizeof(Query)) + if (DB.Format(Query, sizeof(Query), "SELECT bid, ip FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, g_sSteamIDs[client][8], g_sPlayerIP[client]) >= sizeof(Query) - 1) { LogError("OnClientAuthorized query truncated for %L", client); return; @@ -571,7 +571,7 @@ public Action CommandBanIp(int client, int args) char sQuery[256]; if (DB.Format(sQuery, sizeof(sQuery), "SELECT bid FROM %!s_bans WHERE type = 1 AND ip = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", - DatabasePrefix, arg) >= sizeof(sQuery)) + DatabasePrefix, arg) >= sizeof(sQuery) - 1) { LogError("CommandBanIp query truncated"); return Plugin_Handled; @@ -630,13 +630,13 @@ public Action CommandUnban(int client, int args) if (strncmp(arg, "STEAM_", 6) == 0) { - if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 0 AND authid = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query)) + if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 0 AND authid = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query) - 1) { LogError("CommandUnban query truncated (steam)"); return Plugin_Handled; } } else { - if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 1 AND ip = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query)) + if (DB.Format(query, sizeof(query), "SELECT bid FROM %!s_bans WHERE (type = 1 AND ip = '%s') AND (length = '0' OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", DatabasePrefix, arg) >= sizeof(query) - 1) { LogError("CommandUnban query truncated (ip)"); return Plugin_Handled; @@ -722,7 +722,7 @@ public Action CommandAddBan(int client, int args) char sQuery[256]; if (DB.Format(sQuery, sizeof sQuery, "SELECT bid FROM %!s_bans WHERE type = 0 AND authid = '%s' AND (length = 0 OR ends > UNIX_TIMESTAMP()) AND RemoveType IS NULL", - DatabasePrefix, authid) >= sizeof(sQuery)) + DatabasePrefix, authid) >= sizeof(sQuery) - 1) { LogError("CommandAddBan query truncated"); return Plugin_Handled; @@ -1299,7 +1299,7 @@ public void SelectBanIpCallback(Database db, DBResultSet results, const char[] e (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query) - 1) { LogError("SelectBanIpCallback insert query truncated"); delete dataPack; @@ -1310,7 +1310,7 @@ public void SelectBanIpCallback(Database db, DBResultSet results, const char[] e (1, '%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query)) + DatabasePrefix, ip, targetAuth, targetName, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query) - 1) { LogError("SelectBanIpCallback insert query truncated"); delete dataPack; @@ -1430,7 +1430,7 @@ public void SelectUnbanCallback(Database db, DBResultSet results, const char[] e char query[1024]; if (db.Format(query, sizeof(query), "UPDATE %!s_bans SET RemovedBy = (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), RemoveType = 'U', RemovedOn = UNIX_TIMESTAMP(), ureason = '%s' WHERE bid = %d", - DatabasePrefix, DatabasePrefix, adminAuth, adminAuth[8], reason, bid) >= sizeof(query)) + DatabasePrefix, DatabasePrefix, adminAuth, adminAuth[8], reason, bid) >= sizeof(query) - 1) { LogError("SelectUnbanCallback update query truncated"); delete dataPack; @@ -1522,7 +1522,7 @@ public void SelectAddbanCallback(Database db, DBResultSet results, const char[] ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query) - 1) { LogError("SelectAddbanCallback insert query truncated"); delete dataPack; @@ -1533,7 +1533,7 @@ public void SelectAddbanCallback(Database db, DBResultSet results, const char[] ('%s', '', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query)) + DatabasePrefix, authid, (minutes * 60), (minutes * 60), reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(Query) - 1) { LogError("SelectAddbanCallback insert query truncated"); delete dataPack; @@ -1619,7 +1619,7 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1))", - DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(query)) + DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(query) - 1) { LogError("ProcessQueueCallback insert query truncated"); continue; @@ -1632,7 +1632,7 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] ('%s', '%s', '%s', %d, %d, %d, '%s', (SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d)", - DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(query)) + DatabasePrefix, ip, auth, name, startTime, startTime + time * 60, time * 60, reason, DatabasePrefix, adminAuth, adminAuth[8], adminIp, DatabasePrefix, adminAuth, adminAuth[8], serverID) >= sizeof(query) - 1) { LogError("ProcessQueueCallback insert query truncated"); continue; @@ -1644,7 +1644,7 @@ public void ProcessQueueCallback(Database db, DBResultSet results, const char[] db.Query(AddedFromSQLiteCallback, query, authPack); } else { // The ban is no longer valid and should be deleted from the queue - if (db.Format(query, sizeof(query), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(query)) + if (db.Format(query, sizeof(query), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(query) - 1) { LogError("ProcessQueueCallback delete query truncated"); continue; @@ -1665,7 +1665,7 @@ public void AddedFromSQLiteCallback(Database db, DBResultSet results, const char if (results != null) { // The insert was successful so delete the record from the queue - if (SQLiteDB.Format(buffer, sizeof(buffer), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(buffer)) + if (SQLiteDB.Format(buffer, sizeof(buffer), "DELETE FROM queue WHERE steam_id = '%s'", auth) >= sizeof(buffer) - 1) { LogError("AddedFromSQLiteCallback delete query truncated"); delete dataPack; @@ -1708,7 +1708,7 @@ public void ServerInfoCallback(Database db, DBResultSet results, const char[] er } } - if (db.Format(query, sizeof(query), "INSERT INTO %!s_servers (ip, port, rcon, modid) VALUES ('%s', '%s', '%s', (SELECT mid FROM %!s_mods WHERE modfolder = '%s'))", DatabasePrefix, ServerIp, ServerPort, rcon, DatabasePrefix, desc) >= sizeof(query)) + if (db.Format(query, sizeof(query), "INSERT INTO %!s_servers (ip, port, rcon, modid) VALUES ('%s', '%s', '%s', (SELECT mid FROM %!s_mods WHERE modfolder = '%s'))", DatabasePrefix, ServerIp, ServerPort, rcon, DatabasePrefix, desc) >= sizeof(query) - 1) { LogError("ServerInfoCallback insert query truncated"); return; @@ -1761,7 +1761,7 @@ public void VerifyBan(Database db, DBResultSet results, const char[] error, int { char sQuery[256]; - if (DB.Format(sQuery, sizeof sQuery, "UPDATE %!s_bans SET `ip` = '%s' WHERE `bid` = '%d'", DatabasePrefix, clientIp, iBid) < sizeof(sQuery)) + if (DB.Format(sQuery, sizeof sQuery, "UPDATE %!s_bans SET `ip` = '%s' WHERE `bid` = '%d'", DatabasePrefix, clientIp, iBid) < sizeof(sQuery) - 1) { DB.Query(SQL_OnIPMend, sQuery, client); } @@ -1777,7 +1777,7 @@ public void VerifyBan(Database db, DBResultSet results, const char[] error, int if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_banlog (sid ,time ,name ,bid) VALUES \ ((SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), UNIX_TIMESTAMP(), '%s', \ (SELECT bid FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", - DatabasePrefix, DatabasePrefix, ServerIp, ServerPort, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query)) + DatabasePrefix, DatabasePrefix, ServerIp, ServerPort, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query) - 1) { db.Query(ErrorCheckCallback, Query, client, DBPrio_High); } @@ -1791,7 +1791,7 @@ public void VerifyBan(Database db, DBResultSet results, const char[] error, int if (db.Format(Query, sizeof(Query), "INSERT INTO %!s_banlog (sid ,time ,name ,bid) VALUES \ (%d, UNIX_TIMESTAMP(), '%s', \ (SELECT bid FROM %!s_bans WHERE ((type = 0 AND authid REGEXP '^STEAM_[0-9]:%s$') OR (type = 1 AND ip = '%s')) AND RemoveType IS NULL LIMIT 0,1))", - DatabasePrefix, serverID, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query)) + DatabasePrefix, serverID, g_sName[client], DatabasePrefix, clientAuth[8], clientIp) < sizeof(Query) - 1) { db.Query(ErrorCheckCallback, Query, client, DBPrio_High); } @@ -2481,7 +2481,7 @@ public int Native_SBReportPlayer(Handle plugin, int numParams) int iQueryLen = 768 + (iReasonLen * 2 + 1); char[] sQuery = new char[iQueryLen]; if (DB.Format(sQuery, iQueryLen, "INSERT INTO %!s_submissions (`submitted`, `modid`, `SteamId`, `name`, `email`, `reason`, `ip`, `subname`, `sip`, `archiv`, `server`)" - ... "VALUES ('%d', 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, '%d')", DatabasePrefix, iTime, g_sSteamIDs[iTarget], g_sName[iTarget], g_sSteamIDs[iReporter], sReason, g_sPlayerIP[iReporter], g_sName[iReporter], g_sPlayerIP[iTarget], (serverID != -1) ? serverID : 0) >= iQueryLen) + ... "VALUES ('%d', 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, '%d')", DatabasePrefix, iTime, g_sSteamIDs[iTarget], g_sName[iTarget], g_sSteamIDs[iReporter], sReason, g_sPlayerIP[iReporter], g_sName[iReporter], g_sPlayerIP[iTarget], (serverID != -1) ? serverID : 0) >= iQueryLen - 1) { ThrowNativeError(SP_ERROR_NATIVE, "Failed to build report query (buffer too small)"); return 0; @@ -2659,7 +2659,7 @@ stock void UTIL_InsertBan(int time, const char[] Name, const char[] Authid, cons ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ (SELECT sid FROM %!s_servers WHERE ip = '%s' AND port = '%s' LIMIT 0,1), ' ')", - DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query)) + DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], DatabasePrefix, ServerIp, ServerPort) >= sizeof(Query) - 1) { LogError("UTIL_InsertBan query truncated"); CleanupBanDataPack(dataPack); @@ -2670,7 +2670,7 @@ stock void UTIL_InsertBan(int time, const char[] Name, const char[] Authid, cons ('%s', '%s', '%s', UNIX_TIMESTAMP(), UNIX_TIMESTAMP() + %d, %d, '%s', IFNULL((SELECT aid FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'),'0'), '%s', \ IFNULL((SELECT user FROM %!s_admins WHERE authid = '%s' OR authid REGEXP '^STEAM_[0-9]:%s$'), ''), \ %d, ' ')", - DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], serverID) >= sizeof(Query)) + DatabasePrefix, Ip, Authid, Name, (time * 60), (time * 60), Reason, DatabasePrefix, AdminAuthid, AdminAuthid[8], AdminIp, DatabasePrefix, AdminAuthid, AdminAuthid[8], serverID) >= sizeof(Query) - 1) { LogError("UTIL_InsertBan query truncated"); CleanupBanDataPack(dataPack); @@ -2715,7 +2715,7 @@ stock void UTIL_InsertTempBan(int time, const char[] name, const char[] auth, co char query[1024]; if (SQLiteDB.Format(query, sizeof(query), "INSERT INTO queue VALUES ('%s', %i, %i, '%s', '%s', '%s', '%s', '%s')", - auth, time, GetTime(), reason, name, ip, adminAuth, adminIp) >= sizeof(query)) + auth, time, GetTime(), reason, name, ip, adminAuth, adminIp) >= sizeof(query) - 1) { LogError("UTIL_InsertTempBan query truncated"); return;