drf-extensions icon indicating copy to clipboard operation
drf-extensions copied to clipboard

is_if_none_match_failed: check for unquoted etag in list of quoted etags.

Open jkeyes opened this issue 7 years ago • 3 comments

Hey folks :wave:

I ran into an issue using a local copy of drf-extensions, and I was just going to suggest a fix when I found #197 had fixed the issue by stripping quotes from etags:

if res_etag and if_none_match:
    etags = [etag.strip('"') for etag in etags]
    return res_etag in etags or '*' in etags

I was going to suggest this, which is a little bit easier to read IMO:

if res_etag and if_none_match:
    return quote_etag(res_etag) in etags or '*' in etags

I presume the fix from #197 should also have been applied to is_if_match_failed too.

jkeyes avatar Jan 19 '18 11:01 jkeyes

I agree with your suggestion to fix is_if_match_failed. I spent hours having "Precondition Failed" on a PUT request. This means both functions require striped etags therefore, they should be done out side both functions, probably before calling both functions or parse_etags function should return striped tags ?

sakhunzai avatar Dec 13 '18 04:12 sakhunzai

I also see another issue with line: etags = [etag.strip('"') for etag in etags]

if we have weak etag its not striped properly and matching will fail e.g

if_match='W/"6ff096757676aaecfe35d932aa04e4f8"'
res_etag='6ff096757676aaecfe35d932aa04e4f8'
etags=['W/"6ff096757676aaecfe35d932aa04e4f8"']
etags = [etag.strip('"') for etag in etags]
#estags : ['W/"6ff096757676aaecfe35d932aa04e4f8']
# A better option seems 
etags = [etag.strip('W/"') for etag in etags]
# ['6ff096757676aaecfe35d932aa04e4f8']

sakhunzai avatar Dec 13 '18 05:12 sakhunzai

I am not sure what will be consequences given 1.11 documentation read as:

Changed in Django 1.11:

In older versions, the return value from etag_func() was interpreted as the unquoted part of the ETag. That prevented the use of weak ETags, which have the format W/"". The return value is now expected to be an ETag as defined by the specification (including the quotes), although the unquoted format is also accepted for backwards compatibility.

sakhunzai avatar Dec 13 '18 05:12 sakhunzai