Skip to content

fix(rows): return 404 when a row is not reachable through the view or table - #3178

Open
rubenvdlinde wants to merge 10 commits into
mainfrom
fix/noid/view-row-404
Open

rubenvdlinde wants to merge 10 commits into
mainfrom
fix/noid/view-row-404

Conversation

@rubenvdlinde

@rubenvdlinde rubenvdlinde commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Updating or deleting a row through a view answered with a 500 when the row was not in that view. The same happened when you deleted through a view that does not exist, and when you updated or deleted a row through a table it does not belong to. In all these cases the row simply isn't there for you, so the answer should be a 404.

@remko48 pointed this out on #3075: it "was already a 500 for normal views too". The 404 guard that came out of that only covers views without a table. This pull request does the same for normal views and tables.

What changes

In RowService::updateSet() and RowService::delete():

  • A row that is not in the view throws NotFoundError instead of InternalError.
  • In delete(), a view that does not exist throws NotFoundError. updateSet() already did this.
  • A row that does not belong to the given table throws NotFoundError.

Permission failures still throw PermissionError and stay 403. The controllers did not need changes: api/1, api/2 and the internal row routes already map NotFoundError to 404. Api1Controller::updateRow() does not catch it itself, but PermissionMiddleware::afterException() turns it into a 404.

How it was tested

New unit test tests/unit/Service/RowServiceRowNotFoundTest.php, one test per case plus one that checks a missing delete permission is still a PermissionError. Without the fix, the five not-found tests fail with InternalError:

Without the fix: Tests: 6, Assertions: 7, Failures: 5.
With the fix:    OK (6 tests, 12 assertions)
Full unit suite, PostgreSQL and SQLite: OK (428 tests, 846 assertions)
composer cs:check: no files to fix
psalm: No errors found!

On a local instance, with row 12 in table 3 but outside the filter of view 1, and table 4 that does not own row 12:

Request Before After
api/1 PUT /rows/12?viewId=1 500 404
api/1 DELETE /views/1/rows/12 500 404
api/2 PUT views/1/rows/12 500 404
api/2 DELETE views/1/rows/12 500 404
internal PUT /row/12?viewId=1 500 404
internal DELETE /view/1/row/12 500 404
api/2 PUT tables/4/rows/12 500 404
api/2 DELETE tables/4/rows/12 500 404

🖼️ Screenshots

Backend only, no visual change.

🏁 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/view-row-404 branch from 9fe0711 to d5805fd Compare November 3, 2026 14:51
@rubenvdlinde
rubenvdlinde marked this pull request as ready for review November 6, 2026 08:25

@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, security, clean code and architecture (backend only). The checks ran on a dev instance (MariaDB) with this PR on main, through API v1, API v2 and the import.

The 404s come out as intended on API v2 for a row outside the view and for a row of another table. Two callers outside the diff still expect InternalError from updateSet()/delete(), though: the import (comment on line 655) and federation (comment on line 640).

Not commented inline (files not in this PR):

  • API v1 updateRow() now answers the 404 with an empty body. Api1Controller::updateRow() (line 1477) catches only BadRequestError and InternalError, so the new NotFoundError is answered by the middleware's afterException(), whose DataResponse isn't rendered for a non-OCS controller: HTTP 404, text/html, Content-Length: 0, where openapi.json promises {message} and the request answered 500 with a JSON body before. deleteRow()/deleteRowByView() catch NotFoundError themselves. Adding catch (NotFoundError) and catch (PermissionError) to updateRow() fixes it. Validated: reproduced on a deployed instance (MariaDB, S-main restored and this PR at d5805fde, through API v1, API v2 and the import API as alice and bob: PUT /index.php/apps/tables/api/1/rows/11?viewId=2 for a row of the view's table outside its filter).
  • The membership check behind the new 404 loads the whole view. Row2Mapper::isRowInViewPresent() (line 771) selects every row id the view matches, with GROUP BY and ORDER BY and no row-id predicate, and runs in_array() for one id, on every update or delete through a view; the UI repeats it right after each edit (/view/{v}/row/{r}/present), and a view import with an ID column runs it once per file row. andWhere(sleeves.id = :rowId) with setMaxResults(1) and no ORDER BY/GROUP BY (as countRowsForView() builds it) answers the same, and a row of another table needs no query at all. Validated: code check only.
  • The UI doesn't use the new 404 yet. src/store/data.js line 350 (and 446 in removeRow()) has a "Outdated data. View is reloaded" path for exactly this case, but it matches the prefix "User should not be able to access row", which no backend code emits since 5def698, and reads e.response.data.message where OCS nests it under ocs.data. Keying it on e.response.status === 404 makes the reload reachable: today the user gets "Could not update row." and the stale row stays. Validated: code check only.

CI: green.

Comment thread lib/Service/RowService.php Outdated
$e = new \Exception('Row does not belong to table with id ' . $tableId);
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(static::class . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
throw new NotFoundError(static::class . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());

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.

ImportService::upsertRow() (lines 703-708) counts an InternalError from updateSet() as a failed row but rethrows NotFoundError, so with this change an import whose ID column holds a row id of another table, or of a row outside the target view, aborts part-way instead of counting one error. There is no transaction, so the rows before it are already written and the rest never are, and ImportTableJob turns it into an import failure for asynchronous imports. Every Tables CSV export starts with an ID column, and the preview maps it to "ID (Meta)" by default, so this is the normal path for re-importing an export into another table. No test covers a foreign id in the ID column. Counting a NotFoundError from updateSet() as a row error in upsertRow() keeps the import going.

Validated: reproduced on a deployed instance (MariaDB, S-main restored and this PR at d5805fde, through API v1, API v2 and the import API as alice and bob: importing [ID 14 (a row of target table 8), ID 10 (a row of table 6), no ID] into table 8 answered 404 "... upsertRow: ... updateSet: Row does not belong to table with id 8"; row 14 was written and the new row never inserted. A view import with a file row outside the view's filter aborted the same way with "Row not found.").

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 import stopped on a foreign id where it used to count an error and carry on. upsertRow() now catches the NotFoundError from updateSet(), logs a warning, counts the row as an error and moves on; other not-found cases still abort as before. ImportServiceUpsertRowTest imports an ID column with a row that is not reachable followed by one that is, and expects one error and one update.

cc @blizzz

Comment thread lib/Service/RowService.php Outdated
$e = new \Exception('Row not found.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(static::class . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
throw new NotFoundError(static::class . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());

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.

For federated tables and views the host's new 404 becomes a success on the other instance. FederationProxy::request() returns any 4xx with a JSON body as a normal response (lines 90-103), and getOCSData() only debug-logs the status (lines 130-131), so RowOCSController::updateRow()/deleteRow() answer 200 with {message} in place of the row. src/store/data.js then treats the update as done (nothing replaced, no toast) and removeRow() drops the row and lowers the counts although nothing was deleted. Before this PR the 500 took the ServerException branch, and the user got "Could not update row.". Mapping non-2xx answers in getOCSData()/FederationService (404 to NotFoundError, 403 to PermissionError) together with this change keeps federated edits honest.

Validated: code check only (Guzzle's http_errors default is true in the server's bundled client and OC\Http\Client doesn't override it).

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 catch, I had not followed the 404 through the federation proxy. getOCSData() now raises NotFoundError for a 404 and PermissionError for a 403 when the caller did not allow that status, so a federated edit of a row outside the view fails the same way as a local one. FederationProxyTest covers both, plus a caller that allows the 404.

cc @blizzz

Comment thread lib/Service/RowService.php Outdated
if (!$this->row2Mapper->isRowInViewPresent($id, $view, $userId)) {
$e = new \Exception('Update row is not allowed.');
$e = new \Exception('Row not found.');
$this->logger->error($e->getMessage(), ['exception' => $e]);

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.

Every branch this PR turns into a 404 still builds a throwaway \Exception and logs it at error level with a stack trace before throwing (here and on lines 654, 762, 767 and 773), and the controller logs the same NotFoundError again. Client-caused 404s, such as a stale grid after someone else's edit, therefore land in nextcloud.log as server errors, about 2.5 KB each plus the controller's entry, and error-level monitoring fires on them. Dropping the synthetic exception and the error log in these branches, or logging at debug, leaves the record to the controller.

Validated: reproduced on a deployed instance (each such 404 wrote an error entry "Row not found." or "Row does not belong to table with id 6" of 2.3-2.7 KB with trace, plus an info or warning entry from the controller).

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 stale grid should not write an error with a stack trace. The 404 branches of updateSet() and delete(), including the older read check branches, now throw NotFoundError directly without a synthetic exception or an error log, and the controller keeps its info record. The tests assert that nothing is logged at error level on those paths.

cc @blizzz

Comment thread lib/Service/RowService.php Outdated
} catch (InternalError|MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(static::class . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
} catch (DoesNotExistException $e) {

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 DoesNotExistException catch in delete() can't be reached by a logged-in user: PermissionsService::basisCheckById() loads the view itself and returns false when it doesn't exist, so delete() throws NotFoundError('Row not found.') on lines 746-749 first (and on API v2 the middleware answers 404 even earlier). testDeletingARowThroughAViewThatDoesNotExistIsNotFound reaches it only by stubbing canReadRowsByElementId() to true for a view that doesn't exist. If it fires (a race with a view delete), it echoes QBMapper's SQL debug text to API v1 and internal clients, while create() uses a fixed message. Dropping it, or giving it a fixed message, and testing the canReadRowsByElementId() = false path instead covers the real behaviour. The InternalError alternative in the catch above is dead as well, since ViewMapper::find() never throws it.

Validated: reproduced on a deployed instance (DELETE /api/1/views/999999/rows/10 and DELETE /index.php/apps/tables/view/999999/row/10 answered 404 "RowService - delete: Row not found." from the read check, before and after this PR).

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 that the read check answers first for a view that does not exist, and my test only got there by stubbing it. The DoesNotExistException catch in both updateSet() and delete() now gives a fixed "Row not found." without the mapper text, and the dead InternalError alternative is gone. The tests now cover the read check returning false for a missing view (with find() never called) and a view that vanishes after the read check, asserting no SQL text leaks.

cc @blizzz

@@ -755,20 +755,23 @@ public function delete(int $id, ?int $viewId, string $userId, ?int $tableId = nu
}
try {

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.

updateSet() and delete() each inline the same read check, write check, view lookup and row-in-view or table check (both marked @noinspection DuplicatedCode, and create() has a third view lookup), so this PR makes one change twice and copies updateSet()'s catch into delete() by hand. The delete() bug this PR fixes was drift between these copies, and they still differ: delete() calls canDeleteRowsByViewId($viewId) without $userId (line 751) where updateSet() passes it (line 620); both delete PermissionErrors say "Update row is not allowed." (lines 752 and 784); and the table check is written two ways (lines 651-652 and 771). One private helper shared by both, for example authorizeRowChange(Row2 $row, ?int $viewId, ?int $tableId, string $userId, string $operation): ?View, or at least a findView() helper for create(), updateSet() and delete(), removes the copies.

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.

Thanks, the drift is real. The missing $userId on canDeleteRowsByViewId() and the "Update row is not allowed." message on delete are fixed in this PR, with tests for both. I would like to do the shared helper for create(), updateSet() and delete() in a separate PR so this one stays about the status codes, and I will open a follow-up issue for it.

cc @blizzz

/**
* Changing or deleting a row that is not reachable through the given view or table is a 404, not a 500.
*/
class RowServiceRowNotFoundTest extends TestCase {

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 assert only the service's exception classes, so the HTTP mapping the PR is about (including API v1 updateRow()'s empty 404 described in the review body) and the import and federation callers stay untested. The Behat scenario "A read-only recipient of a filtered view never receives the excluded rows" (tests/integration/features/APIv2.feature lines 961-987) already builds a filtered view with an excluded row; appending an update and a delete of that row with Then the reported status is "404" uses existing steps and would have caught the empty body. The unit class also adds another 12-argument RowService construction that every new constructor dependency has to update.

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 point, the unit tests would not have caught the empty API v1 body. The filtered view scenario in APIv2.feature now updates and deletes the excluded row through the view as the owner, in API v2 and API v1, and expects a 404 with a message each time. I also added unit tests for the API v1 mapping, the import and the federation proxy. There is no RowServiceTest on this branch's base yet, so the cases stay in their own class for now; I can fold them in when this is rebased onto a main that has it.

cc @blizzz

@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

Copy link
Copy Markdown
Contributor Author

Thanks for the extra points. API v1 updateRow() now answers 404 and 403 with a JSON message like deleteRow() does. isRowInViewPresent() now runs the view filter for that one row with a limit of one instead of loading every row id, and the service skips it when the view is on another table. The store reloads on a 404 now instead of matching a message nothing sends, and a table reloads its own rows.

cc @blizzz

… table

Updating or deleting a row through a view returned an internal error,
and so a 500, when the row was not in that view. Deleting through a view
that does not exist, and updating or deleting a row through a table it
does not belong to, did the same. These now throw NotFoundError, which
every API layer already maps to 404. Permission failures stay 403.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
A row outside the view or table, or a view that is gone, is a client
mistake. The 404 branches of updateSet() and delete() no longer build a
synthetic exception and log it at error level, and a missing view
answers with a fixed message instead of the mapper's text.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
delete() asked canDeleteRowsByViewId() without the user id, so the
permission was resolved for the session user instead of the user the
call is made for, and both delete refusals said "Update row is not
allowed.".

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
updateSet() now answers a row of another table or outside the view with
NotFoundError. upsertRow() rethrew it and stopped the import after the
rows already written; it now counts the row as an error and continues.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
…s data

A host now answers an update or delete of a row outside the view with
404. FederationProxy::getOCSData() only logged the unexpected status and
returned the error body as data, so the remote side answered 200. A 404
becomes NotFoundError and a 403 PermissionError, which the row
controllers already map to the same status.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
… with a message

updateRow() caught only BadRequestError and InternalError, so a row
outside the view came back as an empty 404. It now maps NotFoundError
to 404 and PermissionError to 403 with a message, as deleteRow() does.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
The filtered view scenario now changes the row the view leaves out,
through API v2 and API v1, and expects a 404 with a message each time.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
RowService answers 404 for a row whose table is not the table of the view
without querying the view filter. isRowInViewPresent() itself already
looks up the one row on main.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
updateRow() and removeRow() reloaded only when the error message started
with "User should not be able to access row", which nothing sends, and
read it off the response instead of the OCS body. They now reload on a
404, and a table reloads its own rows instead of asking for a view.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ruben van der Linde <ruben@conduction.nl>
@rubenvdlinde

Copy link
Copy Markdown
Contributor Author

A correction to my comment above about isRowInViewPresent(): main now has the single-row lookup already (e042b6c), so after rebasing onto main this PR keeps only the check that skips the lookup when the view belongs to another table, plus its test. I also updated the row service test for the new RowVisibilityService argument on main.

cc @blizzz

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.

3 participants