Opened 4 years ago

Closed 4 years ago

#31982 closed Cleanup/optimization (fixed)

Convert max_age to an int in set_cookie()

Reported by: Matt Johnson Owned by: Hasan Ramezani
Component: HTTP handling Version: 3.1
Severity: Normal Keywords:
Cc: Florian Apolloner, Adam Johnson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The max-age attribute of cookie is supposed to be an integer

https://tools.ietf.org/html/rfc6265#page-20

I think it would be helpful to convert the max_age parameter of set_cookie() to an integer for the user. The benefit is simply that there are some cookie parsers that don't handle decimals gracefully. It's pretty easy to pass in a float without understanding the consequences. I spent a good chunk of time today trying to track down the problem.

Things to consider:

  1. Do we only convert floats where the decimal part is 0? Or do we round or truncate?
  2. If we can't successfully convert to an int, do we throw an exception, or just pass in the original value?

Change History (6)

in reply to:  description comment:1 by Mariusz Felisiak, 4 years ago

Cc: Florian Apolloner Adam Johnson added
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Agreed, we should also mention this in docs, e.g.:

max_age should be an integer number of seconds, ...

Replying to Matt Johnson:

  1. Do we only convert floats where the decimal part is 0? Or do we round or truncate?
  2. If we can't successfully convert to an int, do we throw an exception, or just pass in the original value?

I would simply call int(max_age) without catching exceptions.

comment:2 by Adam Johnson, 4 years ago

+1 to calling int()

comment:3 by Hasan Ramezani, 4 years ago

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Mariusz Felisiak, 4 years ago

Needs tests: set

comment:5 by Mariusz Felisiak, 4 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In d2d08c8:

Fixed #31982 -- Made HttpResponse.set_cookie() cast max_age argument to an integer.

Note: See TracTickets for help on using tickets.
Back to Top