Code

Opened 3 years ago

Closed 3 years ago

#15637 closed New feature (fixed)

Add a require_safe decorator for views to accept GET or HEAD

Reported by: aaugustin Owned by: nobody
Component: HTTP handling Version: master
Severity: Normal Keywords:
Cc: tomchristie, leidel@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

It is a good practice to reject POST requests on views that are not intended to process them. Django provides the require_GET decorator for this purpose.

Although browsers only implement GET and POST methods, other software such as link checkers uses HEAD requests. Views protected by require_GET will (obviously) reject such requests with "405 Method Not Allowed". I've encountered the problem with the "Check My Links" extension for Google Chrome.

However, RFC2616 says that "The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response." Django already enforces this behavior by stripping the body of the responses to HEAD requests. So I think that in most cases, it makes more sense to accept GET and HEAD than only GET.

That is why I'm proposing a "require_safe" decorator for this purpose.

Attachments (3)

add-require-safe-http-decorator.patch (2.3 KB) - added by aaugustin 3 years ago.
add-require-safe-http-decorator.2.patch (4.7 KB) - added by aaugustin 3 years ago.
15637.require_safe.diff (5.9 KB) - added by julien 3 years ago.
Tweaked doc

Download all attachments as: .zip

Change History (13)

Changed 3 years ago by aaugustin

comment:1 Changed 3 years ago by aaugustin

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by julien

  • Patch needs improvement set

The patch looks good but needs a few improvements. The doc should explain why the decorator is called require_safe (i.e. GET and HEAD methods are commonly referred to as "safe" methods), and the tests should actually check that the decorator actually only allows GET and HEAD requests.

Changed 3 years ago by aaugustin

comment:4 Changed 3 years ago by aaugustin

  • Patch needs improvement unset

Thanks for accepting the feature, julien. Here is a new patch with more details in the documentation and more tests.

comment:5 Changed 3 years ago by tomchristie

  • Cc tomchristie added

comment:6 Changed 3 years ago by lukeplant

  • Type set to New feature

comment:7 Changed 3 years ago by lukeplant

  • Severity set to Normal

Changed 3 years ago by julien

Tweaked doc

comment:8 Changed 3 years ago by julien

  • Triage Stage changed from Accepted to Ready for checkin

Great work, thank you. I've just made a few minor improvements to the doc.

comment:9 Changed 3 years ago by jezdez

  • Cc leidel@… added
  • Easy pickings unset

comment:10 Changed 3 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

In [16115]:

Fixed #15637 -- Added a require_safe decorator for views to accept GET or HEAD. Thanks, aaugustin and Julien.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.