Skip to content

stateful_session: encode expiry time and reject expired cookies - #26761

Merged
wbpcode merged 27 commits into
envoyproxy:mainfrom
cpakulski:issue/26347
May 10, 2023
Merged

wbpcode merged 27 commits into
envoyproxy:mainfrom
cpakulski:issue/26347

Conversation

@cpakulski

@cpakulski cpakulski commented Apr 14, 2023 •

Copy link
Copy Markdown
Contributor

Commit Message:
encode expiry time and reject expired cookies

Additional Description:
Changed internal format of cookies to protobuf. Added expires field. When processing received cookie, expires timestamp is compared to the current time. Expired cookies are ignored and new upstream host is selected. Backwards compatibility is preserved, so cookies in "old" non-proto format are accepted, but support for them will be removed in the future.

Risk Level: Low
Testing: Expanded unit and integration tests.
Docs Changes: No.
Release Notes: Yes.
Platform Specific Features:
Runtime guard: envoy.reloadable_features.stateful_session_encode_ttl_in_cookie
Fixes #26347

Signed-off-by: Christoph Pakulski <[email protected]>
Signed-off-by: Christoph Pakulski <[email protected]>
Signed-off-by: Christoph Pakulski <[email protected]>
Signed-off-by: Christoph Pakulski <[email protected]>
Signed-off-by: Christoph Pakulski <[email protected]>
@cpakulski
cpakulski requested a review from wbpcode as a code owner April 14, 2023 16:18
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/runtime-guard-changes: FYI only for changes made to (source/common/runtime/runtime_features.cc).

🐱

Caused by: #26761 was opened by cpakulski.

see: more, trace.

@wbpcode

wbpcode commented Apr 15, 2023

Copy link
Copy Markdown
Member

/assign

@wbpcode

wbpcode commented Apr 17, 2023 •

Copy link
Copy Markdown
Member

Json is great. But would it be better to use some simple way to extended this cookie value? For example, a simple space could be used to seperate different fields in the value.
All 1.2.3.4, 1.2.3.4 -, 1.2.3.4 <timestamp> could be valid values.
Although we may have more fields in the cookie value in the future, the Json still too heavy for this simple requirement.

@wbpcode

wbpcode commented Apr 17, 2023

Copy link
Copy Markdown
Member

/wait-any

@cpakulski

Copy link
Copy Markdown
Contributor Author

Json is great. But would it be better to use some simple way to extended this cookie value? For example, a simple space could be used to seperate different fields in the value.
All 1.2.3.4, 1.2.3.4 -, 1.2.3.4 could be valid values.
Although we may have more fields in the cookie value in the future, the Json still too heavy for this simple requirement.

That was my first approach - to separate by say ";". But then I started to think how to protect against crashing when the payload is not correct. Currently, the cookie is expected to beip:port. The stateful_session filter does not do any syntax validation, because it offloads the checks to the cluster. For example, instead of valid IP address you can send "blahlahbalah" in the cookie and stateful_session filter will ask the cluster to use "blahlahbalah" as upstream address. The cluster does syntax validation and rejects it as address. With a new timestamp field, I have to ask solid parsing, dealing with missing fields, validating that the field is indeed an integer, etc. Most of those problem are already solved by json parser.
Let me write microbenchmark to see how json compares to tokenizing a string with fields separated by ";".

@mattklein123

Copy link
Copy Markdown
Member

For encoding the data I would strongly recommend something like a base64 encoded protobuf. This will be much easier to deal with if we add more fields in the future.

@cpakulski

Copy link
Copy Markdown
Contributor Author

@mattklein123 Thanks for your suggestion. Let me refactor the code to use protobuf.

@wbpcode

wbpcode commented Apr 18, 2023

Copy link
Copy Markdown
Member

For encoding the data I would strongly recommend something like a base64 encoded protobuf. This will be much easier to deal with if we add more fields in the future.

SGTM.

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your quick update and some comments are added to start the review.

@cpakulski

Copy link
Copy Markdown
Contributor Author

@wbpcode Thanks for reviewing. I tried to address all your comments.

@cpakulski

Copy link
Copy Markdown
Contributor Author

/retest

@cpakulski

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #26761 (comment) was created by @cpakulski.

see: more, trace.

@cpakulski

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #26761 (comment) was created by @cpakulski.

see: more, trace.

@wbpcode

wbpcode commented May 6, 2023

Copy link
Copy Markdown
Member

Could merge the main and try the CI again.

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM overall and only one single comment, thanks again for your time, patience and great works.

@cpakulski
cpakulski requested a review from wbpcode May 10, 2023 14:05

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, lots of thanks.

@wbpcode
wbpcode merged commit c11f42e into envoyproxy:main May 10, 2023
@cpakulski

Copy link
Copy Markdown
Contributor Author

@wbpcode Thanks for code reviews! It really helps.

@cpakulski
cpakulski deleted the issue/26347 branch May 10, 2023 15:49
wbpcode pushed a commit to wbpcode/envoy that referenced this pull request May 16, 2023
…yproxy#26761)

* Hacked version with expiry added after semicolon.

Signed-off-by: Christoph Pakulski <[email protected]>

* Implemented cookie in JSON format.

Signed-off-by: Christoph Pakulski <[email protected]>

* Added integration tests.

Signed-off-by: Christoph Pakulski <[email protected]>

* Helper class to simplify comparing JSON encoded cookies in tests.

Signed-off-by: Christoph Pakulski <[email protected]>

* Log when old cookie format is detected.

Signed-off-by: Christoph Pakulski <[email protected]>

* Formatting.

Signed-off-by: Christoph Pakulski <[email protected]>

* Updated release notes.

Signed-off-by: Christoph Pakulski <[email protected]>

* Typo.

Signed-off-by: Christoph Pakulski <[email protected]>

* Changed cookie encoding from JSON to protobuf.

Signed-off-by: Christoph Pakulski <[email protected]>

* Fixed clang-tidy.

Signed-off-by: Christoph Pakulski <[email protected]>

* Changed cookie expiry type to uint64.

Signed-off-by: Christoph Pakulski <[email protected]>

* Generate single log regardless of number of old-style cookies.

Signed-off-by: Christoph Pakulski <[email protected]>

* Used ENVOY_LOG_ONCE to supress subsequent logs.

Signed-off-by: Christoph Pakulski <[email protected]>

* Always reply in new format, regardless of received cookie's format.

Signed-off-by: Christoph Pakulski <[email protected]>

* Remove unnecessary intermediate local variables.

Signed-off-by: Christoph Pakulski <[email protected]>

* Kick CI

Signed-off-by: Christoph Pakulski <[email protected]>

---------

Signed-off-by: Christoph Pakulski <[email protected]>
reskin89 pushed a commit to reskin89/envoy that referenced this pull request Jul 11, 2023
…yproxy#26761)

* Hacked version with expiry added after semicolon.

Signed-off-by: Christoph Pakulski <[email protected]>

* Implemented cookie in JSON format.

Signed-off-by: Christoph Pakulski <[email protected]>

* Added integration tests.

Signed-off-by: Christoph Pakulski <[email protected]>

* Helper class to simplify comparing JSON encoded cookies in tests.

Signed-off-by: Christoph Pakulski <[email protected]>

* Log when old cookie format is detected.

Signed-off-by: Christoph Pakulski <[email protected]>

* Formatting.

Signed-off-by: Christoph Pakulski <[email protected]>

* Updated release notes.

Signed-off-by: Christoph Pakulski <[email protected]>

* Typo.

Signed-off-by: Christoph Pakulski <[email protected]>

* Changed cookie encoding from JSON to protobuf.

Signed-off-by: Christoph Pakulski <[email protected]>

* Fixed clang-tidy.

Signed-off-by: Christoph Pakulski <[email protected]>

* Changed cookie expiry type to uint64.

Signed-off-by: Christoph Pakulski <[email protected]>

* Generate single log regardless of number of old-style cookies.

Signed-off-by: Christoph Pakulski <[email protected]>

* Used ENVOY_LOG_ONCE to supress subsequent logs.

Signed-off-by: Christoph Pakulski <[email protected]>

* Always reply in new format, regardless of received cookie's format.

Signed-off-by: Christoph Pakulski <[email protected]>

* Remove unnecessary intermediate local variables.

Signed-off-by: Christoph Pakulski <[email protected]>

* Kick CI

Signed-off-by: Christoph Pakulski <[email protected]>

---------

Signed-off-by: Christoph Pakulski <[email protected]>
Signed-off-by: Ryan Eskin <[email protected]>
@kyessenov

Copy link
Copy Markdown
Contributor

This change is not backwards compatible. TTL was optional in the raw encoding but becomes mandatory for the new proto encoding. I think a required TTL is a better security posture, but this definitely would break someone upgrading that didn't set TTL.

@cpakulski

Copy link
Copy Markdown
Contributor Author

Is it because cookie is instantly expired when ttl is zero? I did not change API, so if you do not set ttl, the value is zero. If so, the fix should be relatively easy.

@kyessenov

Copy link
Copy Markdown
Contributor

Yes, TTL set to 0 (as recommended by docs for sessions) makes it expire immediately. I'll send a patch.

@cpakulski

Copy link
Copy Markdown
Contributor Author

OK. Thanks!

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.

cookies are still routing the traffic after the ttl's expiry

4 participants