Skip to content

Commit

Permalink
Adding validation in incapsula_security_rule_exception for url_patter…
Browse files Browse the repository at this point in the history
…ns <> url (#461)
  • Loading branch information
Pavel-Koev authored Oct 1, 2024
1 parent 7701e1c commit 31fb8ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions incapsula/client_security_rule_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/url"
"strconv"
"strings"
)

// Endpoints (unexported consts)
Expand Down Expand Up @@ -48,6 +49,11 @@ func (c *Client) AddSecurityRuleException(siteID int, ruleID, clientAppTypes, cl

log.Printf("[INFO] Adding new security rule exception for rule_id (%s) for site id (%d)\n", ruleID, siteID)

err := validateListSizes(urlPatterns, urls)
if err != nil {
return nil, err
}

// Check to see if ruleID is correct, then iterate rule specific parameters
if ruleParams, ok := securityRuleExceptionParamMapping[ruleID]; ok {
for i := 0; i < len(ruleParams); i++ {
Expand Down Expand Up @@ -117,6 +123,11 @@ func (c *Client) EditSecurityRuleException(siteID int, ruleID, clientAppTypes, c

log.Printf("[INFO] Updating existing security rule exception for rule_id (%s) whitelist_id (%s) for site_id (%d)\n", ruleID, whitelistID, siteID)

err := validateListSizes(urlPatterns, urls)
if err != nil {
return nil, err
}

// Check to see if ruleID is correct, then iterate rule specific parameters
if ruleParams, ok := securityRuleExceptionParamMapping[ruleID]; ok {
for i := 0; i < len(ruleParams); i++ {
Expand Down Expand Up @@ -273,3 +284,14 @@ func (c *Client) DeleteSecurityRuleException(siteID int, ruleID, whitelistID str

return nil
}

func validateListSizes(urlPatterns, urls string) error {
urlPatternsList := strings.Split(urlPatterns, ",")
urlsList := strings.Split(urls, ",")

if len(urlPatternsList) != len(urlsList) {
return fmt.Errorf("error: url_patterns and urls lists do not have the same number of elements")
}

return nil
}
4 changes: 2 additions & 2 deletions website/docs/r/security_rule_exception.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ At least one Optional argument is required.
* `countries` - (Optional) A comma separated list of country codes.
* `continents` - (Optional) A comma separated list of continent codes.
* `ips=` - (Optional) A comma separated list of IPs or IP ranges, e.g: 192.168.1.1, 192.168.1.1-192.168.1.100 or 192.168.1.1/24
* `urls=` - (Optional) A comma separated list of resource paths. For example, /home and /admin/index.html are resource paths, while http://www.example.com/home is not. Each URL should be encoded separately using percent encoding as specified by RFC 3986 (http://tools.ietf.org/html/rfc3986#section-2.1). An empty URL list will remove all URLs. urls="/someurl1,/path/to/my/resource/2.html,/some/url/3"
* `url_patterns` - (Optional) A comma separated list of patters that correlate to the list of urls. url_patterns are required if you have urls specified, and patters are applied in the order specified and map literally to the list of urls. Supported values are: contains,equals,prefix,suffix,not_equals,not_contain,not_prefix,not_suffix. Example of how to apply url_patters to the three urls listed above in order: url_patters="prefix,equals,prefix".
* `urls=` - (Optional) A comma separated list of resource paths. For example, /home and /admin/index.html are resource paths, while http://www.example.com/home is not. Each URL should be encoded separately using percent encoding as specified by RFC 3986 (http://tools.ietf.org/html/rfc3986#section-2.1). An empty URL list will remove all URLs. urls="/someurl1,/path/to/my/resource/2.html,/some/url/3". If this argument is used, the url_patterns argument is required.
* `url_patterns` - (Optional) A comma separated list of patterns that correlate to the list of urls. url_patterns are required if you have urls specified, and patterns are applied in the order specified and map literally to the list of urls. Supported values are: contains,equals,prefix,suffix,not_equals,not_contain,not_prefix,not_suffix. Example of how to apply url_patterns to the three urls listed above in order: url_patterns="prefix,equals,prefix".
* `user_agents` - (Optional) A comma separated list of encoded user agents.
* `parameters` - (Optional) A comma separated list of encoded parameters.

Expand Down

0 comments on commit 31fb8ab

Please sign in to comment.