0

I am using postsharps OnMethodBoundaryAspect. Overriding on entry in this example so I can log the parameters for tracing. I have a requirement to mask potentially personally identifiable information.

Using the code below, I can do that by scanning those properties I have marked with "PII" attribute. However, the problem is for some reason, I can't create a copy of the Arguments object. I've even tried using the "Clone" method of Arguments but it still gets changed whenever I use the SetValue to update the property.

Is there something I'm missing here?

public override void OnEntry(MethodExecutionArgs args){
    int count = args.Arguments.Count();
    object[] argsCopy = new object[count];
    args.Arguments.CopyTo(argsCopy, 0);
    foreach(var arg in argsCopy)
    {
         MaskPiiDecorated(arg);
    }
    WriteEvent((int)EventId.Info, argsCopy.ToArray());
}

private object MaskPiiDecorated(object arg)
{
    //Gets all properties
    IEnumerable<PropertyInfo> topProperties = arg.GetType().GetProperties();

    //Gets sub properties that could have properties 
    IEnumerable<PropertyInfo> nonValueTypeSubProperties = topProperties.Where(
            prop => !prop.PropertyType.IsValueType
            && prop.PropertyType != typeof(string)
            && prop.PropertyType.BaseType != typeof(Array));

    //Recursion for sub properties
    nonValueTypeSubProperties.ToList().ForEach(
            subProperty => { MaskPiiDecorated(subProperty.GetValue(arg)); });

    topProperties.Where(prop => prop.IsDefined(typeof(PiiAttribute), false))
            .ToList().ForEach(decor => { decor.SetValue(arg, "***REMOVED****"); });
    return arg;

}
MichaelChan
  • 1,808
  • 17
  • 34

1 Answers1

0

Clone on Arguments creates just a shallow copy according to documentation. It means that property values of original argument instance are still modified.

It would be easier (and better for performance in terms of allocation and speed) to do the masking as part of WriteEvent method when parameters are serialized to a text representation.

Jakub Linhart
  • 4,062
  • 1
  • 26
  • 42
  • I also considered that but it doesn't quite fit the requirement since I need to mask specific fields. the solution I came up with is build up a new list based on the reflected properties and mask / log those instead. – MichaelChan Jan 16 '16 at 10:03