-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sshd_config): Add analyzer support for tokens
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ func Analyze( | |
} | ||
|
||
analyzeMatchBlocks(ctx) | ||
analyzeTokens(ctx) | ||
|
||
return ctx.diagnostics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |