Skip to content

fix(auth-proxy-set-headers): Validate authorization header values #13371

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 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 14 additions & 4 deletions internal/ingress/annotations/authreq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ func (e1 *Config) Equal(e2 *Config) bool {
}

var (
methodsRegex = regexp.MustCompile("(GET|HEAD|POST|PUT|PATCH|DELETE|CONNECT|OPTIONS|TRACE)")
headerRegexp = regexp.MustCompile(`^[a-zA-Z\d\-_]+$`)
statusCodeRegex = regexp.MustCompile(`^\d{3}$`)
durationRegex = regexp.MustCompile(`^\d+(ms|s|m|h|d|w|M|y)$`) // see http://nginx.org/en/docs/syntax.html
methodsRegex = regexp.MustCompile("(GET|HEAD|POST|PUT|PATCH|DELETE|CONNECT|OPTIONS|TRACE)")
headerRegexp = regexp.MustCompile(`^[a-zA-Z\d\-_]+$`)
authorizationValueRegexp = regexp.MustCompile(`^[^\n\r'{}]+$`)
statusCodeRegex = regexp.MustCompile(`^\d{3}$`)
durationRegex = regexp.MustCompile(`^\d+(ms|s|m|h|d|w|M|y)$`) // see http://nginx.org/en/docs/syntax.html
)

// ValidMethod checks is the provided string a valid HTTP method
Expand All @@ -263,6 +264,11 @@ func ValidHeader(header string) bool {
return headerRegexp.MatchString(header)
}

// ValidAuthorizationValue checks is the provided string satisfies the authorization value regexp
func ValidAuthorizationValue(header string) bool {
return authorizationValueRegexp.MatchString(header)
}

// ValidCacheDuration checks if the provided string is a valid cache duration
// spec: [code ...] [time ...];
// with: code is an http status code
Expand Down Expand Up @@ -461,6 +467,10 @@ func (a authReq) Parse(ing *networking.Ingress) (interface{}, error) {
if !ValidHeader(header) {
return nil, ing_errors.NewLocationDenied("invalid proxy-set-headers in configmap")
}

if !ValidAuthorizationValue(proxySetHeadersMapContents.Data[header]) {
return nil, ing_errors.NewLocationDenied("invalid proxy-set-headers in configmap")
}
}

proxySetHeaders = proxySetHeadersMapContents.Data
Expand Down