Skip to content

Keyword CheckConstraint always evaluates to True, allowing empty keywords #556

Description

@lvgl-oh

The following CheckConstraint is supposed to guarantee that keywords are neither null nor the empty string. Instead it checks wether at least one of these conditions is true, which is always the case.

models.CheckConstraint(
condition=~Q(keyword="") | ~Q(keyword__isnull=True),
name="%(app_label)s_%(class)s_non_empty_keywords",
)

The correct constraint would look like this:

models.CheckConstraint( 
    condition=~Q(keyword="") & Q(keyword__isnull=False), 
    name="%(app_label)s_%(class)s_non_empty_keywords", 
)

I propose also checking that strings contain at least one non-whitespace character:

models.CheckConstraint(
    condition=Q(keyword__regex=r"\S") & Q(keyword__isnull=False),
    name="%(app_label)s_%(class)s_non_empty_keywords",
)

The current codebase doesn't sanitize keywords. Changing this right now would fail at least one test (tests.django.registry.tasks.tests_service.BuildOgcServiceTaskTest.test_success_with_collect_metadata_true) and prevent services with empty keywords from being registered.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions