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 -pr flag path regex filtering #136

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
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
Loading