Эх сурвалжийг харах

Validate webhook URLs when using set-relay --url

Tulir Asokan 2 жил өмнө
parent
commit
7bfa885530
1 өөрчлөгдсөн 22 нэмэгдсэн , 10 устгасан
  1. 22 10
      commands.go

+ 22 - 10
commands.go

@@ -387,8 +387,7 @@ func fnSetRelay(ce *WrappedCommandEvent) {
 	}
 	}
 	log := ce.ZLog.With().Str("channel_id", portal.Key.ChannelID).Logger()
 	log := ce.ZLog.With().Str("channel_id", portal.Key.ChannelID).Logger()
 	createType := strings.ToLower(strings.TrimLeft(ce.Args[0], "-"))
 	createType := strings.ToLower(strings.TrimLeft(ce.Args[0], "-"))
-	var webhookID int64
-	var webhookSecret string
+	var webhookMeta *discordgo.Webhook
 	switch createType {
 	switch createType {
 	case "url":
 	case "url":
 		if len(ce.Args) < 2 {
 		if len(ce.Args) < 2 {
@@ -396,12 +395,20 @@ func fnSetRelay(ce *WrappedCommandEvent) {
 			return
 			return
 		}
 		}
 		ce.Redact()
 		ce.Redact()
+		var webhookID int64
+		var webhookSecret string
 		_, err := fmt.Sscanf(ce.Args[1], webhookURLFormat, &webhookID, &webhookSecret)
 		_, err := fmt.Sscanf(ce.Args[1], webhookURLFormat, &webhookID, &webhookSecret)
 		if err != nil {
 		if err != nil {
 			log.Warn().Str("webhook_url", ce.Args[1]).Err(err).Msg("Failed to parse provided webhook URL")
 			log.Warn().Str("webhook_url", ce.Args[1]).Err(err).Msg("Failed to parse provided webhook URL")
 			ce.Reply("Invalid webhook URL")
 			ce.Reply("Invalid webhook URL")
 			return
 			return
 		}
 		}
+		webhookMeta, err = relayClient.WebhookWithToken(strconv.FormatInt(webhookID, 10), webhookSecret)
+		if err != nil {
+			log.Warn().Err(err).Msg("Failed to get webhook info")
+			ce.Reply("Failed to get webhook info: %v", err)
+			return
+		}
 	case "create":
 	case "create":
 		perms, err := ce.User.Session.UserChannelPermissions(ce.User.DiscordID, portal.Key.ChannelID)
 		perms, err := ce.User.Session.UserChannelPermissions(ce.User.DiscordID, portal.Key.ChannelID)
 		if err != nil {
 		if err != nil {
@@ -418,24 +425,29 @@ func fnSetRelay(ce *WrappedCommandEvent) {
 			name = strings.Join(ce.Args[1:], " ")
 			name = strings.Join(ce.Args[1:], " ")
 		}
 		}
 		log.Debug().Str("webhook_name", name).Msg("Creating webhook")
 		log.Debug().Str("webhook_name", name).Msg("Creating webhook")
-		webhook, err := ce.User.Session.WebhookCreate(portal.Key.ChannelID, name, "")
+		webhookMeta, err = ce.User.Session.WebhookCreate(portal.Key.ChannelID, name, "")
 		if err != nil {
 		if err != nil {
 			log.Warn().Err(err).Msg("Failed to create webhook")
 			log.Warn().Err(err).Msg("Failed to create webhook")
 			ce.Reply("Failed to create webhook: %v", err)
 			ce.Reply("Failed to create webhook: %v", err)
 			return
 			return
 		}
 		}
-		webhookID, _ = strconv.ParseInt(webhook.ID, 10, 64)
-		ce.Reply("Created webhook %s", webhook.Name)
-		webhookSecret = webhook.Token
 	default:
 	default:
 		ce.Reply(selectRelayHelp)
 		ce.Reply(selectRelayHelp)
 		return
 		return
 	}
 	}
-	log.Debug().Int64("webhook_id", webhookID).Msg("Setting portal relay webhook")
-	portal.RelayWebhookID = strconv.FormatInt(webhookID, 10)
-	portal.RelayWebhookSecret = webhookSecret
+	if portal.Key.ChannelID != webhookMeta.ChannelID {
+		log.Debug().
+			Str("portal_channel_id", portal.Key.ChannelID).
+			Str("webhook_channel_id", webhookMeta.ChannelID).
+			Msg("Provided webhook is for wrong channel")
+		ce.Reply("That webhook is not for the right channel (expected %s, webhook is for %s)", portal.Key.ChannelID, webhookMeta.ChannelID)
+		return
+	}
+	log.Debug().Str("webhook_id", webhookMeta.ID).Msg("Setting portal relay webhook")
+	portal.RelayWebhookID = webhookMeta.ID
+	portal.RelayWebhookSecret = webhookMeta.Token
 	portal.Update()
 	portal.Update()
-	ce.Reply("Saved webhook ID %s as portal relay webhook", portal.RelayWebhookID)
+	ce.Reply("Saved webhook %s (%s) as portal relay webhook", webhookMeta.Name, portal.RelayWebhookID)
 }
 }
 
 
 var cmdGuilds = &commands.FullHandler{
 var cmdGuilds = &commands.FullHandler{