IntelOwl icon indicating copy to clipboard operation
IntelOwl copied to clipboard

bug: SetField merge with append=True introduces duplicate values

Open deepak0x opened this issue 1 month ago • 3 comments

Name

SetField merge with append=True introduces duplicate values

Analyzers

No response

Connectors

No response

Runtime configuration

File: api_app/data_model_manager/models.py, line 146 Commit: 862df0a0

The merge() method iterates over all model fields and, for any ArrayField, extends the result list with values from the other instance:

if isinstance(field, ArrayField):
    if not result_attr:
        result_attr = []
    result_attr.extend(other_attr)

Permalink to buggy code

SetField is a subclass of ArrayField. The isinstance(field, ArrayField) check at line 146 therefore matches SetField fields. The extend() call appends all elements from other_attr with no deduplication.

SetField's uniqueness enforcement lives in to_python(), which Django calls only during form validation — not during setattr + save(). PostgreSQL's ArrayField does not enforce uniqueness at the database level either. Duplicates produced by extend() are therefore persisted as-is.

Use case

When two model instances share a value in a SetField column (e.g., both IPDataModel records have '1.1.1.1' in their resolutions field) and merge(append=True) is called, the merged result contains duplicates: ['1.1.1.1', '1.1.1.1', '2.2.2.2'] instead of ['1.1.1.1', '2.2.2.2']. This violates SetField's uniqueness contract and the duplicate values are persisted to the database.

Steps to reproduce:

  1. Create two IPDataModel instances with overlapping resolutions values — e.g., both containing '1.1.1.1'.
  2. Call merge(append=True) on the first instance.
  3. Call refresh_from_db() on the first instance.
  4. Inspect len(resolutions) — it is 3 instead of the expected 2.

Reproducing test

Add to tests/api_app/data_model_manager/test_models.py. This test fails on the current code and is proof the bug is real:

def test_merge_set_field_no_duplicates(self):
    ip1 = IPDataModel.objects.create(resolutions=['1.1.1.1'])
    ip2 = IPDataModel.objects.create(resolutions=['1.1.1.1', '2.2.2.2'])
    ip1.merge(ip2, append=True)
    ip1.refresh_from_db()
    # SetField must preserve uniqueness: ['1.1.1.1', '2.2.2.2'], NOT ['1.1.1.1', '1.1.1.1', '2.2.2.2']
    self.assertEqual(len(ip1.resolutions), 2)  # FAILS on buggy code: actual len is 3
    self.assertCountEqual(ip1.resolutions, ['1.1.1.1', '2.2.2.2'])
    ip1.delete()
    ip2.delete()

deepak0x avatar Oct 06 '26 15:10 deepak0x

Hi @mlodic, I would like to work on this issue as a first-time contributor. Could you please assign it to me?

Izhaan60 avatar Oct 07 '26 20:10 Izhaan60

I have checked and confirmed that the bug is real.

Root Cause

  1. In api_app/data_model_manager/fields.py, SetField inherits directly from ArrayField (class SetField(ArrayField)).
  2. In api_app/data_model_manager/models.py, merge() checks isinstance(field, ArrayField) at line 147 before isinstance(field, (JSONField, SetField)) at line 151.
  3. Because SetField is a subclass of ArrayField, isinstance(field, ArrayField) evaluates to True, so result_attr.extend(other_attr) executes without deduplication, and the SetField branch is never reached.
  4. SetField deduplication (to_python) is only called during Django form validation, so duplicate list elements are persisted directly to the database. @mlodic i would like to work on this issue please assign it to me

anshulsharma200817-svg avatar Oct 10 '26 04:10 anshulsharma200817-svg

This issue has been marked as stale because it has had no activity for 30 days. If you are still working on this, please provide some updates.

github-actions[bot] avatar Nov 09 '26 19:11 github-actions[bot]