Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add semgrep rule that protects against mistakes when registering (#18970)
Marcin Wcisłod9a482b8e613
diff --git a/.gitignore b/.gitignoreindex b9dd0d0318..611d64b23e 100644--- a/.gitignore+++ b/.gitignore@@ -5,6 +5,7 @@!.github!.gitignore!.vscode+!.semgrep/*.log*.pot*.pycdiff --git a/.semgrep/correctness/django/django-migration-wrong-app-config.py b/.semgrep/correctness/django/django-migration-wrong-app-config.pynew file mode 100644index 0000000000..df5f7db1b7--- /dev/null+++ b/.semgrep/correctness/django/django-migration-wrong-app-config.py@@ -0,0 +1,54 @@+from django.apps import apps as registry+from django.db import migrations+from django.db.models.signals import post_migrate+++def wrong_app_config(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ pass++ # ruleid: django-migration-wrong-app-config+ sender = registry.get_app_config("attribute")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++def correct_app_config(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ pass++ # ok: django-migration-wrong-app-config+ sender = registry.get_app_config("order")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++def wrong_app_config_multiline(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ pass++ # ruleid: django-migration-wrong-app-config+ sender = registry.get_app_config(+ "attribute"+ )+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++def correct_app_config_multiline(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ pass++ # ok: django-migration-wrong-app-config+ sender = registry.get_app_config(+ "order"+ )+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++class Migration(migrations.Migration):+ dependencies = [+ ("order", "0219_merge"),+ ]++ operations = [+ migrations.RunPython(wrong_app_config, migrations.RunPython.noop),+ migrations.RunPython(correct_app_config, migrations.RunPython.noop),+ ]diff --git a/.semgrep/correctness/django/django-migration-wrong-app-config.yaml b/.semgrep/correctness/django/django-migration-wrong-app-config.yamlnew file mode 100644index 0000000000..b26b249d94--- /dev/null+++ b/.semgrep/correctness/django/django-migration-wrong-app-config.yaml@@ -0,0 +1,44 @@+rules:+ - id: django-migration-wrong-app-config+ message: |+ registry.get_app_config("$APP") does not match the first app in Migration.dependencies.+ The argument must be the app label of the migration's own app.+ Nature of semgrep and how it can verify this rule it's expected that the first tuple in Migration.dependencies+ will be dependency to a previous migration from the same app.+ If dependency to a previous migration from the same app is not required it's fine to ignore this semgrep rule.+ Fix: change "$APP" to match the app name where the migration is.+ metadata:+ category: correctness+ technology:+ - django+ languages:+ - python+ severity: ERROR+ paths:+ include:+ - "**/migrations/*.py"+ patterns:+ - pattern: |+ $SENDER = registry.get_app_config("$APP")+ - pattern-not-inside: |+ ...+ $SENDER = registry.get_app_config("$APP")+ ...+ class Migration(...):+ dependencies = [+ ("$APP", ...),+ ...,+ ]+ ...+ ...+ - pattern-not-inside: |+ ...+ class Migration(...):+ dependencies = [+ ("$APP", ...),+ ...,+ ]+ ...+ ...+ $SENDER = registry.get_app_config("$APP")+ ...diff --git a/saleor/account/migrations/0095_repopulate_user_number_of_orders.py b/saleor/account/migrations/0095_repopulate_user_number_of_orders.pyindex 72e0dd24dc..661ffa429a 100644--- a/saleor/account/migrations/0095_repopulate_user_number_of_orders.py+++ b/saleor/account/migrations/0095_repopulate_user_number_of_orders.py@@ -11,7 +11,7 @@ def populate_user_number_of_orders(apps, _schema_editor):def on_migrations_complete(sender=None, **kwargs):populate_user_number_of_orders_task.delay()- sender = registry.get_app_config("order")+ sender = registry.get_app_config("account")post_migrate.connect(on_migrations_complete, weak=False, sender=sender)diff --git a/saleor/channel/migrations/0001_initial.py b/saleor/channel/migrations/0001_initial.pyindex 94e6b08331..febc62bc69 100644--- a/saleor/channel/migrations/0001_initial.py+++ b/saleor/channel/migrations/0001_initial.py@@ -28,6 +28,7 @@ def assign_permissions(apps, schema_editor):for group in Group.objects.iterator():group.permissions.add(manage_channels)+ # nosemgrep: django-migration-wrong-app-configsender = registry.get_app_config("channel")post_migrate.connect(on_migrations_complete, weak=False, sender=sender)diff --git a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.pyindex b7b87d55e2..936abe2905 100644--- a/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py+++ b/saleor/checkout/migrations/0040_add_handle_checkouts_permission.py@@ -43,9 +43,9 @@ def assign_permissions(apps, schema_editor):class Migration(migrations.Migration):dependencies = [+ ("checkout", "0039_alter_checkout_email"),("product", "0159_auto_20220209_1501"),("order", "0133_rename_order_token_id"),- ("checkout", "0039_alter_checkout_email"),]operations = [diff --git a/saleor/checkout/migrations/0088_reapply_fix_shared_address_instances.py b/saleor/checkout/migrations/0088_reapply_fix_shared_address_instances.pyindex 4c8c8147d4..7bcd8305be 100644--- a/saleor/checkout/migrations/0088_reapply_fix_shared_address_instances.py+++ b/saleor/checkout/migrations/0088_reapply_fix_shared_address_instances.py@@ -11,7 +11,7 @@ def fix_shared_address_instances(apps, schema_editor):def on_migrations_complete(sender=None, **kwargs):fix_shared_address_instances_task.delay()- sender = registry.get_app_config("order")+ sender = registry.get_app_config("checkout")post_migrate.connect(on_migrations_complete, weak=False, sender=sender)diff --git a/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.py b/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.pyindex a28d8a16da..1f2aa81aa7 100644--- a/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.py+++ b/saleor/giftcard/migrations/0023_mark_gift_cards_search_vector_as_dirty.py@@ -11,7 +11,7 @@ def mark_gift_cards_search_index_as_dirty(apps, _schema_editor):def on_migrations_complete(sender=None, **kwargs):mark_gift_cards_search_index_as_dirty_task.delay()- sender = registry.get_app_config("attribute")+ sender = registry.get_app_config("giftcard")post_migrate.connect(on_migrations_complete, weak=False, sender=sender)diff --git a/saleor/order/migrations/0198_clean_duplicated_gift_lines.py b/saleor/order/migrations/0198_clean_duplicated_gift_lines.pyindex 00493556f4..dcdb310fea 100644--- a/saleor/order/migrations/0198_clean_duplicated_gift_lines.py+++ b/saleor/order/migrations/0198_clean_duplicated_gift_lines.py@@ -11,7 +11,7 @@ def clean_duplicated_gift_lines(apps, _schema_editor):def on_migrations_complete(sender=None, **kwargs):clean_duplicated_gift_lines_task.delay()- sender = registry.get_app_config("checkout")+ sender = registry.get_app_config("order")post_migrate.connect(on_migrations_complete, weak=False, sender=sender)