-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix timezone issue in dev-certs: use DateTimeOffset.UtcNow instead of DateTimeOffset.Now #63285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-63003
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+258
−2
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35d3203
Initial plan
Copilot 1fc2884
Fix timezone issue in dev-certs: use DateTimeOffset.UtcNow instead of…
Copilot c57236e
Remove incorrect test that was not testing the actual timezone fix
Copilot f527219
Fix IDE2000 linting error: remove multiple blank lines in Certificate…
Copilot b8a1ba4
Add unit tests for timezone fix in CertificateGenerator.GenerateAspNe…
Copilot 23fbde6
Add comprehensive unit tests that verify timezone fix and would detec…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.DeveloperCertificates.XPlat; | ||
| using Microsoft.AspNetCore.InternalTesting; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
@@ -516,6 +517,105 @@ public void ListCertificates_AlwaysReturnsTheCertificate_WithHighestVersion() | |
| e.RawData[0] == 2); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GenerateAspNetHttpsCertificate_UsesUtcTime_CertificateIsImmediatelyValid() | ||
| { | ||
| // This test verifies that CertificateGenerator.GenerateAspNetHttpsCertificate() uses UTC time | ||
| // instead of local time, ensuring certificates are immediately valid regardless of timezone | ||
|
|
||
| try | ||
| { | ||
| _fixture.CleanupCertificates(); | ||
|
|
||
| // Record both UTC and local time before calling the method | ||
| var beforeCallUtc = DateTimeOffset.UtcNow; | ||
| var beforeCallLocal = DateTimeOffset.Now; | ||
|
|
||
| // Call the method that was fixed to use DateTimeOffset.UtcNow | ||
| CertificateGenerator.GenerateAspNetHttpsCertificate(); | ||
|
|
||
| // Record both UTC and local time after calling the method | ||
| var afterCallUtc = DateTimeOffset.UtcNow; | ||
| var afterCallLocal = DateTimeOffset.Now; | ||
|
|
||
| // Get the certificate that was created | ||
| var certificates = _manager.ListCertificates(StoreName.My, StoreLocation.CurrentUser, isValid: true); | ||
| Assert.True(certificates.Count > 0, "Expected at least one certificate to be created"); | ||
|
|
||
| var certificate = certificates.First(); | ||
|
|
||
| // Convert certificate NotBefore to UTC (certificates store time as local time) | ||
| var notBefore = new DateTimeOffset(certificate.NotBefore.ToUniversalTime(), TimeSpan.Zero); | ||
|
|
||
| // The certificate's NotBefore should be close to UTC time, not local time | ||
| // If it used DateTimeOffset.Now in a non-UTC timezone, it would be off by the timezone offset | ||
| var utcTimeDiff = Math.Abs((notBefore - beforeCallUtc).TotalSeconds); | ||
| var localTimeDiff = Math.Abs((notBefore - beforeCallLocal.ToUniversalTime()).TotalSeconds); | ||
|
|
||
| // In UTC timezone, both differences would be small, but in non-UTC timezone, | ||
| // UTC difference should be much smaller than local time difference | ||
| Assert.True(utcTimeDiff <= 10, | ||
| $"Certificate NotBefore should be close to UTC time. NotBefore: {notBefore:yyyy-MM-dd HH:mm:ss} UTC, " + | ||
| $"BeforeCall UTC: {beforeCallUtc:yyyy-MM-dd HH:mm:ss} UTC, Difference: {utcTimeDiff:F2} seconds"); | ||
|
|
||
| // Verify the certificate is immediately valid (NotBefore <= now in UTC) | ||
| var utcNow = DateTimeOffset.UtcNow; | ||
| Assert.True(notBefore <= utcNow, | ||
| $"Certificate should be immediately valid. NotBefore: {notBefore:yyyy-MM-dd HH:mm:ss} UTC, Current UTC: {utcNow:yyyy-MM-dd HH:mm:ss} UTC"); | ||
|
|
||
| // Output diagnostic information to help understand the test | ||
| Output.WriteLine($"Certificate NotBefore: {notBefore:yyyy-MM-dd HH:mm:ss} UTC"); | ||
| Output.WriteLine($"UTC time before call: {beforeCallUtc:yyyy-MM-dd HH:mm:ss} UTC"); | ||
| Output.WriteLine($"Local time before call: {beforeCallLocal:yyyy-MM-dd HH:mm:ss zzz}"); | ||
| Output.WriteLine($"UTC time difference: {utcTimeDiff:F2} seconds"); | ||
| Output.WriteLine($"Local time difference: {localTimeDiff:F2} seconds"); | ||
| } | ||
| finally | ||
| { | ||
| _fixture.CleanupCertificates(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CertificateGenerator_FixedToUseUtcNow_NotLocalNow() | ||
| { | ||
| // This test documents the fix that was made to CertificateGenerator.GenerateAspNetHttpsCertificate() | ||
| // The method was changed from DateTimeOffset.Now to DateTimeOffset.UtcNow to fix timezone issues | ||
|
|
||
| // Test that the method behaves correctly by ensuring the certificate uses UTC-based time | ||
| // In non-UTC timezones, using DateTimeOffset.Now would create certificates with future NotBefore timestamps | ||
|
|
||
| try | ||
| { | ||
| _fixture.CleanupCertificates(); | ||
|
|
||
| // Call the fixed method | ||
| CertificateGenerator.GenerateAspNetHttpsCertificate(); | ||
|
|
||
| // Verify a certificate was created and is immediately valid | ||
| var certificates = _manager.ListCertificates(StoreName.My, StoreLocation.CurrentUser, isValid: true); | ||
| Assert.True(certificates.Count > 0, "Certificate should be created"); | ||
|
|
||
| var certificate = certificates.First(); | ||
|
|
||
| // The certificate should be immediately valid - NotBefore should not be in the future | ||
| Assert.True(certificate.NotBefore <= DateTime.UtcNow.AddSeconds(5), | ||
|
||
| "Certificate NotBefore should not be in the future (which would happen with DateTimeOffset.Now in non-UTC timezones)"); | ||
|
|
||
| // The certificate should be valid for approximately 1 year from now | ||
| var expectedExpiry = DateTime.UtcNow.AddYears(1); | ||
| var actualExpiry = certificate.NotAfter; | ||
| var expiryDiff = Math.Abs((expectedExpiry - actualExpiry).TotalDays); | ||
|
|
||
| Assert.True(expiryDiff <= 1, | ||
| $"Certificate should expire approximately 1 year from now. Expected: {expectedExpiry:yyyy-MM-dd}, Actual: {actualExpiry:yyyy-MM-dd}"); | ||
| } | ||
| finally | ||
| { | ||
| _fixture.CleanupCertificates(); | ||
| } | ||
| } | ||
|
|
||
| [ConditionalFact] | ||
| [OSSkipCondition(OperatingSystems.Windows, SkipReason = "UnixFileMode is not supported on Windows.")] | ||
| [OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "https://github.com/dotnet/aspnetcore/issues/6720")] | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot I guess this will fail if during the test there is a daylight savings change or timezone change.. can that be made robust without too much complexity?
also, since a fault is likely to show up as a diff of several hours, I suggest using something significantly longer than 10 seconds, to allow for a very slow machine?