Files
vikunja/pkg/models/notifications_database.go
T
kolaenteandkolaente 0638200ac0 fix(notifications): check project access when reading notifications
Notification rows outlived access. A subscription survives a project being
unshared, so every notification already written for a revoked user stayed
readable — comment bodies, task titles, project names, deletion notices. The
read paths filtered on notifiable_id alone, with no permission check anywhere.
#3325 stopped the sender writing new ones; this is the other half.

The project a notification is about is persisted on the row when it is written
and the read paths filter on it in SQL, so LIMIT, OFFSET and total are all
computed on the filtered set. Notification types declare their project through
a capability interface in pkg/notifications, the same way they already declare
SubjectID, ThreadID and ToTitle — which is what lets the package below
pkg/models stay ignorant of what a project is.

project_id 0 means account-scoped and always visible, a positive value is
checked against the projects the caller can read, and -1 marks a project-scoped
row whose project could not be determined, so it is visible to nobody. Filtering
reuses the existing accessibleProjectIDsSubquery, so the page query and the
count cannot drift apart. A migration backfills existing rows from their stored
payloads, resolving through soft-deleted tasks so task.deleted rows still land
on their project.

Covers every read path: the v1 and v2 list endpoints, mark-as-read (which
echoes the payload back), the Atom feed, and the websocket push — the last of
which is load-bearing, since a row is still written for a revoked subscriber.
Deliberately no instance-admin bypass: notifications are always the caller's
own, and being an admin says nothing about whether they should still read a
comment out of a project they were removed from.
2026-07-29 07:58:17 +00:00

97 lines
4.3 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/notifications"
"code.vikunja.io/api/pkg/web"
"xorm.io/xorm"
)
// DatabaseNotifications is a wrapper around the crud operations that come with a database notification.
type DatabaseNotifications struct {
notifications.DatabaseNotification
// Whether or not to mark this notification as read or unread.
// True is read, false is unread.
Read bool `xorm:"-" json:"read" doc:"Set true to mark the notification read, false to mark it unread."`
web.CRUDable `xorm:"-" json:"-"`
web.Permissions `xorm:"-" json:"-"`
}
// ReadAll returns all database notifications for a user
// @Summary Get all notifications for the current user
// @Description Returns an array with all notifications for the current user. Notifications about a project the current user can no longer read are omitted; the filtering happens in the query, so paging and the `x-pagination-*` headers all describe the visible notifications only.
// @tags subscriptions
// @Accept json
// @Produce json
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
// @Security JWTKeyAuth
// @Success 200 {array} notifications.DatabaseNotification "The notifications"
// @Failure 403 {object} web.HTTPError "Link shares cannot have notifications."
// @Failure 500 {object} models.Message "Internal error"
// @Router /notifications [get]
func (d *DatabaseNotifications) ReadAll(s *xorm.Session, a web.Auth, _ string, page int, perPage int) (ls interface{}, resultCount int, numberOfEntries int64, err error) {
if _, is := a.(*LinkSharing); is {
return nil, 0, 0, ErrGenericForbidden{}
}
limit, start := getLimitFromPageIndex(page, perPage)
ns, resultCount, total, err := notifications.GetNotificationsForUser(s, a.GetID(), NotificationProjectFilter(a), limit, start)
if err != nil {
return nil, 0, 0, err
}
refreshNotificationsUsers(s, ns)
return ns, resultCount, total, nil
}
// CanUpdate checks if a user can mark a notification as read.
func (d *DatabaseNotifications) CanUpdate(s *xorm.Session, a web.Auth) (bool, error) {
if _, is := a.(*LinkSharing); is {
return false, nil
}
can, err := notifications.CanMarkNotificationAsRead(s, &d.DatabaseNotification, a.GetID())
if err != nil || !can {
return can, err
}
// Marking a notification read echoes its payload back, so it needs the read check too.
return CanReadNotification(s, a, &d.DatabaseNotification)
}
// Update marks a notification as read.
// @Summary Mark a notification as (un-)read
// @Description Marks a notification as either read or unread. A user can only mark their own notifications as read.
// @tags subscriptions
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Notification ID"
// @Success 200 {object} models.DatabaseNotifications "The notification to mark as read."
// @Failure 403 {object} web.HTTPError "The user does not have access to that notification."
// @Failure 403 {object} web.HTTPError "Link shares cannot have notifications."
// @Failure 404 {object} web.HTTPError "The notification does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /notifications/{id} [post]
func (d *DatabaseNotifications) Update(s *xorm.Session, _ web.Auth) (err error) {
return notifications.MarkNotificationAsRead(s, &d.DatabaseNotification, d.Read)
}