diff --git a/v2/pkg/protocols/http/operators.go b/v2/pkg/protocols/http/operators.go index c9c0000db..41de04f84 100644 --- a/v2/pkg/protocols/http/operators.go +++ b/v2/pkg/protocols/http/operators.go @@ -12,6 +12,7 @@ import ( "github.com/projectdiscovery/nuclei/v2/pkg/output" "github.com/projectdiscovery/nuclei/v2/pkg/protocols" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/utils" "github.com/projectdiscovery/nuclei/v2/pkg/types" ) @@ -121,7 +122,6 @@ func (request *Request) responseToDSLMap(resp *http.Response, host, matched, raw data["response"] = rawResp data["status_code"] = resp.StatusCode data["body"] = body - data["content_length"] = resp.ContentLength data["all_headers"] = headers data["header"] = headers data["duration"] = duration.Seconds() @@ -129,6 +129,8 @@ func (request *Request) responseToDSLMap(resp *http.Response, host, matched, raw data["template-info"] = request.options.TemplateInfo data["template-path"] = request.options.TemplatePath + data["content_length"] = utils.CalculateContentLength(resp.ContentLength, int64(len(body))) + if request.StopAtFirstMatch || request.options.StopAtFirstMatch { data["stop-at-first-match"] = true } diff --git a/v2/pkg/protocols/offlinehttp/operators.go b/v2/pkg/protocols/offlinehttp/operators.go index 8296269cd..007bfe4d0 100644 --- a/v2/pkg/protocols/offlinehttp/operators.go +++ b/v2/pkg/protocols/offlinehttp/operators.go @@ -12,6 +12,7 @@ import ( "github.com/projectdiscovery/nuclei/v2/pkg/output" "github.com/projectdiscovery/nuclei/v2/pkg/protocols" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/utils" "github.com/projectdiscovery/nuclei/v2/pkg/types" ) @@ -112,7 +113,6 @@ func (request *Request) responseToDSLMap(resp *http.Response, host, matched, raw data["matched"] = matched data["request"] = rawReq data["response"] = rawResp - data["content_length"] = resp.ContentLength data["status_code"] = resp.StatusCode data["body"] = body data["type"] = request.Type().String() @@ -121,6 +121,8 @@ func (request *Request) responseToDSLMap(resp *http.Response, host, matched, raw data["template-id"] = request.options.TemplateID data["template-info"] = request.options.TemplateInfo data["template-path"] = request.options.TemplatePath + data["content_length"] = utils.CalculateContentLength(resp.ContentLength, int64(len(body))) + return data } diff --git a/v2/pkg/protocols/utils/utils.go b/v2/pkg/protocols/utils/utils.go index f07acef7f..cdc0b367d 100644 --- a/v2/pkg/protocols/utils/utils.go +++ b/v2/pkg/protocols/utils/utils.go @@ -38,3 +38,11 @@ func AddConfiguredClientCertToRequest(tlsConfig *tls.Config, options *types.Opti } return tlsConfig, nil } + +// CalculateContentLength calculates content-length of the http response +func CalculateContentLength(contentLength, bodyLength int64) int64 { + if contentLength > -1 { + return contentLength + } + return bodyLength +} diff --git a/v2/pkg/protocols/utils/utils_test.go b/v2/pkg/protocols/utils/utils_test.go new file mode 100644 index 000000000..54f0652ab --- /dev/null +++ b/v2/pkg/protocols/utils/utils_test.go @@ -0,0 +1,27 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCalculateContentLength(t *testing.T) { + tests := []struct { + name string + expected int64 + contentLengthHeader int64 + bodyLength int64 + }{ + {"content-length-header", 10, 10, 10}, + {"content-length-header-with-body-length", 10, 10, 1000}, + {"no-content-length-header-with-body-length", 1000, -1, 1000}, + {"content-length-header-without-body-length", 10, 10, -1}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := CalculateContentLength(test.contentLengthHeader, test.bodyLength) + require.Equal(t, test.expected, got) + }) + } +}