Path: blob/dev/pkg/protocols/common/hosterrorscache/hosterrorscache.go
2843 views
package hosterrorscache12import (3"errors"4"net"5"net/url"6"regexp"7"strings"8"sync"9"sync/atomic"1011"github.com/projectdiscovery/gcache"12"github.com/projectdiscovery/gologger"13"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"14"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"15"github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr"16"github.com/projectdiscovery/utils/errkit"17stringsutil "github.com/projectdiscovery/utils/strings"18)1920// CacheInterface defines the signature of the hosterrorscache so that21// users of Nuclei as embedded lib may implement their own cache22type CacheInterface interface {23SetVerbose(verbose bool) // log verbosely24Close() // close the cache25Check(protoType string, ctx *contextargs.Context) bool // return true if the host should be skipped26Remove(ctx *contextargs.Context) // remove a host from the cache27MarkFailed(protoType string, ctx *contextargs.Context, err error) // record a failure (and cause) for the host28MarkFailedOrRemove(protoType string, ctx *contextargs.Context, err error) // record a failure (and cause) for the host or remove it29IsPermanentErr(ctx *contextargs.Context, err error) bool // return true if the error is permanent for the host30}3132var (33_ CacheInterface = (*Cache)(nil)34)3536// Cache is a cache for host based errors. It allows skipping37// certain hosts based on an error threshold.38//39// It uses an LRU cache internally for skipping unresponsive hosts40// that remain so for a duration.41type Cache struct {42MaxHostError int43verbose bool44failedTargets gcache.Cache[string, *cacheItem]45TrackError []string46}4748type cacheItem struct {49sync.Once50errors atomic.Int3251isPermanentErr bool52cause error // optional cause53mu sync.Mutex54}5556const DefaultMaxHostsCount = 100005758// New returns a new host max errors cache59func New(maxHostError, maxHostsCount int, trackError []string) *Cache {60gc := gcache.New[string, *cacheItem](maxHostsCount).ARC().Build()6162return &Cache{63failedTargets: gc,64MaxHostError: maxHostError,65TrackError: trackError,66}67}6869// SetVerbose sets the cache to log at verbose level70func (c *Cache) SetVerbose(verbose bool) {71c.verbose = verbose72}7374// Close closes the host errors cache75func (c *Cache) Close() {76if config.DefaultConfig.IsDebugArgEnabled(config.DebugArgHostErrorStats) {77items := c.failedTargets.GetALL(false)78for k, val := range items {79gologger.Info().Label("MaxHostErrorStats").Msgf("Host: %s, Errors: %d", k, val.errors.Load())80}81}82c.failedTargets.Purge()83}8485// NormalizeCacheValue processes the input value and returns a normalized cache86// value.87func (c *Cache) NormalizeCacheValue(value string) string {88var normalizedValue = value8990u, err := url.ParseRequestURI(value)91if err != nil || u.Host == "" {92if strings.Contains(value, ":") {93return normalizedValue94}95u, err2 := url.ParseRequestURI("https://" + value)96if err2 != nil {97return normalizedValue98}99100normalizedValue = u.Host101} else {102port := u.Port()103if port == "" {104switch u.Scheme {105case "https":106normalizedValue = net.JoinHostPort(u.Host, "443")107case "http":108normalizedValue = net.JoinHostPort(u.Host, "80")109}110} else {111normalizedValue = u.Host112}113}114115return normalizedValue116}117118// ErrUnresponsiveHost is returned when a host is unresponsive119// var ErrUnresponsiveHost = errors.New("skipping as host is unresponsive")120121// Check returns true if a host should be skipped as it has been122// unresponsive for a certain number of times.123//124// The value can be many formats -125// - URL: https?:// type126// - Host:port type127// - host type128func (c *Cache) Check(protoType string, ctx *contextargs.Context) bool {129finalValue := c.GetKeyFromContext(ctx, nil)130131cache, err := c.failedTargets.GetIFPresent(finalValue)132if err != nil {133return false134}135136cache.mu.Lock()137defer cache.mu.Unlock()138139if cache.isPermanentErr {140cache.Do(func() {141gologger.Info().Msgf("Skipped %s from target list as found unresponsive permanently: %s", finalValue, cache.cause)142})143return true144}145146if cache.errors.Load() >= int32(c.MaxHostError) {147cache.Do(func() {148gologger.Info().Msgf("Skipped %s from target list as found unresponsive %d times", finalValue, cache.errors.Load())149})150return true151}152153return false154}155156// Remove removes a host from the cache157func (c *Cache) Remove(ctx *contextargs.Context) {158key := c.GetKeyFromContext(ctx, nil)159_ = c.failedTargets.Remove(key) // remove even the cache is not present160}161162// MarkFailed marks a host as failed previously163//164// Deprecated: Use MarkFailedOrRemove instead.165func (c *Cache) MarkFailed(protoType string, ctx *contextargs.Context, err error) {166if err == nil {167return168}169170c.MarkFailedOrRemove(protoType, ctx, err)171}172173// MarkFailedOrRemove marks a host as failed previously or removes it174func (c *Cache) MarkFailedOrRemove(protoType string, ctx *contextargs.Context, err error) {175if err != nil && !c.checkError(protoType, err) {176return177}178179if err == nil {180// Remove the host from cache181//182// NOTE(dwisiswant0): The decision was made to completely remove the183// cached entry for the host instead of simply decrementing the error184// count (using `(atomic.Int32).Swap` to update the value to `N-1`).185// This approach was chosen because the error handling logic operates186// concurrently, and decrementing the count could lead to UB (unexpected187// behavior) even when the error is `nil`.188//189// To clarify, consider the following scenario where the error190// encountered does NOT belong to the permanent network error category191// (`errkit.ErrKindNetworkPermanent`):192//193// 1. Iteration 1: A timeout error occurs, and the error count for the194// host is incremented.195// 2. Iteration 2: Another timeout error is encountered, leading to196// another increment in the host's error count.197// 3. Iteration 3: A third timeout error happens, which increments the198// error count further. At this point, the host is flagged as199// unresponsive.200// 4. Iteration 4: The host becomes reachable (no error or a transient201// issue resolved). Instead of performing a no-op and leaving the202// host in the cache, the host entry is removed entirely to reset its203// state.204// 5. Iteration 5: A subsequent timeout error occurs after the host was205// removed and re-added to the cache. The error count is reset and206// starts from 1 again.207//208// This removal strategy ensures the cache is updated dynamically to209// reflect the current state of the host without persisting stale or210// irrelevant error counts that could interfere with future error211// handling and tracking logic.212c.Remove(ctx)213214return215}216217cacheKey := c.GetKeyFromContext(ctx, err)218cache, cacheErr := c.failedTargets.GetIFPresent(cacheKey)219if errors.Is(cacheErr, gcache.KeyNotFoundError) {220cache = &cacheItem{errors: atomic.Int32{}}221}222223cache.mu.Lock()224defer cache.mu.Unlock()225226if errkit.IsKind(err, errkit.ErrKindNetworkPermanent) {227cache.isPermanentErr = true228}229230cache.cause = err231cache.errors.Add(1)232233_ = c.failedTargets.Set(cacheKey, cache)234}235236// IsPermanentErr returns true if the error is permanent for the host.237func (c *Cache) IsPermanentErr(ctx *contextargs.Context, err error) bool {238if err == nil {239return false240}241242if errkit.IsKind(err, errkit.ErrKindNetworkPermanent) {243return true244}245246cacheKey := c.GetKeyFromContext(ctx, err)247cache, cacheErr := c.failedTargets.GetIFPresent(cacheKey)248if cacheErr != nil {249return false250}251252cache.mu.Lock()253defer cache.mu.Unlock()254255return cache.isPermanentErr256}257258// GetKeyFromContext returns the key for the cache from the context259func (c *Cache) GetKeyFromContext(ctx *contextargs.Context, err error) string {260// Note:261// ideally any changes made to remote addr in template like {{Hostname}}:81 etc262// should be reflected in contextargs but it is not yet reflected in some cases263// and needs refactor of ScanContext + ContextArgs to achieve that264// i.e why we use real address from error if present265var address string266267// 1. the address carried inside the error (if the transport sets it)268if err != nil {269if v := errkit.GetAttrValue(err, "address"); v.Any() != nil {270address = v.String()271}272}273274if address == "" {275address = ctx.MetaInput.Address()276}277278finalValue := c.NormalizeCacheValue(address)279return finalValue280}281282var reCheckError = regexp.MustCompile(`(no address found for host|could not resolve host|connection refused|connection reset by peer|could not connect to any address found for host|timeout awaiting response headers)`)283284// checkError checks if an error represents a type that should be285// added to the host skipping table.286// it first parses error and extracts the cause and checks for blacklisted287// or common errors that should be skipped288func (c *Cache) checkError(protoType string, err error) bool {289if err == nil {290return false291}292if protoType != "http" {293return false294}295kind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic)296switch kind {297case nucleierr.ErrTemplateLogic:298// these are errors that are not related to the target299// and are due to template logic300return false301case errkit.ErrKindNetworkTemporary:302// these should not be counted as host errors303return false304case errkit.ErrKindNetworkPermanent:305// these should be counted as host errors306return true307case errkit.ErrKindDeadline:308// these should not be counted as host errors309return false310default:311// parse error for further processing312errX := errkit.FromError(err)313tmp := errX.Cause()314cause := tmp.Error()315if stringsutil.ContainsAll(cause, "ReadStatusLine:", "read: connection reset by peer") {316// this is a FP and should not be counted as a host error317// because server closes connection when it reads corrupted bytes which we send via rawhttp318return false319}320if strings.HasPrefix(cause, "ReadStatusLine:") {321// error is present in last part when using rawhttp322// this will be fixed once errkit is used everywhere323lastIndex := strings.LastIndex(cause, ":")324if lastIndex == -1 {325lastIndex = 0326}327if lastIndex >= len(cause)-1 {328lastIndex = 0329}330cause = cause[lastIndex+1:]331}332for _, msg := range c.TrackError {333if strings.Contains(cause, msg) {334return true335}336}337return reCheckError.MatchString(cause)338}339}340341342