Repository navigation
Conversation
…ce to redirect to a 404 page
| raise NotFound404(404, _("Model does not exist")) | ||
| if model_params_form is None: | ||
| model_params_form = [] | ||
| if not isinstance(model_params_form, list): |
There was a problem hiding this comment.
Here is a concise analysis of the provided code patch:
1. Potential Issues
-
Performance (N+1/Redundant Query): In
is_valid, even ifworkspace_idmatches, you do not check authorization. However, in the block where they *don't match, you re-query the database viaget_authorized_model. This is acceptable for security but inefficient if called frequently. Consider caching or batching if this is a hot path. -
Duplicate Logic & Bug Risk: In
save_model_params_form, you callself.is_valid(raise_exception=True)which already validates existence and potentially checks authorization. Immediately after, you add another check:if model.workspace_id != self.validated_data.get("workspace_id"): ...
This duplicate check is redundant because
is_validwas just called withraise_exception=True. Ifis_validsucceeded, it means either:workspace_idmatched directly (so this new check passes).- The
get_authorized_modellogic passed (meaning the user is authorized for that workspace), so accessing it viamodel.workspace_iddirectly should be allowed.
Crucial Flaw: If the model is shared/authorized from another workspace (handled in
is_valid), thenmodel.workspace_idWILL be different fromvalidated_data['workspace_id']. Therefore, this new line insave_model_params_formwill always raise a 404 for valid shared models, breaking the feature implemented inis_valid.
2. Irregularities
- Comment Language Mismatch: The code contains Chinese comments (
# 共享模型属于其它工作空间...). Ensure consistency in team/codebase language policy. - Hardcoded Error Messages: Using
_("Model does not exist")for both non-existence and unauthorized access is good for security (prevents enumeration), but ensure translations are updated for the new behavior if needed.
3. Optimization Suggestions
- Fix the Redundant/Broken Check in
save_model_params_form: Remove the newly added workspace check insave_model_params_form. The validation inis_validis sufficient and correct. The extra check contradicts the "shared model" logic. - Refactor Authorization Check: Extract the authorization logic into a reusable helper if it's used elsewhere.
- Null Safety:
self.validated_data.get("workspace_id")might returnNone. Insave_model_params_form, ifworkspace_idisNone, comparingmodel.workspace_id != Nonemight raise unexpected errors depending onmodel.workspace_id's type. Use explicit checks:req_ws = self.validated_data.get("workspace_id") if req_ws is not None and model.workspace_id != req_ws: # But again, remove this block entirely as it breaks shared models. pass
Recommended Fix
In save_model_params_form, remove the added lines:
- if model.workspace_id != self.validated_data.get("workspace_id"):
- raise NotFound404(404, _("Model does not exist"))Because is_valid() already ensures the user has access to the model in the context of the requested workspace_id, whether it's owned by that workspace or shared/authored there.
fix: selecting 'share model' within an application causes the interface to redirect to a 404 page