Skip to content
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

Fix IndexDefect errors in httpclient on invalid/weird headers #22886

Merged
merged 6 commits into from Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 27 additions & 10 deletions lib/pure/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ proc parseResponse(client: HttpClient | AsyncHttpClient,
var parsedStatus = false
var linei = 0
var fullyRead = false
var lastHeaderName = ""
var line = ""
result.headers = newHttpHeaders()
while true:
Expand Down Expand Up @@ -890,16 +891,32 @@ proc parseResponse(client: HttpClient | AsyncHttpClient,
parsedStatus = true
else:
# Parse headers
var name = ""
var le = parseUntil(line, name, ':', linei)
if le <= 0: httpError("invalid headers")
inc(linei, le)
if line[linei] != ':': httpError("invalid headers")
inc(linei) # Skip :

result.headers.add(name, line[linei .. ^1].strip())
if result.headers.len > headerLimit:
httpError("too many headers")
# There's at least one char because empty lines are handled above (with client.close)
if line[0] in {' ', '\t'}:
# Check if it's a multiline header value, if so, append to the header we're currently parsing
# This works because a line with a header must start with the header name without any leading space
# See https://datatracker.ietf.org/doc/html/rfc7230, section 3.2 and 3.2.4
# Multiline headers are deprecated in the spec, but it's better to parse them than crash
if lastHeaderName == "":
# This should ideally throw an error, but old httpclient actually treated lines like this
# as a header with an empty name and didn't crash
#httpError("Invalid headers - received multiline header value without previous header")
discard
This conversation was marked as resolved.
Show resolved Hide resolved
else:
result.headers.table[result.headers.toCaseInsensitive(lastHeaderName)][^1].add "\n" & line
else:
var name = ""
var le = parseUntil(line, name, ':', linei)
if le <= 0: httpError("Invalid headers - received empty header name")
if line.len == le: httpError("Invalid headers - no colon after header name")
inc(linei, le) # Skip the parsed header name
inc(linei) # Skip :
if linei == line.len: httpError("Invalid headers - no header value after colon")
# Remember the header name for the possible multi-line header
lastHeaderName = name
result.headers.add(name, line[linei .. ^1].strip())
if result.headers.len > headerLimit:
httpError("too many headers")

if not fullyRead:
httpError("Connection was closed before full request has been made")
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/httpcore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func toTitleCase(s: string): string =
result[i] = if upper: toUpperAscii(s[i]) else: toLowerAscii(s[i])
upper = s[i] == '-'

func toCaseInsensitive(headers: HttpHeaders, s: string): string {.inline.} =
func toCaseInsensitive*(headers: HttpHeaders, s: string): string {.inline.} =
Araq marked this conversation as resolved.
Show resolved Hide resolved
return if headers.isTitleCase: toTitleCase(s) else: toLowerAscii(s)

func newHttpHeaders*(titleCase=false): HttpHeaders =
Expand Down
Loading