Files
mattermost/server/channels/api4/post_utils.go
T
e46bea673d MM-67312: Restrict Burn-on-Read for self DMs and bot users (#35116)
* MM-67312: Restrict Burn-on-Read for self DMs and bot users

* fix lint issues

* use utility function to make code more reliable

* add test case for deleted user and handle restrictively that scenario

* fix i18n

* Allow bots to send BoR; block only self-DMs & DMs with bots

* Refactor BoR validation to API layer with individual params

* adjust comment

* Fix BoR validation to fail-closed when context unavailable

* Fix variable shadowing in CreatePost burn-on-read validation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* remove translation entry

* fix linter

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-12 14:10:05 -05:00

80 lines
2.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/app"
)
func userCreatePostPermissionCheckWithContext(c *Context, channelId string) {
hasPermission := false
if ok, _ := c.App.SessionHasPermissionToChannel(c.AppContext, *c.AppContext.Session(), channelId, model.PermissionCreatePost); ok {
hasPermission = true
} else if channel, err := c.App.GetChannel(c.AppContext, channelId); err == nil {
// Temporary permission check method until advanced permissions, please do not copy
if channel.Type == model.ChannelTypeOpen && c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), channel.TeamId, model.PermissionCreatePostPublic) {
hasPermission = true
}
}
if !hasPermission {
c.SetPermissionError(model.PermissionCreatePost)
return
}
}
func postHardenedModeCheckWithContext(where string, c *Context, props model.StringInterface) {
isIntegration := c.AppContext.Session().IsIntegration()
if appErr := app.PostHardenedModeCheckWithApp(c.App, isIntegration, props); appErr != nil {
appErr.Where = where
c.Err = appErr
}
}
func postPriorityCheckWithContext(where string, c *Context, priority *model.PostPriority, rootId string) {
appErr := app.PostPriorityCheckWithApp(where, c.App, c.AppContext.Session().UserId, priority, rootId)
if appErr != nil {
appErr.Where = where
c.Err = appErr
}
}
func postBurnOnReadCheckWithContext(where string, c *Context, post *model.Post, channel *model.Channel) {
appErr := app.PostBurnOnReadCheckWithApp(where, c.App, c.AppContext, post.UserId, post.ChannelId, post.Type, channel)
if appErr != nil {
appErr.Where = where
c.Err = appErr
}
}
// checkUploadFilePermissionForNewFiles checks upload_file permission only when
// adding new files to a post, preventing permission bypass via cross-channel file attachments.
func checkUploadFilePermissionForNewFiles(c *Context, newFileIds []string, originalPost *model.Post) {
if len(newFileIds) == 0 {
return
}
originalFileIDsMap := make(map[string]bool, len(originalPost.FileIds))
for _, fileID := range originalPost.FileIds {
originalFileIDsMap[fileID] = true
}
hasNewFiles := false
for _, fileID := range newFileIds {
if !originalFileIDsMap[fileID] {
hasNewFiles = true
break
}
}
if hasNewFiles {
if ok, _ := c.App.SessionHasPermissionToChannel(c.AppContext, *c.AppContext.Session(), originalPost.ChannelId, model.PermissionUploadFile); !ok {
c.SetPermissionError(model.PermissionUploadFile)
return
}
}
}