Skip to content

Strip OpenSSH-style inline comments from SSHConfig values - #2727

Open
inchang-ing wants to merge 1 commit into
paramiko:mainfrom
inchang-ing:fix/sshconfig-inline-comments
Open

inchang-ing wants to merge 1 commit into
paramiko:mainfrom
inchang-ing:fix/sshconfig-inline-comments

Conversation

@inchang-ing

Copy link
Copy Markdown

Fixes #2726.

Problem

SSHConfig.parse() keeps trailing comments as part of a config value:

Host web
    User admin # backup

lookup("web")["user"] returns 'admin # backup', while ssh -F c.conf -G web
reports admin. Quoted values and mid-token # characters must stay literal:
"ad # min" # tail should yield ad # min, and admin#frag must remain
admin#frag. Stripping every # would therefore be wrong.

Change

Add SSHConfig._strip_inline_comment() and apply it to the value captured by
SETTINGS_REGEX, before the existing Host/Match/keyword handling. It
follows OpenSSH's argv_split comment handling: # starts a comment only at a
token boundary (start of the value or right after whitespace), never inside
single or double quotes, and never in the middle of a token. Quotes and escaped
characters are otherwise left alone, so the existing quote removal, multi-word
values, =-style values, IdentityFile lists and %-substitution keep their
current behaviour. No control flow is changed: a Host/Match line still
switches context even when its value consists only of a comment.

Verification

  • 12 new cases in tests/test_config.py: trailing comments (including after a
    tab and at end of value), quoted #, literal mid-token #, values without
    comments, Host lines with comments, and a mixed block with multi-word,
    quoted and ProxyCommand/IdentityFile values.
  • Pre-fix (tests only, source unchanged): 6 failed, 119 passed, 7 skipped.
    The six failures are exactly the trailing-comment, Host-line and quoted-value
    cases; the six cases that were already correct still pass.
  • Post-fix: 125 passed, 7 skipped on tests/test_config.py.
  • flake8 7.3.x reports zero issues on both changed files (this is the lint
    command pinned by the project's dev dependencies). black 22.12 and local
    ruff produce only pre-existing findings on untouched lines; the patch adds
    none.
  • On a broader explicit file list, baseline and patch have identical failure
    sets (one pre-existing Windows-only integration failure), with 202 -> 214
    passed, i.e. exactly the 12 new cases.

Limitations

  • The OpenSSH binary was not run locally; the token-boundary rule comes from the ssh -G result quoted in the issue and from OpenSSH's argv_split comment handling.
  • A full-repository pytest run cannot be collected on Windows with pytest 9 because of the pre-existing module-level pytest.skip() in tests/test_sftp.py (identical before and after this change); an explicit list of test files was used instead to compare failure sets.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSHConfig keeps trailing comments in values, unlike OpenSSH

1 participant