Validate slack response url
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"go.gearno.de/kit/httpclient"
|
"go.gearno.de/kit/httpclient"
|
||||||
"go.gearno.de/kit/log"
|
"go.gearno.de/kit/log"
|
||||||
@@ -30,6 +31,7 @@ const (
|
|||||||
slackAPIPostMessage = "https://slack.com/api/chat.postMessage"
|
slackAPIPostMessage = "https://slack.com/api/chat.postMessage"
|
||||||
slackAPIUpdateMessage = "https://slack.com/api/chat.update"
|
slackAPIUpdateMessage = "https://slack.com/api/chat.update"
|
||||||
slackAPIConversationJoin = "https://slack.com/api/conversations.join"
|
slackAPIConversationJoin = "https://slack.com/api/conversations.join"
|
||||||
|
slackWebhookHost = "hooks.slack.com"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -104,6 +106,10 @@ func (c *Client) CreateMessage(ctx context.Context, accessToken string, channelI
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UpdateInteractiveMessage(ctx context.Context, responseURL string, body map[string]any) error {
|
func (c *Client) UpdateInteractiveMessage(ctx context.Context, responseURL string, body map[string]any) error {
|
||||||
|
if err := validateSlackResponseURL(responseURL); err != nil {
|
||||||
|
return fmt.Errorf("invalid Slack response URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
updatePayload := map[string]any{
|
updatePayload := map[string]any{
|
||||||
"replace_original": true,
|
"replace_original": true,
|
||||||
"text": body["text"],
|
"text": body["text"],
|
||||||
@@ -257,3 +263,20 @@ func (c *Client) JoinChannel(ctx context.Context, accessToken string, channelID
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateSlackResponseURL(responseURL string) error {
|
||||||
|
parsedURL, err := url.Parse(responseURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid URL format: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsedURL.Scheme != "https" {
|
||||||
|
return fmt.Errorf("invalid URL scheme: must be https")
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsedURL.Host != slackWebhookHost {
|
||||||
|
return fmt.Errorf("invalid URL host: must be %s", slackWebhookHost)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
slackMessageDeduplicationWindow = 7 * 24 * time.Hour
|
slackMessageDeduplicationWindow = 7 * 24 * time.Hour
|
||||||
trustCenterAccessURLFormat = "https://%s/organizations/%s/trust-center/access"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|||||||
Reference in New Issue
Block a user