Skip to content

Commit 86ca913

Browse files
Maffoochclaude
andcommitted
fix(cwe): guard save_cwes on partial update; bound CWE number to column width
Two data-safety fixes for the multi-CWE relation: - FindingSerializer.update() called save_cwes() unconditionally, so any PATCH/PUT that omitted `cwes` deleted a finding's extra Finding_CWE rows (leaving only the primary). Guard the call on `parsed_cwes is not None`, mirroring the vulnerability-id path. - cwe_number() had no upper bound, but Finding_CWE.cwe stores "CWE-<n>" in a varchar(11). A number > 9,999,999 passed validation then raised DataError at insert (API/form 500, and would abort the 0280 backfill's bulk_create, since ignore_conflicts does not swallow DataError). Reject it as invalid input (None) so it becomes a clean 400/skip. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 9ec9a1c commit 86ca913

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

dojo/finding/api/serializer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,12 @@ def update(self, instance, validated_data):
483483
)
484484

485485
# Sync the CWE relation (separate from vulnerability ids) after the new cwe is applied.
486+
# Only touch the CWE rows when the request actually carried `cwes`; calling save_cwes()
487+
# unconditionally would wipe a finding's extra Finding_CWE rows on any partial PATCH that
488+
# omits the field (mirrors the guarded vulnerability-id path above).
486489
if parsed_cwes is not None:
487490
instance.unsaved_cwes = parsed_cwes[1:]
488-
save_cwes(instance)
491+
save_cwes(instance)
489492

490493
if settings.V3_FEATURE_LOCATIONS and locations is not None:
491494
for location_ref in instance.locations.all():

dojo/finding/cwe.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ def cwe_number(value) -> int | None:
1313
if not token.isdigit():
1414
return None
1515
number = int(token)
16-
return number if number > 0 else None
16+
# Finding_CWE.cwe stores the "CWE-<n>" label in a varchar(11), so n is bounded to 7 digits.
17+
# Reject anything larger here (clean None -> 400/skip) instead of 500-ing at insert.
18+
if number <= 0 or number > 9_999_999:
19+
return None
20+
return number
1721

1822

1923
def cwe_label(value) -> str | None:

0 commit comments

Comments
 (0)