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

Add envoy config route validations for emoticons and substrings #3561

Merged
merged 2 commits into from
Aug 5, 2024
Merged
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
56 changes: 55 additions & 1 deletion adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net"
"regexp"
"slices"
"strconv"

"github.com/wso2/product-microgateway/adapter/internal/interceptor"
Expand Down Expand Up @@ -793,6 +794,20 @@ func createRoute(params *routeCreateParams) *routev3.Route {
)
basePath := getFilteredBasePath(xWso2Basepath, endpointBasepath)

if enableRouterConfigValidation {
for _, method := range resourceMethods {
err := validateEnvoyRouteMethodString(method)
if err != nil {
if panicOnValidationFailure {
logger.LoggerOasparser.Fatal(fmt.Sprintf("Error while validating http method %s", method), err)
break
} else {
logger.LoggerOasparser.Error(fmt.Sprintf("Error while validating http method %s", method), err)
}
}
}
}

// OPTIONS is always added even if it is not listed under resources
// This is required to handle CORS preflight request fail scenario
methodRegex := strings.Join(resourceMethods, "|")
Expand All @@ -813,7 +828,6 @@ func createRoute(params *routeCreateParams) *routev3.Route {
}
resourcePath = resourcePathParam
routePath := generateRoutePaths(xWso2Basepath, endpointBasepath, resourcePath)

match = &routev3.RouteMatch{
PathSpecifier: &routev3.RouteMatch_SafeRegex{
SafeRegex: &envoy_type_matcherv3.RegexMatcher{
Expand Down Expand Up @@ -1316,6 +1330,16 @@ func generateRoutePaths(xWso2Basepath, basePath, resourcePath string) string {
resourcePath = strings.Split(resourcePath, "?")[0]
}
fullpath := prefix + resourcePath
if enableRouterConfigValidation {
err := validateEnvoyRoutePathString(fullpath)
if err != nil {
if panicOnValidationFailure {
logger.LoggerOasparser.Fatal("Error while validating route path config. ", err)
} else {
logger.LoggerOasparser.Error("Error while validating route path config. ", err)
}
}
}
newPath = generateRegex(fullpath)
return newPath
}
Expand Down Expand Up @@ -1707,3 +1731,33 @@ func addSubscriptionRatelimitActions(actions []*routev3.RateLimit) []*routev3.Ra
},
})
}

// Validate envoy configuration for smileys/special characters/escape characters. Including regex validation
func validateEnvoyRoutePathString(configValue string) error {
var smileyValidationRegex string = "[:;=Xx8][-~]?[)DPOp3\\[\\]{}]|<3|:\\^\\)|:[)]|:[(]|:[/]|:[Pp]|:[Dd]|:[Oo]|:[|]|:S|:>|:[oO]"
smileyValidated, err := regexp.MatchString(smileyValidationRegex, configValue)
if err != nil {
return err
}
var substitutionStringValidationRegex string = `\{\{\s*[^}]+\s*\}\}|\$\{\s*[^}]+\s*\}|\{%\s*[^%]+\s*%\}|<%[-=]?\s*[^%]+?\s*%>|%[^%\s]+%`
subValidated, err := regexp.MatchString(substitutionStringValidationRegex, configValue)
if err != nil {
return err
}
if smileyValidated && subValidated {
return nil
} else if !smileyValidated {
return errors.New("route path contains an emoticon")
}
return errors.New("route path contains a substitution string")
}

// Validate envoy route method whether its of known type
func validateEnvoyRouteMethodString(method string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From enforcer what we support is anyway "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS".
Shall we remove the rest

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Fixed.

var httpMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
methodValidated := slices.Contains(httpMethods, method)
if methodValidated {
return nil
}
return errors.New("method is invalid ")
}
Loading