Skip to content

fix(reimport): create one test for concurrent auto-create reimports - #17056

Open
k0nnect wants to merge 1 commit into
DefectDojo:devfrom
k0nnect:reimport-autocreate-race
Open

k0nnect wants to merge 1 commit into
DefectDojo:devfrom
k0nnect:reimport-autocreate-race

Conversation

@k0nnect

@k0nnect k0nnect commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Description

fixes #16807. two reimport-scan requests with auto_create_context=true and the same test_title could each create their own test when none existed yet

  • get_target_test_if_exists() is a plain read, and on a miss the importer creates the test with nothing in between, so two requests that both miss both create one
  • added AutoCreateContextManager.get_or_create_test(), modelled on get_or_create_engagement(): lock the parent engagement row, look for the test again, and only create it if it's still missing
  • the new test is locked for import (the lock from fix(importers): run imports and reimports into one test one at a time #16843) before the transaction commits, so the request that lost the race waits for the first import to finish and then reimports into that test
  • no migration. a unique constraint on engagement / title / scan type isn't an option, since import-scan creates a new test every time on purpose

Test results

reproduced over http on dev with postgres, firing pairs of simultaneous reimport-scan requests (same product, engagement and test_title):

code scenario rounds with duplicate tests
dev zap scan, 2 parallel 19 of 20
this pr zap scan, 2 parallel 0 of 20
this pr zap scan, 4 parallel 0 of 10
this pr generic findings import, 2 parallel 0 of 10

new unittests/test_reimport_auto_create_test_race.py: 5 tests using two threads on separate connections, ordered with a barrier and events rather than sleeps. all 5 fail with the source change reverted and pass with it

also ran the existing import / reimport modules (706 tests, 0 failures) with locations on and off. ruff 0.16.10 is clean and makemigrations --check reports no changes

not covered here:

  • api v3 auto_import_scan has the same race. left out to keep this to the reported path
  • with DD_SERIALIZE_TEST_IMPORTS=False the second request isn't held back
  • there's a separate existing race on product auto-create that can return 400 name: Product with this Name already exists. not touched, can open an issue for it if useful

Documentation

no docs change needed

Checklist

  • Make sure to rebase your PR against the very latest dev.
  • Submit all PRs, features and bug fixes alike, against the dev branch.
  • Give a meaningful name to your PR, as it may end up being used in the release notes.
  • Your code is Ruff compliant (see ruff.toml).
  • Your code is python 3.13 compliant.
  • If this is a new feature and not a bug fix, you've included the proper documentation in the docs at https://github.com/DefectDojo/django-DefectDojo/tree/dev/docs as part of this PR.
  • Model changes must include the necessary migrations in the dojo/db_migrations folder.
  • Add applicable tests to the unit tests.
  • Add the proper label to categorize your PR.

two reimport-scan requests with auto_create_context and the same test_title
could both miss the lookup and each create a test.

add AutoCreateContextManager.get_or_create_test(), which locks the parent
engagement row and looks for the test again before creating it. the new
test is locked for import before the transaction commits, so the request
that lost the race waits and then reimports into it.
@dryrunsecurity

Copy link
Copy Markdown

DryRun Security

This pull request contains critical security findings because the user 'k0nnect' modified sensitive codepaths in dojo/importers/auto_create_context.py and dojo/api_v2/serializers.py without being on the allowed authors list.

🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/importers/auto_create_context.py (drs_0b089f6e)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/importers/auto_create_context.py' matches configured sensitive codepath pattern 'dojo/importers/*.py' and was modified by 'k0nnect' (commit 758b903) who is not in the allowed authors list.
🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/api_v2/serializers.py (drs_b58b48c3)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/api_v2/serializers.py' matches configured sensitive codepath pattern 'dojo/api_v2/*.py' and was modified by 'k0nnect' (commit 758b903) who is not in the allowed authors list.

We've notified @mtesauro.


Comment to provide feedback on these findings.

Report false positive: @dryrunsecurity fp [FINDING ID] [FEEDBACK]
Report low-impact: @dryrunsecurity nit [FINDING ID] [FEEDBACK]

Example: @dryrunsecurity fp drs_90eda195 This code is not user-facing

All finding details can be found in the DryRun Security Dashboard.

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.

1 participant