Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate uuid id/pk #1514

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

validate uuid id/pk #1514

wants to merge 1 commit into from

Conversation

akihikokuroda
Copy link
Collaborator

Summary

Validate UUID used as the id/pk for the model instance

Details and comments

Copy link
Member

@Tansito Tansito left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, @akihikokuroda . You don't need to validate perse the UUID format. This is something that django is doing in its ORM. The problem is that we are not capturing the exception for that. The idea is to add a try/except in the queries where we are using the UUID.

@Tansito Tansito requested review from a team and removed request for Tansito and psschwei October 10, 2024 16:30
@akihikokuroda
Copy link
Collaborator Author

These changes check the uuid before django validates it. So it stops raising exceptions and returns 404 for the requests.

@Tansito
Copy link
Member

Tansito commented Oct 10, 2024

These changes check the uuid before django validates it. So it stops raising exceptions and returns 404 for the requests.

I know, I know. But we can rely on Django validation perfectly, we just need to capture the exception that it raises. We don't need to create our own validator for that.

@akihikokuroda
Copy link
Collaborator Author

I believe that the django validation happens when it saves to or retrieves from the database. It's much cleaner and safer if we validate it at the entry of the apis.

@Tansito
Copy link
Member

Tansito commented Oct 10, 2024

My proposal @akihikokuroda :

  • We need to continue handling the exception in the queries anyway because we are not doing that right now. As the issue was proposing.
  • I don't see bad to add additional checks but not through custom checks. We can use UUID utility from python.
  • Move the logic to utils and reuse it in those specific points.

@akihikokuroda
Copy link
Collaborator Author

handling the exception in the queries

Are the queries database query? Where do you propose to put the try clause?

The UUID utility seems for generating an UUID. I don't see the method for validating it.

@Tansito
Copy link
Member

Tansito commented Oct 10, 2024

Are the queries database query? Where do you propose to put the try clause?

It happens in every find, filter, etc that uses the id to search for something, example here.

You just need to encapsulate that call in a try/except, and in this case, return None is fine for example.

The UUID utility seems for generating an UUID. I don't see the method for validating it.

If you try to create an UUID with a value that is not a valid UUID it will raise an exception that you can capture similar to what you were doing with the pattern. If you see that that logic doesn't fit correctly I'm fine looking for alternatives @akihikokuroda like what you did with the pattern, but let's try something that it just exists first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants