-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Mask sensitive data in logs with the MessageAccessor #35
base: main
Are you sure you want to change the base?
Conversation
…o string converter
…w exception if this is attempt; docs + test improvements
@flexponsive thanks so much for this PR. This is definitely a nice feature. This is a breaking change because of the below, but that should not stop us from implementing it:
The current implementation makes a number of change to the |
…sage accessor class; add e2e tests
Thank you for reviewing this PR and creating such a useful package! One of the great strengths of I appreciate the suggestion to add middleware support for modifying requests/responses before logging. However, I have concerns that this added complexity could detract from the package's simplicity. That said, I recognize the original PR had a limitation: ad-hoc configuration of MessageAccessor wasn't possible. To address this, I made two key changes:
This approach provides full customization where needed while maintaining simplicity for most use cases. What do you think about this direction? |
I have been thinking about this for some time, because this is a very nice addition. If we go for a middleware driven approach, then it could look like this: Dedicated masking middlewareHttp::log()
->pushLogMiddleware(
new SentryMasker
)
->post(...); where the class SentryMasker implements LogMiddlewareInterface
{
/**
* Mask sensitive data in the request and response.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return array{0: RequestInterface, 1: ResponseInterface}
*/
public function handle(
RequestInterface $request,
ResponseInterface $response
): array
{
return [
PsrRequestProcessor::replaceHeader($request, 'Authorization', '***'),
PsrResponseProcessor::replaceBody($response, '3566002020360505', '************0505'),
];
}
} Config parameterIf config is passed to the log function like so: Http::log(config: [
'replace' => ['3566002020360505' => '************0505'],
'replace_headers' => ['Authorization'],
])
->post(...); then all that we do it that we push a new middleware class SentryMasker implements LogMiddlewareInterface
{
public function __construct(
private array $replace = [],
private array $replaceHeaders = [],
) {}
....
} I think the above implementation is more flexible and can be implemented without breaking backwards compatibility because this is simply a matter of implementing a middleware stack. Let me know your thoughts @flexponsive ? |
Thank you, @bilfeldt, for the detailed feedback and for expanding on the middleware concept—this certainly adds a lot of flexibility! One way forward would be to implement a Another potential enhancement made possible by the middleware approach could be setting the log level based on conditions. For instance, some APIs respond with a 200 status but include an error field in the JSON payload. By slightly adjusting the middleware interface, middlewares could enable custom log level settings in cases like these if you think it would be beneficial. Let me know what you think! |
Exactly. Http::log() // The DefaultLogMasker is pushed to the stack with the global masking settings
->post(...);
Http::log(config: [
'replace' => ['3566002020360505' => '************0505'],
'replace_headers' => ['Authorization'],
]) // The DefaultLogMasker is pushed to the stack with a merger of the global settings and those provided above
->post(...);
I get that some APIs do not use proper HTTP status codes like your example. But I am not sure that I completely understand how you intend to use middleware to do this? I would expect one to do: Http::log(filter: new SentryFilter)->post(...) but now that I think about it, it would be cool to offer a syntax like: Http::logWhen(fn ($response, $request) => $response->json('error'))->post(...) But that is an entirely new PR :)
|
Description
This PR improves the handling of sensitive data, like passwords and auth tokens, by making it possible to easily configure the replacing of sensitive values, headers etc. in the package configuration. It does this by integrating the existing
MessageAccessor
(#13) with the `PsrMessageToStringConverter``The configuration file template now includes detailed documentation on replacement options available for sensitive data, including:
Backward Compatibility: By default, this update does not alter the package’s behavior, ensuring compatibility for existing users. However, since logging sensitive data is such a widespread problem, it may be worthwhile to consider in a future release to mask the
Authentication
andAuthorization
headers by default.Type of change
Please delete options that are not relevant.
Checklist