Repository navigation
fix(contexts): answer an unknown and an unmanageable uuid alike on scheme import - #3202
rubenvdlinde wants to merge 5 commits into
Conversation
1c7a206 to
1b9a79d
Compare
2471ff4 to
6368c26
Compare
1b9a79d to
1c2f7f3
Compare
6368c26 to
c203b3a
Compare
1c2f7f3 to
cab2a56
Compare
c203b3a to
5cf92cf
Compare
cab2a56 to
11fde57
Compare
AndyScherzinger
left a comment
There was a problem hiding this comment.
Review focused on performance, clean code and architecture. The checks ran through the OCS API on a dev instance (MariaDB) with this PR on top of #3138.
Unknown and unmanageable uuids now get the same status, body and log text, for nodes and for tables to modify. The cost is that a node that doesn't resolve no longer stops the import: it is dropped, and update() then removes what was there (comment on line 1206).
Not commented inline (file not in this PR): the new 400 for a table to modify lands in ImportContextScheme.vue handleResponse()'s generic branch, which reads e.response.data.message. An OCS response doesn't have that field, so the dialog shows "Could not import scheme due to unknown errors." where the base showed "Could not import, missing needed permission." Reading e.response.data.ocs.data.message, as src/shared/utils/displayError.js does, or answering 403 for both cases, keeps a usable text.
Validated: reproduced on a deployed instance (a table shared read-only with alice that also has a public link: the preview answered 200 and listed it under the tables to change, the import answered 400). The dialog text comes from reading the code.
| continue; | ||
| } | ||
| $found = $this->findNodeByUuid($kind, $node['node_uuid']); | ||
| if ($found === null |
There was a problem hiding this comment.
Skipping a node turns an import that used to be refused into a partial one. update() still replaces the whole node list, so when the scheme's nodes don't resolve, the application loses its current tables and views, along with their page contents, widget sources and menu items, and the import answers 200. The base answered 404, or 403 for an unmanageable node, and changed nothing. The dialog sends such nodes whenever a new table is switched off under "New Tables" while "Resources" stays on (ImportContextSchemePreview.vue lines 368-375). Refusing unresolvable nodes with the same 400 as the tables to modify keeps the import all-or-nothing and still answers unknown and unmanageable alike.
Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at 11fde574, scheme imports through the OCS API as alice and bob: an application holding alice's table, then an import whose only node has a random uuid: 200, and the application had no node afterwards).
There was a problem hiding this comment.
You are right, the skip turned a refused import into a partial one. An unknown uuid and one the user cannot manage now refuse the whole import with the same 400, checked before the first write, and nothing is skipped. A node that is already part of the application is still accepted. The dialog now leaves out the nodes of a new table that is switched off, so switching a table off no longer gets the import refused.
cc @blizzz
| if ($found === null | ||
| || (!isset($currentNodes[sprintf('t%di%d', $node['node_type'], $found->getId())]) | ||
| && !$this->permissionsService->canManageNodeById($node['node_type'], $found->getId(), $userId))) { | ||
| $this->logger->warning('Skipping a node of an imported scheme: no ' . $kind . ' with the uuid ' . $node['node_uuid'] . ' is here and can be managed by the user.'); |
There was a problem hiding this comment.
Dropped nodes are only written to the server log. The preview still lists them under nodes.to and keeps the menu items that point at them. The import response has no skipped list, and the dialog says "Import completed successfully." This is the #3138 point about menu skips being only logged, now for nodes. The @param of $nodes (ContextController.php line 360) doesn't say nodes may be skipped, although the one of $menu does. One resolver shared by the preview and the import, returning the skipped nodes with one reason for both cases, fixes this and keeps the two cases alike.
Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at 11fde574, scheme imports through the OCS API as alice and bob: a scheme whose node and menu item name a table alice can't manage; the preview listed the node under nodes.to with menu.skipped empty, and the import answered 200 without the node and without the item).
There was a problem hiding this comment.
Agreed, the preview and the import should say the same thing. Both now go through one resolver, resolveSchemeNodes(). The preview returns nodes.unresolved with the index, uuid, title and reason of each node the import would refuse, and the dialog lists them under Resources. The import refuses those same nodes with one 400, and the @param of $nodes says so.
cc @blizzz
| $currentNodes[sprintf('t%di%d', $node['node_type'], $node['node_id'])] = true; | ||
| } | ||
| $resolvedNodes = []; | ||
| foreach ($nodes as $node) { |
There was a problem hiding this comment.
An unknown node uuid no longer ends the request, so every entry of the uncapped nodes list now costs an instance-wide lookup and a WARNING line holding the raw, unvalidated node_uuid. For a view that lookup is a full scan of tables_views (EXPLAIN shows type=ALL). The base stopped at the first unknown uuid with a 404 and one info line. The per-entry lookup, the missing cap and the missing rate limit were already there before this PR. Several changes together bound it: Uuid::isValid() before the lookup (as importTable() and importView() already do), deduplicated uuids, a cap, one chunked lookup per kind, and one summary log line.
Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at 11fde574, scheme imports through the OCS API as alice and bob: 2000 random uuids answered 200 after 2.1 s, against 0.7 s for one, and wrote 2000 warnings; 50 nodes with 20 KB strings wrote 1 MB of log).
There was a problem hiding this comment.
You are right, the skip made an old cost worse. Uuids are now checked with Uuid::isValid() and deduplicated first, a scheme takes at most 1000 nodes (400 above that), and the lookup is one IN() query per kind through the new findAllByUuids() on both mappers. Since an unusable node now refuses the import, there is no per-node warning any more.
cc @blizzz
| if ($kind === null) { | ||
| continue; | ||
| } | ||
| $found = $this->findNodeByUuid($kind, $node['node_uuid']); |
There was a problem hiding this comment.
A node without node_uuid or with a non-string one, and a table to modify with a non-string uuid, reach findNodeByUuid(string $uuid) under strict_types. That ends in a TypeError: the request answers 500 and the controller's rollBack() never runs. A node_type other than the integers 0 and 1 (for example "0") is dropped without even the warning. The base failed the same way at the mapper call, but this PR's rule is to skip what can't be used, and MenuService already guards with is_string() (line 427).
Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at 11fde574, scheme imports through the OCS API as alice and bob: node_uuid: 123 gave 500; modifyTables [{uuid: 123}] gave 500; node_type: "0" gave 200 with the node gone and nothing logged).
There was a problem hiding this comment.
Thanks for the reproductions. A node without a string node_uuid, with a string that is not a uuid, with a node_type that is not exactly 0 or 1, or that is not an object at all, now gets the same 400 as an unknown uuid. A table to modify with a non-string uuid gets the same 400 as an unknown table. None of them reach the mapper, and each case has a test.
cc @blizzz
| $tableService->validateTableScheme($tableScheme); | ||
| $table = $this->tableMapper->findByUuid($tableScheme['uuid']); | ||
| $table = $this->findNodeByUuid('table', $tableScheme['uuid']); | ||
| if ($table === null || !$this->permissionsService->canManageNodeById(Application::NODE_TYPE_TABLE, $table->getId(), $userId)) { |
There was a problem hiding this comment.
A table to modify is still refused only after every new table, column and view has been inserted, and after every earlier table to modify has been rewritten. A refused import does all that work before it rolls back. Effects outside the transaction remain: a view removed by an earlier entry has already been reported to federated instances by ViewService::delete() (notifyNodeDelete()), while the local rollback restores it. The order is the same as in the base; this PR changes the answer to 400. Checking all tables to modify (and validateTableScheme()) before the first write makes a refusal cost nothing.
Validated: code check only.
There was a problem hiding this comment.
Agreed, a refusal should cost nothing. validateTableScheme() for every table, the find-and-manage check for the tables to modify and the node checks all run before the first write. The write passes reuse the tables found. The tests check that a refused import calls neither importTable() nor updateTableStructure().
cc @blizzz
| // A node whose table or view is not here, or is new to the application and cannot be | ||
| // managed by the user, is skipped the same way, like a menu item. | ||
| $currentNodes = []; | ||
| foreach ($this->contextMapper->findById($contextId, $userId)->getNodes() ?? [] as $node) { |
There was a problem hiding this comment.
This loads the whole application again, with all its pages (the context join plus the pages query), only to collect node keys. update() then loads it once more and repeats the same t%di%d split between kept and added nodes, with the same manage check. ?? [] is dead code, and the new test mocks update(), so nothing checks that the two splits agree. If they don't agree, for example because another request removes a node between the two reads, the import answers 403 again. findById($contextId, $userId, false) returns the same nodes without the pages query. Letting updateInTransaction() skip a node instead of throwing, when the import calls it, removes both the second load and the second copy of the rule.
Validated: reproduced on a deployed instance (MariaDB, S-3138 restored and this PR at 11fde574, scheme imports through the OCS API as alice and bob: the query log shows one extra application query and one extra pages query per import). The race: code check only.
There was a problem hiding this comment.
The import no longer loads the application with pages for its own copy of the rule. It reads it once with findById(..., false) for the check that has to happen before the first write, and the ?? [] is gone. update() keeps its own check for nodes it adds, which is the same rule: a current node is kept, a new one must be manageable. One test now runs the real update() with a current node the user cannot manage, to show the two agree.
cc @blizzz
| $found = $this->findNodeByUuid($kind, $node['node_uuid']); | ||
| if ($found === null | ||
| || (!isset($currentNodes[sprintf('t%di%d', $node['node_type'], $found->getId())]) | ||
| && !$this->permissionsService->canManageNodeById($node['node_type'], $found->getId(), $userId))) { |
There was a problem hiding this comment.
canManageNodeById($type, $found->getId()) reads the table or view by id again right after findNodeByUuid() returned it, because findByUuid() doesn't fill the mapper's id cache. The same applies on line 1183. canManageTable($found) / canManageView($found) take the entity directly and save one primary-key query per checked node.
Validated: code check only.
There was a problem hiding this comment.
Good point. The import now checks the entity it just found with canManageTable() or canManageView(), for nodes and for tables to modify, so there is no second read per node. The test stubs match on the entity id.
cc @blizzz
| * @throws Exception | ||
| */ | ||
| private function nodeRelIdByUuid(Context $context, string $kind, string $uuid): ?int { | ||
| private function findNodeByUuid(string $kind, string $uuid): Table|View|null { |
There was a problem hiding this comment.
The docblock promises the node "or null when it is not here" and declares only @throws Exception. MultipleObjectsReturnedException from ViewMapper::findByUuid() (view uuids are unique per table only) passes through this helper into the node loop and into nodeRelIdByUuid(), which declares nothing, and the tests only stub DoesNotExistException. The resulting 500 is the one reported on #3138 (nodeRelIdByUuid()); the new helper is the place to document or handle it. Mapping the exception to null isn't enough, because update() would then drop current nodes (comment on line 1206). Resolving against the application's nodes, the views this import created and the views the user can manage avoids the instance-wide lookup altogether.
Validated: code check only.
There was a problem hiding this comment.
Agreed, the helper hid the real failure. findNodeByUuid() is gone. Views are looked up by uuid in one query that returns every view with that uuid, and a node resolves to the one that is already a node of the application, comes with a table the import adds, or is the only one the user can manage. Several manageable views with one uuid give a 400 with their own reason instead of a 500. The menu import now maps uuids through the nodes that were resolved, so it no longer looks anything up again. I did not scope the lookup to the tables of the scheme, because an unchanged table is not part of modifyTables and its views would then be refused.
cc @blizzz
| $table->id = $tableId; | ||
| return $table; | ||
| }); | ||
| $this->permissionsService->method('canManageNodeById')->willReturn(false); |
There was a problem hiding this comment.
The new tests can't fail for a check that refuses everything or mixes up node types. The modifyTables test has only refusal cases, with canManageNodeById stubbed to false for any arguments. The node test's only view is refused for either type. Replacing the modifyTables check with an unconditional BadRequestError keeps both data sets green, since no test imports a manageable table to modify and MenuServiceTest passes modifyTables: []. Passing NODE_TYPE_TABLE instead of $node['node_type'] to canManageNodeById also stays green, although in production it would check view N as table N. A case where the user can manage the table or view, with the stub matching on type and id, closes both. Separately, setUp() (line 65 f.) keeps $this->logger = $logger = ... and $this->permissionsService = $permissionsService = ..., but the constructor now gets the properties, so neither local is read.
Validated: code check only.
There was a problem hiding this comment.
Fair, those tests could not tell a correct check from one that refuses everything. There are now positive cases for a table to modify and for nodes the user may manage, with stubs that match on the entity, plus a case where two views share a uuid. The setup no longer keeps the unused locals.
cc @blizzz
5cf92cf to
ee0564c
Compare
11fde57 to
745edfa
Compare
ee0564c to
00c1b92
Compare
745edfa to
b61edc8
Compare
00c1b92 to
6ffaf94
Compare
b61edc8 to
541eefe
Compare
|
Hello there, We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process. Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6 Thank you for contributing to Nextcloud and we hope to hear from you soon! (If you believe you should not receive this message, you can add yourself to the blocklist.) |
6ffaf94 to
f7549c7
Compare
541eefe to
adf7252
Compare
f7549c7 to
b464a42
Compare
adf7252 to
b67868a
Compare
b464a42 to
c0627d7
Compare
b67868a to
4380c60
Compare
|
Thanks for catching the dialog side. For a 400 the dialog now shows the message from the OCS body as it is, so the user sees which node or table was refused instead of "unknown errors". Other errors no longer pass a server message through cc @blizzz |
…heme import Importing a scheme answered 404 for a node whose uuid was unknown and 403 for a node on a table or view the user cannot manage. Such a node is now skipped the same way as an unknown one, with one log line, unless it already belongs to the application, the way the menu import skips the items it cannot use. A table to modify that is not here or cannot be managed is refused with the same 400 in both cases, and the controller now answers a refused import with 400 and rolls it back. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…, before the first write An import whose nodes name a table or view that is not here, cannot be managed by the user and is not yet a node of the application, or whose uuid or node type is malformed, is refused with one 400. A table to modify that is not here or cannot be managed gets the same 400. All of it is checked before the first table, column or view is written, so a refused import changes nothing. The preview and the import resolve nodes with the same rule and the preview lists the nodes the import would refuse. Uuids are validated, deduplicated and looked up with one query per kind, at most 1000 nodes. A view uuid that several views share resolves to the one that is a node of the application or the only one the user can manage. Permissions are checked on the found entities, and the menu import maps uuids through the resolved nodes instead of looking them up again. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…ched off tables The import dialog showed "unknown errors" for a refused import, because it read the message from a field the OCS answer does not have. It now shows the message of a 400 as the server gives it, and no longer passes a server message through the translations for other errors. The preview lists the nodes the import would refuse, and leaves the nodes of a new table out of the import when that table is switched off. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…nodes Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
… no longer use Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
c0627d7 to
b57a52f
Compare
4380c60 to
fa4f674
Compare
Sits on #3138 (menu export and import). Importing an application scheme now handles a table or view that isn't on this instance the same way as one the user can't manage. The menu import in #3138 also skips what it can't use and goes on.
Size: 215 lines: 64 code and 151 tests.
What was wrong
ContextService::importScheme()looked up every node of the scheme by uuid. An unknown uuid answered 404. A uuid of someone else's table was resolved and handed toupdate(), which refused it with a 403. So the answer told a user whether a uuid exists here. The tables to modify had the same split: 404 for an unknown uuid, 403 fromupdateTableStructure()for a table that can't be managed.Which answer, and why
The menu import in #3138 already skips an item whose table or view isn't part of the application, with the reason in the log, and goes on. That also lets an import from another instance go through when some of its tables didn't come along. Nodes now follow the menu:
update()does today, so an import doesn't drop it.<uuid>to modify is not here or cannot be managed by the user." I chose 400 over 403 because a 403 for a uuid that doesn't exist would be odd.ContextController::importScheme()didn't catchBadRequestError, so it now answers such a refusal with 400 and rolls the import back.This and #3201 both rework the node loop in
importScheme(), so whichever is merged second needs a small manual merge. It's simplest to merge this one first.How it was tested
On Nextcloud 34 with PostgreSQL, as alice importing into her application, with a table of bob's she can't see:
ContextServiceTest: a scheme with a manageable table, a table that is already a node, and an unknown and an unmanageable table and view;update()gets the manageable one and the shared node, and the four skips log the same line. A table to modify that is unknown or can't be managed gives the sameBadRequestError.ContextControllerTest: a refused import answers 400 and rolls back.composer cs:check, psalm and rector, all clean🖼️ Screenshots
No UI changes.
🏁 Checklist
/backport to stableX.X🤖 AI (if applicable)