fix(auth): use exact timestamp for api key expiry check#29736
fix(auth): use exact timestamp for api key expiry check#29736TechGenius-Karan wants to merge 3 commits into
Conversation
|
Welcome to Cal.diy, @TechGenius-Karan! Thanks for opening this pull request. A few things to keep in mind:
A maintainer will review your PR soon. Thanks for contributing! |
📝 WalkthroughWalkthroughRelated PRs: None specified Suggested labels: None specified Suggested reviewers: None specified ChangesThis 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 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
Poem 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
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 the branch to keep this PR active |
|
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. |
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 withexpiresAtset to midnight today would compare astoday_midnight > today_midnight=false— accepted as valid all day.There was also a side effect:
.setHours()mutates the Date object in place, sokeyData.expiresAtwas gettingits time component zeroed after the check ran (this is what the reporter saw when the stored timestamp looked
altered).
Before:
After:
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)
API or config changes.
added a regression test in
api-auth.strategy.e2e-spec.ts— creates a key withexpiresAtset 1 second in thepast and asserts
apiKeyStrategythrowsUnauthorizedException.How should this be tested?
run the existing e2e suite for the auth strategy:
cd apps/api && yarn test:e2e --testPathPattern=api-auth.strategythe new test (
should throw UnauthorizedException for an api key that expired seconds ago) will fail on the oldcode and pass on the fix.
Checklist