mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-12-02 11:54:04 +01:00
security: escape XSS sinks client-side and add CSP + security headers
The client trusted the server to pre-escape chat content and rendered user-supplied fields via .html()/innerHTML — one server regression away from stored XSS, and the admin panel injected user-controlled payout addresses into its authenticated DOM. - Add a single shared escapeHtml helper (escapes all five HTML-significant chars including both quotes); leaderboard now delegates to it. - Escape chat username + message in onChatBroadcast/onChatHistory. - Escape user-controlled fields in the admin panel (payout address, socket id, player name, status) and make its local escapeHtml quote-safe so values in title="..." can't break out. - Add CSP plus X-Content-Type-Options, X-Frame-Options and Referrer-Policy headers via middleware. connect-src 'self' keeps Socket.IO working; removing 'unsafe-inline' (nonces) is tracked as a later cleanup. Full suite: 13 suites / 119 tests.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Shared HTML-escaping helper — the single source of truth for escaping untrusted
|
||||
* (server- or user-supplied) text before it is inserted into innerHTML / jQuery .html().
|
||||
* Escapes all five HTML-significant characters, including both quote types so the
|
||||
* result is safe in both element text and quoted attribute values.
|
||||
*/
|
||||
(function (global) {
|
||||
function escapeHtml(value) {
|
||||
if (value === null || value === undefined) return '';
|
||||
return String(value)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
global.escapeHtml = escapeHtml;
|
||||
})(typeof window !== 'undefined' ? window : this);
|
||||
@@ -288,7 +288,9 @@ const SocketHandlers = {
|
||||
onChatBroadcast: function(data) {
|
||||
const msgElement = $('<li style="color: #aaa;">');
|
||||
if (data.socketId) {
|
||||
msgElement.html('<strong>' + data.socketId.substring(0, 6) + ':</strong> ' + data.message);
|
||||
// Escape both fields — chat content is attacker-controlled. Defense in depth:
|
||||
// the server also escapes, but the client must never trust that.
|
||||
msgElement.html('<strong>' + escapeHtml(String(data.socketId).substring(0, 6)) + ':</strong> ' + escapeHtml(data.message));
|
||||
} else {
|
||||
msgElement.text(data.message);
|
||||
}
|
||||
@@ -320,7 +322,7 @@ const SocketHandlers = {
|
||||
if (msg.timestamp) {
|
||||
timeStr = '<span style="color:#555;font-size:10px;">[' + SocketHandlers._formatTimeAgo(new Date(msg.timestamp)) + ']</span> ';
|
||||
}
|
||||
msgElement.html(timeStr + '<strong>' + username + ':</strong> ' + msg.message);
|
||||
msgElement.html(timeStr + '<strong>' + escapeHtml(username) + ':</strong> ' + escapeHtml(msg.message));
|
||||
$('#messages').append(msgElement);
|
||||
});
|
||||
|
||||
|
||||
@@ -127,12 +127,15 @@ const Leaderboard = {
|
||||
},
|
||||
|
||||
_escapeHtml: function(str) {
|
||||
// Delegate to the shared helper (single source of truth). Falls back to a local
|
||||
// implementation if the helper somehow isn't loaded.
|
||||
if (typeof window !== 'undefined' && typeof window.escapeHtml === 'function') {
|
||||
return window.escapeHtml(str);
|
||||
}
|
||||
if (!str) return '';
|
||||
return String(str)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"');
|
||||
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/"/g, '"').replace(/'/g, ''');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user