Skip to content

add glob pattern support for known_hosts #15508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025

Conversation

motorailgun
Copy link
Contributor

What does this PR try to resolve?

This PR implements glob pattern match for known_hosts file. Hosts written with * and ? now matches correctly.

Tests

Tests are added accordingly.

Miscs

This is my first time submitting PR, sorry if there's anything that's off although I've read the contributor guide.

@rustbot
Copy link
Collaborator

rustbot commented May 9, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-git Area: anything dealing with git S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2025
@motorailgun
Copy link
Contributor Author

motorailgun commented May 9, 2025

Oops, fixing the failing tests... (edit: fixed)

regex::escape(&pattern.replace("*", ",,,,").replace("?", ";;;;"))
.replace(",,,,", ".*")
.replace(";;;;", ".");
if let Ok(regex) = Regex::new(&regex_pattern) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do anything on errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, my thought is that if the pattern string could not be interpreted as glob pattern it's better to just pass glob matching.

Copy link
Member

Choose a reason for hiding this comment

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

We could probably emit a tracing::warn! on failing cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds nice!

@motorailgun motorailgun requested a review from epage May 10, 2025 09:55
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Thanks. Just one nit and we are good to go!

@@ -588,7 +589,14 @@ impl KnownHost {
}
for pattern in self.patterns.split(',') {
let pattern = pattern.to_lowercase();
// FIXME: support * and ? wildcards
Copy link
Member

Choose a reason for hiding this comment

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

Was under the impression that this feature is tracked in #11577, but apparently not.

It seems like OpenSSH supports only * and ? wildcards. One caveat of using glob here is we provide more than we should. Anyway, it is good to catch up with the common format.

@motorailgun motorailgun requested a review from weihanglo May 10, 2025 15:44
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Great! Thanks for the contribution :)

@weihanglo weihanglo added this pull request to the merge queue May 10, 2025
Merged via the queue into rust-lang:master with commit 13625ce May 10, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-git Area: anything dealing with git S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants