Skip to content

Commit bfb2360

Browse files
committed
[webhooks] add validation of webhook event
1 parent 7811071 commit bfb2360

File tree

7 files changed

+56
-12
lines changed

7 files changed

+56
-12
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22.0
44

55
require (
66
firebase.google.com/go/v4 v4.12.1
7-
github.com/android-sms-gateway/client-go v1.0.2
7+
github.com/android-sms-gateway/client-go v1.0.4
88
github.com/ansrivas/fiberprometheus/v2 v2.6.1
99
github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3
1010
github.com/capcom6/go-infra-fx v0.0.2

Diff for: go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV
2828
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
2929
github.com/android-sms-gateway/client-go v1.0.2 h1:kXWzVeSgBu2bM1yN4ac8tTEm0fX2Tqsn+yr9mMNjNfI=
3030
github.com/android-sms-gateway/client-go v1.0.2/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
31+
github.com/android-sms-gateway/client-go v1.0.3 h1:Q2pMI/1Lhsw7wu+I+nAaMD4yYvzhQPsP2py71HG7rVo=
32+
github.com/android-sms-gateway/client-go v1.0.3/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
33+
github.com/android-sms-gateway/client-go v1.0.4 h1:QZ72TRBJKm11WL/jim+ba7m2J5RLBaICMcy7f/RVfuQ=
34+
github.com/android-sms-gateway/client-go v1.0.4/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
3135
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
3236
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
3337
github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+MnDOJZ3SK66kXM=

Diff for: internal/sms-gateway/handlers/webhooks/3rdparty.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package webhooks
33
import (
44
"fmt"
55

6-
"github.com/android-sms-gateway/client-go/smsgateway"
6+
dto "github.com/android-sms-gateway/client-go/smsgateway/webhooks"
77
"github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base"
88
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
99
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth"
@@ -64,13 +64,17 @@ func (h *ThirdPartyController) get(user models.User, c *fiber.Ctx) error {
6464
//
6565
// Register webhook
6666
func (h *ThirdPartyController) post(user models.User, c *fiber.Ctx) error {
67-
dto := &smsgateway.Webhook{}
67+
dto := &dto.Webhook{}
6868

6969
if err := h.BodyParserValidator(c, dto); err != nil {
7070
return err
7171
}
7272

7373
if err := h.webhooksSvc.Replace(user.ID, dto); err != nil {
74+
if webhooks.IsValidationError(err) {
75+
return fiber.NewError(fiber.StatusBadRequest, err.Error())
76+
}
77+
7478
return fmt.Errorf("can't write webhook: %w", err)
7579
}
7680

Diff for: internal/sms-gateway/modules/webhooks/converters.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package webhooks
22

3-
import "github.com/android-sms-gateway/client-go/smsgateway"
3+
import (
4+
"github.com/android-sms-gateway/client-go/smsgateway/webhooks"
5+
)
46

5-
func webhookToDTO(model *Webhook) smsgateway.Webhook {
6-
return smsgateway.Webhook{
7+
func webhookToDTO(model *Webhook) webhooks.Webhook {
8+
return webhooks.Webhook{
79
ID: model.ExtID,
810
URL: model.URL,
911
Event: model.Event,

Diff for: internal/sms-gateway/modules/webhooks/errors.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package webhooks
2+
3+
import "fmt"
4+
5+
type ValidationError struct {
6+
Field string
7+
Value string
8+
Err error
9+
}
10+
11+
func (e ValidationError) Error() string {
12+
return fmt.Sprintf("invalid `%s` = `%s`: %s", e.Field, e.Value, e.Err)
13+
}
14+
15+
func (e ValidationError) Unwrap() error {
16+
return e.Err
17+
}
18+
19+
func newValidationError(field, value string, err error) ValidationError {
20+
return ValidationError{
21+
Field: field,
22+
Value: value,
23+
Err: err,
24+
}
25+
}
26+
27+
func IsValidationError(err error) bool {
28+
_, ok := err.(ValidationError)
29+
return ok
30+
}

Diff for: internal/sms-gateway/modules/webhooks/models.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package webhooks
22

33
import (
4-
"github.com/android-sms-gateway/client-go/smsgateway"
4+
"github.com/android-sms-gateway/client-go/smsgateway/webhooks"
55
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
66
"gorm.io/gorm"
77
)
@@ -11,8 +11,8 @@ type Webhook struct {
1111
ExtID string `json:"id" gorm:"not null;type:varchar(36);uniqueIndex:unq_webhooks_user_extid,priority:2"`
1212
UserID string `json:"-" gorm:"<-:create;not null;type:varchar(32);uniqueIndex:unq_webhooks_user_extid,priority:1"`
1313

14-
URL string `json:"url" validate:"required,http_url" gorm:"not null;type:varchar(256)"`
15-
Event smsgateway.WebhookEvent `json:"event" gorm:"not null;type:varchar(32)"`
14+
URL string `json:"url" validate:"required,http_url" gorm:"not null;type:varchar(256)"`
15+
Event webhooks.EventType `json:"event" gorm:"not null;type:varchar(32)"`
1616

1717
User models.User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
1818

Diff for: internal/sms-gateway/modules/webhooks/service.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package webhooks
33
import (
44
"fmt"
55

6-
"github.com/android-sms-gateway/client-go/smsgateway"
6+
"github.com/android-sms-gateway/client-go/smsgateway/webhooks"
77
"github.com/capcom6/go-helpers/slices"
88
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/db"
99
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/devices"
@@ -46,7 +46,7 @@ func NewService(params ServiceParams) *Service {
4646
}
4747
}
4848

49-
func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.Webhook, error) {
49+
func (s *Service) Select(userID string, filters ...SelectFilter) ([]webhooks.Webhook, error) {
5050
filters = append(filters, WithUserID(userID))
5151

5252
items, err := s.webhooks.Select(filters...)
@@ -57,7 +57,11 @@ func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.W
5757
return slices.Map(items, webhookToDTO), nil
5858
}
5959

60-
func (s *Service) Replace(userID string, webhook *smsgateway.Webhook) error {
60+
func (s *Service) Replace(userID string, webhook *webhooks.Webhook) error {
61+
if !webhooks.IsValidEventType(webhook.Event) {
62+
return newValidationError("event", string(webhook.Event), fmt.Errorf("enum value expected"))
63+
}
64+
6165
if webhook.ID == "" {
6266
webhook.ID = s.idgen()
6367
}

0 commit comments

Comments
 (0)