Repository navigation
Closed
Labels
Type: EnhancementMost issues will probably ask for additions or changes.
Description
Please describe your feature request:
An option to follow redirects only when they stay on the same host (and scheme). With FollowRedirects: true, rawhttp follows a 302 to any other host and from http to https. The headers given to DoRaw, Authorization included, go to the new host too. Nothing in Options restricts this.
Repro with local listeners: A redirects, B is another host, C is HTTPS.
main.go
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/projectdiscovery/rawhttp"
)
func main() {
// B: a second host (plain HTTP); C: a TLS listener
logHit := func(name string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf(" %s got %s %s (Authorization=%q)\n", name, r.Method, r.URL.Path, r.Header.Get("Authorization"))
fmt.Fprintf(w, "hello from %s", name)
}
}
b := httptest.NewServer(logHit("B"))
defer b.Close()
c := httptest.NewTLSServer(logHit("C (https)"))
defer c.Close()
otherHost := strings.Replace(b.URL, "127.0.0.1", "localhost", 1) + "/landing"
otherScheme := c.URL + "/landing"
// A: the host we actually request; it only redirects
a := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf(" A got %s %s\n", r.Method, r.URL.Path)
switch r.URL.Path {
case "/to-other-host":
w.Header().Set("Location", otherHost)
case "/to-https":
w.Header().Set("Location", otherScheme)
}
w.WriteHeader(http.StatusFound)
}))
defer a.Close()
opts := *rawhttp.DefaultOptions // FollowRedirects: true, MaxRedirects: 10
client := rawhttp.NewClient(&opts)
for _, path := range []string{"/to-other-host", "/to-https"} {
url := a.URL + path
fmt.Printf("GET %s\n", url)
headers := map[string][]string{"Authorization": {"Bearer secret"}}
resp, err := client.DoRaw("GET", url, "", headers, nil)
if err != nil {
fmt.Println(" error:", err)
continue
}
resp.Body.Close()
fmt.Printf(" final status: %d\n", resp.StatusCode)
}
}GET http://127.0.0.1:64023/to-other-host
A got GET /to-other-host
B got GET /landing (Authorization="Bearer secret")
final status: 200
GET http://127.0.0.1:64023/to-https
A got GET /to-https
C (https) got GET /landing (Authorization="Bearer secret")
final status: 200
The output is from v0.2.0; v0.2.2 prints the same. Go 1.27.3 darwin/arm64, macOS 26.7.1.
Describe the use case of this feature:
Scanning one target with redirects turned on, without the client connecting to hosts outside that target, switching scheme, or sending the request's headers to them.
Metadata
Metadata
Assignees
Labels
Type: EnhancementMost issues will probably ask for additions or changes.
Activity