Skip to content

fix(contexts): validate the nodes of an application before saving - #3266

Merged
blizzz merged 10 commits into
nextcloud:mainfrom
foolfitz:fix/3262-context-nodes-validation
Nov 20, 2026
Merged

blizzz merged 10 commits into
nextcloud:mainfrom
foolfitz:fix/3262-context-nodes-validation

Conversation

@foolfitz

@foolfitz foolfitz commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3262

Problem

PUT /api/2/contexts/{contextId} updates an application. It does almost no check on nodes before it gives them to the service. With the three inputs below, the request ends with a 500. For the first and the third input, the 500 comes after earlier changes of the same request are already saved.

  • A new node has no permissions
  • A node that is already in the application has no permissions
  • nodes is an object, not a list

Also, openapi.json describes nodes of this endpoint wrong. It says nodes is one object and all four fields are required. But the endpoint needs a list, and permissions and order are optional. The reason is a wrong type in the docblock of update(). openapi.json is generated from this docblock.

Fix

The controller now parses nodes into value objects (ContextNodeInputSet / ContextNodeInput). It calls the service only when this works:

  • If nodes is not a list, an item is not an object, or id / type / permissions / order is not an integer, it returns 400 with a message. This happens before anything is saved.
  • permissions must include read and may only combine read (1), create (2), update (4), delete (8) and manage (16). Other values return 400 with a message that says this rule.
  • If permissions is missing, a new node gets read. This is the same as when you create an application. A node that is already in the application keeps its permissions.
  • I fixed the @param of update() and generated openapi.json and the TypeScript types again.

"Integer" also includes strings with only digits. Clients that send form data send strings, and the integration tests of this repo do this too. For id and order, the range is 0 to 2147483647. This is what the database columns can store.

Other notes

  1. If a node that is already in the application has no permissions, it keeps its permissions. In the first version it became read, as the issue expects. A reviewer pointed out that this could lower existing permissions, so I changed it in 6eefe89. Before this PR, this request always returned 500. One integration test scenario checks this behavior.

  2. Creating an application now uses the same check. These two endpoints:

  • update (PUT /api/2/contexts/{contextId}, ContextController::update())
  • create (POST /api/2/contexts, ContextController::create())

used the same check function sanitizeInputNodes() before. Also, the documentation of create already says 400, but it returned 500 when a node had no type. The cost is that create is now stricter too. These inputs worked before and now return 400:

  • nodes is an object, or its keys are not in order
  • id or type is a float (1.0), or a string like "5.7", " 5" or "+5"
  • permissions is "", true or an array
  • order is negative
  • any value is bigger than 2147483647

What the frontend sends is not affected. This change is a separate commit (the 3rd one). If you think it is out of the scope of this issue, it can be removed.

  1. permissions without read now return 400. In an application, read is the base permission of a node: the "Read resource" checkbox is locked, and the create and edit dialogs always send read. Before, the API accepted any value, so values like 0 or 2 worked and now return 400, for create and update. Nodes that already have such values keep them. A client that sends such a value back gets the 400, and the message says the rule.

  2. ContextService has one small change. To keep the permissions of a kept node (note 1), update() only changes them when permissions is given. The rest of the fix stays in the controller, to avoid conflicts with following PRs and issues:

Known limitations

  • After json_decode of the request body, {} and an object with keys from 0 in order ({"0": …, "1": …}) look the same as a list. So they are still accepted. If nodes is an object and its keys are not integers from 0 in order (for example {"a": …} or {"5": …, "2": …}), it is rejected.
  • If the input is valid but the request fails in the middle because of missing permissions, the earlier changes are already saved. This belongs to issue Application update can report changes it did not save #3264.
  • The scheme import (ContextService::importScheme()) and the account import (ContextService::importContext()) write node permissions without this check.

Before and after

Nextcloud 33.0.8, PHP 8.4.27, sqlite3. Before: main at 3147c39. After: this branch at 8d9dcc0. t1 and t2 are two tables. Each update starts from an application that holds only t1, with read+create+update. "Application afterwards" says how the request changed it.

Request Before: response Before: application afterwards After: response After: application afterwards
Change t1 to read and add the new table t2 without permissions 500 t1 changed to read 200 t1 changed to read, t2 added (read)
Send t1 without permissions 500 unchanged 200 unchanged
Change t1 to permissions 0 200 t1 changed to none 400 "Node permissions must include read (1) and may only combine 1, 2, 4, 8 and 16" unchanged
nodes as an object ({"a": …}) that changes t1 to read 500 t1 changed to read 400 "Nodes must be given as a list" unchanged
t1 with type 7 403 t1 removed 400 "Unexpected node type" unchanged
nodes is [1, 2] 400, empty body unchanged 400 "Each node must be an object" unchanged
t1 read+create+update and t2 read, as the web UI sends it 200 t2 added (read) 200 t2 added (read)
Create: a node without type 500 nothing created 400 "Unexpected node type" nothing created
Create: nodes as an object 200 application created 400 "Nodes must be given as a list" nothing created

🏁 Checklist

  • ⛑️ Tests (unit and/or integration) are included or not needed
  • 🔙 Backport requests are created or not needed: /backport to stableX.X
  • 📅 Milestone is set
  • 🌸 PR title is meaningful (if it should be in the changelog: is it meaningful to users?)

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

foolfitz and others added 4 commits November 13, 2026 11:39
An application receives its tables and views as a list of nodes. Add
ContextNodeInputSet and ContextNodeInput to turn that request parameter
into a normalised list. It has to be a list of objects whose id, type,
permissions and order are integers, or strings of digits as form encoded
requests send them, within the range of the 32-bit columns they are
stored in. Missing permissions default to read. Anything else is
rejected with an InvalidArgumentException instead of being cast.

Nothing uses the value objects yet.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-fable-5-2
Assisted-by: Codex:gpt-6.1-sol
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
PUT /api/2/contexts/{contextId} passed its nodes parameter to the
service nearly unchecked. A new node without permissions, a node that
is kept without permissions, or an object in place of the list made the
request fail with a 500, in part after earlier changes of the same
request were written.

Parse the nodes into a ContextNodeInputSet before the service is
called. Missing permissions now default to read, as they do when an
application is created, and that includes the nodes that are kept.
Nodes that are not a list, or that carry values other than integers,
are answered with a 400 before anything is written.

A JSON object whose keys count up from zero cannot be told apart from
a list once the request body is decoded, so it is still accepted.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-fable-5-2
Assisted-by: Codex:gpt-6.1-sol
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
POST /api/2/contexts shared its node handling with the update endpoint.
A node without an id or type was answered with a 500, although the
endpoint documents a 400 for invalid parameters, and other values were
cast to integers without a check.

Use the same ContextNodeInputSet as the update endpoint and drop
sanitizeInputNodes(), which has no caller left.

Creating an application is therefore stricter as well. Nodes given as
an object rather than a list, floats, booleans or arrays in place of an
integer, negative values and values beyond the 32-bit range were
accepted so far and are now answered with a 400.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
The nodes parameter of PUT /api/2/contexts/{contextId} was documented
as a single object with four required properties. The endpoint takes a
list of nodes, in which permissions and order are optional.

Regenerate openapi.json and the TypeScript types.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
@foolfitz
foolfitz marked this pull request as ready for review November 13, 2026 04:01

@blizzz blizzz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! One blocker, and two suggestions.

nodeType: $nodeType,
permissions: isset($inputNode['permissions'])
? self::toInteger($inputNode['permissions'], 'Unexpected node permissions')
: Application::PERMISSION_READ,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can potentially lower existing permissions when no new value is provided, and it is optional.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6eefe89. When an application is updated, an existing node without permissions now keeps its permissions, and a newly added node still defaults to read. Creating an application behaves as before. The Behat scenario and the unit tests were updated accordingly.

nodeId: self::toInteger($inputNode['id'] ?? null, 'Unexpected node id'),
nodeType: $nodeType,
permissions: isset($inputNode['permissions'])
? self::toInteger($inputNode['permissions'], 'Unexpected node permissions')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker, but could be an addition: check that the input value is the expected permission range.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I added this check in 152b94f. permissions must include read and may only use the five bits read, create, update, delete and manage. Other values are rejected with a 400 and the message Unexpected node permissions.

This is a bit stricter than a plain range check, because read is a required permission for every node in an application: when setting the resource permissions, the "Read resource" checkbox is locked, and the create and edit dialogs always include read.

Direct shares, however, allow create-only. If you would like applications to support that as well, I can change the rule to "update and delete require read".

I did not exclude manage, because PermissionsService reads that bit. If you prefer a different rule, for example allowing create-only like direct shares, excluding manage, or only checking 0–31, I can adjust it.

Two things to note:

  • Values without read that the API accepted before, such as 0 or 2, are now rejected.
  • The check only covers the create and update API. The scheme import and the user migration still write the permissions directly and are not affected by this check.

}
$nodes[] = ContextNodeInput::fromArray($inputNode);
}
return new self($nodes);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also optional but useful addition: check that no node is duplicated.

cc @rubenvdlinde to double check that this would not cross any plans.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just spotted, this would treat #3255. Can be a follow up as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'd prefer to leave this for a follow-up, so that this PR only handles #3262.

If rubenvdlinde sees no conflict with upcoming plans and nobody has picked up #3255 yet, I'll open a separate PR for it, along the lines suggested in #3255.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ping, @blizzz, and thanks @foolfitz for picking this up. It doesn't cross anything we have planned, so please go ahead with #3255.

Two things worth knowing:

  1. The grid pages stack also changes ContextService::update(): feat(contexts): keep widgets in step with pages and applications #3132 runs it in one transaction and checks every added node before it removes anything. The changes in this PR are small and touch other lines, so whichever lands second can rebase easily. If fix(contexts): validate the nodes of an application before saving #3266 goes first, I'll rebase our stack on it.
  2. For An application can hold the same table or view twice #3255, checking for a repeated node in ContextNodeInputSet fits well, and that part doesn't depend on our stack. For the lock you don't need to add one: feat(contexts): keep widgets in step with pages and applications #3132 now locks the application row at the start of every update (ContextMapper::lock()), so two overlapping saves already run one after the other. The unique index on (context, node type, node id) would still be good to have once feat(contexts): keep widgets in step with pages and applications #3132 is in.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rubenvdlinde Thanks for confirming. I saw how #3132 handles the lock, so I won't add one in the PR for #3255.

@blizzz blizzz added bug Something isn't working 2. developing Work in progress labels Nov 13, 2026
foolfitz and others added 3 commits November 14, 2026 00:29
An application update set the permissions of a node it kept to read
when the request did not state them, although they are optional. A
client that only reorders the nodes would thereby remove the write
permissions of every node.

Leave missing permissions out of ContextNodeInput and let the service
decide: a node that is kept keeps its permissions, a node that is added
gets read. Creating an application is unchanged, as the service already
defaults missing permissions to read there.

The scheme import calls the same update, so a node without permissions
in an imported scheme follows these rules as well. It made the import
fail before.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
The permissions of a node were accepted as any integer within the range
of the 32-bit column.

Read is the base permission of a node in an application. The dialogs
always grant it and only let create, update and delete be added on top,
and without read the people the application is shared with cannot see
the data of a node it lists. Bits beyond manage are not read anywhere.

Accept only a bitmask of read, create, update, delete and manage that
includes read, and answer other values with a 400. Manage stays
allowed, as the permission checks honour it for applications.

The scheme import and the user migration import do not pass through
ContextNodeInput and are not covered by this check.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-fable-5-2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
@foolfitz
foolfitz requested a review from blizzz November 15, 2026 09:11

@AndyScherzinger AndyScherzinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @foolfitz, this fixes #3262. I checked it on a dev instance (MariaDB) against the steps from the issue, first on main and then with this PR:

  • A new node without permissions: 500 on main; now 200, and the node is stored with read.
  • nodes as an object: 500 on main; now 400 "Nodes must be given as a list", and the name sent in the same request is not saved.
  • A kept node without permissions: 500 on main; now 200, and the node keeps its permissions (15 stayed 15). This is the blocker from @blizzz, fixed in 6eefe89.
  • Permission values: 0, 2, 14, 32, -1, 1.0 and true get 400; 31 and the string "5" are accepted. Creating an application with permissions 0 gets 400, and without permissions the node gets read.
  • A node listed twice is still stored twice. That is #3255, which you are taking as a follow-up.

The new unit tests pass in the container (ContextNodeInputSetTest, ContextControllerNodeInputTest, ContextServiceTest), and CI is green. The PR also merges with the top of both Tables 2.0 stacks (#3217 and #3201). The only conflict is in tests/unit/Service/ContextServiceTest.php, and the library code merges cleanly, so Ruben's note on rebasing holds.

Two points below. Neither is a blocker for #3262. @blizzz, could you re-review? Your blocker is fixed.

Validated: reproduced on a deployed instance (MariaDB, main at 3147c391 and then this PR at 4c9b335d, PUT/POST /api/2/contexts and POST /api/2/contexts/{id}/scheme/import as alice).

$nodesBeingKept[$key] = $node;
if ($node['permissions'] !== $currentNodes[$oldNodeResolvableIdMapper[$key]]['permissions']) {
// a node that is kept without permissions keeps the ones it has
if (isset($node['permissions']) && $node['permissions'] !== $currentNodes[$oldNodeResolvableIdMapper[$key]]['permissions']) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update() has a second caller that skips the new validation: importScheme() (line 874) builds its node list from the scheme and passes permissions through unchecked. A scheme import therefore still stores 0 or 64. A scheme node without permissions now goes through the new isset() branch, so the node keeps its old permissions, but it still logs "Undefined array key "permissions"" at line 874. Passing the resolved nodes through ContextNodeInputSet::createFromInputArray() in importScheme() (and answering 400 from ContextController::importScheme()) would put both entry points under the same rules. This could also be a follow-up.

Validated: reproduced on a deployed instance (MariaDB, main at 3147c391 and then this PR at 4c9b335d, PUT/POST /api/2/contexts and POST /api/2/contexts/{id}/scheme/import as alice: scheme import with permissions 0 stored 0, with 64 stored 64, and without permissions kept 64 and logged the warning).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for verifying this, and for catching the warning. I'd like to keep this PR focused on #3262, so I agree the scheme import fits better in a follow-up.

$permissions = self::toInteger($value, 'Unexpected node permissions');
if (($permissions & ~Application::PERMISSION_ALL) !== 0
|| ($permissions & Application::PERMISSION_READ) === 0) {
throw new InvalidArgumentException('Unexpected node permissions');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR any value was accepted, and importContext() (account import) still stores $nodeArr['permissions'] ?? 0, so some existing applications have nodes without read. For those, a client that sends back the permissions it read gets a 400 with "Unexpected node permissions", which doesn't say what is expected. Sending no permissions, or only a new name, still works and keeps the stored value. The UI always sets read, so it isn't affected. A message that names the rule (for example "Node permissions must include read (1) and may only combine 1, 2, 4, 8 and 16") would make the 400 clear.

Validated: reproduced on a deployed instance (MariaDB, main at 3147c391 and then this PR at 4c9b335d, PUT/POST /api/2/contexts and POST /api/2/contexts/{id}/scheme/import as alice: an application created on main with node permissions 0 and 15; on this PR a PUT sending 0 and 15 got the 400, while PUT with only a name or with nodes without permissions answered 200 and kept 0 and 15).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I changed the message as you suggested in 8d9dcc0. It now answers Node permissions must include read (1) and may only combine 1, 2, 4, 8 and 16. Values of the wrong type, such as true or "abc", get the same message.

Invalid node permissions were answered with "Unexpected node
permissions", which does not say what is expected.

Nodes stored before the rule may lack read, so a client that sends
back the permissions it read gets this 400. Name the rule in the
message instead: the permissions must include read (1) and may only
combine 1, 2, 4, 8 and 16.

Refs: nextcloud#3262
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>

@blizzz blizzz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase/resolve the conflicts

@blizzz blizzz added 4. to release Ready to be released and/or waiting for tests to finish and removed 2. developing Work in progress labels Nov 19, 2026
foolfitz and others added 2 commits November 20, 2026 10:42
Resolve the conflict with nextcloud#3295 on the @PARAM of ContextController::update()
by keeping this branch's version, which also lists the nodes and makes
permissions optional. openapi.json and the TypeScript types are generated
again from the merged code.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jiajun Xu <foolfitz@gmail.com>
@blizzz
blizzz merged commit 473b3ce into nextcloud:main Nov 20, 2026
78 checks passed
@foolfitz
foolfitz deleted the fix/3262-context-nodes-validation branch November 20, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4. to release Ready to be released and/or waiting for tests to finish bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Application update answers 500 for valid-looking node input

4 participants