Skip to main content

SKY-D234 · Security in code

Form or serializer accepts every model field

Mass assignment means copying every field a client sends straight onto a database record. In Django and Django REST Framework, fields = "__all__" in a form's or serializer's Meta class does exactly that. SKY-D234 reports that line.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Catalog name
Mass assignment
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The client chooses which fields to send. A user editing their own profile can add "is_staff": true or "is_superuser": true to the request and give themselves admin rights, or change another record's owner.

What Skylos flags

accounts/api.py · PythonReported on line 8
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers  class ProfileSerializer(serializers.ModelSerializer):    class Meta:        model = User        fields = "__all__"  class ProfileView(generics.RetrieveUpdateAPIView):    serializer_class = ProfileSerializer    permission_classes = [permissions.IsAuthenticated]     def get_object(self):        return self.request.user

What Skylos leaves alone

accounts/api.py · Python
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers  class ProfileSerializer(serializers.ModelSerializer):    class Meta:        model = User        fields = ["id", "username", "first_name", "last_name", "email", "is_staff"]        read_only_fields = ["id", "username", "is_staff"]  class ProfileView(generics.RetrieveUpdateAPIView):    serializer_class = ProfileSerializer    permission_classes = [permissions.IsAuthenticated]     def get_object(self):        return self.request.user

Why Skylos leaves it alone

The serializer names its fields one by one, and is_staff is shown but read-only, so a request cannot change it. Only the literal string "__all__" is reported.

More languages

Python

Flagged

shop/forms.py · PythonReported on line 9
from django import forms from shop.models import Order  class OrderForm(forms.ModelForm):    class Meta:        model = Order        fields = "__all__"

Not flagged

shop/forms.py · Python
from django import forms from shop.models import Order  class OrderForm(forms.ModelForm):    class Meta:        model = Order        fields = ["shipping_address", "delivery_notes"]

Why Skylos leaves it alone

The customer can change only the address and the notes. Fields such as the price, the status or the owner stay out of the form.

How Skylos decides

  1. Runs with --danger on Python files that contain both fields and __all__.
  2. Looks at every class for a nested class named exactly Meta. Inside it, reports HIGH on a plain assignment fields = "__all__".
  3. The base class is not checked, so a Django ModelForm, a DRF ModelSerializer or any other class with such a Meta is reported. exclude = [...], a list such as fields = ["__all__"], an annotated assignment and other mass-assignment shapes are not.

How to fix it

List the fields a client may send. For a serializer, mark fields the client may see but not change as read_only_fields; keep permission flags, owners and prices out of writable fields.

accounts/api.py · Python · the fix, against the flagged file
from django.contrib.auth.models import Userfrom rest_framework import generics, permissions, serializers  class ProfileSerializer(serializers.ModelSerializer):    class Meta:        model = UserRemoved:         fields = "__all__"Added:         fields = ["id", "username", "first_name", "last_name", "email"]Added:         read_only_fields = ["id", "username"]  class ProfileView(generics.RetrieveUpdateAPIView):    serializer_class = ProfileSerializer    permission_classes = [permissions.IsAuthenticated]     def get_object(self):        return self.request.user

When it can be wrong

  • Output-only uses are reported too: a serializer used only by a read-only list view, a form used only by staff, or a GraphQL type that only displays a model.
  • Missed: exclude = [...], which also lets new model fields through, and other ways to copy request data onto a record, such as Order(**request.POST.dict()), setattr() loops or obj.__dict__.update(data).

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark the fields = "__all__" line and say why every field may be written.

    Python
    fields = "__all__"  # skylos: ignore[SKY-D234] read-only report serializer
  • [tool.skylos] ignore = ["SKY-D234"] turns the rule off for the whole project.

  • skylos baseline . and then --baseline accept the existing serializers and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.