forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When cookie name+value length exceeds 4096 bytes, it is silently rejected by most of the browsers per https://datatracker.ietf.org/doc/html/rfc6265#section-6.1. Since we don't control access & refresh token lengths and encryption scheme, we need to split value and set/remove multiple cookies in such cases.
- Loading branch information
Showing
6 changed files
with
314 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
core/trino-main/src/main/java/io/trino/server/ui/MultipartUiCookie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.server.ui; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import jakarta.ws.rs.core.Cookie; | ||
import jakarta.ws.rs.core.NewCookie; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
import static jakarta.ws.rs.core.NewCookie.DEFAULT_MAX_AGE; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class MultipartUiCookie | ||
{ | ||
// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis/#section-5.6-3.4.1 | ||
public static final int MAXIMUM_COOKIE_SIZE = 4096; | ||
|
||
private final String cookieName; | ||
private final Pattern cookiePattern; | ||
private final String location; | ||
|
||
public MultipartUiCookie(String cookieName, String location) | ||
{ | ||
this.cookieName = requireNonNull(cookieName, "cookieName is null"); | ||
this.cookiePattern = Pattern.compile("^" + Pattern.quote(cookieName) + "(_\\d+)?$"); | ||
this.location = requireNonNull(location, "location is null"); | ||
} | ||
|
||
public NewCookie[] create(String token, Instant tokenExpiration, boolean isSecure) | ||
{ | ||
Date expiration = Optional.ofNullable(tokenExpiration).map(Date::from).orElse(null); | ||
ImmutableList.Builder<NewCookie> cookiesToSet = ImmutableList.builder(); | ||
int index = 0; | ||
for (String part : splitValueByLength(token)) { | ||
cookiesToSet.add(new NewCookie.Builder(cookieName(index++)) | ||
.value(part) | ||
.path(location) | ||
.domain(null) | ||
.comment(null) | ||
.maxAge(DEFAULT_MAX_AGE) | ||
.expiry(expiration) | ||
.secure(isSecure) | ||
.httpOnly(true) | ||
.build()); | ||
} | ||
return cookiesToSet.build().toArray(new NewCookie[0]); | ||
} | ||
|
||
public Optional<String> read(Map<String, Cookie> existingCookies) | ||
{ | ||
long cookiesCount = existingCookies.values().stream() | ||
.filter(this::matchesName) | ||
.filter(cookie -> !isNullOrEmpty(cookie.getValue())) | ||
.count(); | ||
|
||
if (cookiesCount == 0) { | ||
return Optional.empty(); | ||
} | ||
|
||
StringBuilder token = new StringBuilder(); | ||
for (int i = 0; i < cookiesCount; i++) { | ||
Cookie cookie = existingCookies.get(cookieName(i)); | ||
if (cookie == null || isNullOrEmpty(cookie.getValue())) { | ||
return Optional.empty(); // non continuous | ||
} | ||
token.append(cookie.getValue()); | ||
} | ||
return Optional.of(token.toString()); | ||
} | ||
|
||
public NewCookie[] delete(Map<String, Cookie> existingCookies, boolean isSecured) | ||
{ | ||
ImmutableSet.Builder<NewCookie> cookiesToDelete = ImmutableSet.builder(); | ||
cookiesToDelete.add(deleteCookie(cookieName, isSecured)); // Always invalidate first cookie even if it doesn't exist | ||
for (Cookie existingCookie : existingCookies.values()) { | ||
if (matchesName(existingCookie)) { | ||
cookiesToDelete.add(deleteCookie(existingCookie.getName(), isSecured)); | ||
} | ||
} | ||
return cookiesToDelete.build().toArray(new NewCookie[0]); | ||
} | ||
|
||
private List<String> splitValueByLength(String value) | ||
{ | ||
return Splitter.fixedLength(maximumCookieValueLength()).splitToList(value); | ||
} | ||
|
||
private boolean matchesName(Cookie cookie) | ||
{ | ||
return cookiePattern.matcher(cookie.getName()).matches(); | ||
} | ||
|
||
@VisibleForTesting | ||
String cookieName(int index) | ||
{ | ||
if (index == 0) { | ||
return cookieName; | ||
} | ||
|
||
return cookieName + '_' + index; | ||
} | ||
|
||
int maximumCookieValueLength() | ||
{ | ||
// A browser should be able to accept at least 300 cookies with a maximum size of 4096 bytes, as stipulated by RFC 2109 (#6.3), RFC 2965 (#5.3), and RFC 6265 | ||
return MAXIMUM_COOKIE_SIZE - cookieName(999).length(); | ||
} | ||
|
||
private NewCookie deleteCookie(String name, boolean isSecured) | ||
{ | ||
return new NewCookie.Builder(name) | ||
.value("delete") | ||
.path(location) | ||
.domain(null) | ||
.maxAge(0) | ||
.expiry(null) | ||
.secure(isSecured) | ||
.httpOnly(true) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.