Skip to content

[WIP] fix(ssh): parse and write allowed signers the way OpenSSH does - #53

Closed
timweri wants to merge 3 commits into
mainfrom
fix/allowed-signer-display
Closed

timweri wants to merge 3 commits into
mainfrom
fix/allowed-signer-display

Conversation

@timweri

@timweri timweri commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Why

AllowedSigner's Display wrote lines that OpenSSH rejects: options were space-separated, values were unquoted, and timestamps were raw UNIX seconds. The parser had the same problems in reverse. It accepted lines OpenSSH rejects, and read some lines differently from OpenSSH.

What

Three commits, each green on its own:

  1. Parse like OpenSSH. Replaces AllowedSignerSplitter with a parser that follows OpenSSH's code:
    • from_string follows parse_principals_key_and_options() and the option loop in sshsigopt_parse() (sshsig.c).
    • Its helpers are ports named after the C functions, so they can be diffed against the source: strdelimw() (misc.c), advance_past_options() for sshkey_advance_past_options() (sshkey.c), and opt_flag() / opt_match() / opt_dequote() (auth-options.c).
  2. Write lines OpenSSH reads back. Adds AllowedSigner::to_line() -> Result<String>; Display writes the same line. The format is principals [cert-authority,namespaces="…",valid-after="…Z",valid-before="…Z"] keytype base64. Principals are quoted when needed. to_line() fails, rather than writing something OpenSSH would read back differently. The key's comment is left out: from_string ignores it, and a line break in it would add another entry.
  3. Every parsed entry can be written. from_string rejects what to_line can't write: a line feed or NUL in a principal or namespace, and a certificate as the key. Adds an interop test that checks to_line() output with ssh-keygen -Y verify. The test is skipped when ssh-keygen isn't installed.

Breaking changes (for the 0.16.0 release notes)

  • Rejected now (OpenSSH rejects these too):
    • space-separated options
    • unquoted option values (namespaces=git, valid-after=20240101)
    • unknown options
    • a trailing comma in the options field
    • valid-after / valid-before at or before the epoch
  • Migration: lines written by the old Display with namespaces or timestamps no longer parse, and AllowedSigners fails the whole file on the first bad line. Rewrite those lines in the format above.
  • Accepted now (OpenSSH accepts these too):
    • tabs between fields
    • a Z/UTC suffix in any case
    • \" inside a quoted value
    • an empty option between two commas
    • any text after the key, read as a comment
  • Certificate keys are rejected on both sides. Previously the crate read the wrong bytes as the key.
  • Display can fail, so to_string() panics, for entries built by hand that can't be written. Use to_line() to check first.
  • AllowedSignerParsingError is now #[non_exhaustive], and UnexpectedEnd is removed.
  • chrono must be at least 0.4.31.

Verification

  • cargo test, including:
    • cases ported from OpenSSH's regress/unittests/misc/test_strdelim.c and test_convtime.c, and the allowed-signers lines in regress/sshsig.sh
    • parse → write → parse round trips
    • tests/allowed-signer-interop.rs against ssh-keygen (OpenSSH 10.2p1 locally)
  • The parser was also compared case by case with ssh-keygen -Y verify / find-principals on crafted lines.

Known gaps / follow-ups (not in this PR)

  • Bad lines: AllowedSigners fails the whole file on one bad line; OpenSSH's verify and find-principals skip it. A lenient API could return the signers plus per-line errors. The error's line number also counts from 0.
  • Certificate keys: support them once PublicKey reads and writes certificates correctly.
  • Timestamps that differ from glibc OpenSSH:
    • blanks inside the value ("2020 101Z")
    • out-of-range dates OpenSSH normalizes (20230231Z, second 60/61)
    • the ambiguous local hour when clocks fall back (we pick the earlier instant)
  • Empty principals: OpenSSH accepts empty entries (a,,b); we reject them.
  • Whitespace: AllowedSigners trims Unicode whitespace from each line; OpenSSH trims only space, tab, CR and LF.
  • Patterns: principals and namespaces are OpenSSH pattern lists (*, ?, !), not literal names. The field docs don't say so yet.

@timweri timweri changed the title fix(ssh): write allowed signers in the format OpenSSH accepts [WIP] fix(ssh): write allowed signers in the format OpenSSH accepts Sep 27, 2026
Replace AllowedSignerSplitter with a parser that follows OpenSSH's
parse_principals_key_and_options() and sshsigopt_parse() (sshsig.c).
Its helpers port strdelimw() (misc.c), sshkey_advance_past_options()
(sshkey.c), and opt_flag(), opt_match() and opt_dequote()
(auth-options.c).

Options are one comma-separated field with double-quoted values, as
OpenSSH requires. Space-separated options and unquoted values, which
OpenSSH rejects, are now rejected too. Like OpenSSH 10.2, valid-after
and valid-before must be after the epoch, and the timestamp suffix (Z
or UTC) is case-insensitive.

AllowedSignerParsingError::UnexpectedEnd is removed: like OpenSSH, any
text after the key is a comment.
Display wrote space-separated options with unquoted values and raw
UNIX timestamps, which OpenSSH rejects. Add AllowedSigner::to_line(),
which writes one comma-separated options field with quoted values,
timestamps as YYYYMMDDHHMMSSZ, and quotes the principals when needed.
Display writes the same line.

to_line() fails when OpenSSH would read a field back differently, such
as a comma inside a principal or a timestamp that isn't after the
epoch. It also fails for a certificate key, which PublicKey can't encode.

The key's comment is left out. from_string() ignores it, and a line
break in it would add another entry to the file.

DateTime::from_timestamp() needs chrono 0.4.31.
from_string() accepted a few entries that to_line() can't write, so
to_string() panicked on them. Reject them when parsing instead:

- a line feed or NUL in a principal or namespace. OpenSSH would read a
  line feed as the start of a new entry, and reads a line as a C string.
- a certificate as the key. PublicKey reads the wrong bytes as the key.

Mark AllowedSignerParsingError as #[non_exhaustive].

Add a test that checks lines written by to_line() with ssh-keygen -Y
verify. It's skipped when ssh-keygen isn't installed.
@timweri
timweri force-pushed the fix/allowed-signer-display branch from 74e1294 to bff1450 Compare November 10, 2026 01:14
@timweri timweri changed the title [WIP] fix(ssh): write allowed signers in the format OpenSSH accepts [WIP] fix(ssh): parse and write allowed signers the way OpenSSH does Nov 10, 2026
@timweri timweri closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant