Check timeline activity diffs against the record they describe

Merged
CH
7d ago
+753-473

Follow-up to #31695 (#30993).

Timeline activity diffs were filtered with object-level flags only. A role that can read some records of an object but not all still got the field changes of the records it can't read. A role that reaches a record through a related-record path, or that can't read the timeline relation, lost values it is allowed to read.

What changes

Timeline activities are checked per record. Each row is checked against the record it describes:

  • a linked activity (note, task) against linkedObjectMetadataId + linkedRecordId;
  • a direct activity against its target<Object>Id;
  • the handler reads those columns itself, without permission checks, because the caller may not be allowed to select them, so the query runner no longer adds them to the selection;
  • record ids are batched per object and checked with RecordPermissionsReadService.filterGrantedRecordIds (role grants, row-level rules, access groups, related-record paths, shares, private records);
  • a row whose record is not readable keeps no field in properties; field restrictions still apply on readable records.

The same check runs on API reads and on the event stream (webhooks, subscriptions, triggers). System contexts skip it; any other caller without principals gets every diff stripped.

Read custom permissions move into engine/permissions/record-read-custom-permissions. The per-object read restrictions that run after a query (timeline activity, plus the existing calendar event, message and attachment ones) were registered by each business module into a registry in the API layer. They now live with the permission layer:

  • RecordReadCustomPermissionsService is the registry. At boot it discovers every provider marked @RecordReadCustomPermissionHandler(objectName) through DiscoveryService, the same way MetadataSideEffectHandlerRegistryService does. Handlers no longer inject the registry or register themselves.
  • The protected objects are listed once (RECORD_READ_CUSTOM_PERMISSION_OBJECT_NAME_SINGULARS). A decorated handler for an object missing from the list fails at boot; a protected object whose handler isn't loaded is denied at query time instead of served unrestricted.
  • Its consumers are the common API (CommonRecordReadCustomPermissionsService, which collects nested records from the selection) and RecordEventPermissionsReadService. The engine event utils no longer import timeline code.

Timeline activity read custom permissions, layered by role

Member's roleMainThis PR
Object deniedno valueno value
Row-level rule hides the recordfull diffno value
Field restrictedfield strippedfield stripped
Timeline relation restrictedno valuefull diff
Record shared outside the rulefull difffull diff
Access group (visibility tier) hides the recordfull diffno value
Private record not givenfull diffno value
Record reached through a related-record pathno valuefull diff
Event stream, row-level rule hides the recordfull diffno value

Tests

  • Unit: omit-restricted-fields-from-timeline-activity.util.spec.ts covers unreadable target and linked records, a missing linked record id and an activity with no subject column.
  • Integration: the existing timeline-activity-field-permissions suite stays as on main and passes. One case per permission mechanism in the table above lands in the stacked PR #31746; run against main's code, six of its ten mechanism cases fail.

Cost

When a read returns timeline activity properties, the handler runs one system SELECT for the link columns of the page, resolves the caller's principals (cached), and runs one SELECT id ... WHERE id = ANY(...) per object type present. The link columns used to ride in the caller's own query, but the repository refuses columns the caller cannot read, so a role that couldn't read the timeline relation, or one that reaches a record only through a related-record path, lost the link and every value. Reading them apart is what lets the record check decide.

Not in this PR

Rows on records the member cannot read are still listed with an empty properties. Hiding them is a query-level gate.