Skip to content

Commit 923e9a1

Browse files
committed
Disregard versioning metadata when calculating rule hash.
1 parent b51b1e7 commit 923e9a1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pkg/parser/tree.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,32 @@ func Hash(h string) string {
686686
return base58.Encode(hash[:])
687687
}
688688

689-
func HashRule(data any) (string, error) {
689+
// HashRule to provide a unique stable identity for the rule. It can be used for dupe detection.
690+
// The hash is based on the rule's content, excluding metadata that is not semantically important.
691+
692+
func HashRule(rule ParseRuleT) (string, error) {
693+
694+
// Strip out versioning metadata before calculating the hash.
695+
// The versioning metadata is not semantically important for the rule's content,
696+
// so we can safely ignore it for the purpose of hashing.
697+
// This is important to ensure that the hash remains consistent across changes
698+
// that do not affect the rule's content, such as version bumps or metadata changes.
699+
700+
// The field rule.Metadata.Id is considered part of the rules identity and should be included in the hash.
701+
// Rules can change over time having the following properties:
702+
// - Metadata.Id: Unique identifier for the rule, which is immutable for the lifetime of the rule.
703+
// - Metadata.Hash: A hash of the rule's content, which is regenerated on every semantic change.
704+
// - Metadata.Version: A version string that *should* be incremented on changes, but is not semantically important.
705+
// - Metadata.Gen: A generation counter that is incremented on every change, but is not semantically important.
706+
707+
// NOTE: Modify 'rule' is acceptable because parameter passed by value.
708+
709+
rule.Metadata.Gen = 0 // Gen is bumped on every semantic change, so we don't want it in the hash
710+
rule.Metadata.Hash = "" // Hash is what we are generating here, not semantically important
711+
rule.Metadata.Version = "" // Version may be bumped on change, also not semantically important
712+
690713
// json.Marshal to produce deterministic output
691-
jsonBytes, err := json.Marshal(data)
714+
jsonBytes, err := json.Marshal(rule)
692715
if err != nil {
693716
return "", err
694717
}

0 commit comments

Comments
 (0)