Files
upstream-mattermost/server/channels/app/access_control_decision.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

169 lines
6.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"net/http"
"slices"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
)
// renderableActionConfig controls fallback behavior when ABAC is inactive or evaluation fails.
type renderableActionConfig struct {
ResourceType string
DefaultWhenInactive bool
// FailClosedOnError returns denied+evaluated on subject-build or PDP errors
// rather than falling back to DefaultWhenInactive. Set for security-sensitive actions.
FailClosedOnError bool
}
// renderableABACActions is the allowlist of ABAC actions that may be queried
// through the render-decision (Action Search) API. Any action not present here
// is rejected with 400 to prevent arbitrary action probing and to centralize the
// security posture for each renderable affordance.
var renderableABACActions = map[string]renderableActionConfig{
model.AccessControlPolicyActionUploadFileAttachment: {
ResourceType: model.AccessControlPolicyTypeChannel,
DefaultWhenInactive: true,
FailClosedOnError: true,
},
model.AccessControlPolicyActionDownloadFileAttachment: {
ResourceType: model.AccessControlPolicyTypeChannel,
DefaultWhenInactive: true,
FailClosedOnError: true,
},
}
// SearchAllowedActionsForCurrentUser computes non-authoritative, render-time ABAC
// decisions for the current session user on a single resource. It mirrors the
// enforcement path (BuildAccessControlSubjectForSession + AccessEvaluation with
// the same Resource shape) so a render "allowed" can never disagree with what
// enforcement would decide. Results MUST NOT be used to authorize an action; the
// protected endpoints always re-evaluate the PDP live.
//
// When req.Actions is nil/empty the function operates in discovery mode: it
// evaluates all renderable actions registered for the resource type and returns
// the permitted set. When req.Actions is non-empty only those specific actions
// are evaluated (targeted mode).
func (a *App) SearchAllowedActionsForCurrentUser(rctx request.CTX, req model.ActionSearchRequest) (*model.ActionSearchResponse, *model.AppError) {
if appErr := req.IsValid(); appErr != nil {
return nil, appErr
}
// Subject reservation: any Subject whose ID != session user is rejected.
// This gate exists to make Phase 3 (arbitrary-subject evaluation, Enterprise)
// a non-breaking extension — the field is in the contract but gated here.
if req.Subject != nil && req.Subject.ID != rctx.Session().UserId {
return nil, model.NewAppError(
"SearchAllowedActionsForCurrentUser",
"app.access_control_decision.subject_mismatch.app_error",
nil, "", http.StatusForbidden)
}
// Discovery mode: collect registry entries for the resource type, then sort for
// deterministic wire output — Go map iteration is non-deterministic.
// Targeted mode: validate each requested action against the registry.
var candidates []string
if len(req.Actions) == 0 {
for action, cfg := range renderableABACActions {
if cfg.ResourceType == req.Resource.Type {
candidates = append(candidates, action)
}
}
slices.Sort(candidates)
} else {
for _, action := range req.Actions {
cfg, ok := renderableABACActions[action]
if !ok || cfg.ResourceType != req.Resource.Type {
return nil, model.NewAppError("SearchAllowedActionsForCurrentUser", "app.access_control_decision.unsupported_action.app_error", map[string]any{"Action": action}, "", http.StatusBadRequest)
}
}
candidates = req.Actions
}
resp := &model.ActionSearchResponse{
Resource: req.Resource,
Results: []model.ActionSearchResult{}, // always non-nil so it serializes as []
Decisions: make(map[string]model.RenderPermissionDecision, len(candidates)), // always non-nil so it serializes as {}
}
record := func(action string, d model.RenderPermissionDecision) {
resp.Decisions[action] = d
if d.Allowed {
resp.Results = append(resp.Results, model.ActionSearchResult{Action: model.ActionSearchResultAction{Name: action}})
}
}
// No active policy — return per-action defaults.
acs := a.Srv().Channels().AccessControl
if acs == nil || !a.attributeBasedAccessControlEnabled() {
for _, action := range candidates {
record(action, model.RenderPermissionDecision{
Allowed: renderableABACActions[action].DefaultWhenInactive,
Evaluated: true,
})
}
return resp, nil
}
// All currently registered resource types are channel-scoped, so req.Resource.ID
// is always a channel ID here. If a non-channel resource type is ever added to
// renderableABACActions, this call must be updated to pass the correct channel ID.
subject, appErr := a.BuildAccessControlSubjectForSession(rctx, req.Resource.ID)
if appErr != nil {
rctx.Logger().Info("Failed to build ABAC subject for render-decision search",
mlog.String("resource_type", req.Resource.Type),
mlog.String("resource_id", req.Resource.ID),
mlog.Err(appErr),
)
for _, action := range candidates {
record(action, renderDecisionOnError(action))
}
return resp, nil
}
for _, action := range candidates {
decision, evalErr := acs.AccessEvaluation(rctx, model.AccessRequest{
Subject: *subject,
Resource: req.Resource,
Action: action,
})
if evalErr != nil {
rctx.Logger().Debug("ABAC render-decision evaluation failed",
mlog.String("action", action),
mlog.String("resource_id", req.Resource.ID),
mlog.Err(evalErr),
)
record(action, renderDecisionOnError(action))
continue
}
record(action, model.RenderPermissionDecision{
Allowed: decision.Decision,
Evaluated: true,
})
}
return resp, nil
}
// renderDecisionOnError returns the conservative decision for an action whose
// subject build or PDP evaluation failed: fail closed (deny + generic reason)
// for security-sensitive actions, otherwise fall back to the inactive default.
func renderDecisionOnError(action string) model.RenderPermissionDecision {
cfg := renderableABACActions[action]
if cfg.FailClosedOnError {
return model.RenderPermissionDecision{
Allowed: false,
Evaluated: true,
Reason: model.RenderDecisionReasonRestrictedByPolicy,
}
}
return model.RenderPermissionDecision{
Allowed: cfg.DefaultWhenInactive,
Evaluated: true,
}
}