Skip to content

Commit

Permalink
modify the sanitize and neutralize declaration at the beginning of th…
Browse files Browse the repository at this point in the history
…e file
  • Loading branch information
ruben-garciad committed Feb 9, 2024
1 parent d08ea34 commit 40512ae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
40 changes: 20 additions & 20 deletions steps/common/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ import (
"github.com/google/uuid"
)

// Neutralization for unwanted command injections in domain string
func neutralizeDomain(input string) string {
pattern := "^(?:https?://)?(?:www.)?([^:/\n&=?¿\"!| %]+)"
regex := regexp.MustCompile(pattern)
domainN := regex.FindString(input)

uri, err := url.Parse(domainN)
if err != nil {
return ""
}

return uri.String()
}

func neutralize(p string) string {
p = strings.ReplaceAll(p, "\r", "")
p = strings.ReplaceAll(p, "\n", "")
return p
}

// Steps to initialize common steps.
type Steps struct {
}
Expand Down Expand Up @@ -96,12 +116,6 @@ func (cs Steps) InitializeSteps(ctx context.Context, scenCtx *godog.ScenarioCont
return ctx
}

func neutralize(p string) string {
p = strings.ReplaceAll(p, "\r", "")
p = strings.ReplaceAll(p, "\n", "")
return p
}

// StoreValueInContext stores a value in golium.Context using the key name.
func StoreValueInContext(ctx context.Context, name, value string) error {
golium.GetContext(ctx).Put(name, value)
Expand Down Expand Up @@ -188,17 +202,3 @@ func getLocalIP(ctx context.Context, key string, ipVersion IPVersion) error {
golium.GetContext(ctx).Put(golium.ValueAsString(ctx, key), localAddress.IP.String())
return nil
}

// Neutralization for unwanted command injections in domain string
func neutralizeDomain(input string) string {
pattern := "^(?:https?://)?(?:www.)?([^:/\n&=?¿\"!| %]+)"
regex := regexp.MustCompile(pattern)
domainN := regex.FindString(input)

uri, err := url.Parse(domainN)
if err != nil {
return ""
}

return uri.String()
}
38 changes: 19 additions & 19 deletions steps/dns/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ import (
"github.com/miekg/dns"
)

// Sanitize HTTP parameter pollution. CWE:235
func sanitize(queryParams map[string][]string) string {
params := url.Values{}
for key, values := range queryParams {
for _, value := range values {
if !params.Has(key) {
params.Add(key, value)
}
}
}
return params.Encode()
}

func neutralize(p string) string {
p = strings.ReplaceAll(p, "\r", "")
p = strings.ReplaceAll(p, "\n", "")
return p
}

// Session contains the information related to a DNS query and response.
type Session struct {
// Server is the address to the DNS server, including the server port (e.g. 8.8.8.8:53).
Expand Down Expand Up @@ -289,22 +308,3 @@ func (s *Session) ValidateResponseWithRecords(
}
return nil
}

// Sanitize HTTP parameter pollution. CWE:235
func sanitize(queryParams map[string][]string) string {
params := url.Values{}
for key, values := range queryParams {
for _, value := range values {
if !params.Has(key) {
params.Add(key, value)
}
}
}
return params.Encode()
}

func neutralize(p string) string {
p = strings.ReplaceAll(p, "\r", "")
p = strings.ReplaceAll(p, "\n", "")
return p
}
26 changes: 13 additions & 13 deletions steps/http/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ const (
DefaultTestURL = "https://jsonplaceholder.typicode.com/"
)

// Sanitize HTTP parameter pollution. CWE:235
func sanitize(queryParams map[string][]string) string {
params := url.Values{}
for key, values := range queryParams {
for _, value := range values {
if !params.Has(key) {
params.Add(key, value)
}
}
}
return params.Encode()
}

// Session contains the information of a HTTP session (request and response).
type Session struct {
Request model.Request
Expand Down Expand Up @@ -688,16 +701,3 @@ func (s *Session) GetURL(ctx context.Context) (string, error) {
}
return URL, nil
}

// Sanitize HTTP parameter pollution. CWE:235
func sanitize(queryParams map[string][]string) string {
params := url.Values{}
for key, values := range queryParams {
for _, value := range values {
if !params.Has(key) {
params.Add(key, value)
}
}
}
return params.Encode()
}

0 comments on commit 40512ae

Please sign in to comment.