From 84cd87908e81da33c8daa5646668939d7ea6afa7 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 29 Jun 2026 07:48:17 +0800 Subject: [PATCH] vim-patch:9.2.0742: filetype: SSH keys and related filetypes not recognized (#40469) Problem: filetype: SSH keys and related filetypes not recognized Solution: Detect sshpublickey, sshknownhosts sshauthorizedkeys and sshallowedsigners filetypes, add syntax scripts for those filetypes (Fionn Fitzmaurice) This adds syntax highlighting for SSH public keys, as well as related filetypes derived from this (SSH authorized keys, SSH known hosts and SSH allowed signers). Also add filetype detection based on the path and name. closes: vim/vim#20635 https://github.com/vim/vim/commit/6e66ebc0fdd7804db4baa076e62c78d208adaeaf Co-authored-by: Fionn Fitzmaurice --- runtime/lua/vim/filetype.lua | 13 +++++++++--- runtime/syntax/sshallowedsigners.vim | 31 ++++++++++++++++++++++++++++ runtime/syntax/sshauthorizedkeys.vim | 25 ++++++++++++++++++++++ runtime/syntax/sshknownhosts.vim | 27 ++++++++++++++++++++++++ runtime/syntax/sshpublickey.vim | 30 +++++++++++++++++++++++++++ test/old/testdir/test_filetype.vim | 4 ++++ 6 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 runtime/syntax/sshallowedsigners.vim create mode 100644 runtime/syntax/sshauthorizedkeys.vim create mode 100644 runtime/syntax/sshknownhosts.vim create mode 100644 runtime/syntax/sshpublickey.vim diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 6c4e5fd43a..d04a355cf5 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -1279,6 +1279,7 @@ local extension = { srt = 'srt', ssa = 'ssa', ass = 'ssa', + allowed_signers = 'sshallowedsigners', st = 'st', ipd = 'starlark', sky = 'starlark', @@ -1981,8 +1982,9 @@ local filename = { Snakefile = 'snakemake', ['.sqlite_history'] = 'sql', ['squid.conf'] = 'squid', - ['ssh_config'] = 'sshconfig', - ['sshd_config'] = 'sshdconfig', + allowed_signers = 'sshallowedsigners', + ssh_config = 'sshconfig', + sshd_config = 'sshdconfig', ['/etc/sudoers'] = 'sudoers', ['sudoers.tmp'] = 'sudoers', ['/etc/sysctl.conf'] = 'sysctl', @@ -2181,9 +2183,11 @@ local pattern = { ['/etc/slp%.conf$'] = 'slpconf', ['/etc/slp%.reg$'] = 'slpreg', ['/etc/slp%.spi$'] = 'slpspi', - ['/etc/sudoers%.d/'] = starsetf('sudoers'), ['/etc/ssh/ssh_config%.d/.*%.conf$'] = 'sshconfig', ['/etc/ssh/sshd_config%.d/.*%.conf$'] = 'sshdconfig', + ['^/etc/ssh/ssh_known_hosts$'] = 'sshknownhosts', + ['^/etc/ssh/.+%.pub$'] = 'sshpublickey', + ['/etc/sudoers%.d/'] = starsetf('sudoers'), ['/etc/sudoers$'] = 'sudoers', ['/etc/sysctl%.conf$'] = 'sysctl', ['/etc/sysctl%.d/.*%.conf$'] = 'sysctl', @@ -2543,6 +2547,9 @@ local pattern = { ['/%.pinforc$'] = 'pinfo', ['^${HOME}/%.xinitrc$'] = 'sh', ['^${HOME}/%.xserverrc$'] = 'sh', + ['/%.ssh/authorized_keys$'] = 'sshauthorizedkeys', + ['/%.ssh/known_hosts$'] = 'sshknownhosts', + ['/%.ssh/.+%.pub$'] = 'sshpublickey', ['/%.cargo/credentials$'] = 'toml', ['/%.init/.*%.override$'] = 'upstart', ['/%.kube/kuberc$'] = 'yaml', diff --git a/runtime/syntax/sshallowedsigners.vim b/runtime/syntax/sshallowedsigners.vim new file mode 100644 index 0000000000..8392747e8a --- /dev/null +++ b/runtime/syntax/sshallowedsigners.vim @@ -0,0 +1,31 @@ +" Vim syntax file +" Language: OpenSSH allowed signers file +" Author: Fionn Fitzmaurice (github.com/fionn) +" Maintainer: Fionn Fitzmaurice (github.com/fionn) +" License: Vim & Apache 2.0 + +if exists("b:current_syntax") + finish +endif + +syn match sshAllowedSignersPrincipal "!\?[a-zA-Z0-9.*?_+-]\+@[a-zA-Z0-9.*?-]\+" nextgroup=sshAllowedSignersPrincipalSeparator,sshAllowedSignersOptions,sshKeyType skipwhite +syn match sshAllowedSignersPrincipalSeparator "," contained nextgroup=sshAllowedSignersPrincipal + +syn region sshAllowedSignersOptions start="[a-z]" end="\s\@=" contains=@sshAllowedSignersOption nextgroup=sshKeyType skipwhite oneline contained +syn cluster sshAllowedSignersOption contains=sshAllowedSignersOptionKeyword,sshAllowedSignersOptionSeparator,sshAllowedSignersOptionAssignment,sshAllowedSignersOptionValue +syn keyword sshAllowedSignersOptionKeyword namespaces cert-authority valid-after valid-before contained +syn match sshAllowedSignersOptionSeparator "," contained +syn match sshAllowedSignersOptionAssignment "=" contained +syn match sshAllowedSignersOptionValue '"\(\\\"\|[^"]\)*"' contained + +runtime! syntax/sshpublickey.vim + +hi def link sshAllowedSignersPrincipal Identifier +hi def link sshAllowedSignersPrincipalSeparator Punctuation + +hi def link sshAllowedSignersOptionKeyword Keyword +hi def link sshAllowedSignersOptionSeparator Punctuation +hi def link sshAllowedSignersOptionAssignment Operator +hi def link sshAllowedSignersOptionValue String + +let b:current_syntax = "sshallowedsigners" diff --git a/runtime/syntax/sshauthorizedkeys.vim b/runtime/syntax/sshauthorizedkeys.vim new file mode 100644 index 0000000000..cf6b912fbc --- /dev/null +++ b/runtime/syntax/sshauthorizedkeys.vim @@ -0,0 +1,25 @@ +" Vim syntax file +" Language: OpenSSH authorized keys file +" Author: Fionn Fitzmaurice (github.com/fionn) +" Maintainer: Fionn Fitzmaurice (github.com/fionn) +" License: Vim & Apache 2.0 + +if exists("b:current_syntax") + finish +endif + +syn region sshAuthorizedKeyOptions start="^[a-z]" end="\s" contains=@sshAuthorizedKeyOption nextgroup=sshKeyType skipwhite oneline +syn cluster sshAuthorizedKeyOption contains=sshAuthorizedKeyOptionKeyword,sshAuthorizedKeyOptionSeparator,sshAuthorizedKeyOptionAssignment,sshAuthorizedKeyOptionValue +syn match sshAuthorizedKeyOptionKeyword "[a-z-]\+" contained +syn match sshAuthorizedKeyOptionSeparator "," contained +syn match sshAuthorizedKeyOptionAssignment "=" contained +syn match sshAuthorizedKeyOptionValue '"\(\\\"\|[^"]\)*"' contained + +runtime! syntax/sshpublickey.vim + +hi def link sshAuthorizedKeyOptionKeyword Keyword +hi def link sshAuthorizedKeyOptionSeparator Punctuation +hi def link sshAuthorizedKeyOptionAssignment Operator +hi def link sshAuthorizedKeyOptionValue String + +let b:current_syntax = "sshauthorizedkeys" diff --git a/runtime/syntax/sshknownhosts.vim b/runtime/syntax/sshknownhosts.vim new file mode 100644 index 0000000000..5d41deb79b --- /dev/null +++ b/runtime/syntax/sshknownhosts.vim @@ -0,0 +1,27 @@ +" Vim syntax file +" Language: OpenSSH known hosts file +" Author: Fionn Fitzmaurice (github.com/fionn) +" Maintainer: Fionn Fitzmaurice (github.com/fionn) +" License: Vim & Apache 2.0 + +if exists("b:current_syntax") + finish +endif + +runtime! syntax/sshpublickey.vim + +syn match sshKnownHostsMarker "^@cert-authority\>" nextgroup=sshKnownHostsHostname,sshKnownHostsHashedHostname skipwhite +syn match sshKnownHostsMarker "^@revoked\>" nextgroup=sshKnownHostsHostname,sshKnownHostsHashedHostname skipwhite + +syn match sshKnownHostsHostname "!\?[a-zA-Z0-9.*-]\+" nextgroup=sshKnownHostsHostnameSeparator,sshKeyType skipwhite +syn match sshKnownHostsHostname "!\?\[[a-zA-Z0-9.*-]\+\]:[0-9]\{1,5}" nextgroup=sshKnownHostsHostnameSeparator,sshKeyType skipwhite +syn match sshKnownHostsHostnameSeparator "," contained nextgroup=sshKnownHostsHostname + +syn match sshKnownHostsHashedHostname "|1|[a-zA-Z0-9/+]\+=\{,2}|[a-zA-Z0-9/+]\+=\{,2}" nextgroup=sshKeyType skipwhite + +hi def link sshKnownHostsMarker Statement +hi def link sshKnownHostsHostname Identifier +hi def link sshKnownHostsHostnameSeparator Punctuation +hi def link sshKnownHostsHashedHostname Identifier + +let b:current_syntax = "sshknownhosts" diff --git a/runtime/syntax/sshpublickey.vim b/runtime/syntax/sshpublickey.vim new file mode 100644 index 0000000000..200378ae0e --- /dev/null +++ b/runtime/syntax/sshpublickey.vim @@ -0,0 +1,30 @@ +" Vim syntax file +" Language: OpenSSH public key +" Author: Fionn Fitzmaurice (github.com/fionn) +" Maintainer: Fionn Fitzmaurice (github.com/fionn) +" License: Vim & Apache 2.0 + +if exists("b:current_syntax") + finish +endif + +setlocal iskeyword=_,.,@-@,-,a-z,A-Z,48-57 + +syn keyword sshKeyType ssh-ed25519 nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType sk-ssh-ed25519@openssh.com nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType ecdsa-sha2-nistp256 nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType ecdsa-sha2-nistp384 nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType ecdsa-sha2-nistp521 nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType sk-ecdsa-sha2-nistp256@openssh.com nextgroup=sshKeyBase64Encoded skipwhite +syn keyword sshKeyType ssh-rsa nextgroup=sshKeyBase64Encoded skipwhite + +syn match sshKeyBase64Encoded "AAAA[a-zA-Z0-9/+]\{64,8000}=\{,2}" contained nextgroup=sshKeyComment +syn match sshKeyComment ".*$" contained + +syn match sshKeyComment "#.*$" + +hi def link sshKeyType Type +hi def link sshKeyBase64Encoded String +hi def link sshKeyComment Comment + +let b:current_syntax = "sshpublickey" diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index 1bf7bb4df6..37c539c53f 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -789,8 +789,12 @@ func s:GetFilenameChecks() abort \ 'srec': ['file.s19', 'file.s28', 'file.s37', 'file.mot', 'file.srec'], \ 'srt': ['file.srt'], \ 'ssa': ['file.ass', 'file.ssa'], + \ 'sshallowedsigners': ['any/allowed_signers', 'any/.allowed_signers', 'any/file.allowed_signers'], + \ 'sshauthorizedkeys': ['any/.ssh/authorized_keys'], \ 'sshconfig': ['ssh_config', '/.ssh/config', '/etc/ssh/ssh_config.d/file.conf', 'any/etc/ssh/ssh_config.d/file.conf', 'any/.ssh/config', 'any/.ssh/file.conf'], \ 'sshdconfig': ['sshd_config', '/etc/ssh/sshd_config.d/file.conf', 'any/etc/ssh/sshd_config.d/file.conf'], + \ 'sshpublickey': ['any/.ssh/file.pub', '/etc/ssh/file.pub'], + \ 'sshknownhosts': ['any/.ssh/known_hosts', '/etc/ssh/ssh_known_hosts'], \ 'st': ['file.st'], \ 'starlark': ['file.ipd', 'file.sky', 'file.star', 'file.starlark'], \ 'stata': ['file.ado', 'file.do', 'file.imata', 'file.mata'],