mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-06-09 22:08:15 +00:00
perf(lib): iterate s.policy.Bots by index to drop per-call heap copy (#1639)
Signed-off-by: jvoisin <julien.voisin@dustri.org>
This commit is contained in:
@@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- Add config option to add ASN to logs/metrics.
|
- Add config option to add ASN to logs/metrics.
|
||||||
- Log weight when issuing challenge
|
- Log weight when issuing challenge
|
||||||
- Fix `path_regex` and CEL `path` rules not matching when using Traefik `forwardAuth` middleware. Anubis now checks `X-Forwarded-Uri` (Traefik) in addition to `X-Original-URI` (nginx) when resolving the request path in subrequest mode ([#1628](https://github.com/TecharoHQ/anubis/issues/1628)).
|
- Fix `path_regex` and CEL `path` rules not matching when using Traefik `forwardAuth` middleware. Anubis now checks `X-Forwarded-Uri` (Traefik) in addition to `X-Original-URI` (nginx) when resolving the request path in subrequest mode ([#1628](https://github.com/TecharoHQ/anubis/issues/1628)).
|
||||||
|
- Marginally increase the performances of requests processing
|
||||||
|
- Marginally improve the performances of PoW validation
|
||||||
|
|
||||||
## v1.25.0: Necron
|
## v1.25.0: Necron
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -11,9 +11,8 @@ import (
|
|||||||
// SHA256sum computes a cryptographic hash. Still used for proof-of-work challenges
|
// SHA256sum computes a cryptographic hash. Still used for proof-of-work challenges
|
||||||
// where we need the security properties of a cryptographic hash function.
|
// where we need the security properties of a cryptographic hash function.
|
||||||
func SHA256sum(text string) string {
|
func SHA256sum(text string) string {
|
||||||
hash := sha256.New()
|
sum := sha256.Sum256([]byte(text))
|
||||||
hash.Write([]byte(text))
|
return hex.EncodeToString(sum[:])
|
||||||
return hex.EncodeToString(hash.Sum(nil))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FastHash is a high-performance non-cryptographic hash function suitable for
|
// FastHash is a high-performance non-cryptographic hash function suitable for
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func (i *Impl) Validate(r *http.Request, lg *slog.Logger, in *chall.ValidateInpu
|
|||||||
return chall.NewError("validate", "invalid response", fmt.Errorf("%w nonce", chall.ErrMissingField))
|
return chall.NewError("validate", "invalid response", fmt.Errorf("%w nonce", chall.ErrMissingField))
|
||||||
}
|
}
|
||||||
|
|
||||||
nonce, err := strconv.Atoi(nonceStr)
|
_, err := strconv.Atoi(nonceStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return chall.NewError("validate", "invalid response", fmt.Errorf("%w: nonce: %w", chall.ErrInvalidFormat, err))
|
return chall.NewError("validate", "invalid response", fmt.Errorf("%w: nonce: %w", chall.ErrInvalidFormat, err))
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ func (i *Impl) Validate(r *http.Request, lg *slog.Logger, in *chall.ValidateInpu
|
|||||||
return chall.NewError("validate", "invalid response", fmt.Errorf("%w response", chall.ErrMissingField))
|
return chall.NewError("validate", "invalid response", fmt.Errorf("%w response", chall.ErrMissingField))
|
||||||
}
|
}
|
||||||
|
|
||||||
calcString := fmt.Sprintf("%s%d", challenge, nonce)
|
calcString := challenge + nonceStr
|
||||||
calculated := internal.SHA256sum(calcString)
|
calculated := internal.SHA256sum(calcString)
|
||||||
|
|
||||||
if subtle.ConstantTimeCompare([]byte(response), []byte(calculated)) != 1 {
|
if subtle.ConstantTimeCompare([]byte(response), []byte(calculated)) != 1 {
|
||||||
|
|||||||
+15
-4
@@ -1,8 +1,6 @@
|
|||||||
package policy
|
package policy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
"github.com/TecharoHQ/anubis/lib/config"
|
"github.com/TecharoHQ/anubis/lib/config"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
||||||
@@ -13,9 +11,22 @@ type Bot struct {
|
|||||||
Challenge *config.ChallengeRules
|
Challenge *config.ChallengeRules
|
||||||
Weight *config.Weight
|
Weight *config.Weight
|
||||||
Name string
|
Name string
|
||||||
Action config.Rule
|
// hash caches the result of Hash() when populated at parse time, see ParseConfig
|
||||||
|
hash string
|
||||||
|
Action config.Rule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hash returns a stable identifier for this Bot derived from its Name
|
||||||
|
// and Rules. When the cached value is present (populated by
|
||||||
|
// ParseConfig) it is returned directly; otherwise the hash is
|
||||||
|
// recomputed on demand so callers do not have to know about the cache.
|
||||||
func (b Bot) Hash() string {
|
func (b Bot) Hash() string {
|
||||||
return internal.FastHash(fmt.Sprintf("%s::%s", b.Name, b.Rules.Hash()))
|
if b.hash != "" {
|
||||||
|
return b.hash
|
||||||
|
}
|
||||||
|
var rulesHash string
|
||||||
|
if b.Rules != nil { // defensive, should never happen
|
||||||
|
rulesHash = b.Rules.Hash()
|
||||||
|
}
|
||||||
|
return internal.FastHash(b.Name + "::" + rulesHash)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -219,6 +219,7 @@ func ParseConfig(ctx context.Context, fin io.Reader, fname string, defaultDiffic
|
|||||||
result.Impressum = c.Impressum
|
result.Impressum = c.Impressum
|
||||||
|
|
||||||
parsedBot.Rules = cl
|
parsedBot.Rules = cl
|
||||||
|
parsedBot.hash = parsedBot.Hash()
|
||||||
|
|
||||||
result.Bots = append(result.Bots, parsedBot)
|
result.Bots = append(result.Bots, parsedBot)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user