Escape message tag values (ref #684)

This commit is contained in:
J-P Nurmi
2014-09-29 14:23:06 +02:00
parent 615801c40e
commit 92c9a2e6ae
5 changed files with 81 additions and 9 deletions
+2 -1
View File
@@ -72,7 +72,8 @@ public:
EHTML,
ESQL,
ENAMEDFMT,
EDEBUG
EDEBUG,
EMSGTAG
} EEscape;
explicit CString(bool b) : std::string(b ? "true" : "false") {}
+4 -2
View File
@@ -484,7 +484,9 @@ MCString CUtils::GetMessageTags(const CString& sLine) {
MCString mssTags;
for (VCString::const_iterator it = vsTags.begin(); it != vsTags.end(); ++it) {
mssTags[it->Token(0, false, "=", true)] = it->Token(1, true, "=", true);
CString sKey = it->Token(0, false, "=", true);
CString sValue = it->Token(1, true, "=", true);
mssTags[sKey] = sValue.Escape(CString::EMSGTAG, CString::CString::EASCII);
}
return mssTags;
}
@@ -504,7 +506,7 @@ void CUtils::SetMessageTags(CString& sLine, const MCString& mssTags) {
}
sTags += it->first;
if (!it->second.empty())
sTags += "=" + it->second;
sTags += "=" + it->second.Escape_n(CString::EMSGTAG);
}
sLine = "@" + sTags + " " + sLine;
}
+39
View File
@@ -174,6 +174,8 @@ CString::EEscape CString::ToEscape(const CString& sEsc) {
return ENAMEDFMT;
} else if (sEsc.Equals("DEBUG")) {
return EDEBUG;
} else if (sEsc.Equals("MSGTAG")) {
return EMSGTAG;
}
return EASCII;
@@ -314,6 +316,33 @@ CString CString::Escape_n(EEscape eFrom, EEscape eTo) const {
} else {
ch = *p;
}
break;
case EMSGTAG:
if (*p != '\\' || iLength < (a +1)) {
ch = *p;
} else {
a++;
p++;
if (*p == ':') {
ch = ';';
} else if (*p == 's') {
ch = ' ';
} else if (*p == '0') {
ch = '\0';
} else if (*p == '\\') {
ch = '\\';
} else if (*p == 'r') {
ch = '\r';
} else if (*p == 'n') {
ch = '\n';
} else {
ch = *p;
}
}
break;
}
switch (eTo) {
@@ -372,6 +401,16 @@ CString CString::Escape_n(EEscape eFrom, EEscape eTo) const {
sRet += ch;
}
break;
case EMSGTAG:
if (ch == ';') { sRet += '\\'; sRet += ':';
} else if (ch == ' ') { sRet += '\\'; sRet += 's';
} else if (ch == '\0') { sRet += '\\'; sRet += '0';
} else if (ch == '\\') { sRet += '\\'; sRet += '\\';
} else if (ch == '\r') { sRet += '\\'; sRet += 'r';
} else if (ch == '\n') { sRet += '\\'; sRet += 'n';
} else { sRet += ch; }
break;
}
}
+8 -6
View File
@@ -38,21 +38,23 @@ protected:
}
void testString(const CString& in, const CString& url,
const CString& html, const CString& sql) {
const CString& html, const CString& sql, const CString& tag) {
SCOPED_TRACE("String: " + in);
testEncode(in, url, "URL");
testEncode(in, html, "HTML");
testEncode(in, sql, "SQL");
testEncode(in, tag, "MSGTAG");
}
};
TEST_F(EscapeTest, Test) {
// input url html sql
testString("abcdefg", "abcdefg", "abcdefg", "abcdefg");
testString("\n\t\r", "%0A%09%0D", "\n\t\r", "\\n\\t\\r");
testString("'\"", "%27%22", "'&quot;", "\\'\\\"");
testString("&<>", "%26%3C%3E", "&amp;&lt;&gt;", "&<>");
// input url html sql msgtag
testString("abcdefg","abcdefg", "abcdefg", "abcdefg", "abcdefg");
testString("\n\t\r", "%0A%09%0D", "\n\t\r", "\\n\\t\\r", "\\n\t\\r");
testString("'\"", "%27%22", "'&quot;", "\\'\\\"", "'\"");
testString("&<>", "%26%3C%3E", "&amp;&lt;&gt;", "&<>", "&<>");
testString(" ;", "+%3B", " ;", " ;", "\\s\\:");
}
TEST(StringTest, Bool) {
+28
View File
@@ -43,6 +43,19 @@ TEST(IRC32, GetMessageTags) {
exp["d"] = "";
EXPECT_EQ(exp, CUtils::GetMessageTags("@a;b=c;d :rest"));
exp.clear();
exp["semi-colon"] += ';';
exp["space"] += ' ';
exp["NUL"] += '\0';
exp["backslash"] += '\\';
exp["CR"] += '\r';
exp["LF"] += '\n';
EXPECT_EQ(exp, CUtils::GetMessageTags(R"(@semi-colon=\:;space=\s;NUL=\0;backslash=\\;CR=\r;LF=\n :rest)"));
exp.clear();
exp["a"] = "; \\\r\n";
EXPECT_EQ(exp, CUtils::GetMessageTags(R"(@a=\:\s\\\r\n :rest)"));
exp.clear();
}
TEST(IRC32, SetMessageTags) {
@@ -64,5 +77,20 @@ TEST(IRC32, SetMessageTags) {
tags["e"] = "";
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ("@a=b;c=d;e :rest", sLine);
tags.clear();
tags["semi-colon"] += ';';
tags["space"] += ' ';
tags["NUL"] += '\0';
tags["backslash"] += '\\';
tags["CR"] += '\r';
tags["LF"] += '\n';
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(R"(@CR=\r;LF=\n;NUL=\0;backslash=\\;semi-colon=\:;space=\s :rest)", sLine);
tags.clear();
tags["a"] = "; \\\r\n";
CUtils::SetMessageTags(sLine, tags);
EXPECT_EQ(R"(@a=\:\s\\\r\n :rest)", sLine);
}