Skip to content
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

throwing new mysql exception mssg with no sensitive information. #1823

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to do something other than a full string comparison against exceptionType.FullName?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception comes from the MySQL provider and there are plenty of options out there. I tried two different providers and based on that, a FullName check seem to satisfy both of them (they were the two most popular).

{
if (ex.Data.Keys.Count > 0 &&
ex.Data["Server Error Code"] != null &&
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
//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