refactor: casl factory - datasets#2760
Open
HayenNico wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In several places you now rely on
ability.can(Action.DatasetRead, DatasetClass)(or other dataset actions) with the class as subject, but the newdatasetAccessrules are all condition-based; CASL ignores conditions when checking against a type, so these checks effectively become unconditional and may grant broader access than intended—consider switching those checks to use an instance or reintroducing explicit owner/access/published predicates where you need them. - The new unauthenticated behaviour in
DatasetsAccessService.addDatasetAccess/addDatasetAccessToPipelineappears to no longer enforceisPublished: truefor lookups (becausecanViewnow becomes true due to the genericDatasetReadrule on the class), which could expose non‑public datasets via lookups; it would be safer to explicitly add theisPublishedfilter for unauthenticated users instead of relying on the generic ability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In several places you now rely on `ability.can(Action.DatasetRead, DatasetClass)` (or other dataset actions) with the class as subject, but the new `datasetAccess` rules are all condition-based; CASL ignores conditions when checking against a type, so these checks effectively become unconditional and may grant broader access than intended—consider switching those checks to use an instance or reintroducing explicit owner/access/published predicates where you need them.
- The new unauthenticated behaviour in `DatasetsAccessService.addDatasetAccess` / `addDatasetAccessToPipeline` appears to no longer enforce `isPublished: true` for lookups (because `canView` now becomes true due to the generic `DatasetRead` rule on the class), which could expose non‑public datasets via lookups; it would be safer to explicitly add the `isPublished` filter for unauthenticated users instead of relying on the generic ability.
## Individual Comments
### Comment 1
<location path="src/datasets/datasets-access.service.ts" line_range="121-126" />
<code_context>
- Action.DatasetReadManyAccess,
- DatasetClass,
- );
+ const ability = this.caslAbilityFactory.datasetAccess(currentUser);
+ const canViewAny = ability.can(Action.AccessAny, DatasetClass);
+ const canView = ability.can(Action.DatasetRead, DatasetClass);
if (!canViewAny) {
- if (canViewAccess) {
+ if (canView) {
fieldValue.$lookup.pipeline?.unshift({
$match: {
</code_context>
<issue_to_address>
**issue (bug_risk):** Potential runtime error in `addDatasetAccess` when `currentUser` is undefined.
In `addDatasetAccess`, `currentUser` is still dereferenced when it may be `undefined`:
```ts
const currentUser = this.request.user as JWTUser;
const ability = this.caslAbilityFactory.datasetAccess(currentUser);
const canViewAny = ability.can(Action.AccessAny, DatasetClass);
const canView = ability.can(Action.DatasetRead, DatasetClass);
if (!canViewAny) {
if (canView) {
fieldValue.$lookup.pipeline?.unshift({
$match: {
$or: [
{ ownerGroup: { $in: currentUser.currentGroups } },
{ accessGroups: { $in: currentUser.currentGroups } },
{ sharedWith: { $in: [currentUser.email] } },
{ isPublished: true },
],
},
});
}
}
```
With the new `datasetAccess` behavior, unauthenticated users can have `canView === true` for `DatasetRead` on `DatasetClass`, so this branch will run with `currentUser === undefined`, causing a runtime error. Previously, the branch depended on `canViewAccess`, which was `false` for unauthenticated users.
Please gate this branch on the presence of a user (e.g. `if (currentUser && canView)`) or handle the unauthenticated case separately, similar to `getAccessDetailsForLookup`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Subsection of PR #2748 for datasets. PR depends on #2759 to be merged first.
This unifies the datasetEndpointAccess and datasetInstanceAccess functions in CaslAbilityFactory, removes instance-level Action elements and adjusts all affected controllers to accommodate the change.
Fixes
Changes:
Tests included
Documentation
official documentation info
Summary by Sourcery
Consolidate dataset authorization into a single CASL ability and align controllers and access services with the simplified permission model.
Bug Fixes:
Enhancements:
Tests: