set content_length as len(body) if response ContentLength is -1 (#2407)

* set content_length as len(body) if response ContentLength is -1

* move content-length calculation to utils

* adding basic tests

Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
dev
Sajad 2022-10-24 20:07:09 +05:30 committed by GitHub
parent 92ff305e74
commit 928f082109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 2 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
})
}
}