Skip to content

Duplicating XBlocks across courses creates the block in the source course #602

Done
openedx/openedx-platform
#39409

Description

@BryanttV

Description

Duplicating a block from another course writes the new block into the source course and attaches it as a child of a parent in a different course. This bypasses access checks and leaves a cross-course reference that cannot be loaded or deleted. It is backend only and needs a direct API call.

Current Behavior

Two problems:

  • With authz enabled, nothing checks access to the source course. The read check on the source only runs on the legacy path (view_handlers.py:362-366).
  • duplicate_block builds the new key from the source location (contentstore/utils.py:1132: source_item.location.replace(name=uuid4().hex)), so the new block is created in the source course and then attached to a parent in another course. The resulting structure fails to load and to delete (KeyError). The same happens when duplicating a v1 library block into a course.

Expected Behavior

  • The duplicated block is created in the destination course (the parent's course), never in the source course. The source course is left unchanged.
  • The user must have view access on the source course and write access on the destination, in both modes (AuthZ and legacy). A user who can edit the destination but cannot view the source gets 403.
  • Duplicating a v1 library block into a course creates the copy in the course. Previously it returned 200 but created the copy in the library and left the course with a dangling child.
  • Same-course duplication keeps working as before.

Steps to reproduce

This cannot be reproduced from the Studio UI, because the Duplicate button always sends a source and a parent from the same course. It needs a direct request to POST /xblock/ on the Studio (CMS) host.

Setup

  1. Create two courses, course A and course B (e.g. course-v1:SandboxOrg01+DemoX01A+2026_T1 and course-v1:SandboxOrg02+DemoX02A+2026_T1).
  2. In course A, add a section, subsection, unit and an HTML component. Copy the component's usage key: block-v1:SandboxOrg01+DemoX01A+2026_T1+type@html+block@<HTML_A>.
  3. In course B, add a section, subsection and unit. Copy the unit's usage key: block-v1:SandboxOrg02+DemoX02A+2026_T1+type@vertical+block@<VERTICAL_B>.
  4. Create a user userb, not a superuser and not global staff. Make sure it has no legacy course role (Staff/Instructor) in either course, no org-level role, and no authz role in course A.
  5. Enable authz on the platform: turn on the waffle flag authz.enable_course_authoring globally (Django admin, Waffle flags, set it to Everyone / On).
  6. Assign userb an authz role in course B only that allows editing content, for example Course Editor. The role must be granted through authz (Admin Console / authz API), not from the legacy course team page.

Send the request

  1. Send the following request to the Studio (CMS) host, authenticated as userb:

    • Endpoint: POST http://studio.local.openedx.io:8001/xblock/

    • Headers: Content-Type: application/json

    • Body:

      {
        "parent_locator": "block-v1:SandboxOrg02+DemoX02A+2026_T1+type@vertical+block@<VERTICAL_B>",
        "duplicate_source_locator": "block-v1:SandboxOrg01+DemoX01A+2026_T1+type@html+block@<HTML_A>"
      }

    Replace <VERTICAL_B> and <HTML_A> with the keys from the setup.

    Note: for the request to work, two cookies of userb must be added to the request, copied from the browser (DevTools → Application → Cookies, on the Studio domain after logging in to Studio):

    • studio_session_id: the Studio session cookie. Without it the request is anonymous ([user None] in the CMS logs) and fails.
    • csrftoken: the CSRF cookie. Its value must also be sent in the X-CSRFToken header. Without it Studio rejects the request with 403 CSRF cookie not set.

Observed

  1. The response is 200 with a new locator, even though userb has no access to course A.
  2. Open course B: the course fails to load, with ItemNotFoundError for that block in the CMS logs, because the unit lists a child that does not exist in B's structure.
  3. Trying to delete the block from the unit raises KeyError.

Control (legacy path)

  1. Turn the flag authz.enable_course_authoring off. With the flag off, authz roles are ignored, so give userb the legacy Staff role in course B (from the course team page) to pass the destination check. Make sure userb still has no legacy role in A (no direct, org-level or global role), and repeat step 7. The request returns 403, because has_studio_read_access on course A fails (view_handlers.py:362-366).

Acceptance Criteria

  • The duplicated block key is built in the parent's course (or library), not in the source's.
  • Duplicating requires view access on the source course: courses.view_course when AuthZ is enabled for it, has_studio_read_access otherwise (including v1 libraries, which AuthZ does not cover).
  • Write access on the destination is still required.
  • A user without view access on the source gets 403, and no block is created in the source or the destination.
  • Cross-course duplication by a user with the required access creates the block in the destination course, which loads and can be deleted, and leaves the source unchanged.
  • Duplicating a v1 library block into a course creates the copy in the course.
  • Same-course duplication keeps working with and without AuthZ.
  • Tests are added or updated.

Proposed Solution

  • In duplicate_block (cms/djangoapps/contentstore/utils.py), build dest_usage_key from the parent's course or library instead of the source location.
  • In the xblock handler (xblock_storage_handlers/view_handlers.py), add _check_duplicate_source_view_access, which checks courses.view_course on the source when AuthZ is enabled for it, and has_studio_read_access otherwise.
  • Keep the existing write-access check on the destination.

Fix: openedx/openedx-platform#39409

Activity

  1. added theissue type on Nov 5, 2026
  2. added
    willow
    on Nov 5, 2026
  3. moved this to Ready for Development in RBAC AuthZ Boardon Nov 5, 2026
  4. self-assigned this
    on Nov 11, 2026
  5. moved this from Ready for Development to In Progress in RBAC AuthZ Boardon Nov 19, 2026
  6. changed the title [-]Reject duplicating XBlocks across courses[/-] [+]Duplicating XBlocks across courses creates the block in the source course[/+] on Nov 20, 2026
  7. moved this from In Progress to In Review in RBAC AuthZ Boardon Nov 20, 2026
  8. moved this from In Review to Done in RBAC AuthZ Boardon Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

willow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions