Skip to content

Commit

Permalink
fix(namespace): avoid collision with base http
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed Mar 5, 2022
1 parent 1e3461c commit d103e05
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 72 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
16 changes: 14 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@ snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
use: github
groups:
- title: Features
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
order: 1
- title: Others
order: 999
filters:
exclude:
- '^docs'
- '^test'
release:
prerelease: auto

nfpms:
- id: "ftw"
Expand All @@ -49,10 +61,10 @@ nfpms:
file_name_template: "{{ .ProjectName }}-{{ .Version }}.{{ .Arch }}"
deb:
file_name_template: "{{ .ProjectName }}-{{ .Version }}_{{ .Arch }}"
vendor:
vendor:
homepage: https://github.com/fzipi/go-ftw
maintainer: [email protected]
description:
description:
Framework for Testing WAFs - Go version

It uses the OWASP Core Ruleset V3 as a baseline to test rules on a WAF. Each rule from the ruleset is loaded into a YAML file that issues HTTP requests that will trigger these rules. Users can verify the execution of the rule after the tests are issued to make sure the expected response is received from an attack
Expand Down
2 changes: 1 addition & 1 deletion http/client.go → ftwhttp/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"crypto/tls"
Expand Down
2 changes: 1 addition & 1 deletion http/client_test.go → ftwhttp/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import "testing"

Expand Down
13 changes: 8 additions & 5 deletions http/connection.go → ftwhttp/connection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package http provides low level abstractions for sending/receiving raw http messages
package http
// Package ftwhttp provides low level abstractions for sending/receiving raw http messages
package ftwhttp

import (
"bufio"
Expand All @@ -16,8 +16,11 @@ import (
)

// DestinationFromString create a Destination from String
func DestinationFromString(urlString string) *Destination {
u, _ := url.Parse(urlString)
func DestinationFromString(urlString string) (*Destination, error) {
u, err := url.Parse(urlString)
if err != nil {
return nil, err
}
host, port, _ := net.SplitHostPort(u.Host)
p, _ := strconv.Atoi(port)

Expand All @@ -27,7 +30,7 @@ func DestinationFromString(urlString string) *Destination {
Protocol: u.Scheme,
}

return d
return d, nil
}

// StartTrackingTime initializes timer
Expand Down
2 changes: 1 addition & 1 deletion http/connection_test.go → ftwhttp/connection_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion http/header.go → ftwhttp/header.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion http/header_test.go → ftwhttp/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package http
package ftwhttp

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion http/request.go → ftwhttp/request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion http/request_test.go → ftwhttp/request_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion http/response.go → ftwhttp/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"io"
Expand Down
17 changes: 11 additions & 6 deletions http/response_test.go → ftwhttp/response_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"fmt"
Expand Down Expand Up @@ -81,12 +81,15 @@ func TestResponse(t *testing.T) {

defer server.Close()

d := DestinationFromString(server.URL)
d, err := DestinationFromString(server.URL)

if err != nil {
t.Error(err)
}
req := generateRequestForTesting(true)

client := NewClient()
err := client.NewConnection(*d)
err = client.NewConnection(*d)

if err != nil {
t.Fatalf("Error! %s", err.Error())
Expand All @@ -109,12 +112,14 @@ func TestResponseWithCookies(t *testing.T) {

defer server.Close()

d := DestinationFromString(server.URL)

d, err := DestinationFromString(server.URL)
if err != nil {
t.Fatalf("Error! %s", err.Error())
}
req := generateRequestForTesting(true)

client := NewClient()
err := client.NewConnection(*d)
err = client.NewConnection(*d)

if err != nil {
t.Fatalf("Error! %s", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion http/rtt.go → ftwhttp/rtt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import "time"

Expand Down
2 changes: 1 addition & 1 deletion http/types.go → ftwhttp/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package ftwhttp

import (
"net"
Expand Down
20 changes: 10 additions & 10 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/fzipi/go-ftw/check"
"github.com/fzipi/go-ftw/config"
"github.com/fzipi/go-ftw/http"
"github.com/fzipi/go-ftw/ftwhttp"
"github.com/fzipi/go-ftw/test"
"github.com/fzipi/go-ftw/utils"

Expand All @@ -27,7 +27,7 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests []

printUnlessQuietMode(output, ":rocket:Running go-ftw!\n")

client := http.NewClient()
client := ftwhttp.NewClient()

for _, tests := range ftwtests {
changed := true
Expand Down Expand Up @@ -69,10 +69,10 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests []
continue
}

var req *http.Request
var req *ftwhttp.Request

// Destination is needed for an request
dest := &http.Destination{
dest := &ftwhttp.Destination{
DestAddr: testRequest.GetDestAddr(),
Port: testRequest.GetPort(),
Protocol: testRequest.GetProtocol(),
Expand Down Expand Up @@ -179,7 +179,7 @@ func overridenTestResult(c *check.FTWCheck, id string) TestResult {
}

// checkResult has the logic for verifying the result for the test sent
func checkResult(c *check.FTWCheck, response *http.Response, responseError error) TestResult {
func checkResult(c *check.FTWCheck, response *ftwhttp.Response, responseError error) TestResult {
// Request might return an error, but it could be expected, we check that first
if responseError != nil && c.AssertExpectError(responseError) {
return Success
Expand Down Expand Up @@ -214,8 +214,8 @@ func checkResult(c *check.FTWCheck, response *http.Response, responseError error
return Failed
}

func getRequestFromTest(testRequest test.Input) *http.Request {
var req *http.Request
func getRequestFromTest(testRequest test.Input) *ftwhttp.Request {
var req *ftwhttp.Request
// get raw request, if anything
raw, err := testRequest.GetRawRequest()
if err != nil {
Expand All @@ -224,17 +224,17 @@ func getRequestFromTest(testRequest test.Input) *http.Request {

// If we use raw or encoded request, then we don't use other fields
if raw != nil {
req = http.NewRawRequest(raw, !testRequest.StopMagic)
req = ftwhttp.NewRawRequest(raw, !testRequest.StopMagic)
} else {
rline := &http.RequestLine{
rline := &ftwhttp.RequestLine{
Method: testRequest.GetMethod(),
URI: testRequest.GetURI(),
Version: testRequest.GetVersion(),
}

data := testRequest.ParseData()
// create a new request
req = http.NewRequest(rline, testRequest.Headers,
req = ftwhttp.NewRequest(rline, testRequest.Headers,
data, !testRequest.StopMagic)

}
Expand Down
18 changes: 12 additions & 6 deletions runner/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/fzipi/go-ftw/config"
httpftw "github.com/fzipi/go-ftw/http"
"github.com/fzipi/go-ftw/ftwhttp"
"github.com/fzipi/go-ftw/test"
"github.com/fzipi/go-ftw/utils"
)
Expand Down Expand Up @@ -259,9 +259,7 @@ func newTestServer() *httptest.Server {
}

// replace localhost or 127.0.0.1 in tests with test url
func replaceLocalhostWithTestServer(yaml string, url string) string {
d := httpftw.DestinationFromString(url)

func replaceLocalhostWithTestServer(yaml string, d ftwhttp.Destination) string {
destChanged := strings.ReplaceAll(yaml, "TEST_ADDR", d.DestAddr)
replacedYaml := strings.ReplaceAll(destChanged, "TEST_PORT", strconv.Itoa(d.Port))

Expand All @@ -280,7 +278,11 @@ func TestRun(t *testing.T) {

// setup test webserver (not a waf)
server := newTestServer()
yamlTestContent := replaceLocalhostWithTestServer(yamlTest, server.URL)
d, err := ftwhttp.DestinationFromString(server.URL)
if err != nil {
t.Fatalf("Failed to parse destination")
}
yamlTestContent := replaceLocalhostWithTestServer(yamlTest, *d)

filename, err := utils.CreateTempFileWithContent(yamlTestContent, "goftw-test-*.yaml")
if err != nil {
Expand Down Expand Up @@ -492,7 +494,11 @@ func TestFailedTestsRun(t *testing.T) {

// setup test webserver (not a waf)
server := newTestServer()
yamlTestContent := replaceLocalhostWithTestServer(yamlFailedTest, server.URL)
d, err := ftwhttp.DestinationFromString(server.URL)
if err != nil {
t.Fatalf("Failed to parse destination")
}
yamlTestContent := replaceLocalhostWithTestServer(yamlFailedTest, *d)

filename, err := utils.CreateTempFileWithContent(yamlTestContent, "goftw-test-*.yaml")
if err != nil {
Expand Down
23 changes: 3 additions & 20 deletions test/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@ import (
"bytes"
"testing"

"github.com/fzipi/go-ftw/http"
"github.com/fzipi/go-ftw/ftwhttp"
)

/*
type Input struct {
DestAddr *string `yaml:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty"`
Version *string `yaml:"version,omitempty"`
Headers http.Header `yaml:"headers,omitempty"`
Method *string `yaml:"method,omitempty"`
Data string `yaml:"data,omitempty"`
SaveCookie bool `yaml:"save_cookie,omitempty"`
StopMagic bool `yaml:"stop_magic"`
EncodedRequest string `yaml:"encoded_request,omitempty"`
RAWRequest string `yaml:"raw_request,omitempty"`
}
*/

func getTestInputDefaults() *Input {
data := "My Data"

inputDefaults := Input{
Headers: make(http.Header),
Headers: make(ftwhttp.Header),
Data: &data,
SaveCookie: false,
StopMagic: false,
Expand All @@ -50,7 +33,7 @@ func getTestExampleInput() *Input {
Protocol: &protocol,
URI: &uri,
Version: &version,
Headers: make(http.Header),
Headers: make(ftwhttp.Header),
Method: &method,
Data: nil,
EncodedRequest: "TXkgRGF0YQo=",
Expand Down
26 changes: 13 additions & 13 deletions test/types.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package test

import "github.com/fzipi/go-ftw/http"
import "github.com/fzipi/go-ftw/ftwhttp"

// Input represents the input request in a stage
// The fields `Version`, `Method` and `URI` we want to explicitly now when they are set to ""
type Input struct {
DestAddr *string `yaml:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty"`
Version *string `yaml:"version,omitempty"`
Headers http.Header `yaml:"headers,omitempty"`
Method *string `yaml:"method,omitempty"`
Data *string `yaml:"data,omitempty"`
SaveCookie bool `yaml:"save_cookie,omitempty"`
StopMagic bool `yaml:"stop_magic"`
EncodedRequest string `yaml:"encoded_request,omitempty"`
RAWRequest string `yaml:"raw_request,omitempty"`
DestAddr *string `yaml:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty"`
Version *string `yaml:"version,omitempty"`
Headers ftwhttp.Header `yaml:"headers,omitempty"`
Method *string `yaml:"method,omitempty"`
Data *string `yaml:"data,omitempty"`
SaveCookie bool `yaml:"save_cookie,omitempty"`
StopMagic bool `yaml:"stop_magic"`
EncodedRequest string `yaml:"encoded_request,omitempty"`
RAWRequest string `yaml:"raw_request,omitempty"`
}

// Output is the response expected from the test
Expand Down

0 comments on commit d103e05

Please sign in to comment.