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

Improve views query #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

nnsnodnb
Copy link

  • Update GitHub Actions' steps version.
  • Update query and syntax in passkey/views.py.

key=UserPasskey.objects.get(id=request.GET["id"])
if key.user.pk == request.user.pk:
key.delete()
return HttpResponse("Deleted Successfully")
return HttpResponse("Error: You own this token so you can't delete it", status=403)

I think there is a risk that Passkey ID on database will be leaked.
If you are checking user matches, I think it would be a good idea to retrieve it as a query and set it as Not Found.

id=request.GET["id"]

id is reserved as a built-in function.

$ python
>>> id
<built-in function id>

q=UserPasskey.objects.filter(user=request.user, id=id)
if q.count()==1:
key=q[0]
key.enabled=not key.enabled

if q.count()==1:  # a query
    key=q[0]  # a query
    # follow code...

This code generates the query twice.
Also, here we only have one matching data, so we can improve it by using first().

return HttpResponse("Error: You own this token so you can't toggle it", status=403)

django.http.response provides HttpResponseForbidden and the status is set to 403.

return HttpResponse("OK")
return HttpResponse("Error: You own this token so you can't toggle it", status=403)
return HttpResponseForbidden("Error: You own this token so you can't toggle it")

Choose a reason for hiding this comment

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

I think this should be "You don't own [...]".

@rafaelurben rafaelurben mentioned this pull request Sep 3, 2024
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