fix: When other users download the public model, they cannot obtain the download data#2785
fix: When other users download the public model, they cannot obtain the download data#2785shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| raise Exception(_('No permission to use this model') + f"{model.name}") | ||
| model = QuerySet(Model).get(id=self.data.get('id'), user_id=self.data.get('user_id')) | ||
| return {'id': str(model.id), 'provider': model.provider, 'name': model.name, 'model_type': model.model_type, | ||
| 'model_name': model.model_name, |
There was a problem hiding this comment.
The provided code snippet has several issues and opportunities for improvement:
- Redundancy: The
one_metamethod callsself.is_valid()multiple times. This can lead to unnecessary validation checks during each call. - Security Risk: If
with_valid=False, there's no safety margin against unauthorized access since the data is directly retrieved fromself.data. - Duplicate Code: The
if model_permission_type...logic is duplicated at two different points (both checking for existence and permissions). - Error Handling: The error messages are missing when raising exceptions, which could make debugging difficult.
- Code Duplication: The retrieval of the model after checking its validity should be moved to a more reusable function.
Here’s a revised version with improvements:
from django.core.exceptions import ObjectDoesNotExist
from .exceptions import AppApiException
class OneMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def one_meta(self, with_valid=False):
if with_valid:
self.check_validity(raise_exception=True)
# Ensure that the model exists first
try:
model = Model.objects.filter(id=self.data.get("id")).get()
except ObjectDoesNotExist:
raise AppApiException(500, _('Model does not exist'))
# Check permission type
if model.permission_type == 'PRIVATE':
current_user_id = str(get_current_user_id()) # Assuming get_current_user_id() returns the authenticated user ID
if current_user_id != str(model.user_id):
raise Exception(_('No permission to use this model') + f" {model.name}")
return self.get_model()
def check_validity(self, raise_exception=False):
# Implement your validation logic here
pass
def get_model(self):
try:
model = Model.objects.get(
id=int(self.data.get('id')),
user_id=get_current_user_id())
except IntegrityError as e:
raise AppApiException(500, _('Database integrity error')) from e
except ValueError as e:
raise AppApiException(400, _('Invalid input')) from e
else:
return {'id': str(model.id), 'provider': model.provider, 'name': model.name,
'model_type': model.model_type, 'model_name': model.model_name}Key Improvements:
- Simplified Validation Logic: Removed redundant
is_valid()calls by delegating validation to a separatecheck_validitymethod. - Check for Existence Early: Ensured that the model exists before proceeding with the permission check.
- Reused Functionality: Moved the duplicate code around the model fetching process into the
get_modelmethod for better readability and maintainability. - Added Error Messages: Included detailed error messages in exception instances where necessary.
- User Authentication: Used a placeholder function
get_current_user_id()assuming it retrieves the currently authenticated user's ID. You need to replace this with an actual implementation based on your authentication system.
fix: When other users download the public model, they cannot obtain the download data