Skip to content
Myr

Don't overwrite Vary header when setting for cookie access #2317

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nina Zakharenko · committed by Nina Zakharenko · parent 4ec1fbc · Browse files · 2 files+36 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+36 added−5 removed

  • tests:+22 added−4 removed
  • other:+14 added−1 removed

2 files+36 added−5 removed

modified:flask/sessions.py+14 added-1 removed
403 unmodified lines
404 return405406 # Add a "Vary: Cookie" header if the session was accessed at all.407 if session.accessed:408 response.headers.add('Vary', 'Cookie')408 self._patch_vary_cookie_header(response)409410 if not self.should_set_cookie(app, session):411 return412
9 unmodified lines
422 domain=domain,423 path=path,424 secure=secure425 )426427 def _patch_vary_cookie_header(self, response):428 """429 Add a 'Cookie' value to the 'Vary' header if one is not already present.430 """431 header = response.headers.get('Vary', '')432 headers = [h.strip() for h in header.split(',') if h]433434 if not any(h.lower() == 'cookie' for h in headers):435 headers.append('Cookie')436437 updated_header = ', '.join(headers)438 response.headers['Vary'] = updated_header
modified:tests/test_basic.py+22 added-4 removed
518 unmodified lines
519 @app.route('/setdefault')520 def setdefault():521 return flask.session.setdefault('test', 'default')522523 @app.route('/vary-cookie-header-set')524 def vary_cookie_header_set():525 response = flask.Response()526 response.headers['Vary'] = 'Cookie'527 flask.session['test'] = 'test'528 return response529530 @app.route('/vary-header-set')531 def vary_header_set():532 response = flask.Response()533 response.headers['Vary'] = 'Accept-Encoding, Accept-Language'534 flask.session['test'] = 'test'535 return response536537 @app.route('/no-vary-header')538 def no_vary_header():539 return ''540527 def expect(path, header=True):541 def expect(path, header_value='Cookie'):542 rv = client.get(path)543530 if header:531 assert rv.headers['Vary'] == 'Cookie'544 if header_value:545 # The 'Vary' key should exist in the headers only once.546 assert len(rv.headers.get_all('Vary')) == 1547 assert rv.headers['Vary'] == header_value548 else:549 assert 'Vary' not in rv.headers550551 expect('/set')552 expect('/get')553 expect('/getitem')554 expect('/setdefault')555 expect('/vary-cookie-header-set')556 expect('/vary-header-set', 'Accept-Encoding, Accept-Language, Cookie')539 expect('/no-vary-header', False)557 expect('/no-vary-header', None)558559560def test_flashes(app, req_ctx):561 app.secret_key = 'testkey'
1255 unmodified lines