Вы хорошо отметили важность безопасности и корректности данных. Расскажите, как бы вы реализовали фильтрацию по владельцу в Django ORM, чтобы избежать таких утечек?
Asked at
AI answer
I would restrict the queryset to the current user on the server, rather than trusting owner_id supplied by the client. For example:
from django.shortcuts import get_object_or_404
class DocumentDetailView(DetailView):
def get_queryset(self):
return Document.objects.filter(owner=self.request.user)
# In the API:
document = get_object_or_404(
Document.objects.filter(owner=request.user),
pk=document_id,
)
That way, another user's object won't appear in either the list or the detail request. The same restriction should be applied to updates and deletions, including nested resources. When creating an object, the owner should be assigned from request.user, rather than taken from the request body. I would also add tests for access to other users' objects and check all data-access paths, including background tasks and admin/API endpoints.
Also asked at ylab
- Could you tell me about your overall role in this project? Were you fully responsible for it, or were you responsible for a specific part?
- Could you tell about a situation where you made a mistake at work?
- How can you find and analyze slow queries in PostgreSQL?
- 20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. 3. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false
Similar questions
- How would you describe your experience with Django?
- The permission layer is a single point. How did you check permissions when they needed to be checked inside the business logic?
- How to implement table joins using Django ORM?
- How can you access related models or fields through Django ORM?
- When saving a new model in Django, who assigns the unique identifier: the ORM itself or the database?