Skip to content

fix: warn and skip unknown Match criteria in SSHConfig - #2640

Open
kaizeenn wants to merge 1 commit into
paramiko:mainfrom
kaizeenn:fix/2632-unknown-match-criteria
Open

kaizeenn wants to merge 1 commit into
paramiko:mainfrom
kaizeenn:fix/2632-unknown-match-criteria

Conversation

@kaizeenn

@kaizeenn kaizeenn commented Jul 3, 2026

Copy link
Copy Markdown

Summary

Fixes #2632.

When paramiko encounters an unknown Match criterion in ssh_config (e.g. localnetwork, a valid OpenSSH keyword that paramiko doesn't support), it silently treats the criterion as matching. This causes Match blocks with unsupported criteria to be incorrectly applied.

Root cause

The _does_match method in config.py has an if/elif chain for each known Match type (canonical, final, all, host, originalhost, user, localuser, exec). Unknown types like localnetwork fall through without setting passed, so passed stays None and the candidate is silently added to the matched list.

Fix

  1. Log a warning for unknown Match criteria
  2. Set passed = False so the Match block is treated as not matching (matching OpenSSH behavior)
  3. Restructured the canonical handling from a standalone if into the elif chain to ensure the new else clause only catches truly unknown types

Test evidence

Added match-unknown-criteria test config and TestMatchUnknownCriteria class with two tests:

$ .venv/bin/pytest tests/test_config.py -k TestMatchUnknownCriteria -v
...
MatchUnknownCriteria
    unknown criteria skipped ... PASSED
    unknown criteria does not pollute know hosts ... PASSED

$ .venv/bin/pytest tests/test_config.py
...
115 passed, 7 skipped in 0.14s

The new warning is emitted at runtime when unknown criteria are encountered:

WARNING paramiko.config: Unknown Match criterion 'localnetwork' not supported by paramiko; treating Match block as not matching.

When paramiko encounters an unknown Match criterion in ssh_config
(e.g. `localnetwork`, a valid OpenSSH keyword that paramiko doesn't
support), it silently treats the criterion as matching. This causes
Match blocks with unsupported criteria to be incorrectly applied.

Fix by logging a warning and setting `passed = False` for any
unknown Match criterion, so the Match block is treated as not
matching — matching OpenSSH's own behavior for unsupported keywords.

Also restructures the `if type_ == "canonical"` block from a
standalone `if` to match `elif` consistency so the new `else`
clause correctly only catches truly unknown types.

Adds a test config file and two test methods covering the case
where an unknown criterion causes the block to be skipped while
other blocks are unaffected.

Fixes paramiko#2632.

Signed-off-by: kaizeenn <[email protected]>
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.

[BUG] - Configuration under Match statements with invalid or unknown Match criteria words is

1 participant