Despite all the helpful tips, I still am not able to hide password of the following :gen_statem
-based RabbitMQ consumer:
defmodule Rabbit.Consumer do
@derive {Inspect, only: [:channel]}
defstruct [:config, :channel]
end
I’d like that the :config
field (above), which includes the password, be hidden in all these cases:
:sys.get_state Rabbit.Consumer
- when
Rabbit.Consumer
outright crashes and produces such stacktrace (observe password
in plain sight)
[error] CRASH REPORT Process 'Rabbit.Consumer' with 0 neighbours exited with reason:
no such process or port in call to
gen_server:call(<0.1026.0>, {call,{'basic.ack',1,false},none,<0.1012.0>}, 15000)
in gen_server:call/3 line 382
[error] Supervisor 'Rabbit.Application' had child 'Rabbit.Consumer' started with
'Rabbit.Consumer':start_link([
{host,<<"localhost">>},
{username,<<"admin">>},
{password,<<"admin">>},
{virtual_host,<<"/">>},
...])
at <0.3177.0> exit with reason
{shutdown,{gen_server,call,[<0.3196.0>,
{call,{'queue.declare',0,nil,false,true,false,false,false,[]},none,<0.3177.0>},15000]}}
in context child_terminated
What I tried already
format_status
I added :gen_statem
c:format_status/2
and also OTP 25 c:format_status/1:
def format_status(s) do # format_status/1 in OTP 25
Map.drop s.data, [:config]
end
This did help as :sys.get_status
didn’t print the sensitive :config
anymore. But, it didn’t help with neither of the cases 1. or 2. above.
Prune args from stacktrace
I wrapped Rabbit.Consumer
’s call to a different process with a rescue
and pruned args from System.stacktrace()
(using the suggested Plug.Crypto.prune_args_from_stacktrace/1
). But, this doesn’t help when :gen_statem
/Rabbit.Consumer
itself crashes. How can one prune_args_from_stacktrace
of the process itself (be it GenServer, :gen_statem, etc.)?
Process.flag(:sensitive, true)
def init(opts) do
Process.flag(:sensitive, true)
# etc.
Neither of the cases 1. or 2. above were solved.
Docs for the :sensitive
flag say
Sets or clears flag sensitive for the current process. When a process has been marked as sensitive by calling process_flag(sensitive, true), features in the runtime system that can be used for examining the data or inner working of the process are silently disabled.
Features that are disabled include (but are not limited to) the following:
-
Tracing. Trace flags can still be set for the process, but no trace messages of any kind are generated. (If flag sensitive is turned off, trace messages are again generated if any trace flags are set.)
-
Sequential tracing. The sequential trace token is propagated as usual, but no sequential trace messages are generated.
process_info/1,2 cannot be used to read out the message queue or the process dictionary (both are returned as empty lists).
Stack back-traces cannot be displayed for the process.
In crash dumps, the stack, messages, and the process dictionary are omitted.
If {save_calls,N} has been set for the process, no function calls are saved to the call saving list. (The call saving list is not cleared. Also, send, receive, and time-out events are still added to the list.)
The following bolded part from above
“Features that are disabled include (but are not limited to)”.
made me hopeful that state will also be included (among disabled features).
So, I saw the following on erlef.github.io:
“Process state […] cannot be introspected”
Alas, this doesn’t seem to be true - :sys.get_state Rabbit.Consumer
still shows the password.
Here’s this section verbatim:
Finally, a process can be marked as ‘sensitive’, using erlang:process_flag/2. This has the following effect:
- Message queue contents cannot be introspected, and is not written to a crash dump
- Process dictionary cannot be introspected, and is not written to a crash dump
- Process state of a gen_server, gen_event or gen_statem process cannot be introspected, and is not written to a crash dump
- Process heap and stack are not written to a crash dump
- The process cannot be traced