Skip to content

Commit

Permalink
fixes issue where the token was appended to the query (pointer mistake)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicYaben committed Aug 1, 2024
1 parent cfb0d9f commit 6c617a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
21 changes: 6 additions & 15 deletions modules/webproxy/request/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,21 @@ func (builder *httpProxyRequestBuilder) setHeaders(headers http.Header) {
}

func (builder *httpProxyRequestBuilder) Build(token string) (*http.Request, error) {

// Add the body
var b *strings.Reader
if len(token) > 0 {
b = strings.NewReader(token)
// Create the request
req, err := http.NewRequest(builder.method, builder.url.String(), strings.NewReader(token))
if err != nil {
return nil, err
}

// Slug token if needed
uri := builder.url
if builder.slug {
q := uri.Query()
q := req.URL.Query()
q.Add("token", token)
uri.RawQuery = q.Encode()
}

// Create the request
req, err := http.NewRequest(builder.method, uri.String(), b)
if err != nil {
return nil, err
req.URL.RawQuery = q.Encode()
}

// Add the headers
// The constructor does not add any header.
req.Header = builder.headers

return req, nil
}
25 changes: 22 additions & 3 deletions modules/webproxy/webproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"io"
"net"
"net/http"
"testing"
"time"

"github.com/golang-jwt/jwt"
"github.com/zmap/zgrab2"
"github.com/zmap/zgrab2/lib/http"
"github.com/zmap/zgrab2/modules/webproxy/request"
)

type webproxyTester struct {
Expand Down Expand Up @@ -72,6 +73,7 @@ func (cfg *webproxyTester) runTest(t *testing.T, testName string) {

// Run the server and start the scan
go cfg.runHTTPServer(t)

_, _, err = scanner.Scan(target)
if err != nil {
t.Fatalf("[%s] error while sending: %v", testName, err)
Expand Down Expand Up @@ -102,12 +104,11 @@ func (cfg *webproxyTester) runTest(t *testing.T, testName string) {
var tests = map[string]*webproxyTester{
"success": {
paddress: "10.176.21.85",
laddress: "10.176.21.141",
laddress: "10.253.211.188",
pport: 8080,
lport: 8081,
bChan: make(chan string, 1),
hmackey: "gz13WcqhVBy09Mnw7ZZYNCqqlWvyRfJx",
slug: true,
},
}

Expand Down Expand Up @@ -136,3 +137,21 @@ func TestProxy(t *testing.T) {
cfg.runTest(t, tname)
}
}

func TestRequestBuilder(t *testing.T) {
b, err := request.NewHttpRequestBuilder("POST", "localhost:8080", http.Header{"cookie": {"123test"}}, true)
if err != nil {
t.Fatal(err)
}

var times = 3
for range times {
r, err := b.Build("123test")
if err != nil {
t.Fatal(err)
}
if r.URL.RawQuery != "token=123test" {
t.Fatalf("unexpected query: %s", r.URL.RawQuery)
}
}
}

0 comments on commit 6c617a0

Please sign in to comment.