The vulnerability lies in the direct printing of user-controllable input to the console without proper sanitization. The badkeys command-line tool processes various inputs like DKIM keys, SSH keys, filenames, and hostnames. The analysis of the patches reveals that several functions were printing these inputs directly within informational or warning messages.
The root cause is the lack of escaping for ASCII control characters. An attacker could provide a specially crafted filename, hostname, or key data containing sequences like ANSI escape codes. When the badkeys tool would print a message containing this data, the terminal would interpret these sequences, allowing the attacker to manipulate the console output, hide information, or create misleading results.
The patches address this by introducing a new utility function, _esc, which uses repr() to escape non-printable characters in the input string before it is printed. This sanitization is applied in all identified vulnerable locations within the runcli and dkim modules, effectively neutralizing the injection vector.