Skip to content

Commit

Permalink
Merge pull request #9 from rarimo/fix/rand-v2
Browse files Browse the repository at this point in the history
Debug of DDoS guard
  • Loading branch information
artemskriabin authored May 9, 2024
2 parents 64faac6 + 9365983 commit 3a08c4d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/assets/migrations/003_banned.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- +migrate Up
ALTER TABLE claims ADD COLUMN is_banned BOOLEAN NOT NULL DEFAULT FALSE;

-- -migrate Down
-- +migrate Down
ALTER TABLE claims DROP COLUMN is_banned;
2 changes: 1 addition & 1 deletion internal/data/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ClaimQ interface {
New() ClaimQ
Insert(value Claim) error
Update(value Claim) error
Update(fields map[string]any) error
FilterBy(column string, value any) ClaimQ
Get() (*Claim, error)
Select() ([]Claim, error)
Expand Down
7 changes: 3 additions & 4 deletions internal/data/pg/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func (q *claimsQ) Insert(value data.Claim) error {
return err
}

func (q *claimsQ) Update(value data.Claim) error {
clauses := structs.Map(value)
stmt := q.upd.SetMap(clauses)
func (q *claimsQ) Update(fields map[string]any) error {
stmt := q.upd.SetMap(fields)
err := q.db.Exec(stmt)
return err
}
Expand Down Expand Up @@ -80,7 +79,7 @@ func (q *claimsQ) Count() (int, error) {
var result struct {
Count int `db:"count"`
}
err := q.db.Select(&result, q.count)
err := q.db.Get(&result, q.count)
return result.Count, err
}

Expand Down
14 changes: 8 additions & 6 deletions internal/service/api/handlers/create_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"encoding/pem"
"fmt"
"math/big"
"math/rand/v2"
"math/rand"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -211,31 +211,33 @@ func CreateIdentity(w http.ResponseWriter, r *http.Request) {
return
}

existing, err := masterQ.Claim().FilterBy("document_hash", documentHash).Get()
existing, err := masterQ.Claim().FilterBy("document_hash", documentHash.String()).Get()
if err != nil {
Log(r).WithError(err).Error("failed to get claim by document hash")
ape.RenderErr(w, problems.InternalError())
return
}
if existing != nil {
log := Log(r).WithField("document_hash", documentHash)
log := Log(r).WithField("document_hash", documentHash.String())
if existing.IsBanned {
log.Info("user of the provided document is banned")
ape.RenderErr(w, problems.InternalError())
return
}

count, err := masterQ.Claim().FilterBy("document_hash", documentHash).Count()
count, err := masterQ.Claim().FilterBy("document_hash", documentHash.String()).Count()
if err != nil {
log.WithError(err).Error("failed to count claims by document hash")
ape.RenderErr(w, problems.InternalError())
return
}

if count > 0 {
allowed := rand.IntN(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit
allowed := rand.Intn(cfg.MultiAccMaxLimit-cfg.MultiAccMinLimit+1) + cfg.MultiAccMinLimit
if count >= allowed {
err = masterQ.Claim().FilterBy("document_hash", documentHash).Update(data.Claim{IsBanned: true})
err = masterQ.Claim().FilterBy("document_hash", documentHash.String()).Update(map[string]any{
"is_banned": true,
})

if err != nil {
log.WithError(err).Error("failed to ban user")
Expand Down

0 comments on commit 3a08c4d

Please sign in to comment.