Skip to content

Commit

Permalink
Fix fulfillPollEvent request validation. Change http method for fulfi…
Browse files Browse the repository at this point in the history
…llPollEvent to POST. Add challenged event id validation
  • Loading branch information
Zaptoss committed Aug 2, 2024
1 parent 70379e2 commit 8594e4f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
9 changes: 7 additions & 2 deletions internal/config/poll_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
const pollVerificationKey = "./proof_keys/poll.json"

var (
ErrInvalidProposalEventID = errors.New("proposal event id mismatching")
ErrInvalidRoot = errors.New("invalid root")
ErrInvalidProposalEventID = errors.New("invalid proposal event id")
ErrInvalidRoot = errors.New("invalid root")
ErrInvalidChallengedEventID = errors.New("invalid challenged event id")
)

const (
Expand Down Expand Up @@ -112,6 +113,10 @@ func (v *PollVerifier) VerifyProof(proof zkptypes.ZKProof, proposalID, proposalE
return ErrInvalidRoot
}

if proof.PubSignals[PollChallengedEventID] != proofEventIDValue {
return ErrInvalidChallengedEventID
}

if err = zkpverifier.VerifyGroth16(proof, v.verificationKey); err != nil {
return fmt.Errorf("failed to verify proof: %w", err)
}
Expand Down
11 changes: 9 additions & 2 deletions internal/service/handlers/fulfill_poll_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"net/http"
"strings"

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/rarimo/geo-auth-svc/pkg/auth"
Expand All @@ -26,9 +27,13 @@ func FulfillPollEvent(w http.ResponseWriter, r *http.Request) {
}

proof := req.Data.Attributes.Proof
nullifier := proof.PubSignals[config.PollChallengedNullifier]

nullifierDec, _ := new(big.Int).SetString(proof.PubSignals[config.PollChallengedNullifier], 10)
nullifier := "0x" + strings.ToLower(nullifierDec.Text(16))

proposalID, _ := new(big.Int).SetString(req.Data.Attributes.ProposalId, 10)
proposalEventID, _ := new(big.Int).SetString(proof.PubSignals[config.PollParticipationEventID], 10)

if !auth.Authenticates(UserClaims(r), auth.UserGrant(nullifier)) {
ape.RenderErr(w, problems.Unauthorized())
return
Expand Down Expand Up @@ -96,7 +101,9 @@ func FulfillPollEvent(w http.ResponseWriter, r *http.Request) {
err = PollVerifier(r).VerifyProof(proof, proposalID, proposalEventID)
if err != nil {
log.WithError(err).Debug("Failed to verify passport")
if errors.Is(err, config.ErrInvalidProposalEventID) || errors.Is(err, config.ErrInvalidRoot) {
if errors.Is(err, config.ErrInvalidProposalEventID) ||
errors.Is(err, config.ErrInvalidRoot) ||
errors.Is(err, config.ErrInvalidChallengedEventID) {
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"proof": err,
})...)
Expand Down
4 changes: 1 addition & 3 deletions internal/service/requests/fulfill_poll_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
val "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/rarimo/geo-points-svc/resources"
zk "github.com/rarimo/zkverifier-kit"
)

func NewFulfillPollEvent(r *http.Request) (req resources.FulfillPollEventRequest, err error) {
Expand All @@ -18,13 +17,12 @@ func NewFulfillPollEvent(r *http.Request) (req resources.FulfillPollEventRequest

var (
proof = req.Data.Attributes.Proof
count = zk.PubSignalsCount(zk.PollParticipation)
)

return req, val.Errors{
"data/type": val.Validate(req.Data.Type, val.Required, val.In(resources.FULFILL_POLL_EVENT)),
"data/attributes/proof/proof": val.Validate(proof.Proof, val.Required),
"data/attributes/proof/pub_signals": val.Validate(proof.PubSignals, val.Required, val.Length(count, count)),
"data/attributes/proof/pub_signals": val.Validate(proof.PubSignals, val.Required, val.Length(4, 4)),
"data/attributes/proposal_id": val.Validate(req.Data.Attributes.ProposalId, val.Required, is.Digit),
}.Filter()
}
2 changes: 1 addition & 1 deletion internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Run(ctx context.Context, cfg config.Config) {
r.Route("/events", func(r chi.Router) {
r.Use(authMW)
r.Get("/", handlers.ListEvents)
r.Patch("/poll", handlers.FulfillPollEvent)
r.Post("/poll", handlers.FulfillPollEvent)
r.Route("/{id}", func(r chi.Router) {
r.Get("/", handlers.GetEvent)
r.Patch("/", handlers.ClaimEvent)
Expand Down

0 comments on commit 8594e4f

Please sign in to comment.