Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion notification-dingtalk/dingtalk_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
log.Errorf("user %s has no webhook url", msg.ReceiverUserID)
return
}
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
log.Errorf("user %s has an invalid webhook url", msg.ReceiverUserID)
return
}

notificationMsg, notificationTitle := renderNotification(msg)
// no need to send empty message
Expand All @@ -119,7 +123,7 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
}

// Create a Resty Client
client := resty.New()
client := resty.New().SetRedirectPolicy(resty.NoRedirectPolicy())
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(NewWebhookReq(notificationMsg, notificationTitle)).
Expand Down
16 changes: 16 additions & 0 deletions notification-dingtalk/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package dingtalk
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"

"github.com/apache/answer-plugins/notification-dingtalk/i18n"
Expand Down Expand Up @@ -68,6 +70,15 @@ func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
return userIDs
}

func validateWebhookURL(rawURL string) error {
u, err := url.Parse(rawURL)
if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "oapi.dingtalk.com") ||
(u.Port() != "" && u.Port() != "443") || u.User != nil || u.Fragment != "" {
return fmt.Errorf("invalid webhook url")
}
return nil
}

func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
Expand Down Expand Up @@ -132,6 +143,11 @@ func (n *Notification) UserConfigReceiver(userID string, config []byte) error {
if err != nil {
return fmt.Errorf("unmarshal user config failed: %w", err)
}
if len(userConfig.WebhookURL) > 0 {
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
return err
}
}
n.UserConfigCache.SetUserConfig(userID, &userConfig)
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions notification-dingtalk/webhook_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0.
*/

package dingtalk

import "testing"

func TestValidateWebhookURL(t *testing.T) {
for _, test := range []struct {
url string
want bool
}{
{"https://oapi.dingtalk.com/robot/send?access_token=test", true},
{"http://oapi.dingtalk.com/robot/send", false},
{"https://127.0.0.1/robot/send", false},
{"https://oapi.dingtalk.com.evil.test/robot/send", false},
{"https://oapi.dingtalk.com:8443/robot/send", false},
{"https://user@oapi.dingtalk.com/robot/send", false},
} {
err := validateWebhookURL(test.url)
if (err == nil) != test.want {
t.Errorf("validateWebhookURL(%q) error = %v, want success = %t", test.url, err, test.want)
}
}
}
6 changes: 5 additions & 1 deletion notification-slack/slack_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
log.Errorf("user %s has no webhook url", msg.ReceiverUserID)
return
}
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
log.Errorf("user %s has an invalid webhook url", msg.ReceiverUserID)
return
}

notificationMsg := renderNotification(msg)
// no need to send empty message
Expand All @@ -138,7 +142,7 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
}

// Create a Resty Client
client := resty.New()
client := resty.New().SetRedirectPolicy(resty.NoRedirectPolicy())
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(NewWebhookReq(notificationMsg)).
Expand Down
16 changes: 16 additions & 0 deletions notification-slack/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package slack_notification
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"

"github.com/apache/answer-plugins/notification-slack/i18n"
Expand Down Expand Up @@ -72,6 +74,15 @@ func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
return userIDs
}

func validateWebhookURL(rawURL string) error {
u, err := url.Parse(rawURL)
if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "hooks.slack.com") ||
(u.Port() != "" && u.Port() != "443") || u.User != nil || u.Fragment != "" {
return fmt.Errorf("invalid webhook url")
}
return nil
}

func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
Expand Down Expand Up @@ -160,6 +171,11 @@ func (n *Notification) UserConfigReceiver(userID string, config []byte) error {
if err != nil {
return fmt.Errorf("unmarshal user config failed: %w", err)
}
if len(userConfig.WebhookURL) > 0 {
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
return err
}
}
n.UserConfigCache.SetUserConfig(userID, &userConfig)
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions notification-slack/webhook_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0.
*/

package slack_notification

import "testing"

func TestValidateWebhookURL(t *testing.T) {
for _, test := range []struct {
url string
want bool
}{
{"https://hooks.slack.com/services/test", true},
{"http://hooks.slack.com/services/test", false},
{"https://127.0.0.1/services/test", false},
{"https://hooks.slack.com.evil.test/services/test", false},
{"https://hooks.slack.com:8443/services/test", false},
{"https://user@hooks.slack.com/services/test", false},
} {
err := validateWebhookURL(test.url)
if (err == nil) != test.want {
t.Errorf("validateWebhookURL(%q) error = %v, want success = %t", test.url, err, test.want)
}
}
}
6 changes: 5 additions & 1 deletion notification-wecom/notification_wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
log.Errorf("user %s has no webhook url", msg.ReceiverUserID)
return
}
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
log.Errorf("user %s has an invalid webhook url", msg.ReceiverUserID)
return
}

notificationMsg := renderNotification(msg)
// no need to send empty message
Expand All @@ -118,7 +122,7 @@ func (n *Notification) Notify(msg plugin.NotificationMessage) {
}

// Create a Resty Client
client := resty.New()
client := resty.New().SetRedirectPolicy(resty.NoRedirectPolicy())
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(NewWebhookReq(notificationMsg)).
Expand Down
16 changes: 16 additions & 0 deletions notification-wecom/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package wecom
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"

"github.com/apache/answer-plugins/notification-wecom/i18n"
Expand Down Expand Up @@ -68,6 +70,15 @@ func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
return userIDs
}

func validateWebhookURL(rawURL string) error {
u, err := url.Parse(rawURL)
if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "qyapi.weixin.qq.com") ||
(u.Port() != "" && u.Port() != "443") || u.User != nil || u.Fragment != "" {
return fmt.Errorf("invalid webhook url")
}
return nil
}

func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
Expand Down Expand Up @@ -132,6 +143,11 @@ func (n *Notification) UserConfigReceiver(userID string, config []byte) error {
if err != nil {
return fmt.Errorf("unmarshal user config failed: %w", err)
}
if len(userConfig.WebhookURL) > 0 {
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
return err
}
}
n.UserConfigCache.SetUserConfig(userID, &userConfig)
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions notification-wecom/webhook_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0.
*/

package wecom

import "testing"

func TestValidateWebhookURL(t *testing.T) {
for _, test := range []struct {
url string
want bool
}{
{"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=test", true},
{"http://qyapi.weixin.qq.com/cgi-bin/webhook/send", false},
{"https://127.0.0.1/cgi-bin/webhook/send", false},
{"https://qyapi.weixin.qq.com.evil.test/cgi-bin/webhook/send", false},
{"https://qyapi.weixin.qq.com:8443/cgi-bin/webhook/send", false},
{"https://user@qyapi.weixin.qq.com/cgi-bin/webhook/send", false},
} {
err := validateWebhookURL(test.url)
if (err == nil) != test.want {
t.Errorf("validateWebhookURL(%q) error = %v, want success = %t", test.url, err, test.want)
}
}
}
6 changes: 5 additions & 1 deletion user-center-slack/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (uc *UserCenter) Notify(msg plugin.NotificationMessage) {
log.Errorf("user %s has no webhook url", msg.ReceiverUserID)
return
}
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
log.Errorf("user %s has an invalid webhook url", msg.ReceiverUserID)
return
}

notificationMsg := renderNotification(msg)
// no need to send empty message
Expand All @@ -106,7 +110,7 @@ func (uc *UserCenter) Notify(msg plugin.NotificationMessage) {
}

// Create a Resty Client
client := resty.New()
client := resty.New().SetRedirectPolicy(resty.NoRedirectPolicy())
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(NewWebhookReq(notificationMsg)).
Expand Down
16 changes: 16 additions & 0 deletions user-center-slack/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package slack_user_center
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"

"github.com/apache/answer-plugins/user-center-slack/i18n"
Expand Down Expand Up @@ -72,6 +74,15 @@ func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
return userIDs
}

func validateWebhookURL(rawURL string) error {
u, err := url.Parse(rawURL)
if err != nil || !strings.EqualFold(u.Scheme, "https") || !strings.EqualFold(u.Hostname(), "hooks.slack.com") ||
(u.Port() != "" && u.Port() != "443") || u.User != nil || u.Fragment != "" {
return fmt.Errorf("invalid webhook url")
}
return nil
}

func (uc *UserCenter) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
Expand Down Expand Up @@ -160,6 +171,11 @@ func (uc *UserCenter) UserConfigReceiver(userID string, config []byte) error {
if err != nil {
return fmt.Errorf("unmarshal user config failed: %w", err)
}
if len(userConfig.WebhookURL) > 0 {
if err := validateWebhookURL(userConfig.WebhookURL); err != nil {
return err
}
}
uc.UserConfigCache.SetUserConfig(userID, &userConfig)
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions user-center-slack/webhook_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0.
*/

package slack_user_center

import "testing"

func TestValidateWebhookURL(t *testing.T) {
for _, test := range []struct {
url string
want bool
}{
{"https://hooks.slack.com/services/test", true},
{"http://hooks.slack.com/services/test", false},
{"https://127.0.0.1/services/test", false},
{"https://hooks.slack.com.evil.test/services/test", false},
{"https://hooks.slack.com:8443/services/test", false},
{"https://user@hooks.slack.com/services/test", false},
} {
err := validateWebhookURL(test.url)
if (err == nil) != test.want {
t.Errorf("validateWebhookURL(%q) error = %v, want success = %t", test.url, err, test.want)
}
}
}
Loading