Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: validate token for slack (#12309)
p0ny774502644abe
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 0bedea5f92..ec3dd02168 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -25,6 +25,7 @@ import (api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/plugin/mail""github.com/bytebase/bytebase/backend/plugin/schema"+ "github.com/bytebase/bytebase/backend/plugin/webhook/slack""github.com/bytebase/bytebase/backend/store"storepb "github.com/bytebase/bytebase/proto/generated-go/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"@@ -328,6 +329,7 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Updatereturn nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)}storeSettingValue = string(bytes)+case api.SettingAppIM:payload := new(storepb.AppIMSetting)if err := convertV1PbToStorePb(request.Setting.Value.GetAppImSettingValue(), payload); err != nil {@@ -341,18 +343,33 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Updateswitch path {case "value.app_im_setting_value.slack":setting.Slack = payload.Slack+ if err := slack.ValidateToken(ctx, payload.Slack.GetToken()); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "token doesn't pass validation, error: %v", err)+ }+case "value.app_im_setting_value.feishu":setting.Feishu = payload.Feishudefault:return nil, status.Errorf(codes.InvalidArgument, "invalid update mask path %v", path)}}+ if request.ValidateOnly {+ return &v1pb.Setting{+ Name: request.Setting.Name,+ Value: &v1pb.Value{+ Value: &v1pb.Value_AppImSettingValue{+ AppImSettingValue: &v1pb.AppIMSetting{},+ },+ },+ }, nil+ }bytes, err := protojson.Marshal(setting)if err != nil {return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s, error: %v", apiSettingName, err)}storeSettingValue = string(bytes)+case api.SettingWorkspaceExternalApproval:oldSetting, err := s.store.GetWorkspaceExternalApprovalSetting(ctx)if err != nil {diff --git a/backend/plugin/webhook/slack/app.go b/backend/plugin/webhook/slack/app.goindex 6ebb89851a..8a8ef9845c 100644--- a/backend/plugin/webhook/slack/app.go+++ b/backend/plugin/webhook/slack/app.go@@ -3,6 +3,7 @@ package slackimport ("context""encoding/json"+ "fmt""io""net/http""net/url"@@ -25,6 +26,15 @@ func newProvider(token string) *provider {}}+func ValidateToken(ctx context.Context, token string) error {+ return newProvider(token).listUsers(ctx, 1)+}++type listUsersResponse struct {+ OK bool `json:"ok"`+ Error string `json:"error"`+}+type lookupByEmailResponse struct {OK bool `json:"ok"`Error string `json:"error"`@@ -46,6 +56,43 @@ type chatPostMessageResponse struct {Error string `json:"error"`}+// https://api.slack.com/methods/users.list+func (p *provider) listUsers(ctx context.Context, limit int) error {+ q := url.Values{}+ q.Set("limit", fmt.Sprintf("%d", limit))+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://slack.com/api/users.list", nil)+ if err != nil {+ return errors.Wrapf(err, "failed to new request")+ }+ req.URL.RawQuery = q.Encode()+ req.Header.Add("Authorization", "Bearer "+p.token)+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")++ resp, err := p.c.Do(req)+ if err != nil {+ return errors.Wrapf(err, "failed to send GET request")+ }+ defer resp.Body.Close()++ if resp.StatusCode != http.StatusOK {+ return errors.Errorf("received non-200 status code %d", resp.StatusCode)+ }++ body, err := io.ReadAll(resp.Body)+ if err != nil {+ return errors.Wrapf(err, "failed to read body")+ }+ var res listUsersResponse+ if err := json.Unmarshal(body, &res); err != nil {+ return errors.Wrapf(err, "failed to unmarshal")+ }+ if !res.OK {+ return errors.Errorf("failed to list users, error: %v", res.Error)+ }++ return nil+}+// https://api.slack.com/methods/users.lookupByEmailfunc (p *provider) lookupByEmail(ctx context.Context, email string) (string, error) {q := url.Values{}