Skip to content

fix(keys): keep key enrolment on its own line and across rotation - #12

Open
andreas-byfut wants to merge 1 commit into
LordOfPolls:masterfrom
andreas-byfut:fix/key-enrolment
Open

andreas-byfut wants to merge 1 commit into
LordOfPolls:masterfrom
andreas-byfut:fix/key-enrolment

Conversation

@andreas-byfut

Copy link
Copy Markdown
Contributor
  • Pushing a key to a host whose authorized_keys doesn't end in a newline glued the new key onto
    the last entry and corrupted both. The command now adds a newline only when the file ends
    mid-line.
    • The command is built by enrolmentCommand(key). Tests run it through /bin/sh for missing,
      empty, newline-terminated, unterminated and already-present files.
    • It uses only syntax that both POSIX sh and csh/tcsh accept, since sshd runs it through the
      account's login shell. It has no $(…), braces or [ ].
  • The enrolment section's result, failure flag and switched flag survive rotation
    (rememberSaveable).

Known limits:

  • The generated private key is deliberately not saved. If the screen rotates after a push and
    before "Use this key for this host", the section says the key was not kept and must be pushed
    again. The earlier key stays in authorized_keys.
  • csh/tcsh syntax is checked by reading the code and by a syntax test, not by running a csh.

Tests: KeyEnrolmentCommandTest.

Pushing a key appended it without checking that authorized_keys ended in a
newline, so a file without one had the new key glued onto its last entry.
The command now adds a newline only when the file ends mid-line, and is
built by a testable function. It uses only syntax that both POSIX sh and
csh/tcsh accept, since sshd runs it through the account's login shell.

The enrolment section's result message, failure flag and switched flag are
now rememberSaveable so a rotation no longer drops them. The generated key
stays in plain remember because it holds the private key; when a rotation
leaves a restored success without it, the section says the key was not kept
and must be pushed again.
@LordOfPolls

Copy link
Copy Markdown
Owner

Thanks. Tested and this works under sh, dash, bash and tcsh. Needs 2 revisions before i can merge however.

echo >> $file doesn't write a newline under BSD csh, Suggest printf '\n' >> $file.

grep -q . depends on the locale. A file ending in a multibyte character with no trailing newline (user@café) gets the key glued on under UTF-8.
Maybe tail -c1 $file | tr -d '\n' | grep -q '' && printf '\n' >> $file ? Would suggest writing a test and addressing accordingly.
Candidly, i dont think the master branch handles that properly either, so more of a nit

@LordOfPolls LordOfPolls added bug Something isn't working needs-revision labels Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs-revision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants