HTTPSock: extract IsValidHeaderField helper and add tests (#2010)

This commit is contained in:
MarkLee131
2026-04-25 17:38:31 +08:00
parent 04cf89beec
commit 20e8f73b03
4 changed files with 49 additions and 3 deletions
+5 -2
View File
@@ -762,14 +762,17 @@ void CHTTPSock::SetContentType(const CString& sContentType) {
m_sContentType = sContentType;
}
bool CHTTPSock::IsValidHeaderField(const CString& s) {
return s.find_first_of("\r\n") == CString::npos;
}
void CHTTPSock::AddHeader(const CString& sName, const CString& sValue) {
// Reject CR/LF in either half so we never emit a malformed header or
// give a caller (e.g. a future module) a cheap response-splitting
// primitive. No in-tree caller reaches this with attacker-controlled
// bytes today; this is a defensive guard, not a fix for an existing
// exploit.
if (sName.find_first_of("\r\n") != CString::npos) return;
if (sValue.find_first_of("\r\n") != CString::npos) return;
if (!IsValidHeaderField(sName) || !IsValidHeaderField(sValue)) return;
m_msHeaders[sName] = sValue;
}