Skip to content

Commit 1abbf1e

Browse files
fix: don't allow setting an entity to another entity's version
1 parent d778661 commit 1abbf1e

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

  • src/openedx_content/applets/publishing
  • tests/openedx_content/applets/publishing

src/openedx_content/applets/publishing/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,15 @@ def set_draft_version(
932932
# block is bookkeeping in our DraftChangeLog.
933933
draft.version_id = publishable_entity_version_pk
934934

935+
# Validate the entity
936+
if publishable_entity_version_pk is not None:
937+
if draft.entity.id != PublishableEntityVersion.objects.only("entity_id").get(
938+
pk=publishable_entity_version_pk
939+
).entity_id:
940+
raise ValidationError(
941+
"Entity mismatch - the specified PublishableEntityVersion does not match the PublishableEntity"
942+
)
943+
935944
# Check to see if we're inside a context manager for an active
936945
# DraftChangeLog (i.e. what happens if the caller is using the public
937946
# bulk_draft_changes_for() API call), or if we have to make our own.

tests/openedx_content/applets/publishing/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ def test_set_draft_version_rejects_version_from_different_entity(self) -> None:
24402440
# This should raise an error because version_b_v1 belongs to entity_b,
24412441
# not entity_a. Without validation, this silently corrupts entity_a's
24422442
# draft to point to entity_b's content.
2443-
with pytest.raises((ValidationError, ValueError)):
2443+
with pytest.raises(ValidationError, match="Entity mismatch"):
24442444
publishing_api.set_draft_version(entity_a.id, version_b_v1.pk)
24452445

24462446
def test_publish_from_drafts_rejects_cross_package_drafts(self) -> None:

0 commit comments

Comments
 (0)