Validate the request body on a finding metadata PUT - #15785
Open
svader0 wants to merge 1 commit into
Open
Conversation
_edit_metadata declared FindingMetaSerializer in its schema. It never built the serializer. It read name and value off request.data and wrote them straight into update_or_create. A value longer than 300 characters reached the column and raised DataError. The except IntegrityError below does not catch DataError, so the caller got a 500 instead of a 400. A missing name wrote NULL and returned the wrong error message. _add_metadata directly below already validates the same body with the same serializer. The PUT branch now does the same. The response carries the serialized name and value instead of the raw request body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
_edit_metadatahandles the PUT on/api/v2/findings/{id}/metadata/. It declaresFindingMetaSerializerin its schema. It never builds that serializer. It readsnameandvalueoffrequest.dataand writes them straight intoupdate_or_create.Two things go wrong:
valuelonger than 300 characters, Postgres raisesDataError. Theexcept IntegrityErrorbelow does not catchDataError. The caller gets a 500.name, the code writes NULL. The caller gets a 400, but the message says "Update failed because the new name already exists". That message is wrong._add_metadatasits directly below and handles the POST. It already validates the same body with the same serializer. This change makes the PUT branch do the same.One other change: the 200 response now returns the serialized
nameandvalue. Before, it echoed the raw request body.Test results
I added two tests to
FindingMetadataTestinunittests/test_rest_framework.py. I ran the class both ways.Ran 22 tests ... OK (skipped=10)FAILED (failures=2, skipped=10)The unfixed run shows the bug:
Ruff 0.16.0 passes.
Documentation
No documentation change. The endpoint contract stays the same. The declared schema already promised this validation.
Checklist
bugfix.