Skip to content

SSHConfig keeps trailing comments in values, unlike OpenSSH #2726

Open

Description

@Aathi-27

SSHConfig.lookup() includes trailing comments in returned values. For example:

import paramiko
cfg = paramiko.SSHConfig.from_text("Host web\n User admin # backup\n")
print(repr(cfg.lookup("web")["user"]))

Result on Paramiko 5.1.0: 'admin # backup'

With the same configuration saved as c.conf:

ssh -F c.conf -G web

OpenSSH 8.9p1 returns the username as admin. Paramiko's current main has the same parser as 5.1.0.

Quoted values also differ. For User "ad # min" # tail, OpenSSH returns ad # min, while Paramiko retains the quotes and trailing comment. User admin#frag matches in both, so stripping every # would be incorrect.

The observed problem is incorrect values from lookup(). These could cause connection or authentication failures if a caller uses them unchanged; downstream consumers have not been tested.

Paramiko's parser skips full-line comments but keeps trailing text:
https://github.com/paramiko/paramiko/blob/main/paramiko/config.py#L122-L167

OpenSSH uses argv_split with terminate_on_comment enabled:
https://github.com/openssh/openssh-portable/blob/V_10_0_P2/readconf.c#L1197
https://github.com/openssh/openssh-portable/blob/V_10_0_P2/misc.c#L2065-L2117

This difference is not listed among the documented per-keyword departures:
https://docs.paramiko.org/en/latest/api/config.html

Would matching OpenSSH's token-boundary comment handling be welcome? Regression tests could cover trailing comments, quoted # characters and literal mid-token # characters before changing the parser.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions