Repository navigation
fix(contexts): validate the nodes of an application before saving - #3266
Conversation
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>
blizzz
left a comment
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
This can potentially lower existing permissions when no new value is provided, and it is optional.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
not a blocker, but could be an addition: check that the input value is the expected permission range.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
also optional but useful addition: check that no node is duplicated.
cc @rubenvdlinde to double check that this would not cross any plans.
There was a problem hiding this comment.
Just spotted, this would treat #3255. Can be a follow up as well.
There was a problem hiding this comment.
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:
- 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. - For An application can hold the same table or view twice #3255, checking for a repeated node in
ContextNodeInputSetfits 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.
There was a problem hiding this comment.
@rubenvdlinde Thanks for confirming. I saw how #3132 handles the lock, so I won't add one in the PR for #3255.
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>
AndyScherzinger
left a comment
There was a problem hiding this comment.
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. nodesas 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.0andtrueget 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']) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Please rebase/resolve the conflicts
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>
Fixes #3262
Problem
PUT /api/2/contexts/{contextId}updates an application. It does almost no check onnodesbefore 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.permissionspermissionsnodesis an object, not a listAlso,
openapi.jsondescribesnodesof this endpoint wrong. It saysnodesis one object and all four fields are required. But the endpoint needs a list, andpermissionsandorderare optional. The reason is a wrong type in the docblock ofupdate().openapi.jsonis generated from this docblock.Fix
The controller now parses
nodesinto value objects (ContextNodeInputSet/ContextNodeInput). It calls the service only when this works:nodesis not a list, an item is not an object, orid/type/permissions/orderis not an integer, it returns 400 with a message. This happens before anything is saved.permissionsmust 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.permissionsis 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.@paramofupdate()and generatedopenapi.jsonand 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
idandorder, the range is 0 to 2147483647. This is what the database columns can store.Other notes
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.Creating an application now uses the same check. These two endpoints:
PUT /api/2/contexts/{contextId},ContextController::update())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 notype. The cost is that create is now stricter too. These inputs worked before and now return 400:nodesis an object, or its keys are not in orderidortypeis a float (1.0), or a string like"5.7"," 5"or"+5"permissionsis"",trueor an arrayorderis negativeWhat 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.
permissionswithout 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.ContextServicehas one small change. To keep the permissions of a kept node (note 1),update()only changes them whenpermissionsis given. The rest of the fix stays in the controller, to avoid conflicts with following PRs and issues:Known limitations
json_decodeof 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. Ifnodesis an object and its keys are not integers from 0 in order (for example{"a": …}or{"5": …, "2": …}), it is rejected.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.
permissionspermissionspermissions0nodesas an object ({"a": …}) that changes t1 to readtype7nodesis[1, 2]typenodesas an object🏁 Checklist
/backport to stableX.X🤖 AI (if applicable)