kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Bulk user import and export crash on malformed CSVs and bad options instead of reporting an error

Open nucleogenesis opened this issue 1 year ago • 16 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Target branch: develop

Observed behavior

  • Web: selecting an empty CSV in Facility > Data > Import and clicking Continue shows nothing. The server rejects the upload with a 400 that nothing in the frontend handles.
  • CLI: kolibri manage bulkimportusers on an empty file crashes in csv_headers_validation.
  • bulkimportusers and bulkexportusers also raise an uncaught exception on each input in the table below. In Facility > Data > Import, a failed task with no overall_error goes to handleApiError with its traceback (actions.js#L61-L63) instead of the import's error list.

https://github.com/user-attachments/assets/b092da9b-a9ea-478a-9219-b7a8712fac9a

Errors and logs

CLI:

  File "kolibri/core/auth/management/commands/bulkimportusers.py", line 497, in csv_headers_validation
    header = next(csv.reader(f, strict=True))
StopIteration
Input Exception Raised at
Header with an unclosed quote: "UUID,USERNAME _csv.Error: unexpected end of data bulk_import.py#L466
Row with no USERNAME cell AttributeError: 'NoneType' object has no attribute 'lower' bulk_import.py#L273
Row missing later cells, e.g. ,bob,pass,Bob IndexError: Replacement index 1 out of range bulk_import.py#L350
Unknown or malformed --facility, import Facility.DoesNotExist / ValueError bulk_import.py#L695
Unknown or malformed --facility, export Facility.DoesNotExist / ValueError bulk_export.py#L230
--errorlines <path> from the CLI AttributeError: 'str' object has no attribute 'write' bulk_import.py#L840
Malformed --userid with --delete, import ValueError: badly formed hexadecimal UUID string bulk_import.py#L716-L726

Steps to reproduce

  1. Create an empty empty.csv.
  2. As a facility admin, open Facility > Data, click Import, select empty.csv, click Continue.
  3. On a device with a facility, run kolibri manage bulkimportusers empty.csv.
  4. Run kolibri manage bulkimportusers <file> --facility <facility id> with each CSV in the table.
  5. Run kolibri manage bulkimportusers <valid file> --facility <id> and kolibri manage bulkexportusers --facility <id> with an unknown hex ID, then with notauuid.
  6. Run kolibri manage bulkimportusers <file with an unknown UUID row> --facility <facility id> --errorlines errors.txt.
  7. Run kolibri manage bulkimportusers <valid file> --facility <facility id> --delete --userid notauuid.

Acceptance Criteria

  • [ ] Selecting an empty file in the import file input shows an inline error under the input saying the file is empty; Continue stays disabled.
  • [ ] Selecting a non-empty file afterwards clears the error and enables Continue.
  • [ ] kolibri manage bulkimportusers on an empty file raises CommandError with an untranslated English message saying the file is empty.
  • [ ] A Jest spec covers the empty-file error in Init.vue; a pytest test covers the CommandError.
  • [ ] A CSV whose header row the CSV reader rejects stops the import with a file error, printed in the CLI's CommandError and listed in Facility > Data > Import under "Importing is not possible due to the following errors:".
  • [ ] A row with no USERNAME cell, or missing any later cell, is reported as an error on that row; the file's valid rows import.
  • [ ] An unknown or malformed --facility makes bulkimportusers and bulkexportusers exit with a CommandError.
  • [ ] bulkimportusers --errorlines <path> writes each row error to <path>.
  • [ ] bulkimportusers --userid notauuid, with and without --delete, exits with a CommandError naming the value and changes no user or class; a pytest test covers both.
  • [ ] These repro tests from #15792 pass: test_empty_csv_reports_error, test_unparseable_header_reports_error, test_row_without_username_cell_reports_row_error, test_row_without_user_type_cell_reports_row_error, test_unknown_facility_import_reports_error, test_malformed_facility_import_reports_error, test_unknown_facility_export_reports_error, test_malformed_facility_export_reports_error, test_errorlines_path_from_cli.

AI usage

@rtibbles chose the frontend check and the untranslated CLI error; Claude Code reproduced the CLI crash and drafted this revision.

nucleogenesis avatar Mar 26 '25 21:03 nucleogenesis

Hi! I’m a CS major at the University of Michigan taking a software engineering course, where one of our assignments is to contribute to an opens source project. I want to contribute to Kolibri for the next week or so. I’d love to work on this issue (and potentially another one) for my assignment. Would appreciate any advice or confirmation that no one else is actively working on this. Thanks!

amyjun26 avatar Apr 10 '25 01:04 amyjun26

Hi @amyjun26. This issue is not available. You're welcome to find an unassigned 'help wanted' issue instead. There aren't many available issues right now so it's best to wait and keep an eye on the list. We label new ones on a weekly basis. I'd also invite you to have a look at the contributing guidelines (CONTRIBUTING.md) if you haven't seen them yet. Thank you.

AlexVelezLl avatar Apr 10 '25 15:04 AlexVelezLl

The empty file is one of several inputs bulkimportusers and bulkexportusers crash on instead of reporting an error. From #15792; added to the body.

Input Exception Raised at
Header with an unclosed quote: "UUID,USERNAME _csv.Error: unexpected end of data bulk_import.py#L466
Row with no USERNAME cell AttributeError: 'NoneType' object has no attribute 'lower' bulk_import.py#L273
Row missing later cells, e.g. ,bob,pass,Bob IndexError: Replacement index 1 out of range bulk_import.py#L350
Unknown or malformed --facility, import Facility.DoesNotExist / ValueError bulk_import.py#L695
Unknown or malformed --facility, export Facility.DoesNotExist / ValueError bulk_export.py#L230
--errorlines <path> from the CLI AttributeError: 'str' object has no attribute 'write' bulk_import.py#L840
  • In Facility > Data > Import, a failed task with no overall_error reaches handleApiError with its traceback (actions.js#L61-L63), not the import's error list.
  • append_error / exit_if_error already carry a file error to the CLI's CommandError and to Preview.vue; Validator.validate yields the row-error shape.
  • Repro tests, in #15792's Testing Requirements: test_empty_csv_reports_error, test_unparseable_header_reports_error, test_row_without_username_cell_reports_row_error, test_row_without_user_type_cell_reports_row_error, test_unknown_facility_import_reports_error, test_malformed_facility_import_reports_error, test_unknown_facility_export_reports_error, test_malformed_facility_export_reports_error, test_errorlines_path_from_cli. Their fieldnames import now lives in kolibri/core/auth/utils/bulk_import.py.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 08 '26 08:11 rtibblesbot

Plan: Issue #13260. Every input in the issue's table, plus an empty file, turns into a reported error (inline in the UI, a file error, a row error, or a CommandError) instead of an uncaught exception.

  • [ ] Inline empty-file error on the import page
  • [ ] Empty and unparseable files become file errors
  • [ ] Rows with missing cells become row errors
  • [ ] Unknown or malformed --facility exits with CommandError
  • [ ] --errorlines <path> from the CLI
  • [ ] Self-review the produced code

Assumptions — the issue did not settle these; say so if one is wrong.

  • Missing-cell row error reuses REQUIRED_COLUMN ("The column '{}' is required"), one per missing cell; if wrong, a new translatable MESSAGES entry is added.
  • A row missing only optional trailing cells (e.g. ENROLLED_IN) is an error, per "missing any later cell"; if wrong, such rows import with blanks via restval="". Open PR #15800's test_row_shorter_than_header_creates_user asserts the opposite for a missing trailing UUID; a reply draft on #15800 flags it.
  • Empty-file error goes through exit_if_error, so the CLI prints File errors: ['<message>']; if a bare message is wanted, raise BulkUserImportError directly.
  • get_facility catching malformed ids also turns registerfacility/deletefacility/recreatefacility/sync crashes into CommandError; if unwanted, catch in the bulk managers only.
  • Init.vue stays Options API with a new $trs entry; if a setup() conversion is wanted, the strings move to createTranslator and their ids change.
  • Clearing the file picker clears the empty-file error; if it should persist, filesChanged resets the flag only on a non-empty pick.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 11 '26 22:11 rtibblesbot

Another bad option bulkimportusers crashes on; added to the body.

Input Exception Raised at
Malformed --userid with --delete ValueError: badly formed hexadecimal UUID string bulk_import.py#L716-L726
  • Repro: kolibri manage bulkimportusers <valid file> --facility <facility id> --delete --userid notauuid.
  • With #15926 applied, the same --userid raises without --delete too, from BulkUserImportManager.__init__.
  • New criterion: --userid notauuid, with and without --delete, exits with a CommandError naming the value and changes no user or class; a pytest test covers both.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 18 '26 18:11 rtibblesbot

rtibbles Self-review did not converge on this branch, so nothing was pushed, and any replies to this round wait for the push. Open findings:

  • suggestion kolibri/core/auth/test/test_bulk_input_errors.py:116: :116, :142 and :152 patch the first-party Facility.get_default_facility so a fallback to the default facility would surface. Call provision_device() from `kolibri/core/auth/test/helpers.py:4
  • blocking kolibri/core/auth/utils/bulk_import.py:782: One row with blank UUID and USERNAME turns off --delete for the whole import, and nothing reports it. Spreadsheets often export trailing rows that are only commas, so this will happen in practic
  • suggestion kolibri/core/auth/utils/bulk_import.py:76: Most of the uncommitted bulk_import.py rewrite belongs to sibling issues, not #13260. Move it to the PRs for those issues:

Reply just push to push this tree as it is, or reply with direction and another review round runs.


Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 19 '26 14:11 rtibblesbot

These all sound like important fixes - carry on.

rtibbles avatar Nov 19 '26 14:11 rtibbles

@rtibbles Blocked — these cannot be assumed either way:

  • A deleted facility leaves a queued web export stuck in Exporting. The fix would record the lookup error on the job and have the web poller display it and reset the export state; #13260 names only CLI export. Fix it here, file a follow-up issue, or leave it?

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 19 '26 19:11 rtibblesbot

Fix it here.

rtibbles avatar Nov 19 '26 20:11 rtibbles

@rtibbles Blocked — these cannot be assumed either way:

  • A retry POST returning 400 leaves an existing CSV unavailable. The fix preserves the pending file lookup until the retry queues; consult 4 explicitly leaves this race open. Fix it here, file a follow-up issue, or leave it?

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 20 '26 18:11 rtibblesbot

Fix it here.

rtibbles avatar Nov 20 '26 18:11 rtibbles

@rtibbles Blocked — these cannot be assumed either way:

  • A second tab can clear a failed export before the initiating tab sees it. The fix would leave failed jobs in the task list, as develop does; consult 5 explicitly requires clearing them. Fix it here, file a follow-up issue, or leave it?

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 22 '26 19:11 rtibblesbot

Leave it - multitab behaviour is always going to give edge cases.

rtibbles avatar Nov 22 '26 19:11 rtibbles

@rtibbles Self-review did not converge on this branch, so nothing was pushed, and any replies to this round wait for the push. Open findings:

  • suggestion kolibri/plugins/facility/frontend/modules/manageCSV/actions.js:117: The missing-task reconciliation handles only a tracked export that something else cleared, which in practice means another tab. That covers this block, requestedId, the usersOnly lookup, `exportUs
  • suggestion kolibri/plugins/facility/frontend/views/DataPage/__tests__/DataPage.spec.js:534: All 22 tests still pass after either of two changes: replace the task-id check at actions.js:92 with true, or reduce index.js:150 to if (state.exportUsersTaskId). This test's "before the expor
  • suggestion kolibri/plugins/facility/frontend/views/DataPage/index.vue:305: The facility-list callback starts polling even after the component is destroyed.

Reply just push to push this tree as it is, or reply with direction and another review round runs.


Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 23 '26 14:11 rtibblesbot

Decided without asking; a reply here changes it.

  • Pushing the tree as it is. All three open self-review findings are suggestions, and the tree passes the facility Jest specs (152), the bulk import/export pytest files (121) and prek on the changed files. "Carry on" (2026-11-19) answered a round with a blocking finding; this round has none.
    • Missing-task handling in manageCSV (acts only when another tab clears the tracked export): kept. "Leave it" (2026-11-22) rules out another round on multi-tab behaviour, to remove code as much as to add it. It is already written, small beside the branch, and returns the tab to idle once a user CSV exists, where develop leaves it in Exporting. Removal can be asked for on the PR.
    • Guards at manageCSV/actions.js:92 and index.js:150 that no spec exercises: they act only in that same multi-tab state, so no test is added.
    • Task polling that keeps running when the Data page is left before the facility list loads: upstream/develop does the same, and the fix is outside this issue. Not fixed here.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 23 '26 21:11 rtibblesbot

Decided without asking; a reply here changes it.

All three concern changing the facility while Facility > Data stays open. That happens only when the facility id in the URL is edited by hand: Change learning facility goes through the facility list and reopens the page (checked live in QA). A Jest probe of develop, the branch and the branch without its reload-on-facility-change (.plans/advice2_probe) shows develop refetches nothing after such an edit: the new facility stays on "Generating CSV..." and Generate sends no request. The branch's reload fixes that and stays; it is written, small, and on the export state this issue reworks.

  • Stale export error and lost export after a URL-edit switch: left as it is. Now broken: until the facility list returns, the new facility shows the previous facility's export error, and an export queued in that window that then fails is not reported. The branch's reload causes both, which argues for fixing here; no user is known to reach them, and the fix adds facility-keyed export state the issue does not need, so the edge-case line wins. Opening the page has the same window, already left as develop has it.
  • Usage-log dates: follow-up issue queued, no change here. Clicking Generate log before the first log date loads raises TypeError: Cannot read properties of null (reading 'getDate') and the date dialog never opens, on develop at every page open; the branch adds the same window after a URL-edit switch. Disabling the usage-log buttons until the date loads is a new mechanism in a feature this branch does not touch. A late date response from the previous facility replacing the new one's is left: develop always keeps the previous facility's date there.
  • Facility-list reloads after a URL-edit switch: left as they are. Now broken: a failed facility-list request rejects unhandled and keeps the previous facility's state, and repeated switches repeat the metadata requests. Develop keeps the previous facility's state on every such switch and rejects unhandled the same way when the page opens. Edge case with no known affected user; deduplication and a separate refresh path are remedies nobody asked for.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 24 '26 13:11 rtibblesbot