Skip to content

fix(contexts): answer an unknown and an unmanageable uuid alike on scheme import - #3202

Open
rubenvdlinde wants to merge 5 commits into
feat/noid/app-menu-export-importfrom
fix/noid/scheme-import-unknown-uuid
Open

rubenvdlinde wants to merge 5 commits into
feat/noid/app-menu-export-importfrom
fix/noid/scheme-import-unknown-uuid

Conversation

@rubenvdlinde

@rubenvdlinde rubenvdlinde commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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 to update(), 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 from updateTableStructure() 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:

  • A node whose table or view isn't here, or is new to the application and can't be managed by the user, is skipped with the same log line, and the import answers 200. A node that already belongs to the application is kept without a new permission check, as update() does today, so an import doesn't drop it.
  • A table to modify stays a hard failure, because the user explicitly asked to change it. It's now refused with the same 400 and the same message in both cases: "The table <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 catch BadRequestError, 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:

Request Before After
node with an unknown uuid 404 200, node skipped
node with bob's table 403 200, node skipped
table to modify with an unknown uuid 404 400, same message as below
table to modify with bob's table 403 400 "not here or cannot be managed"
  • 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 same BadRequestError.
  • ContextControllerTest: a refused import answers 400 and rolls back.
  • the full unit suite on PostgreSQL: 617 tests
  • composer cs:check, psalm and rector, all clean

🖼️ Screenshots

No UI changes.

🏁 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

@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 1c7a206 to 1b9a79d Compare November 3, 2026 14:51
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch 2 times, most recently from 2471ff4 to 6368c26 Compare November 4, 2026 12:35
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 1b9a79d to 1c2f7f3 Compare November 4, 2026 12:35
@rubenvdlinde
rubenvdlinde marked this pull request as ready for review November 6, 2026 08:24
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 6368c26 to c203b3a Compare November 10, 2026 09:29
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 1c2f7f3 to cab2a56 Compare November 10, 2026 09:29
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from c203b3a to 5cf92cf Compare November 10, 2026 10:13
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from cab2a56 to 11fde57 Compare November 10, 2026 10:14

@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.

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.

Comment thread lib/Service/ContextService.php Outdated
continue;
}
$found = $this->findNodeByUuid($kind, $node['node_uuid']);
if ($found === null

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.

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).

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.

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

Comment thread lib/Service/ContextService.php Outdated
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.');

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.

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).

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.

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

Comment thread lib/Service/ContextService.php Outdated
$currentNodes[sprintf('t%di%d', $node['node_type'], $node['node_id'])] = true;
}
$resolvedNodes = [];
foreach ($nodes as $node) {

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.

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).

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.

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

Comment thread lib/Service/ContextService.php Outdated
if ($kind === null) {
continue;
}
$found = $this->findNodeByUuid($kind, $node['node_uuid']);

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.

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).

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 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

Comment thread lib/Service/ContextService.php Outdated
$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)) {

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.

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.

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.

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

Comment thread lib/Service/ContextService.php Outdated
// 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) {

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 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.

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.

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

Comment thread lib/Service/ContextService.php Outdated
$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))) {

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.

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.

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.

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

Comment thread lib/Service/ContextService.php Outdated
* @throws Exception
*/
private function nodeRelIdByUuid(Context $context, string $kind, string $uuid): ?int {
private function findNodeByUuid(string $kind, string $uuid): Table|View|null {

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.

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.

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.

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);

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.

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.

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.

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

@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 5cf92cf to ee0564c Compare November 13, 2026 21:04
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 11fde57 to 745edfa Compare November 13, 2026 21:04
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from ee0564c to 00c1b92 Compare November 16, 2026 09:23
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 745edfa to b61edc8 Compare November 16, 2026 09:23
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 00c1b92 to 6ffaf94 Compare November 17, 2026 09:11
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from b61edc8 to 541eefe Compare November 17, 2026 09:12
@github-actions

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

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.)

@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from 6ffaf94 to f7549c7 Compare November 19, 2026 08:28
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 541eefe to adf7252 Compare November 19, 2026 08:28
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from f7549c7 to b464a42 Compare November 19, 2026 13:36
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from adf7252 to b67868a Compare November 19, 2026 13:36
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from b464a42 to c0627d7 Compare November 20, 2026 10:53
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from b67868a to 4380c60 Compare November 20, 2026 10:53
@rubenvdlinde

Copy link
Copy Markdown
Contributor Author

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 t(). There is a component test for both.

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>
@rubenvdlinde
rubenvdlinde force-pushed the feat/noid/app-menu-export-import branch from c0627d7 to b57a52f Compare November 21, 2026 10:53
@rubenvdlinde
rubenvdlinde force-pushed the fix/noid/scheme-import-unknown-uuid branch from 4380c60 to fa4f674 Compare November 21, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants