Skip to content

http/httpguts: reject leading and trailing spaces in field values #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 24 additions & 39 deletions http/httpguts/httplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,48 +262,33 @@ var validHostByte = [256]bool{
'~': true, // unreserved
}

// validFieldValueChar reports whether v is an RFC9110 field-vchar, SP, or HTAB.
func validFieldValueChar(v uint8) bool {
if v < ' ' {
return v == '\t'
} else {
return v != 0x7F
}
}

// ValidHeaderFieldValue reports whether v is a valid "field-value" according to
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 :
//
// message-header = field-name ":" [ field-value ]
// field-value = *( field-content | LWS )
// field-content = <the OCTETs making up the field-value
// and consisting of either *TEXT or combinations
// of token, separators, and quoted-string>
//
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 :
//
// TEXT = <any OCTET except CTLs,
// but including LWS>
// LWS = [CRLF] 1*( SP | HT )
// CTL = <any US-ASCII control character
// (octets 0 - 31) and DEL (127)>
// <https://rfc-editor.org/rfc/rfc9110#name-field-values>:
//
// RFC 7230 says:
//
// field-value = *( field-content / obs-fold )
// obj-fold = N/A to http2, and deprecated
// field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
// field-vchar = VCHAR / obs-text
// obs-text = %x80-FF
// VCHAR = "any visible [USASCII] character"
//
// http2 further says: "Similarly, HTTP/2 allows header field values
// that are not valid. While most of the values that can be encoded
// will not alter header field parsing, carriage return (CR, ASCII
// 0xd), line feed (LF, ASCII 0xa), and the zero character (NUL, ASCII
// 0x0) might be exploited by an attacker if they are translated
// verbatim. Any request or response that contains a character not
// permitted in a header field value MUST be treated as malformed
// (Section 8.1.2.6). Valid characters are defined by the
// field-content ABNF rule in Section 3.2 of [RFC7230]."
//
// This function does not (yet?) properly handle the rejection of
// strings that begin or end with SP or HTAB.
// field-value = *field-content
// field-content = field-vchar
// [ 1*( SP / HTAB / field-vchar ) field-vchar ]
// field-vchar = VCHAR / obs-text
// obs-text = %x80-FF
func ValidHeaderFieldValue(v string) bool {
for i := 0; i < len(v); i++ {
b := v[i]
if isCTL(b) && !isLWS(b) {
l := len(v)
if l == 0 {
return true
}
if v[0] <= ' ' || v[l - 1] <= ' ' {
return false
}
for i := 0; i < l; i++ {
if !validFieldValueChar(v[i]) {
return false
}
}
Expand Down
39 changes: 39 additions & 0 deletions http/httpguts/httplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,45 @@ func TestValidHeaderFieldName(t *testing.T) {
}
}

func TestValidHeaderFieldValue(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"", true},
{" junk", false},
{"\tjunk", false},
{"junk\t", false},
{"junk ", false},
{" ", false},
{"\t", false},
}
for i := byte(0); true; i++ {
bad := i < ' '
if i == 0x7f {
bad = true
}
if i == '\t' {
bad = false
}
tests = append(tests,
struct {
in string
want bool
}{string([]byte{'a', i, 'b'}), !bad})
if i == 255 {
break
}
}

for _, tt := range tests {
got := ValidHeaderFieldValue(tt.in)
if tt.want != got {
t.Errorf("ValidHeaderFieldValue(%q) = %t; want %t", tt.in, got, tt.want)
}
}
}

func BenchmarkValidHeaderFieldName(b *testing.B) {
names := []string{
"",
Expand Down