From c909d296e2be6f441c1bfccfe6ee99037550defc Mon Sep 17 00:00:00 2001 From: "Ben Russell (300178622)" Date: Wed, 31 Jul 2013 20:59:00 +1200 Subject: [PATCH] block attempts to inject HTML into the serverlist, this includes many UTF-8 variants of < --- heart/heartbeat.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/heart/heartbeat.py b/heart/heartbeat.py index ba4d9ff..e918f9d 100644 --- a/heart/heartbeat.py +++ b/heart/heartbeat.py @@ -57,6 +57,24 @@ def stripnul(s): idx = s.find("\x00") return (s if idx == -1 else s[:idx]) +def replace_char_all(s, f, t): + v = ord(f) + s = s.replace(f, t) + s = s.replace(chr(0xC0 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xE0) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xF0) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xF8) + chr(0x80) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + s = s.replace(chr(0xFC) + chr(0x80) + chr(0x80) + chr(0x80) + chr(0x80 | ((v>>6)&3)) + chr(0x80 | (v&63)), t) + # TODO: handle the 6-bit and 8-bit variants and whatnot + return s + +def sanestr(s): + s = str(s) + s = replace_char_all(s, "&", "&") + s = replace_char_all(s, "<", "<") + s = replace_char_all(s, ">", ">") + return s + class HTTPClient: def __init__(self, ct, reactor, server, sockfd): self.reactor = reactor @@ -95,13 +113,13 @@ class HTTPClient: s += "\n" for d in l: s += "" - s += "" + str(d["address"]) + "" - s += "" + str(d["port"]) + "" - s += "" + str(d["name"]) + "" - s += "" + str(d["version"]) + "" - s += "" + str(d["players_current"]) + " / " + str(d["players_max"]) + "" - s += "" + str(d["mode"]) + "" - s += "" + str(d["map"]) + "" + s += "" + sanestr(d["address"]) + "" + s += "" + sanestr(d["port"]) + "" + s += "" + sanestr(d["name"]) + "" + s += "" + sanestr(d["version"]) + "" + s += "" + sanestr(d["players_current"]) + " / " + sanestr(d["players_max"]) + "" + s += "" + sanestr(d["mode"]) + "" + s += "" + sanestr(d["map"]) + "" s += "\n" s += "\n" s += "\n"