Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pkg/storage/jsondb/json_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
// otherwise we let the readers swarm.
type Scanner struct {
db *detection.SignatureDatabase
sigMap map[string]int
matchThreshold float64
entropyTolerance float64
mu sync.RWMutex
Expand All @@ -44,6 +45,7 @@ type Scanner struct {
func NewScanner() *Scanner {
return &Scanner{
db: &detection.SignatureDatabase{},
sigMap: make(map[string]int),
matchThreshold: 0.75, // 75% minimum confidence keeps the false positives manageable
entropyTolerance: 0.5,
}
Expand Down Expand Up @@ -94,6 +96,13 @@ func (s *Scanner) LoadDatabase(path string) error {
}

s.db = &db

// Rebuild the map index.
s.sigMap = make(map[string]int, len(s.db.Signatures))
for i, sig := range s.db.Signatures {
s.sigMap[sig.ID] = i
}

return nil
}

Expand Down Expand Up @@ -213,6 +222,13 @@ func (s *Scanner) AddSignature(sig *detection.Signature) error {

// Append copies the struct value.
s.db.Signatures = append(s.db.Signatures, *sig)

// Update the map index.
if s.sigMap == nil {
s.sigMap = make(map[string]int)
}
s.sigMap[sig.ID] = len(s.db.Signatures) - 1

return nil
}

Expand Down Expand Up @@ -255,11 +271,14 @@ func (s *Scanner) GetSignature(id string) (*detection.Signature, error) {
if s.db == nil {
return nil, fmt.Errorf("database not loaded")
}
for i := range s.db.Signatures {
if s.db.Signatures[i].ID == id {
return s.deepCopySignature(&s.db.Signatures[i]), nil

if idx, ok := s.sigMap[id]; ok {
// Bounds check just in case of state drift, though locks should prevent it.
if idx >= 0 && idx < len(s.db.Signatures) {
return s.deepCopySignature(&s.db.Signatures[idx]), nil
}
}

return nil, fmt.Errorf("signature %q not found", id)
}

Expand Down
Loading