-
Notifications
You must be signed in to change notification settings - Fork 5
fix(nextly): apply related-row field rules to a single's write response #381
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
mobeenabdullah
merged 2 commits into
main
from
fix/single-write-response-related-access
Jul 28, 2026
+269
−2
Merged
Changes from 1 commit
Commits
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
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,33 @@ | ||
| --- | ||
| "nextly": patch | ||
| "create-nextly-app": patch | ||
| "@nextlyhq/admin": patch | ||
| "@nextlyhq/admin-css": patch | ||
| "@nextlyhq/blocks-engine": patch | ||
| "@nextlyhq/ui": patch | ||
| "@nextlyhq/adapter-drizzle": patch | ||
| "@nextlyhq/adapter-postgres": patch | ||
| "@nextlyhq/adapter-mysql": patch | ||
| "@nextlyhq/adapter-sqlite": patch | ||
| "@nextlyhq/storage-s3": patch | ||
| "@nextlyhq/storage-uploadthing": patch | ||
| "@nextlyhq/storage-vercel-blob": patch | ||
| "@nextlyhq/plugin-form-builder": patch | ||
| "@nextlyhq/plugin-page-builder": patch | ||
| "@nextlyhq/plugin-seo": patch | ||
| "@nextlyhq/plugin-sdk": patch | ||
| "@nextlyhq/eslint-config": patch | ||
| "@nextlyhq/prettier-config": patch | ||
| "@nextlyhq/telemetry": patch | ||
| "@nextlyhq/tsconfig": patch | ||
| --- | ||
|
|
||
| Updating a Single no longer returns related fields the writer is not allowed to read. | ||
|
|
||
| The response expands relationships, and those rows belong to another collection carrying its own field-level `access.read` rules. Read paths have evaluated them since the field-access work landed; this path forwarded no caller, so it returned every related field intact — including ones the same caller's `GET` would withhold. That made the write path a way around the rule: write anything, read the response back. | ||
|
|
||
| A writer supplied a relationship id, not the related row's protected fields, so "they supplied the data" does not cover them. The rule that applies is the target collection's own, and it is now evaluated against the caller that made the write. | ||
|
|
||
| This reaches every hop the response expands, not only the first: a related row's own relationships carry the rules of the collection at the far end, and those are evaluated too. | ||
|
|
||
| Enforcement is keyed on there being a caller to judge. A trusted write (`overrideAccess`) and an internal write that forwards no user are unaffected — stripping there would hide fields from a writer nothing denied. |
166 changes: 166 additions & 0 deletions
166
...es/nextly/src/domains/singles/__tests__/write-response-related-access.integration.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,166 @@ | ||
| /** | ||
| * A write response is a read of the rows it expands. | ||
| * | ||
| * Updating a Single returns the document, relationships expanded. Those related | ||
| * rows belong to another collection and carry that collection's own field-level | ||
| * `access.read` rules — rules the read paths have evaluated since #335, and | ||
|
mobeenabdullah marked this conversation as resolved.
Outdated
|
||
| * which this path did not, because it forwarded no caller. | ||
| * | ||
| * The writer supplied a relationship id, not the related row's protected | ||
| * fields, so "they just wrote it" does not cover them. Leaving the response | ||
| * unredacted makes the write path a way around the rule: write anything, read | ||
| * the response, see what a GET would refuse. | ||
| */ | ||
|
|
||
| import { afterEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| checkbox, | ||
| defineCollection, | ||
| defineSingle, | ||
| relationship, | ||
| text, | ||
| } from "../../../config"; | ||
| import { | ||
| createTestNextly, | ||
| type TestNextly, | ||
| } from "../../../plugins/test-nextly"; | ||
| import type { CollectionsHandler } from "../../../services/collections-handler"; | ||
| import type { SingleEntryService } from "../services/single-entry-service"; | ||
|
|
||
| let current: TestNextly | undefined; | ||
| afterEach(async () => { | ||
| await current?.destroy(); | ||
| current = undefined; | ||
| }); | ||
|
|
||
| /** A Single pointing at a collection that hides one of its fields. */ | ||
| async function boot(): Promise<{ | ||
| entry: SingleEntryService; | ||
| authorId: string; | ||
| }> { | ||
| current = await createTestNextly({ | ||
| collections: [ | ||
| defineCollection({ | ||
| slug: "authors", | ||
| fields: [ | ||
| text({ name: "name" }), | ||
| checkbox({ name: "suspended", access: { read: () => false } }), | ||
| ], | ||
| }), | ||
| ], | ||
| singles: [ | ||
| defineSingle({ | ||
| slug: "branding", | ||
| fields: [ | ||
| text({ name: "siteName" }), | ||
| relationship({ name: "author", relationTo: "authors" }), | ||
| ], | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| const handler = current.getService<CollectionsHandler>("collectionsHandler"); | ||
| const author = await handler.createEntry( | ||
| { collectionName: "authors", overrideAccess: true }, | ||
| { name: "Ada", suspended: true } | ||
| ); | ||
| return { | ||
| entry: current.getService<SingleEntryService>("singleEntryService"), | ||
| authorId: (author.data as { id: string }).id, | ||
| }; | ||
| } | ||
|
|
||
| describe("single write response — related-row field access (integration)", () => { | ||
| it("withholds a related field the writer may not read", async () => { | ||
| const { entry, authorId } = await boot(); | ||
|
|
||
| const result = await entry.update( | ||
| "branding", | ||
| { siteName: "Acme", author: authorId }, | ||
| { user: { id: "writer-1" }, routeAuthorized: true } | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| const author = (result.data as { author?: Record<string, unknown> }) | ||
| ?.author; | ||
| // The relationship is still expanded — only the protected field is gone. | ||
| expect(author).toMatchObject({ name: "Ada" }); | ||
| expect(author).not.toHaveProperty("suspended"); | ||
| }); | ||
|
|
||
| it("withholds a protected field a hop further out", async () => { | ||
| // Nested expansion reaches the related row's own relationships, so the | ||
| // rules of the collection at the far end apply too. Threading the caller | ||
| // into the first hop is only half the answer if it stops there. | ||
| current = await createTestNextly({ | ||
| collections: [ | ||
| defineCollection({ | ||
| slug: "orgs", | ||
| fields: [ | ||
| text({ name: "name" }), | ||
| checkbox({ name: "blocked", access: { read: () => false } }), | ||
| ], | ||
| }), | ||
| defineCollection({ | ||
| slug: "authors", | ||
| fields: [ | ||
| text({ name: "name" }), | ||
| relationship({ name: "org", relationTo: "orgs" }), | ||
| ], | ||
| }), | ||
| ], | ||
| singles: [ | ||
| defineSingle({ | ||
| slug: "branding", | ||
| fields: [ | ||
| text({ name: "siteName" }), | ||
| relationship({ name: "author", relationTo: "authors" }), | ||
| ], | ||
| }), | ||
| ], | ||
| }); | ||
| const handler = | ||
| current.getService<CollectionsHandler>("collectionsHandler"); | ||
| const org = await handler.createEntry( | ||
| { collectionName: "orgs", overrideAccess: true }, | ||
| { name: "Acme", blocked: true } | ||
| ); | ||
| const author = await handler.createEntry( | ||
| { collectionName: "authors", overrideAccess: true }, | ||
| { name: "Ada", org: (org.data as { id: string }).id } | ||
| ); | ||
| const entry = current.getService<SingleEntryService>("singleEntryService"); | ||
|
|
||
| const result = await entry.update( | ||
| "branding", | ||
| { siteName: "Acme", author: (author.data as { id: string }).id }, | ||
| { user: { id: "writer-1" }, routeAuthorized: true } | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| const nested = ( | ||
| result.data as { author?: { org?: Record<string, unknown> } } | ||
| )?.author?.org; | ||
| expect(nested).toMatchObject({ name: "Acme" }); | ||
| expect(nested).not.toHaveProperty("blocked"); | ||
| }); | ||
|
|
||
| it("leaves a trusted write's response whole", async () => { | ||
| // The mirror, and the reason enforcement is not simply switched on | ||
| // everywhere: a trusted write supplies no caller to judge, and stripping | ||
| // there would hide fields from an internal writer that nothing denied. | ||
| const { entry, authorId } = await boot(); | ||
|
|
||
| const result = await entry.update( | ||
| "branding", | ||
| { siteName: "Acme", author: authorId }, | ||
| { overrideAccess: true } | ||
| ); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| const author = (result.data as { author?: Record<string, unknown> }) | ||
| ?.author; | ||
| expect(author).toMatchObject({ name: "Ada", suspended: true }); | ||
| }); | ||
| }); | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.