-
Notifications
You must be signed in to change notification settings - Fork 156
Fixed Teams notification #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package teams | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/containrrr/shoutrrr" | ||
|
|
@@ -43,19 +45,41 @@ func (p *Provider) Send(message, CliFormat string) error { | |
| p.counter++ | ||
| for _, pr := range p.Teams { | ||
| msg := utils.FormatMessage(message, utils.SelectFormat(CliFormat, pr.TeamsFormat), p.counter) | ||
| webhookParts := strings.Split(pr.TeamsWebHookURL, "/webhookb2/") | ||
| if len(webhookParts) != 2 { | ||
| err := fmt.Errorf("teams: invalid webhook url for id: %s ", pr.ID) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| } | ||
| teamsHost := strings.TrimPrefix(webhookParts[0], "https://") | ||
| teamsTokens := strings.ReplaceAll(webhookParts[1], "IncomingWebhook/", "") | ||
| url := fmt.Sprintf("teams://%s?host=%s", teamsTokens, teamsHost) | ||
| err := shoutrrr.Send(url, msg) | ||
| if err != nil { | ||
| err = errors.Wrap(err, fmt.Sprintf("failed to send teams notification for id: %s ", pr.ID)) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| continue | ||
| provider := strings.Split(pr.TeamsWebHookURL, "/")[3] | ||
|
|
||
| // Deprecated method | ||
| if provider == "webhookb2" { | ||
| webhookParts := strings.Split(pr.TeamsWebHookURL, "/webhookb2/") | ||
| if len(webhookParts) != 2 { | ||
| err := fmt.Errorf("teams: invalid webhook url for id: %s ", pr.ID) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| } | ||
| teamsHost := strings.TrimPrefix(webhookParts[0], "https://") | ||
| teamsTokens := strings.ReplaceAll(webhookParts[1], "IncomingWebhook/", "") | ||
| url := fmt.Sprintf("teams://%s?host=%s", teamsTokens, teamsHost) | ||
| err := shoutrrr.Send(url, msg) | ||
| if err != nil { | ||
| err = errors.Wrap(err, fmt.Sprintf("failed to send webhook teams notification for id: %s", pr.ID)) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| continue | ||
| } | ||
|
|
||
| // New Power Automate method | ||
| } else if provider == "workflows" { | ||
| htmlMessage := strings.ReplaceAll(msg, "\n", "<br>") | ||
| payload := fmt.Sprintf(`{"text": "%s"}`, htmlMessage) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 JSON Injection in Teams Workflow Payload Construction (CWE-91) — The JSON payload for Power Automate workflows is constructed using fmt.Sprintf without escaping user-controlled message content. An attacker can inject double quotes and control characters to break out of the JSON string value and inject arbitrary JSON fields or malformed data. Attack ExampleSuggested Fix🤖 Prompt for AI Agents |
||
| resp, err := http.Post(pr.TeamsWebHookURL, "application/json", bytes.NewBuffer([]byte(payload))) | ||
| if err != nil { | ||
| err = errors.Wrap(err, fmt.Sprintf("failed to send workflow teams notification for id: %s", pr.ID)) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| continue | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusAccepted { | ||
| err = errors.Errorf("failed to send workflow teams notification for id: %s, got status code: %d", pr.ID, resp.StatusCode) | ||
| TeamsErr = multierr.Append(TeamsErr, err) | ||
| continue | ||
| } | ||
| } | ||
| gologger.Verbose().Msgf("teams notification sent for id: %s", pr.ID) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve JSON payload creation for better security.
The current implementation has two potential issues:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dogancanbakir Thanks for confirming! I'm glad you found the suggestion helpful. Let me know if you need any further assistance with the implementation!
(_/)
(^.^)
/>🎯 Bullseye!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai make the necessary changes in this pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dogancanbakir I cannot commit code directly to the repository, but I can provide you with the complete code changes to apply. Here are the necessary modifications:
1. Update the imports section (add
encoding/jsonandhtml):2. Replace the workflows section (lines 68-71 in the current code):
These changes will:
You can apply these changes by updating the
pkg/providers/teams/teams.gofile accordingly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matheusmpupo fyi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.