Skip to content

[BUG] - ssh_config Match tagged statement always applies #2715

Open

Description

@madduck

Are you using paramiko as a client or server?

Client

What feature(s) aren't working right?

sshconfig

What version(s) of paramiko are you using?

4.0.0

What version(s) of Python are you using?

3.14.8

What operating system and version are you using?

Debian sid

If you're connecting as a client, which SSH server are you connecting to?

No response

If you're using paramiko as part of another tool, which tool/version?

No response

Expected/desired behavior

With an SSH config like:

Match tagged foobar
  User jane

Host example.org
  User john

Paramiko should connect to example.org as user john, especially since tag matching isn't supported at all.

Actual behavior

It connects as jane, but only if the Match appears before the Host.

How to reproduce

Here is the reproduction of the problem. Note how the problem only takes place when the Match appears before the Host:

import paramiko

USER1 = "john"
USER2 = "jane"

SSH_CONFIG_HOST = f"""
Host example.org
  User {USER1}
"""

SSH_CONFIG_TAG = f"""
Match tagged foobar
  User {USER2}
"""

ssh_config = paramiko.SSHConfig.from_text(SSH_CONFIG_HOST)
host_config = ssh_config.lookup("example.org")
assert (user := host_config.get("user")) == USER1, f"SSH config returns User {user}"

ssh_config = paramiko.SSHConfig.from_text(SSH_CONFIG_HOST + SSH_CONFIG_TAG)
host_config = ssh_config.lookup("example.org")
assert (user := host_config.get("user")) == USER1, f"SSH config returns User {user}"

ssh_config = paramiko.SSHConfig.from_text(SSH_CONFIG_TAG + SSH_CONFIG_HOST)
host_config = ssh_config.lookup("example.org")
assert (user := host_config.get("user")) == USER1, f"SSH config returns User {user}"

Anything else?

No response

Activity

  1. AtharvRG commented on Oct 31, 2026

    @AtharvRG

    I'd like to take this. Since paramiko doesn't support tag matching, Match tagged ... should never apply - but the Match handling in SSHConfig.parse() appears to treat unknown/tagged conditions as satisfied when the block precedes the Host, so its User jane overrides the later host-specific User john. My plan: reproduce with the exact three-case script from the report, trace how Match conditions are evaluated in ssh_config.py (looking at how OpenSSH's Match tagged semantics - "matches only if a tag was explicitly given by the CLI" - should map to paramiko, where no CLI tag exists, so it should always be False), and make unsupported Match conditions evaluate to never-match rather than defaulting to match. Regression test with the report's config plus a case with a supported Match condition (e.g. Match host) to confirm those still work. I'd flag whether maintainers want a UserWarning on encountering unsupported Match conditions, since silent no-op vs explicit warning is a behavior choice.

  2. madduck commented on Oct 31, 2026

    @madduck
    Author

    Plan reads well. I would stay silent, unless a Host was encountered with Tag foobar, because that is the other way to select a Match tagged stanza, not only via CLI. In that case, the user should be made aware that paramiko won't honour the tag — which of course might actually be the better fix of the two, i.e. implement Match tagged ;)

  3. Parsiffall1 commented on Nov 7, 2026

    @Parsiffall1

    Working on a fix for this — will open a PR with a regression test covering the three-case repro from the report.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions