Skip to content

Make known_hosts hostname matching more lenient - #36

Open
scop wants to merge 1 commit into
peterhoeg:masterfrom
scop:known-hosts-patterns
Open

scop wants to merge 1 commit into
peterhoeg:masterfrom
scop:known-hosts-patterns

Conversation

@scop

@scop scop commented Mar 9, 2023 •

Copy link
Copy Markdown
Contributor

Documentation for the known_hosts file format specifies "Hostnames is a comma-separated list of patterns [...]". The regexps we had is unnecessariliy strict, allowing only a subset of valid sequences.

Make it more lenient (and simple) to match more valid ones.

https://man.openbsd.org/sshd#SSH_KNOWN_HOSTS_FILE_FORMAT

Wildcard support is still missing, but I'm scratching another itch I have with this (manually crafted entries with hostname followed by more than one IP address).

Summary by CodeRabbit

  • Refactor
    • Improved hostname pattern recognition in SSH known_hosts files.
    • Consolidated support for hostnames, IP addresses, and bracketed host-and-port entries.
    • Preserved highlighting behavior while simplifying pattern handling.

Documentation for the known_hosts file format specifies "Hostnames is a
comma-separated list of patterns [...]". The regexps we had is
unnecessarily strict, allowing only a subset of valid sequences.

Make it more lenient (and simple) to match more valid ones.
@scop
scop force-pushed the known-hosts-patterns branch from a136272 to 7fd4b00 Compare March 9, 2023 21:29
@peterhoeg

Copy link
Copy Markdown
Owner

Thanks for this. I would love to get some tests in place as changing regexps is otherwise very painful. Any chance you could be persuaded into adding a test or two for this as part of the PR?

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds a reusable regex for hostname, IP address, and bracketed host-port forms. The known_hosts font-lock regex uses this pattern to match comma-separated host lists.

Changes

Known hosts regex handling

Layer / File(s) Summary
Reusable hostname pattern integration
ssh-config-mode.el
Adds ssh-known-hosts-regex-hostname-pattern and uses it in ssh-known-hosts-font-lock-keywords for comma-separated hostname patterns.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change to make known_hosts hostname matching more lenient.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ssh-config-mode.el`:
- Around line 334-338: Add a regression font-lock test covering a known-host
entry with multiple comma-separated IP addresses, such as
host.example.com,192.0.2.1,192.0.2.2. Verify that the complete hostname list
receives the expected host face and that each subsequent key field retains its
expected face.
- Around line 304-313: Add ssh-known-hosts-regex-hostname-pattern to the cleanup
list used by ssh-known-hosts-mode-reload in the test helper, so reloading tests
clears the defvar-bound value and rebuilds the pattern.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4eed6bc9-1614-4eac-a5a4-b5a3d9a43bf2

📥 Commits

Reviewing files that changed from the base of the PR and between d560a08 and 7fd4b00.

📒 Files selected for processing (1)
  • ssh-config-mode.el

Comment thread ssh-config-mode.el
Comment on lines +304 to +313
(defvar ssh-known-hosts-regex-hostname-pattern
(concat
"\\(?:"
ssh-known-hosts-regex-hostname
"\\|"
ssh-known-hosts-regex-ip
"\\|"
;; [host-or-ip]:222
"\\(?:\\[" ssh-known-hosts-regex-host "\\]:" ssh-known-hosts-regex-port "\\)"
"\\)"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C 3 \
  'ssh-known-hosts-regex-hostname-pattern|makunbound|ssh-known-hosts-mode-reload' \
  ssh-config-mode.el tests/ssh-config-test-helper-funcs.el

Repository: peterhoeg/ssh-config-mode-el

Length of output: 1896


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- reload helper ---'
cat -n tests/ssh-config-test-helper-funcs.el | sed -n '1,55p'

printf '%s\n' '--- regex definitions and uses ---'
cat -n ssh-config-mode.el | sed -n '270,350p'

printf '%s\n' '--- test references ---'
rg -n -C 3 \
  'ssh-known-hosts-mode-reload|hostname-pattern|comma|known-hosts' \
  tests --glob '*.el' | sed -n '1,240p'

Repository: peterhoeg/ssh-config-mode-el

Length of output: 5848


🏁 Script executed:

#!/bin/bash
set -euo pipefail

if command -v emacs >/dev/null 2>&1; then
  emacs --batch --quick --eval '
    (progn
      (makunbound (quote probe-base))
      (makunbound (quote probe-derived))
      (eval (quote (progn
                     (defvar probe-base "old")
                     (defvar probe-derived (concat probe-base "-derived")))))
      (let ((first probe-derived))
        (makunbound (quote probe-base))
        (eval (quote (progn
                       (defvar probe-base "new")
                       (defvar probe-derived (concat probe-base "-derived")))))
        (princ (format "first=%S second=%S same=%s\n"
                       first probe-derived (eq first probe-derived))))))
  '
else
  printf '%s\n' 'Emacs is unavailable'
fi

Repository: peterhoeg/ssh-config-mode-el

Length of output: 187


Reset ssh-known-hosts-regex-hostname-pattern during test reload.

When ssh-known-hosts-mode-reload runs again, defvar preserves the existing pattern. Add ssh-known-hosts-regex-hostname-pattern to the cleanup list in tests/ssh-config-test-helper-funcs.el.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ssh-config-mode.el` around lines 304 - 313, Add
ssh-known-hosts-regex-hostname-pattern to the cleanup list used by
ssh-known-hosts-mode-reload in the test helper, so reloading tests clears the
defvar-bound value and rebuilds the pattern.

Comment thread ssh-config-mode.el
Comment on lines +334 to +338
;; list of comma separated hostname patterns
ssh-known-hosts-regex-hostname-pattern
"\\(?:,"
ssh-known-hosts-regex-hostname-pattern
"\\)*"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add a regression test for hostname lists with multiple IP addresses.

Add a font-lock test for an entry such as host.example.com,192.0.2.1,192.0.2.2. Verify that the host field and subsequent key fields receive their expected faces.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ssh-config-mode.el` around lines 334 - 338, Add a regression font-lock test
covering a known-host entry with multiple comma-separated IP addresses, such as
host.example.com,192.0.2.1,192.0.2.2. Verify that the complete hostname list
receives the expected host face and that each subsequent key field retains its
expected face.

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.

2 participants