Skip to content

Commit 2f11442

Browse files
committed
Don't create a directory specified by the user
1 parent d9fc02a commit 2f11442

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

src/ProjectTemplates/Shared/DevelopmentCertificate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static string EnsureDevelopmentCertificates(string certificatePath, stri
3535
var manager = CertificateManager.Instance;
3636
var certificate = manager.CreateAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1));
3737
var certificateThumbprint = certificate.Thumbprint;
38-
CertificateManager.ExportCertificate(certificate, path: certificatePath, includePrivateKey: true, certificatePassword, CertificateKeyExportFormat.Pfx);
38+
manager.ExportCertificate(certificate, path: certificatePath, includePrivateKey: true, certificatePassword, CertificateKeyExportFormat.Pfx);
3939

4040
return certificateThumbprint;
4141
}

src/Shared/CertificateGeneration/CertificateManager.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ public EnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate(
323323
{
324324
try
325325
{
326+
// If the user specified a non-existent directory, we don't want to be responsible
327+
// for setting the permissions appropriately, so we'll bail.
328+
var exportDir = Path.GetDirectoryName(path);
329+
if (!string.IsNullOrEmpty(exportDir) && !Directory.Exists(exportDir))
330+
{
331+
throw new InvalidOperationException($"The directory '{exportDir}' does not exist.");
332+
}
333+
326334
ExportCertificate(certificate, path, includePrivateKey, password, keyExportFormat);
327335
}
328336
catch (Exception e)
@@ -486,6 +494,10 @@ public void CleanupHttpsCertificates()
486494

487495
protected abstract void CreateDirectoryWithPermissions(string directoryPath);
488496

497+
/// <remarks>
498+
/// Will create directories to make it possible to write to <paramref name="path"/>.
499+
/// If you don't want that, check for existence before calling this method.
500+
/// </remarks>
489501
internal void ExportCertificate(X509Certificate2 certificate, string path, bool includePrivateKey, string? password, CertificateKeyExportFormat format)
490502
{
491503
if (Log.IsEnabled())
@@ -1232,6 +1244,9 @@ public sealed class CertificateManagerEventSource : EventSource
12321244
[Event(111, Level = EventLevel.LogAlways, Message = "For OpenSSL trust to take effect, '{0}' must be listed in the {2} environment variable. " +
12331245
"See https://aka.ms/dev-certs-trust for more information.")]
12341246
internal void UnixSuggestSettingEnvironmentVariableWithoutExample(string certDir, string envVarName) => WriteEvent(111, certDir, envVarName);
1247+
1248+
[Event(112, Level = EventLevel.Warning, Message = "Directory '{0}' may be readable by other users.")]
1249+
internal void DirectoryPermissionsNotSecure(string directoryPath) => WriteEvent(112, directoryPath);
12351250
}
12361251

12371252
internal sealed class UserCancelledTrustException : Exception

src/Shared/CertificateGeneration/MacOSCertificateManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ protected override void CreateDirectoryWithPermissions(string directoryPath)
485485
{
486486
if ((dirInfo.UnixFileMode & ~DirectoryPermissions) != 0)
487487
{
488-
// TODO (acasey): Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
488+
Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
489489
}
490490
}
491491
else

src/Shared/CertificateGeneration/UnixCertificateManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ protected override void CreateDirectoryWithPermissions(string directoryPath)
459459
{
460460
if ((dirInfo.UnixFileMode & ~DirectoryPermissions) != 0)
461461
{
462-
// TODO (acasey): Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
462+
Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
463463
}
464464
}
465465
else

src/Shared/CertificateGeneration/WindowsCertificateManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics;
7-
using System.IO;
86
using System.Linq;
97
using System.Runtime.Versioning;
108
using System.Security.AccessControl;
@@ -162,7 +160,7 @@ protected override void CreateDirectoryWithPermissions(string directoryPath)
162160
// is to allow access to anyone other than the current user, system, or administrators
163161
// is very complicated. We're not going to do anything but log, so an approximation
164162
// is fine.
165-
// TODO (acasey): Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
163+
Log.DirectoryPermissionsNotSecure(dirInfo.FullName);
166164
break;
167165
}
168166
}

0 commit comments

Comments
 (0)