在使用 MVC 3 的 ELMAH 中,如何从错误日志中隐藏敏感表单数据?

发布于 2024-11-18 22:04:30 字数 610 浏览 7 评论 0原文

这是场景...

用户输入他的用户名。输入“不正确”的密码。 用户名和密码值都被传递到 Elmah 错误日志 通过Exception.Context.Request.Form[“密码”]。 它是只读值,无法修改。

不...我不想忽略异常(失败)。我们以编程方式添加了 ErrorLog 过滤:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
  if (e.Exception is LogOnException)
  {
    ((HttpContext) e.Context).Request.Form.Remove("Password");
    // This is what we want to do, but we can't because it is read-only
  }
}

但无法修改 Request.Form,以便在错误日志中隐藏密码。

有人遇到过解决这个问题的方法吗?

我基本上想要所有没有密码字段的错误数据。我们考虑手动记录它,但与简单地隐藏敏感数据相比,这似乎需要大量工作。

干杯,伙计们。提前致谢。

Here is the scenario...

User types his username. Types an "incorrect" password.
Both username and password values are being passed to the Elmah error log
via the Exception.Context.Request.Form["Password"].
It's a read-only value and cannot be modified.

And no... I don't want to dismiss the exception (fail). We added ErrorLog Filtering programmatically:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
  if (e.Exception is LogOnException)
  {
    ((HttpContext) e.Context).Request.Form.Remove("Password");
    // This is what we want to do, but we can't because it is read-only
  }
}

But cannot modify the Request.Form so that the password is hidden from our error log.

Anybody ever encountered a way around this?

I basically want all the error data without the password field. We considered logging it manually but that seemed to be a lot of work compared to simply hiding the sensitive data.

Cheers guys. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹泡泡o 2024-11-25 22:04:30

您无法修改请求上的表单集合,但可以修改 Elmah 错误实例上的表单集合,然后手动记录它。即

public static class ElmahSensitiveDataFilter
{
  public static void Apply(ExceptionFilterEventArgs e, HttpContext ctx)
  {
    var sensitiveFormData = ctx.Request.Form.AllKeys
            .Where(key => key.Equals("password", StringComparison.OrdinalIgnoreCase)).ToList();
    if (sensitiveFormData.Count == 0)
    {
      return;
    }
    var error = new Error(e.Exception, ctx);
    sensitiveFormData.ForEach(k => error.Form.Set(k, "*****"));
    Elmah.ErrorLog.GetDefault(null).Log(error);
    e.Dismiss();
  }
}

然后在 Global.asax 中

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    var ctx = e.Context as HttpContext;
    if(ctx == null)
    {
      return;
    }
    ElmahSensitiveDataFilter.Apply(e, ctx);
}

You can't modify the form collection on the request but you can modify the form collection on an Elmah Error isntance and then manually log it. I.e.

public static class ElmahSensitiveDataFilter
{
  public static void Apply(ExceptionFilterEventArgs e, HttpContext ctx)
  {
    var sensitiveFormData = ctx.Request.Form.AllKeys
            .Where(key => key.Equals("password", StringComparison.OrdinalIgnoreCase)).ToList();
    if (sensitiveFormData.Count == 0)
    {
      return;
    }
    var error = new Error(e.Exception, ctx);
    sensitiveFormData.ForEach(k => error.Form.Set(k, "*****"));
    Elmah.ErrorLog.GetDefault(null).Log(error);
    e.Dismiss();
  }
}

Then in Global.asax

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
{
    var ctx = e.Context as HttpContext;
    if(ctx == null)
    {
      return;
    }
    ElmahSensitiveDataFilter.Apply(e, ctx);
}
禾厶谷欠 2024-11-25 22:04:30

捕获异常,然后手动在 ELMAH 中记录一些内容,如下所示:


catch (LogOnException e)
{
     Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Bad Password"));
}

Catch the exception, then log something in ELMAH manually, like this:


catch (LogOnException e)
{
     Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Bad Password"));
}
粉红×色少女 2024-11-25 22:04:30

除非修改源本身,否则无法执行此操作。您当然可以修改配置并向其中添加“排除的表单元素”的概念,然后当 Error 类从 HttpContext 复制集合时,您可以删除该列表中的任何项目。

显然,另一种选择是使用其他东西,它可以对日志记录过程提供更明确的控制,例如 EntLib 或 log4net。编写模块或全局异常处理程序来利用这些工具中的任何一个都很简单。此外,它们与 Web 应用程序之外的范围相关。

You can't do this unless you modify the source itself. You could certainly modify the configuration and add the notion of an "Excluded Form Elements" to that, then when the Error class copies the collection from the HttpContext, you can remove any items in that list.

Another alternative would be to use something else, obviously, that provides more explicit control over the logging process like EntLib or log4net. It's trivial to write a module or global exception handler to utilize either one of those tools. Moreover, they are relevant in scopes outside of web applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文