|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync/atomic" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/projectdiscovery/interactsh/pkg/options" |
| 13 | + "github.com/projectdiscovery/interactsh/pkg/server" |
| 14 | + "github.com/projectdiscovery/retryablehttp-go" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestNewAcceptsServerURLWithOptionalTrailingSlash(t *testing.T) { |
| 19 | + var registrationCount atomic.Int64 |
| 20 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | + switch r.URL.EscapedPath() { |
| 22 | + case "//register": |
| 23 | + // Some deployments canonicalize this path with a redirect that drops the POST body. |
| 24 | + http.Redirect(w, r, "/register", http.StatusMovedPermanently) |
| 25 | + case "/register", "/base%2F/register": |
| 26 | + request := &server.RegisterRequest{} |
| 27 | + if err := json.NewDecoder(r.Body).Decode(request); err != nil { |
| 28 | + http.Error(w, fmt.Sprintf(`{"error":"could not decode json body: %s"}`, err), http.StatusBadRequest) |
| 29 | + return |
| 30 | + } |
| 31 | + registrationCount.Add(1) |
| 32 | + _, _ = w.Write([]byte(`{"message":"registration successful"}`)) |
| 33 | + case "/poll": |
| 34 | + _, _ = w.Write([]byte(`{"data":[],"extra":[],"aes_key":""}`)) |
| 35 | + case "/deregister", "/base%2F/deregister": |
| 36 | + w.WriteHeader(http.StatusOK) |
| 37 | + default: |
| 38 | + http.NotFound(w, r) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + testServer := httptest.NewTLSServer(handler) |
| 43 | + t.Cleanup(testServer.Close) |
| 44 | + |
| 45 | + httpClient := retryablehttp.NewClient(retryablehttp.DefaultOptionsSpraying) |
| 46 | + httpClient.HTTPClient = testServer.Client() |
| 47 | + |
| 48 | + for _, test := range []struct { |
| 49 | + name string |
| 50 | + suffix string |
| 51 | + }{ |
| 52 | + {name: "without trailing slash"}, |
| 53 | + {name: "with trailing slash", suffix: "/"}, |
| 54 | + {name: "with percent-encoded slash", suffix: "/base%2F/"}, |
| 55 | + } { |
| 56 | + t.Run(test.name, func(t *testing.T) { |
| 57 | + interactshClient, err := New(&Options{ |
| 58 | + ServerURL: testServer.URL + test.suffix, |
| 59 | + DisableHTTPFallback: true, |
| 60 | + HTTPClient: httpClient, |
| 61 | + }) |
| 62 | + require.NoError(t, err) |
| 63 | + require.NoError(t, interactshClient.Close()) |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + t.Run("resumed session with trailing slash", func(t *testing.T) { |
| 68 | + originalClient, err := New(&Options{ |
| 69 | + ServerURL: testServer.URL, |
| 70 | + DisableHTTPFallback: true, |
| 71 | + HTTPClient: httpClient, |
| 72 | + }) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + publicKey, err := encodePublicKey(originalClient.pubKey) |
| 76 | + require.NoError(t, err) |
| 77 | + sessionInfo := &options.SessionInfo{ |
| 78 | + ServerURL: testServer.URL + "/", |
| 79 | + PrivateKey: string(x509.MarshalPKCS1PrivateKey(originalClient.privKey)), |
| 80 | + CorrelationID: originalClient.correlationID, |
| 81 | + SecretKey: originalClient.secretKey, |
| 82 | + PublicKey: publicKey, |
| 83 | + } |
| 84 | + require.NoError(t, originalClient.Close()) |
| 85 | + |
| 86 | + registrationsBeforeResume := registrationCount.Load() |
| 87 | + resumedClient, err := New(&Options{ |
| 88 | + SessionInfo: sessionInfo, |
| 89 | + HTTPClient: httpClient, |
| 90 | + }) |
| 91 | + require.NoError(t, err) |
| 92 | + require.Equal(t, registrationsBeforeResume+1, registrationCount.Load()) |
| 93 | + require.NoError(t, resumedClient.getInteractions(func(*server.Interaction) {})) |
| 94 | + require.NoError(t, resumedClient.Close()) |
| 95 | + }) |
| 96 | +} |
0 commit comments