Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jwt: make max token size configurable #38461

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jewertow
Copy link
Contributor

Commit Message: jwt: make max token size configurable
Additional Description:
Risk Level: low
Testing: unit tests added
Docs Changes: protobuf comment
Release Notes: added
Fixes: #32790

Copy link

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @adisuissa
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #38461 was opened by jewertow.

see: more, trace.

Copy link
Contributor

@adisuissa adisuissa left a comment

Choose a reason for hiding this comment

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

Thanks!
Left minor comments about the API.
Assigning @yanavlasov as codeowner
/assign @yanavlasov

@@ -380,6 +380,9 @@ message JwtProvider {
message JwtCacheConfig {
// The unit is number of JWTs, default to 100.
uint32 jwt_cache_size = 1;

// Max token size in bytes, default to 4096.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please write the units (I'm assuming "bytes").
Also it is unclear to me if this is the max token size or max cache size (for all tokens). If it is the latter, I suggest renaming the field's name. If the former, this seems like a non-typical cache use, so I suggest adding comments how this cache is working, and making sure the field's comment reflects the correct behavior.
Please also add whether 0 is a valid value, and if so, what is the expected behavior in this case (or consider adding a minimum value constraint for the field).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you please write the units (I'm assuming "bytes").

"Max token size in bytes" - isn't it clear that the unit is byte?``

Also it is unclear to me if this is the max token size

Ok, I changed the comment, I hope it's clear now that it applies to a single token.

@@ -65,6 +72,7 @@ class JwtCacheImpl : public JwtCache {
private:
std::unique_ptr<SimpleLRUCache<std::string, ::google::jwt_verify::Jwt>> jwt_lru_cache_;
TimeSource& time_source_;
unsigned int max_jwt_size_for_cache_;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: uint32_t

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

if (!jwt_lru_cache_ || token.size() > std::numeric_limits<unsigned int>::max()) {
return;
}
if (static_cast<unsigned int>(token.size()) <= max_jwt_size_for_cache_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This implies that only certain tokens will be cached (not a very intuitive cache use-case). Can you please clarify the comments in the API, and explicitly call that the max value is inclusive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, added a comment to the proto file.

Signed-off-by: Jacek Ewertowski <[email protected]>
Signed-off-by: Jacek Ewertowski <[email protected]>
Signed-off-by: Jacek Ewertowski <[email protected]>
Copy link
Contributor

@adisuissa adisuissa left a comment

Choose a reason for hiding this comment

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

Assigning a codeowner who is not OOO.
/assign @TAOXUY

@@ -56,7 +60,10 @@ class JwtCacheImpl : public JwtCache {
}

void insert(const std::string& token, std::unique_ptr<::google::jwt_verify::Jwt>&& jwt) override {
if (jwt_lru_cache_ && token.size() <= kMaxJwtSizeForCache) {
if (!jwt_lru_cache_ || token.size() > std::numeric_limits<unsigned int>::max()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!jwt_lru_cache_ || token.size() > std::numeric_limits<unsigned int>::max()) {
if (!jwt_lru_cache_ || token.size() > std::numeric_limits<uint32_t>::max()) {

FWIW, I don't think the right-hand-side can be triggered, as AFAIK size() return type is size_t which should map to the same type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for that leftover, I fixed it.

I don't think the right-hand-side can be triggered, as AFAIK size() return type is size_t

Thanks for this hint. Actually, this check is unrealistic. Theoretically token.size() could be bigger than the max value of int32_t, but I do not believe that it may ever happen - a token would have to be bigger than 4Gb (4294967295 bytes). Do you think this check makes sense? Or is it better to assume that it won't happen and check static_cast<uint32_t>(token.size()) > max_token_size?

Signed-off-by: Jacek Ewertowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JwtProvider with additional configuration in JwtCacheConfig for max token size
3 participants