Avoid int overflow in expiration time calculations - #37208
Open
a-kogun wants to merge 1 commit into
Open
Conversation
FlashMap.startExpirationPeriod(int) and MockMvcWebConnection's cookie handling both multiplied an int number of seconds by 1000 without widening to long. Above 2_147_483 seconds (about 24.9 days) the multiplication overflows to a negative offset, so the computed expiration time lands in the past. For FlashMap, a flash map configured through AbstractFlashMapManager.setFlashMapTimeout(int) with a large timeout is then treated as expired immediately. For MockMvcWebConnection, a cookie with a large max-age is removed from the CookieManager instead of being stored. This applies the same widening already used for this pattern in spring-projectsgh-25613. Signed-off-by: kogun <akogun@gmail.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two places still compute an expiration time by multiplying an
intnumber of seconds by
1000without widening tolong:FlashMap.startExpirationPeriod(int)(spring-webmvc)MockMvcWebConnection.createCookie(...)(spring-test)Above
2_147_483seconds (~24.9 days) the multiplication overflows andyields a negative offset, so the expiration time lands in the past.
With
Integer.MAX_VALUE,timeToLive * 1000evaluates to-1000, i.e.one second ago.
Impact
FlashMap: a timeout set via the publicAbstractFlashMapManager.setFlashMapTimeout(int)is treated asalready expired, so flash attributes are silently dropped.
MockMvcWebConnection: becausestoreCookiescompares the computedexpiry against now, a cookie with a large
max-ageis passed tocookieManager.removeCookie(...)instead ofaddCookie(...)— thecookie is discarded rather than stored.
Fix
Widen the literal to
1000Lin both places. This is the same changealready applied to this exact pattern in gh-25613
(
ExecutorConfigurationSupport.setAwaitTerminationSeconds(int)andAbstractResourceBasedMessageSource.setCacheSeconds(int)), and matcheshow those methods read today.
Tests
Two regression tests are included, both of which fail before the change:
FlashMapTests.notExpiredWithLargeTimeToLive()— fails withExpecting actual: <now-1000> to be greater than: <now>.MockMvcWebClientBuilderTests.cookieWithLargeMaxAgeIsStored()— failswith
expected: "foo" but was: "NA", showing the cookie was dropped.A scan of
src/main/javafound no other live instances of this pattern:AbstractSockJsService.ONE_YEARandDurationFormatterUtils.microsarealready declared
long.