Skip to content

Commit

Permalink
throwing new mysql exception mssg with no sensitive information. (#1823)
Browse files Browse the repository at this point in the history
* throwing new mysql exception mssg with no sensitive information.

* PR Fixes 1
  • Loading branch information
deepchoudhery committed Feb 14, 2022
1 parent 4901144 commit e0f2448
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Scaffolding/VS.Web.CG.EFCore/MessageStrings.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -168,6 +168,9 @@
<data name="ModelTypeNotFound" xml:space="preserve">
<value>Could not get the reflection type for Model : {0}</value>
</data>
<data name="MySQLDbContextExceptionMssg" xml:space="preserve">
<value>Unable to get DbContext Instance. Access denied for user (MySQL db).</value>
</data>
<data name="NoEntityOfTypeInDbContext" xml:space="preserve">
<value>There is no entity type {0} on DbContext {1}</value>
</data>
Expand Down

0 comments on commit e0f2448

Please sign in to comment.