Skip to content

Warn when extra_field_kwargs includes fields explicitly declared related to #3460) - #8228

Closed
Jorl17 wants to merge 2 commits into
encode:masterfrom
Jorl17:master
Closed

Jorl17 wants to merge 2 commits into
encode:masterfrom
Jorl17:master

Conversation

@Jorl17

@Jorl17 Jorl17 commented Oct 28, 2021 •

Copy link
Copy Markdown

This PR adds an invariant check via a loud assert (as suggested here) to help with issue #3460.

The goal is to notify people when they include fields in extra_kwargs (either directly or, more worryingly because of security, via read_only_fields). For example, I've seen this time and time again in production code:

class MySerializer(ModelSerializer):
    my_sensitive_field = ...
    ...
    class Meta:
        fields = ('my_sensitive_field', ...)
        read_only_fields = ('my_sensitive_field', )

I believe most people assume that my_sensitive_field is now readonly, without knowing that extra_kwargs, and, hence, read_only_fields, only apply to fields not explicitly declared.

@Jorl17

Jorl17 commented Oct 29, 2021

Copy link
Copy Markdown
Author

The PR has a pretty serious oversight and bug. Please allow me some time to fix it. I will update the PR once it is corrected.

@Jorl17

Jorl17 commented Oct 29, 2021

Copy link
Copy Markdown
Author

The latest commit fixes the oversight, but two tests are still failing. I'd like help debugging and fixing these, please.

@stale

stale Bot commented Apr 18, 2022

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Apr 18, 2022
@auvipy
auvipy self-requested a review November 27, 2022 12:56

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but this need to pass the build first and then additional review from more experienced maintainers. we should fix the failing tests first

@samuelchen

Copy link
Copy Markdown

as @auvipy refrered from #3460 , I am facing the issue even the field is not explicitly declared.

e.g. I can update fields in read_only_fields, such as username, is_superuser and etc.

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserModel
        fields = ['id', 'username', 'first_name', 'last_name', 'password',
                  'is_active', 'is_superuser', 'is_staff', 'date_joined',
                  'groups', 'user_permissions']
        read_only_fields = ['username' 'is_superuser', 'is_staff', 'groups',
                            'user_permissions', 'date_joined']
        depth = 1
        extra_kwargs = {'password': {'write_only': True}}
class UserViewSet(mixins.ListModelMixin, mixins.CreateModelMixin,
                  mixins.RetrieveModelMixin, mixins.UpdateModelMixin,
                  mixins.DestroyModelMixin,
                  viewsets.GenericViewSet):
    queryset = UserModel.objects.all()
    serializer_class = UserSerializer
    # permission_classes = [IsAuthenticated]

    def get_queryset(self):
        if self.request.user.is_staff:
            return super().get_queryset()
        else:
            return super().get_queryset().filter(id=self.request.user.id)

    def get_permissions(self):
        if self.action in ['create']:
            permission_classes = [IsAdminUser]
            return [perm() for perm in permission_classes]
        
        # ['list', 'retrieve', 'update', 'partial_udpate', 'destroy']
        return super().get_permissions()

@stale stale Bot closed this Dec 31, 2022
@auvipy auvipy reopened this Jan 3, 2023
@auvipy auvipy removed the stale label Jan 3, 2023
@stale

stale Bot commented Mar 20, 2023

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Mar 20, 2023

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please rebase this once again please to stabilize the CI?

@stale stale Bot removed the stale label Mar 20, 2023
@auvipy

auvipy commented Mar 20, 2023

Copy link
Copy Markdown
Collaborator

a rebase should make the CI green as the attr library related issue was fixed

@stale

stale Bot commented May 21, 2023

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants