Skip to content

Commit

Permalink
feat(sshd_config): Add analyzer support for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 15, 2024
1 parent c463053 commit 33a58a3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/handlers/sshd_config/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Analyze(
}

analyzeMatchBlocks(ctx)
analyzeTokens(ctx)

return ctx.diagnostics
}
49 changes: 49 additions & 0 deletions server/handlers/sshd_config/analyzer/tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package analyzer

import (
"config-lsp/common"
"config-lsp/handlers/sshd_config/fields"
"config-lsp/utils"
"fmt"
"strings"

protocol "github.com/tliron/glsp/protocol_3_16"
)

func analyzeTokens(
ctx *analyzerContext,
) {
for _, option := range ctx.document.Config.GetAllOptions() {
if option.Key == nil || option.OptionValue == nil {
continue
}

key := option.Key.Key
text := option.OptionValue.Value.Value
var tokens []string

if foundTokens, found := fields.OptionsTokensMap[key]; found {
tokens = foundTokens
} else {
tokens = []string{}
}

disallowedTokens := utils.Without(utils.KeysOfMap(fields.AvailableTokens), tokens)

for _, token := range disallowedTokens {
if strings.Contains(text, token) {
optionName := string(key)

if formatted, found := fields.FieldsNameFormattedMap[key]; found {
optionName = formatted
}

ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
Range: option.OptionValue.ToLSPRange(),
Message: fmt.Sprintf("Token '%s' is not allowed for option '%s'", token, optionName),
Severity: &common.SeverityError,
})
}
}
}
}
62 changes: 62 additions & 0 deletions server/handlers/sshd_config/analyzer/tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package analyzer

import (
testutils_test "config-lsp/handlers/sshd_config/test_utils"
"testing"

protocol "github.com/tliron/glsp/protocol_3_16"
)

func TestInvalidTokensForNonExisting(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
ThisOptionDoesNotExist Hello%%World
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}

analyzeTokens(ctx)

if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}

func TestInvalidTokensForExistingOption(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
Tunnel Hello%%World
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}

analyzeTokens(ctx)

if !(len(ctx.diagnostics) == 1) {
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
}
}

func TestValidTokens(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
AuthorizedPrincipalsCommand Hello World %% and %d
`)
ctx := &analyzerContext{
document: d,
diagnostics: make([]protocol.Diagnostic, 0),
}

analyzeTokens(ctx)

if len(ctx.diagnostics) > 0 {
t.Fatalf("Expected no errors, but got %v", len(ctx.diagnostics))
}
}

0 comments on commit 33a58a3

Please sign in to comment.