[Bug 43022] New: Biblio create/update REST endpoints return a raw 500 for undecodable MARC
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Bug ID: 43022 Summary: Biblio create/update REST endpoints return a raw 500 for undecodable MARC Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: normal Priority: P5 - low Component: REST API Assignee: koha-bugs@lists.koha-community.org Reporter: martin.renvoize@openfifth.co.uk QA Contact: testopia@bugs.koha-community.org CC: tomascohen@gmail.com Depends on: 35104 Target Milestone: --- Koha::REST::V1::Biblios's create and update actions call MARC::Record->new_from_xml / new_from_mij_structure / new_from_usmarc directly on the request body with no eval (Koha/REST/V1/Biblios.pm, the create and update methods). A malformed MARCXML/MARC-in-JSON/ISO2709 payload from an API client currently produces a raw 500 response with a Perl exception message in the body, instead of a clean 400-style validation error. Found while auditing other places in the codebase that ingest externally-sourced MARC data, prompted by bug 35104. Test plan: 1. POST to /api/v1/biblios (or PUT to update an existing one) with a Content-Type matching one of the supported MARC formats, and a body that cannot be decoded (e.g. truncated/invalid MARCXML). 2. Before a fix: the response is a 500 with a raw exception/stack trace. 3. After a fix: the response is a 400-class error with a clear message describing the decoding problem. Referenced Bugs: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=35104 [Bug 35104] We should warn when attempting to save MARC records that contain characters invalid in XML -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Assignee|koha-bugs@lists.koha-commun |martin.renvoize@openfifth.c |ity.org |o.uk -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 --- Comment #1 from Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> --- Created attachment 211486 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211486&action=edit Bug 43022: Return 400 for undecodable MARC in biblio create Biblios->add called the MARC::Record parsers without any eval, so a malformed MARCXML, MARC-in-JSON or ISO2709 body produced a raw 500. The parsers also return a record with no fields rather than dying for some invalid input, and add accepted that, saving a biblio with blank metadata. Wrap the parsers the same way update() already does, reject records with no fields with 400 bad_record, and map Koha::Exceptions::Metadata::Invalid raised while saving to 400 bad_record on both create and update. update() now calls C4::Biblio::ModBiblio fully qualified so it can be mocked in tests, as add() already does with AddBiblio. Document the bad_record error_code for POST and PUT /biblios in the swagger spec. To test: 1. POST /api/v1/biblios with Content-Type application/marcxml+xml and a truncated body such as <record><leader>00000nam a2200000 a 4500</leader><datafield tag="245" Before: 500 internal_server_error. After: 400 with error_code bad_record, and no biblio created. 2. Repeat with Content-Type application/marc-in-json and truncated JSON, and with application/marc and the text "this is not a valid usmarc record". Before: 500, or 200 with an empty biblio. After: 400. 3. PUT the same bodies to /api/v1/biblios/{biblio_id}. Expect 400 bad_record and the biblio unchanged. 4. Run prove t/db_dependent/api/v1/biblios.t and prove xt/api.t. Sponsored-by: OpenFifth Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Martin Renvoize (ashimema) <martin.renvoize@openfifth.co.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |Needs Signoff -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Sponsorship status|--- |Sponsored Status|Needs Signoff |Signed Off Comma delimited| |OpenFifth list of Sponsors| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #211486|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 --- Comment #2 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Created attachment 211591 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211591&action=edit Bug 43022: Return 400 for undecodable MARC in biblio create Biblios->add called the MARC::Record parsers without any eval, so a malformed MARCXML, MARC-in-JSON or ISO2709 body produced a raw 500. The parsers also return a record with no fields rather than dying for some invalid input, and add accepted that, saving a biblio with blank metadata. Wrap the parsers the same way update() already does, reject records with no fields with 400 bad_record, and map Koha::Exceptions::Metadata::Invalid raised while saving to 400 bad_record on both create and update. update() now calls C4::Biblio::ModBiblio fully qualified so it can be mocked in tests, as add() already does with AddBiblio. Document the bad_record error_code for POST and PUT /biblios in the swagger spec. To test: 1. POST /api/v1/biblios with Content-Type application/marcxml+xml and a truncated body such as <record><leader>00000nam a2200000 a 4500</leader><datafield tag="245" Before: 500 internal_server_error. After: 400 with error_code bad_record, and no biblio created. 2. Repeat with Content-Type application/marc-in-json and truncated JSON, and with application/marc and the text "this is not a valid usmarc record". Before: 500, or 200 with an empty biblio. After: 400. 3. PUT the same bodies to /api/v1/biblios/{biblio_id}. Expect 400 bad_record and the biblio unchanged. 4. Run prove t/db_dependent/api/v1/biblios.t and prove xt/api.t. Sponsored-by: OpenFifth Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: Adolfo Rodríguez <adolfo.rodriguez@xercode.es> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |adolfo.rodriguez@xercode.es --- Comment #3 from Adolfo Rodríguez Taboada <adolfo.rodriguez@xercode.es> --- Tested the three bodies from the test plan (truncated MARCXML, truncated MARC-in-JSON and a non-MARC text as application/marc), both as POST /api/v1/biblios and PUT /api/v1/biblios/{biblio_id}: - Before the patch: - POST with the MARCXML body returned 500. - POST with the MARC-in-JSON and ISO2709 bodies returned 200 and created biblios with empty metadata. - PUT already returned 400. - After the patch, all six requests return 400 with error_code bad_record, no biblio is created, and the PUT target is unchanged. Minor, not a blocker: for the ISO2709 case the error message ends with an empty reason ("Unable to parse record from request body: "), as the parser does not die there. The QA script is happy, and t/db_dependent/api/v1/biblios.t and xt/api.t pass. -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43022 Lisette Scheer <lisette@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- QA Contact|testopia@bugs.koha-communit |lisette@bywatersolutions.co |y.org |m -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org