Skip to content

preemptExpirtyTime incorrectly be used for TimeToLive calculations #3962

@michael-proctor

Description

@michael-proctor

C#
return exp - now + preemptExpiryTime;
is meant to calculate the remaining time-to-live (TTL) for credentials, but if the intention is to reduce the TTL by a "preempt" period (to refresh credentials before they actually expire), you should subtract preemptExpiryTime, not add it.

Why Adding Is Incorrect
Adding preemptExpiryTime actually increases the TTL, meaning your credentials might be used past their intended expiration window, risking authentication errors.

Example Scenario
Suppose:

exp = 100 (expiration time in seconds since epoch)
now = 90 (current time in seconds since epoch)
preemptExpiryTime = 5 (preemptive buffer in seconds)

Current Code:

C#
return exp - now + preemptExpiryTime; // 100 - 90 + 5 = 15

Interpretation:
The credentials will be considered valid for 15 more seconds.

Intended Behavior (Should Subtract):

C#
return exp - now - preemptExpiryTime; // 100 - 90 - 5 = 5
Interpretation:
The credentials will be considered valid for only 5 more seconds, ensuring they are refreshed 5 seconds before actual expiration.

Why This Matters
By adding preemptExpiryTime, you risk using credentials past their safe window, potentially causing failures in authentication. By subtracting, you ensure credentials are refreshed early, avoiding issues due to clock drift or network delays.

Corrected Code
C#
return exp - now - preemptExpiryTime;

Summary:
You should subtract preemptExpiryTime to get a reduced TTL for early refresh, not add it. Adding increases the TTL and risks late refreshes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.p1This is a high priority issuequeued

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions