Skip to content

fix(auth): use exact timestamp for api key expiry check#29736

Open
TechGenius-Karan wants to merge 3 commits into
calcom:mainfrom
TechGenius-Karan:fix/expired-api-key-timestamp-check-29728
Open

fix(auth): use exact timestamp for api key expiry check#29736
TechGenius-Karan wants to merge 3 commits into
calcom:mainfrom
TechGenius-Karan:fix/expired-api-key-timestamp-check-29728

Conversation

@TechGenius-Karan

@TechGenius-Karan TechGenius-Karan commented Jul 7, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #29728

API keys were staying valid for up to 24 hours past their actual expiry. The check was truncating both sides to
midnight using .setHours(0, 0, 0, 0), so a key with expiresAt set to midnight today would compare as
today_midnight > today_midnight = false — accepted as valid all day.

There was also a side effect: .setHours() mutates the Date object in place, so keyData.expiresAt was getting
its time component zeroed after the check ran (this is what the reporter saw when the stored timestamp looked
altered).

Before:

    ```ts
   const isKeyExpired =
     keyData.expiresAt && new Date().setHours(0, 0, 0, 0) > keyData.expiresAt.setHours(0, 0, 0, 0);

After:

```ts
const isKeyExpired = keyData.expiresAt && new Date() > keyData.expiresAt;

exact timestamp comparison — no truncation, no mutation.

Visual Demo (For contributors especially)

not applicable — this is a logic fix in the auth strategy, no UI changes.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs if this PR makes changes that would require a documentation change. N/A — no
    API or config changes.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

added a regression test in api-auth.strategy.e2e-spec.ts — creates a key with expiresAt set 1 second in the
past and asserts apiKeyStrategy throws UnauthorizedException.

How should this be tested?

run the existing e2e suite for the auth strategy:

cd apps/api && yarn test:e2e --testPathPattern=api-auth.strategy

the new test (should throw UnauthorizedException for an api key that expired seconds ago) will fail on the old
code and pass on the fix.

Checklist

  • Contributing guide has been read.
  • Change is minimal — 1 line in the strategy, 1 import + 1 test in the spec.
  • No new warnings introduced.
  • PR is well under 500 lines and under 10 files.

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Welcome to Cal.diy, @TechGenius-Karan! Thanks for opening this pull request.

A few things to keep in mind:

  • This is Cal.diy, not Cal.com. Cal.diy is a community-driven, fully open-source fork of Cal.com licensed under MIT. Your changes here will be part of Cal.diy — they will not be deployed to the Cal.com production app.
  • Please review our Contributing Guidelines if you haven't already.
  • Make sure your PR title follows the Conventional Commits format.

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added the 🐛 bug Something isn't working label Jul 7, 2026
@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Related PRs: None specified

Suggested labels: None specified

Suggested reviewers: None specified

Changes

This change modifies the API key expiration check in the authentication strategy, replacing a midnight-normalized date comparison with a direct comparison between the current timestamp and the key's expiration timestamp. A corresponding end-to-end test was added to verify that an API key expired just seconds prior is correctly rejected with an UnauthorizedException. The test file's imports were updated to include UnauthorizedException.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant apiKeyStrategy
  Client->>apiKeyStrategy: request with expired API key
  apiKeyStrategy->>apiKeyStrategy: compare new Date() to keyData.expiresAt
  apiKeyStrategy-->>Client: throw UnauthorizedException
Loading

Poem
A rabbit checks the clock with care,
No more midnight math to spare,
Seconds count, the key's expired,
UnauthorizedException fired,
Tests confirm the fix is fair! 🐰⏰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #29728 by using exact timestamp comparison, avoiding mutation, and adding a rejection test.
Out of Scope Changes check ✅ Passed The PR stays focused on the API key expiry bug fix and regression test with no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly summarizes the auth change to exact API key expiry timestamp checks.
Description check ✅ Passed The description directly explains the expiry bug, the timestamp fix, and the regression test.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active.

@github-actions github-actions Bot added the Stale label Jul 15, 2026
@TechGenius-Karan

Copy link
Copy Markdown
Author

I updated the branch to keep this PR active

@github-actions github-actions Bot removed the Stale label Jul 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active.

@github-actions github-actions Bot added the Stale label Jul 23, 2026
@CLAassistant

CLAassistant commented Jul 23, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@TechGenius-Karan

Copy link
Copy Markdown
Author

This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active.

I updated this branch again to keep this PR active. Could any of the maintainers please review this PR and get it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working size/S Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expired API keys accepted as valid for up to 24 hours past their expiration time

2 participants