Skip to content

Commit

Permalink
Merge pull request #136 from OWASP/add-path-regex-flag
Browse files Browse the repository at this point in the history
add -pr flag path regex filtering
  • Loading branch information
dmdhrumilmistry authored Aug 27, 2024
2 parents 3e3fa81 + 4f209f2 commit 0be2e08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cmd/offat/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type FlagConfig struct {
QueryParams KeyValueMap
Proxy *string

// API test filter
PathRegex *string

// SSRF Test
SsrfUrl *string

Expand Down
11 changes: 10 additions & 1 deletion src/cmd/offat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func main() {
config.DisableSchemaDefaultsValidation = flag.Bool("ds", false, "disable schema defaults validation for OAS files")
config.DisableSchemaPatternValidation = flag.Bool("dp", false, "disable schema patterns validation for OAS files")

config.PathRegex = flag.String("pr", "", "run tests for paths matching given regex pattern")

config.SsrfUrl = flag.String("ssrf", "", "injects user defined SSRF url payload in http request components")

config.RequestsPerSecond = flag.Int("r", 60, "number of requests per second")
Expand Down Expand Up @@ -124,8 +126,15 @@ func main() {
SsrfUrl: *config.SsrfUrl,
}

// generate and run api tests
// generate api tests
apiTests := apiTestHandler.GenerateTests()

// filter api tests
if *config.PathRegex != "" {
apiTests = apiTestHandler.FilterTests(apiTests, *config.PathRegex)
}

// run api tests
trunner.RunApiTests(&apiTestHandler, hc, hc.Client.FHClient, apiTests)
log.Info().Msgf("Total Requests: %d", len(apiTests))

Expand Down
19 changes: 19 additions & 0 deletions src/pkg/tgen/tgen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tgen

import (
"regexp"

_ "github.com/OWASP/OFFAT/src/pkg/logging"
"github.com/OWASP/OFFAT/src/pkg/parser"
"github.com/OWASP/OFFAT/src/pkg/utils"
Expand All @@ -27,6 +29,23 @@ type TGenHandler struct {
SsrfUrl string
}

func (t *TGenHandler) FilterTests(apiTests []*ApiTest, pathRegex string) []*ApiTest {
var filteredTests []*ApiTest
for _, apiTest := range apiTests {
match, err := regexp.MatchString(pathRegex, apiTest.Path)
if err != nil {
log.Error().Err(err).Msgf("Failed to match %v regex with endpoint path %v", pathRegex, apiTest.Path)
continue
}

if match {
filteredTests = append(filteredTests, apiTest)
}
}

return filteredTests
}

func (t *TGenHandler) GenerateTests() []*ApiTest {
tests := []*ApiTest{}

Expand Down

0 comments on commit 0be2e08

Please sign in to comment.