2016-09-22 18:00:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cors
|
|
|
|
|
|
|
|
import (
|
2017-10-19 20:03:02 +00:00
|
|
|
"regexp"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2016-09-22 18:00:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-10-19 20:03:02 +00:00
|
|
|
// Default values
|
|
|
|
defaultCorsMethods = "GET, PUT, POST, DELETE, PATCH, OPTIONS"
|
|
|
|
defaultCorsHeaders = "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"
|
2018-01-09 11:19:42 +00:00
|
|
|
defaultCorsMaxAge = 1728000
|
2017-10-19 20:03:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Regex are defined here to prevent information leak, if user tries to set anything not valid
|
|
|
|
// that could cause the Response to contain some internal value/variable (like returning $pid, $upstream_addr, etc)
|
|
|
|
// Origin must contain a http/s Origin (including or not the port) or the value '*'
|
|
|
|
corsOriginRegex = regexp.MustCompile(`^(https?://[A-Za-z0-9\-\.]*(:[0-9]+)?|\*)?$`)
|
|
|
|
// Method must contain valid methods list (PUT, GET, POST, BLA)
|
|
|
|
// May contain or not spaces between each verb
|
|
|
|
corsMethodsRegex = regexp.MustCompile(`^([A-Za-z]+,?\s?)+$`)
|
|
|
|
// Headers must contain valid values only (X-HEADER12, X-ABC)
|
|
|
|
// May contain or not spaces between each Header
|
|
|
|
corsHeadersRegex = regexp.MustCompile(`^([A-Za-z0-9\-\_]+,?\s?)+$`)
|
2016-09-22 18:00:09 +00:00
|
|
|
)
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
type cors struct {
|
2017-11-08 20:58:57 +00:00
|
|
|
r resolver.Resolver
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:36:51 +00:00
|
|
|
// Config contains the Cors configuration to be used in the Ingress
|
|
|
|
type Config struct {
|
2017-10-19 20:03:02 +00:00
|
|
|
CorsEnabled bool `json:"corsEnabled"`
|
|
|
|
CorsAllowOrigin string `json:"corsAllowOrigin"`
|
|
|
|
CorsAllowMethods string `json:"corsAllowMethods"`
|
|
|
|
CorsAllowHeaders string `json:"corsAllowHeaders"`
|
|
|
|
CorsAllowCredentials bool `json:"corsAllowCredentials"`
|
2018-01-09 11:19:42 +00:00
|
|
|
CorsMaxAge int `json:"corsMaxAge"`
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
// NewParser creates a new CORS annotation parser
|
2017-11-08 20:58:57 +00:00
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
|
|
|
return cors{r}
|
2016-12-29 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 20:03:02 +00:00
|
|
|
// Equal tests for equality between two External types
|
2017-11-07 16:36:51 +00:00
|
|
|
func (c1 *Config) Equal(c2 *Config) bool {
|
2017-10-19 20:03:02 +00:00
|
|
|
if c1 == c2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if c1 == nil || c2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-09 11:19:42 +00:00
|
|
|
if c1.CorsMaxAge != c2.CorsMaxAge {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-19 20:03:02 +00:00
|
|
|
if c1.CorsAllowCredentials != c2.CorsAllowCredentials {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c1.CorsAllowHeaders != c2.CorsAllowHeaders {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-25 22:41:29 +00:00
|
|
|
if c1.CorsAllowMethods != c2.CorsAllowMethods {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-19 20:03:02 +00:00
|
|
|
if c1.CorsAllowOrigin != c2.CorsAllowOrigin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c1.CorsEnabled != c2.CorsEnabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:02:06 +00:00
|
|
|
// Parse parses the annotations contained in the ingress
|
2016-09-22 18:00:09 +00:00
|
|
|
// rule used to indicate if the location/s should allows CORS
|
2017-11-08 20:58:57 +00:00
|
|
|
func (c cors) Parse(ing *extensions.Ingress) (interface{}, error) {
|
2018-11-30 22:56:11 +00:00
|
|
|
var err error
|
|
|
|
config := &Config{}
|
|
|
|
|
|
|
|
config.CorsEnabled, err = parser.GetBoolAnnotation("enable-cors", ing)
|
2017-10-19 20:03:02 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsEnabled = false
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsAllowOrigin, err = parser.GetStringAnnotation("cors-allow-origin", ing)
|
|
|
|
if err != nil || config.CorsAllowOrigin == "" || !corsOriginRegex.MatchString(config.CorsAllowOrigin) {
|
|
|
|
config.CorsAllowOrigin = "*"
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsAllowHeaders, err = parser.GetStringAnnotation("cors-allow-headers", ing)
|
|
|
|
if err != nil || config.CorsAllowHeaders == "" || !corsHeadersRegex.MatchString(config.CorsAllowHeaders) {
|
|
|
|
config.CorsAllowHeaders = defaultCorsHeaders
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsAllowMethods, err = parser.GetStringAnnotation("cors-allow-methods", ing)
|
|
|
|
if err != nil || config.CorsAllowMethods == "" || !corsMethodsRegex.MatchString(config.CorsAllowMethods) {
|
|
|
|
config.CorsAllowMethods = defaultCorsMethods
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsAllowCredentials, err = parser.GetBoolAnnotation("cors-allow-credentials", ing)
|
2017-10-19 20:03:02 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsAllowCredentials = true
|
2017-10-19 20:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsMaxAge, err = parser.GetIntAnnotation("cors-max-age", ing)
|
2018-01-09 11:19:42 +00:00
|
|
|
if err != nil {
|
2018-11-30 22:56:11 +00:00
|
|
|
config.CorsMaxAge = defaultCorsMaxAge
|
2018-01-09 11:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:56:11 +00:00
|
|
|
return config, nil
|
2017-10-19 20:03:02 +00:00
|
|
|
|
2016-09-22 18:00:09 +00:00
|
|
|
}
|