Mask sensitive data with custom jackson annotations
2018-06-29 / modified at 2023-10-29 / 372 words / 2 mins
️This article has been over 1 years since the last update.
In this article, we’ll see how to use custom jackson annotations to mask sensitive data with asterisk.
Here is a user
, in which the tel
field contains a customer’s privacy, that may not be allowed to persist on the server under some laws (GDPR or else).
1 | public class User { |
Test case
1 | User user = new User(); |
Solution
First, create an annotation
1 |
|
Wrap the field with annotation.
1 | public class User { |
Create a customer serializer
1 | public class AsteriskSerializer extends StdSerializer<Object> implements ContextualSerializer { |
Let’s use these in test again
1 | User user = new User(); |
Now, you can log the user safely.
Other hints
Here are some other potential leaks of which should be taken care
- SQL: Be careful while logging with mybatis, eg
select user from users where token = ?
may leak the first parameter. - HTTP Header: Don’t print token/key/auth headers, such as okhttp interceptors.
- Socket: Check your
readline()
payload before logging. - Third party crash SDKs: It’s not recommend for allowing outbound traffics as it’s hard to prevent data smuggling.