Skip to content

Commit

Permalink
normalize before verification
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed Dec 1, 2024
1 parent b319797 commit e6a0525
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions pkg/templates/signer/tmpl_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
errorutil "github.com/projectdiscovery/utils/errors"
"gopkg.in/yaml.v2"
)

var (
Expand All @@ -31,21 +30,12 @@ func ExtractSignatureAndContent(data []byte) (signature, content []byte) {
dataStr := string(data)
if idx := strings.LastIndex(dataStr, SignaturePattern); idx != -1 {
signature = []byte(strings.TrimSpace(dataStr[idx:]))
content = []byte(strings.TrimSpace(dataStr[:idx]))
content = bytes.TrimSpace(data[:idx])
} else {
content = data
}
// use yaml unmarshalling and marshalling as standard to normalization
// if we use strings.ReplaceAll(content,"\r\n","\n"), it most likely will break some payload in logic
var normalized interface{}
if err := yaml.Unmarshal(content, &normalized); err != nil {
return signature, content
}
normalizedBytes, err := yaml.Marshal(normalized)
if err != nil {
return signature, content
}
return signature, normalizedBytes
content = bytes.TrimSpace(content)
return signature, content
}

// SignableTemplate is a template that can be signed
Expand Down Expand Up @@ -156,6 +146,10 @@ func (t *TemplateSigner) Verify(data []byte, tmpl SignableTemplate) (bool, error
return false, err
}

// normalize content by removing \r\n everywhere since this only done for verification
// it does not affect the actual template
content = bytes.ReplaceAll(content, []byte("\r\n"), []byte("\n"))

buff := bytes.NewBuffer(content)
// if file has any imports process them
for _, file := range tmpl.GetFileImports() {
Expand Down

0 comments on commit e6a0525

Please sign in to comment.