feat(ssh_config): Do not handle includes if they contain tokens or environmental variables

This commit is contained in:
Myzel394 2024-10-14 12:16:55 +02:00
parent fdb005a75d
commit 4cdc916d51
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
3 changed files with 106 additions and 1 deletions

View File

@ -12,17 +12,44 @@ import (
"path"
"path/filepath"
"regexp"
"strings"
protocol "github.com/tliron/glsp/protocol_3_16"
)
var whitespacePattern = regexp.MustCompile(`\S+`)
var environmtalVariablePattern = regexp.MustCompile(`\${.+?}`)
var availableTokens = []string{
"%%",
"%C",
"%d",
"%f",
"%H",
"%h",
"%l",
"%i",
"%j",
"%K",
"%k",
"%L",
"%l",
"%n",
"%p",
"%r",
"%T",
"%t",
"%u",
}
func analyzeIncludeValues(
ctx *analyzerContext,
) {
for _, include := range ctx.document.Indexes.Includes {
for _, value := range include.Values {
if isImpossibleToVerify(value.Value) {
continue
}
validPaths, err := createIncludePaths(value.Value)
if err != nil {
@ -38,6 +65,24 @@ func analyzeIncludeValues(
}
}
// We can't evaluate environmental variables or tokens as we don't know the actual
// values
func isImpossibleToVerify(
path string,
) bool {
if environmtalVariablePattern.MatchString(path) {
return true
}
for _, token := range availableTokens {
if strings.Contains(path, token) {
return true
}
}
return false
}
func createIncludePaths(
suggestedPath string,
) ([]indexes.ValidPath, error) {
@ -45,6 +90,14 @@ func createIncludePaths(
if path.IsAbs(suggestedPath) {
absolutePath = suggestedPath
} else if strings.HasPrefix(suggestedPath, "~") {
homeFolder, err := os.UserHomeDir()
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not find home folder (error: %s)", err))
}
absolutePath = path.Join(homeFolder, suggestedPath[1:])
} else {
homeFolder, err := os.UserHomeDir()

View File

@ -0,0 +1,43 @@
package handlers
import (
"config-lsp/common"
"config-lsp/handlers/ssh_config/indexes"
"config-lsp/utils"
"slices"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func GetIncludeOptionLocation(
include *indexes.SSHIndexIncludeLine,
index common.IndexPosition,
) []protocol.Location {
foundIndex, found := slices.BinarySearchFunc(
include.Values,
index,
func(current *indexes.SSHIndexIncludeValue, target common.IndexPosition) int {
if current.IsPositionAfterEnd(target) {
return -1
}
if current.IsPositionBeforeStart(target) {
return 1
}
return 0
},
)
if !found {
return nil
}
path := include.Values[foundIndex]
return utils.Map(path.Paths, func(path indexes.ValidPath) protocol.Location {
return protocol.Location{
URI: path.AsURI(),
}
})
}

View File

@ -1,8 +1,10 @@
package lsp
import (
"config-lsp/common"
sshconfig "config-lsp/handlers/ssh_config"
"config-lsp/handlers/ssh_config/fields"
"config-lsp/handlers/ssh_config/handlers"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
@ -12,9 +14,16 @@ var tagOption = fields.CreateNormalizedName("Tag")
func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
line := params.Position.Line
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
d := sshconfig.DocumentParserMap[params.TextDocument.URI]
if include, found := d.Indexes.Includes[line]; found {
return handlers.GetIncludeOptionLocation(
include,
index,
), nil
}
option, _ := d.Config.FindOption(line)
if option != nil && option.Key.Key == tagOption && option.OptionValue != nil {