golang/go · error
invalid format: invalid header line
Error message
invalid format: invalid header line
What it means
Raised inside parseUserAuth for each header line. A header line must (1) contain a `: ` separator (strings.Cut succeeds), (2) have a name passing validHeaderFieldName, and (3) have a value (after TrimSpace) passing validHeaderFieldValue. Failure of any check aborts parsing.
Source
Thrown at src/cmd/go/internal/auth/userauth.go:87
if err != nil {
return nil, fmt.Errorf("could not parse URL %s: %v", line, err)
}
urls = append(urls, u.String())
}
// Parse Headers second.
header := make(http.Header)
for {
line, data, ok = strings.Cut(data, "\n")
if !ok {
return nil, fmt.Errorf("invalid format: missing empty line after headers")
}
if line == "" {
break
}
name, value, ok := strings.Cut(line, ": ")
value = strings.TrimSpace(value)
if !ok || !validHeaderFieldName(name) || !validHeaderFieldValue(value) {
return nil, fmt.Errorf("invalid format: invalid header line")
}
header.Add(name, value)
}
maps.Copy(credentials, mapHeadersToPrefixes(urls, header))
}
return credentials, nil
}
// mapHeadersToPrefixes returns a mapping of prefix → http.Header without
// the leading "https://".
func mapHeadersToPrefixes(prefixes []string, header http.Header) map[string]http.Header {
prefixToHeaders := make(map[string]http.Header, len(prefixes))
for _, p := range prefixes {
p = strings.TrimPrefix(p, "https://")
prefixToHeaders[p] = header.Clone() // Clone the header to avoid sharing
}
return prefixToHeaders
}View on GitHub (pinned to b6b368adc5)
Solutions
- Format headers as `Name: value` (a colon followed by exactly one space).
- Use only RFC 7230 token characters for header names.
- Keep header values on a single line with no control characters.
- Strip any trailing carriage return from CRLF input before emitting.
Example fix
// before Authorization:Bearer x // after Authorization: Bearer x
Defensive patterns
Strategy: validation
Validate before calling
// Validate a header line matches the expected shape.
func validHeaderLine(line string) error {
name, value, ok := strings.Cut(line, ": ")
if !ok || !validHeaderFieldName(name) || !validHeaderFieldValue(strings.TrimSpace(value)) {
return errors.New("invalid header line: expected 'Name: value'")
}
return nil
} Prevention
- Format headers as `Name: value` (colon plus one space).
- Use RFC 7230 token characters for names.
- Keep values single-line with no control characters.
- Strip CRLF carriage returns before emitting.
When it happens
Trigger: A header line without `: ` (e.g. `Authorization:Bearer x` with no space, or a bare token), a name with invalid token characters or whitespace, or a value containing control characters/newlines.
Common situations: Using a colon without the required space; header names containing spaces or non-token characters; header values with embedded newlines; CRLF endings leaving a stray CR.
Related errors
- cannot parse output of GOAUTH command %s: %v
- invalid format: missing empty line after URLs
- could not parse URL %s: %v
- invalid format: missing empty line after headers
- 'git credential fill' failed for url=%s, could not parse url
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/612f4c8ca75a8dbe.
Report an issue: GitHub.