Repository navigation
fix(api-scheduler): gate scheduler access on each app's own permissions #5907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58d6500
fix(api-scheduler): publish and unpublish on non-root tenant — permis…
Pavel910 228d6fd
test(api-headless-cms-scheduler): cover scheduler access through CMS …
adrians5j 68f8583
chore(api-scheduler): keep next's doc comments in the scheduler use c…
adrians5j 3f3be19
fix(api-scheduler): only allow full access when no app owns a schedul…
adrians5j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
packages/api-headless-cms-scheduler/__tests__/schedulerPermissions.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { useHandler } from "./__mocks/context/useHandler.js"; | ||
| import { createMockTargetModelPlugins, MOCK_TARGET_MODEL_ID } from "./__mocks/targetModel.js"; | ||
| import { GetModelUseCase } from "@webiny/api-headless-cms/features/contentModel/GetModel"; | ||
| import { CreateEntryUseCase } from "@webiny/api-headless-cms/features/contentEntry/CreateEntry"; | ||
| import { | ||
| GetScheduledActionUseCase, | ||
| ListScheduledActionsUseCase | ||
| } from "@webiny/api-scheduler/exports/api/scheduler.js"; | ||
| import { IdentityContext } from "@webiny/api-core/exports/api/security.js"; | ||
| import { CMS_NAMESPACE } from "~/utils/namespace.js"; | ||
| import { SchedulePublishEntryUseCase } from "~/exports/api/cms/scheduler.js"; | ||
|
|
||
| /* | ||
| * There is no `scheduler.*` permission to grant in the admin, so a user with CMS permissions but | ||
| * no full access (the usual role on a non-root tenant) has to be able to see what they scheduled. | ||
| * Scheduler access follows the permissions of the app that owns the scheduled action. | ||
| */ | ||
| const setup = async (permissions: { name: string; [key: string]: unknown }[]) => { | ||
| const context = await useHandler({ | ||
| permissions, | ||
| legacyPlugins: [createMockTargetModelPlugins()] | ||
| }).handler(); | ||
| const { container } = context; | ||
|
|
||
| const model = (await container.resolve(GetModelUseCase).execute(MOCK_TARGET_MODEL_ID)).value; | ||
| const entry = await container.resolve(CreateEntryUseCase).execute(model, { | ||
| values: { title: "Scheduled entry" } | ||
| }); | ||
| const scheduled = await container.resolve(SchedulePublishEntryUseCase).execute({ | ||
| id: entry.value.id, | ||
| model, | ||
| tenant: "root", | ||
| scheduleFor: new Date(Date.now() + 100000) | ||
| }); | ||
|
|
||
| return { container, scheduled }; | ||
| }; | ||
|
|
||
| describe("Scheduler permissions", () => { | ||
| it("lets a user with CMS entry permissions see the actions they scheduled", async () => { | ||
| const { container, scheduled } = await setup([{ name: "cms.*" }]); | ||
| expect(scheduled.isOk()).toBe(true); | ||
|
|
||
| const list = await container.resolve(ListScheduledActionsUseCase).execute({ | ||
| where: { namespace_startsWith: CMS_NAMESPACE } | ||
| }); | ||
| expect(list.isOk()).toBe(true); | ||
| expect(list.value.items).toHaveLength(1); | ||
|
|
||
| const action = await container.resolve(GetScheduledActionUseCase).execute({ | ||
| id: scheduled.value.scheduledAction.id, | ||
| namespace: scheduled.value.scheduledAction.namespace | ||
| }); | ||
| expect(action.isOk()).toBe(true); | ||
| }); | ||
|
|
||
| it("keeps a user without CMS entry permissions out of CMS scheduled actions", async () => { | ||
| const { scheduled } = await setup([{ name: "cms.*" }]); | ||
| expect(scheduled.isOk()).toBe(true); | ||
|
|
||
| const { container } = await useHandler({ | ||
| permissions: [{ name: "fm.*" }], | ||
| legacyPlugins: [createMockTargetModelPlugins()] | ||
| }).handler(); | ||
|
|
||
| const action = await container.resolve(GetScheduledActionUseCase).execute({ | ||
| id: scheduled.value.scheduledAction.id, | ||
| namespace: scheduled.value.scheduledAction.namespace | ||
| }); | ||
| expect(action.isFail()).toBe(true); | ||
| expect(action.error.code).toBe("Scheduler/NotAuthorized"); | ||
| }); | ||
| }); | ||
|
|
||
| /* | ||
| * When no app claims an action's namespace, or a caller lists without one, there are no app | ||
| * permissions to check against, so only full-access identities and code running without | ||
| * authorization get through. | ||
| */ | ||
| describe("Scheduler permissions without an owning app", () => { | ||
| const contextWith = (permissions: { name: string }[]) => | ||
| useHandler({ | ||
| permissions, | ||
| legacyPlugins: [createMockTargetModelPlugins()] | ||
| }).handler(); | ||
|
|
||
| it("doesn't let a CMS user list without a namespace", async () => { | ||
| const { container } = await setup([{ name: "cms.*" }]); | ||
|
|
||
| const result = await container.resolve(ListScheduledActionsUseCase).execute({ where: {} }); | ||
|
|
||
| expect(result.isFail()).toBe(true); | ||
| expect(result.error.code).toBe("Scheduler/NotAuthorized"); | ||
| }); | ||
|
|
||
| it("doesn't let a CMS user list a namespace no app claims", async () => { | ||
| const { container } = await contextWith([{ name: "cms.*" }]); | ||
|
|
||
| const result = await container.resolve(ListScheduledActionsUseCase).execute({ | ||
| where: { namespace: "Unknown/App" } | ||
| }); | ||
|
|
||
| expect(result.isFail()).toBe(true); | ||
| expect(result.error.code).toBe("Scheduler/NotAuthorized"); | ||
| }); | ||
|
|
||
| it("lets a full-access user list without a namespace", async () => { | ||
| const { container } = await setup([{ name: "*" }]); | ||
|
|
||
| const result = await container.resolve(ListScheduledActionsUseCase).execute({ where: {} }); | ||
|
|
||
| expect(result.isOk()).toBe(true); | ||
| expect(result.value.items).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("lets code running without authorization list without a namespace", async () => { | ||
| const { container } = await setup([{ name: "cms.*" }]); | ||
|
|
||
| const result = await container | ||
| .resolve(IdentityContext) | ||
| .withoutAuthorization(() => | ||
| container.resolve(ListScheduledActionsUseCase).execute({ where: {} }) | ||
| ); | ||
|
|
||
| expect(result.isOk()).toBe(true); | ||
| expect(result.value.items).toHaveLength(1); | ||
| }); | ||
| }); |
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
35 changes: 35 additions & 0 deletions
35
packages/api-headless-cms-scheduler/src/features/permissions/CmsSchedulerPermissions.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { SchedulerPermissions } from "@webiny/api-scheduler/features/permissions/abstractions.js"; | ||
| import { IdentityContext } from "@webiny/api-core/exports/api/security.js"; | ||
| import { CMS_NAMESPACE } from "~/utils/namespace.js"; | ||
|
|
||
| class CmsSchedulerPermissionsImpl implements SchedulerPermissions.Interface { | ||
| constructor(private identityContext: IdentityContext.Interface) {} | ||
|
|
||
| canHandle(namespace: string): boolean { | ||
| return namespace.startsWith(CMS_NAMESPACE); | ||
| } | ||
|
|
||
| async canRead(): Promise<boolean> { | ||
| if (await this.identityContext.hasFullAccess()) { | ||
| return true; | ||
| } | ||
| const permissions = await this.identityContext.getPermissions("cms.contentEntry"); | ||
| return permissions.length > 0; | ||
| } | ||
|
|
||
| async onlyOwnRecords(): Promise<boolean> { | ||
| if (await this.identityContext.hasFullAccess()) { | ||
| return false; | ||
| } | ||
| const permissions = await this.identityContext.getPermissions("cms.contentEntry"); | ||
| if (!permissions.length) { | ||
| return false; | ||
| } | ||
| return !permissions.some((p: any) => !p.own); | ||
| } | ||
| } | ||
|
|
||
| export const CmsSchedulerPermissions = SchedulerPermissions.createImplementation({ | ||
| implementation: CmsSchedulerPermissionsImpl, | ||
| dependencies: [IdentityContext] | ||
| }); |
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
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Return a typed error when an error update fails. Spreading
updateResult.errorcreates a plain object. It discards the error prototype and inherited fields, so callers can no longer rely on the returned error’s type or a prototype-definedcode.packages/api-scheduler/src/features/ExecuteScheduledAction/ExecuteScheduledActionUseCase.ts#L106-L109: wrap the handler-not-found branch’s update failure in a scheduler persistence error and retain the original failure.packages/api-scheduler/src/features/ExecuteScheduledAction/ExecuteScheduledActionUseCase.ts#L149-L152: use the same error handling for the execution-failure branch.📍 Affects 1 file
packages/api-scheduler/src/features/ExecuteScheduledAction/ExecuteScheduledActionUseCase.ts#L106-L109(this comment)packages/api-scheduler/src/features/ExecuteScheduledAction/ExecuteScheduledActionUseCase.ts#L149-L152🤖 Prompt for AI Agents