From e0f24482ca7d6c0ebbd96e124c3e933d705d8221 Mon Sep 17 00:00:00 2001
From: Deep Choudhery <54324771+deepchoudhery@users.noreply.github.com>
Date: Mon, 14 Feb 2022 13:15:09 -0800
Subject: [PATCH] throwing new mysql exception mssg with no sensitive
information. (#1823)
* throwing new mysql exception mssg with no sensitive information.
* PR Fixes 1
---
.../EntityFrameworkModelProcessor.cs | 16 ++++++++++++++++
.../VS.Web.CG.EFCore/MessageStrings.Designer.cs | 13 ++++++++++++-
.../VS.Web.CG.EFCore/MessageStrings.resx | 5 ++++-
3 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs b/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs
index 53d022bdc..322c08d54 100644
--- a/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs
+++ b/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs
@@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
+using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
@@ -18,12 +19,14 @@
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
using Microsoft.VisualStudio.Web.CodeGeneration.DotNet;
using Microsoft.DotNet.Scaffolding.Shared.Project;
+using System.Collections;
namespace Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
{
internal class EntityFrameworkModelProcessor
{
private const string EFSqlServerPackageName = "Microsoft.EntityFrameworkCore.SqlServer";
+ private const string MySqlException = nameof(MySqlException);
private const string NewDbContextFolderName = "Data";
private bool _useSqlite;
private string _dbContextFullTypeName;
@@ -554,6 +557,19 @@ private DbContext TryCreateContextUsingAppCode(Type dbContextType, Type startupT
}
catch (Exception ex)
{
+ var exceptionType = ex.GetType();
+ // if MySQL exception with error code 1045, discard error message since it contains sensitive dev information
+ if (exceptionType.Name.Equals(MySqlException, StringComparison.OrdinalIgnoreCase) || exceptionType.FullName.Contains(MySqlException, StringComparison.OrdinalIgnoreCase))
+ {
+ if (ex.Data.Keys.Count > 0 &&
+ ex.Data["Server Error Code"] != null &&
+ //based on error code 1045 from here https://dev.mysql.com/doc/
+ ex.Data["Server Error Code"].ToString().Equals("1045", StringComparison.OrdinalIgnoreCase))
+ {
+ ex = new Exception($"{MessageStrings.MySQLDbContextExceptionMssg}\n");
+ throw ex;
+ }
+ }
throw ex.Unwrap(_logger);
}
}
diff --git a/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.Designer.cs b/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.Designer.cs
index 4a2d578bb..8c8f87ca0 100644
--- a/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.Designer.cs
+++ b/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.Designer.cs
@@ -213,7 +213,18 @@ internal class MessageStrings {
return ResourceManager.GetString("ModelTypeNotFound", resourceCulture);
}
}
-
+
+ ///
+ /// Looks up a localized string similar to Unable to get DbContext Instance. Access denied for user (MySQL db).
+ ///
+ internal static string MySQLDbContextExceptionMssg
+ {
+ get
+ {
+ return ResourceManager.GetString("MySQLDbContextExceptionMssg", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to There is no entity type {0} on DbContext {1}.
///
diff --git a/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.resx b/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.resx
index 0de7010ee..1f0e4a1d4 100644
--- a/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.resx
+++ b/src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.resx
@@ -1,4 +1,4 @@
-
+