Skip to content

Harden the questionnaire template delete preview - #17099

Open
svader0 wants to merge 2 commits into
DefectDojo:devfrom
svader0:harden-questionnaire-delete-preview
Open

svader0 wants to merge 2 commits into
DefectDojo:devfrom
svader0:harden-questionnaire-delete-preview

Conversation

@svader0

@svader0 svader0 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Makes the questionnaire template delete page use the same engagement permission checks as the other questionnaire pages. If a user cannot view some of the engagements tied to the template, the page now hides the relationship preview. Deleting the template still works the same way.

Adds a regression test.

Apply the same engagement permission checks to the template delete
preview that the other questionnaire pages already use, and add a
regression test. The delete itself is unchanged.

@Maffooch Maffooch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review

Tier: full (backend authorization change to a server-rendered view). Model: Opus. Reviewed head 39553e83f5.

CI read: all checks green on the head commit: ruff, Migration Graph Check, both Rest Framework Unit Tests jobs (9247 tests run, OK), every User Interface Tests group, k8s, performance, docker builds. No migration, no API shape change.

Regression test: checked statically only; no Docker daemon was available to this pass, so the test was not run against dev. On dev the view runs NestedObjects.collect([survey]) unconditionally when DELETE_PREVIEW is on, and TextAnswer.__str__ returns the answer text, so HIDDEN_ANSWER is in the page and test_preview_hides_answers_of_engagements_the_user_cannot_view fails there. With the fix it passes (CI). test_preview_lists_answers_when_the_user_can_view_every_engagement runs with the same settings and proves the hidden case is not passing only because the preview is off.

What I drove: nothing in a browser. This is a classic Django template page and no stack was available. Instead I read the view, the template (rels|length > 1 -> unordered_list), the URL permission (delete_questionnaire -> config dojo.delete_engagement_survey), the OSS engagement.get_authorized_engagements filter (product authorized_users, with superuser and staff bypasses), and how the other questionnaire views check engagements (view_empty_survey and view_questionnaire check view, the delete views check edit).

Break attempts

  1. Partial visibility: a user who can view one of two engagements. The whole preview is hidden, the user's own answers included, and the new message renders in its place. That is all-or-nothing by design, and the test asserts it. The delete still cascades as before.
  2. Answers with no engagement (general or unassigned responses, engagement=None): the engagement__isnull=False filter leaves these out of the check, so their answers still show. That matches view_empty_survey, which gates engagement-less responses only on the questionnaire config permission. No leak beyond what that page already shows.
  3. Superuser, staff and anonymous: the OSS filter returns every engagement for superusers and staff, so the preview shows (the test covers superuser). An anonymous user gets none(), so the preview hides, and the URL permission refuses anonymous users first anyway. A plugin that overrides the same registry key goes through the same call, so the check follows whatever RBAC is registered.
  4. Other delete-preview surfaces for the same data: no /api/v2 viewset exposes a delete preview for Engagement_Survey (delete_preview lives only in api_v2/mixins.py and is not mixed into a questionnaire viewset). delete_engagement_survey, delete_empty_questionnaire and delete_general_questionnaire render answers only after an engagement check, or render none. No site was left unpatched.
  5. Scale: the check adds one EXISTS over the template's answered surveys with a NOT IN (authorized engagements) subquery. The outer set is one template's responses (small), and the subquery scales with the number of engagements, not findings. That is acceptable on an infrequent admin delete page. The existing NestedObjects walk is still the expensive part when the preview shows.
  6. The tests themselves: they go through the real URL, login and permission middleware, not a mock. They use the real cascade on POST. The negative test checks only that both answers are absent, not that the page says why (R1).

Screenshots: none. Server-rendered page, no stack in this pass.

Findings

  • R1 nit this-pr unittests/test_questionnaire_template_delete_preview.py:59: the hidden-preview test asserts only that both answers are absent, so it would also pass if the page dropped the relationship list for any other reason. Also assert that the new explanation ("The preview is hidden because some answers to this questionnaire belong to engagements you cannot view.") is in the page, which pins the user-visible behaviour this PR adds.

Recommendation

changes required: 1 open this-pr finding (0 blocker, 0 suggestion, 1 nit). The fix is correct, minimal, and consistent with the engagement checks on the other questionnaire views.

Checked only statically: whether the regression test fails on dev (reasoned from the code, not run), and the rendered page (no browser pass).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Maffooch commented Dec 4, 2026

Copy link
Copy Markdown
Contributor
  • R1: fixed in 52a7e98. test_preview_hides_answers_of_engagements_the_user_cannot_view now also asserts the explanation ("The preview is hidden because some answers to this questionnaire belong to engagements you cannot view.") is on the page, and test_preview_lists_answers_when_the_user_can_view_every_engagement asserts it is absent. Not run locally (no Django stack here); CI runs it on this push.

@Maffooch Maffooch added this to the 3.6.0 milestone Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants