Skip to content

Commit ab455db

Browse files
Frozen-H2OVauff
authored andcommitted
Block html injections in c_hsay
1 parent 7e345cd commit ab455db

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/adminsystem.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,9 @@ CON_COMMAND_CHAT_FLAGS(hsay, "<message> - Say something as a hud hint", ADMFLAG_
561561
return;
562562
}
563563

564-
SendHudMessageAll(10, 99, "<span class='fontSize-l'><span color='#FFFFFF'>ADMIN: </span><span color='#D11313'>%s</span></span>", args.ArgS());
564+
SendHudMessageAll(
565+
10, 99, "<span class='fontSize-l'><span color='#FFFFFF'>ADMIN: </span><span color='#D11313'>%s</span></span>",
566+
EscapeHTMLSpecialCharacters(args.ArgS()).c_str());
565567
}
566568

567569
CON_COMMAND_CHAT_FLAGS(rcon, "<command> - Send a command to server console", ADMFLAG_RCON)

src/utils/hud_manager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,24 @@ void StartFlashingFixTimer()
145145

146146
return 0.5f;
147147
});
148+
}
149+
150+
std::string EscapeHTMLSpecialCharacters(std::string strMsg)
151+
{
152+
// Always replace & first, as it is used in html escaped characters (so dont want to replace inside an escape)
153+
for (size_t pos = 0; (pos = strMsg.find('&', pos)) != std::string::npos; pos += 5)
154+
strMsg.replace(pos, 1, "&amp;");
155+
156+
std::unordered_map<std::string, std::string> mapReplacements{
157+
{"<", "&lt;"},
158+
{">", "&gt;"},
159+
{"\"", "&quot;"},
160+
{"\'", "&apos;"}
161+
};
162+
163+
for (const auto& [badChar, escapedChar] : mapReplacements)
164+
for (size_t pos = 0; (pos = strMsg.find(badChar, pos)) != std::string::npos; pos += escapedChar.length())
165+
strMsg.replace(pos, 1, escapedChar);
166+
167+
return strMsg;
148168
}

src/utils/hud_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ void SendHudMessage(ZEPlayer* pPlayer, int iDuration, int iPriority, const char*
5353
void SendHudMessageAll(int iDuration, int iPriority, const char* pszMessage, ...);
5454

5555
void StartFlashingFixTimer();
56+
std::string EscapeHTMLSpecialCharacters(std::string strMsg);
5657

5758
extern CConVar<bool> g_cvarFixHudFlashing;

0 commit comments

Comments
 (0)