mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 19:35:27 +00:00
* Enhance matcher compilation with caching for regex and DSL expressions to improve performance. Update template parsing to conditionally retain raw templates based on size constraints. * Implement caching for regex and DSL expressions in extractors and matchers to enhance performance. Introduce a buffer pool in raw requests to reduce memory allocations. Update template cache management for improved efficiency. * feat: improve concurrency to be bound * refactor: replace fmt.Sprintf with fmt.Fprintf for improved performance in header handling * feat: add regex matching tests and benchmarks for performance evaluation * feat: add prefix check in regex extraction to optimize matching process * feat: implement regex caching mechanism to enhance performance in extractors and matchers, along with tests and benchmarks for validation * feat: add unit tests for template execution in the core engine, enhancing test coverage and reliability * feat: enhance error handling in template execution and improve regex caching logic for better performance * Implement caching for regex and DSL expressions in the cache package, replacing previous sync.Map usage. Add unit tests for cache functionality, including eviction by capacity and retrieval of cached items. Update extractors and matchers to utilize the new cache system for improved performance and memory efficiency. * Add tests for SetCapacities in cache package to ensure cache behavior on capacity changes - Implemented TestSetCapacities_NoRebuildOnZero to verify that setting capacities to zero does not clear existing caches. - Added TestSetCapacities_BeforeFirstUse to confirm that initial cache settings are respected and not overridden by subsequent capacity changes. * Refactor matchers and update load test generator to use io package - Removed maxRegexScanBytes constant from match.go. - Replaced ioutil with io package in load_test.go for NopCloser usage. - Restored TestValidate_AllowsInlineMultiline in load_test.go to ensure inline validation functionality. * Add cancellation support in template execution and enhance test coverage - Updated executeTemplateWithTargets to respect context cancellation. - Introduced fakeTargetProvider and slowExecuter for testing. - Added Test_executeTemplateWithTargets_RespectsCancellation to validate cancellation behavior during template execution.
75 lines
2.5 KiB
Go
75 lines
2.5 KiB
Go
package generators
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
|
folderutil "github.com/projectdiscovery/utils/folder"
|
|
)
|
|
|
|
// validate validates the payloads if any.
|
|
func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {
|
|
for name, payload := range payloads {
|
|
switch payloadType := payload.(type) {
|
|
case string:
|
|
if strings.ContainsRune(payloadType, '\n') {
|
|
continue
|
|
}
|
|
|
|
// For historical reasons, "validate" checks to see if the payload file exist.
|
|
// If we're using a custom helper function, then we need to skip any validation beyond just checking the string syntax.
|
|
// Actually attempting to load the file will determine whether or not it exists.
|
|
if g.options.LoadHelperFileFunction != nil {
|
|
return nil
|
|
}
|
|
|
|
// check if it's a file and try to load it
|
|
if fileutil.FileExists(payloadType) {
|
|
continue
|
|
}
|
|
// if file already exists in nuclei-templates directory, skip any further checks
|
|
if fileutil.FileExists(filepath.Join(config.DefaultConfig.GetTemplateDir(), payloadType)) {
|
|
continue
|
|
}
|
|
|
|
// in below code, we calculate all possible paths from root and try to resolve the payload
|
|
// at each level of the path. if the payload is found, we break the loop and continue
|
|
// ex: template-path: /home/user/nuclei-templates/cves/2020/CVE-2020-1234.yaml
|
|
// then we check if helper file "my-payload.txt" exists at below paths:
|
|
// 1. /home/user/nuclei-templates/cves/2020/my-payload.txt
|
|
// 2. /home/user/nuclei-templates/cves/my-payload.txt
|
|
// 3. /home/user/nuclei-templates/my-payload.txt
|
|
// 4. /home/user/my-payload.txt
|
|
// 5. /home/my-payload.txt
|
|
changed := false
|
|
|
|
dir, _ := filepath.Split(templatePath)
|
|
templatePathInfo, _ := folderutil.NewPathInfo(dir)
|
|
payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)
|
|
|
|
for _, payloadPath := range payloadPathsToProbe {
|
|
if fileutil.FileExists(payloadPath) {
|
|
payloads[name] = payloadPath
|
|
changed = true
|
|
break
|
|
}
|
|
}
|
|
if !changed {
|
|
return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)
|
|
}
|
|
case interface{}:
|
|
loadedPayloads := types.ToStringSlice(payloadType)
|
|
if len(loadedPayloads) == 0 {
|
|
return fmt.Errorf("the payload %s does not contain enough elements", name)
|
|
}
|
|
default:
|
|
return fmt.Errorf("the payload %s has invalid type", name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|