-
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 match block analyzer
- Loading branch information
Showing
11 changed files
with
228 additions
and
15 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
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
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
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,112 @@ | ||
package analyzer | ||
|
||
import ( | ||
"config-lsp/common" | ||
sshdconfig "config-lsp/handlers/sshd_config" | ||
match_parser "config-lsp/handlers/sshd_config/fields/match-parser" | ||
"config-lsp/utils" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func analyzeMatchBlocks( | ||
d *sshdconfig.SSHDocument, | ||
) []common.LSPError { | ||
errs := make([]common.LSPError, 0) | ||
|
||
for _, indexOption := range d.Indexes.AllOptionsPerName["Match"] { | ||
matchBlock := indexOption.MatchBlock.MatchValue | ||
|
||
// Check if the match block has filled out all fields | ||
if matchBlock == nil || len(matchBlock.Entries) == 0 { | ||
errs = append(errs, common.LSPError{ | ||
Range: indexOption.Option.LocationRange, | ||
Err: errors.New("A match expression is required"), | ||
}) | ||
continue | ||
} | ||
|
||
for _, entry := range matchBlock.Entries { | ||
if entry.Values == nil { | ||
errs = append(errs, common.LSPError{ | ||
Range: entry.LocationRange, | ||
Err: errors.New(fmt.Sprintf("A value for %s is required", entry.Criteria.Type)), | ||
}) | ||
} else { | ||
errs = append(errs, analyzeMatchValuesContainsPositiveValue(entry.Values)...) | ||
|
||
for _, value := range entry.Values.Values { | ||
errs = append(errs, analyzeMatchValueNegation(value)...) | ||
} | ||
} | ||
} | ||
|
||
// Check if match blocks are not empty | ||
if indexOption.MatchBlock.Options.Size() == 0 { | ||
errs = append(errs, common.LSPError{ | ||
Range: indexOption.Option.LocationRange, | ||
Err: errors.New("This match block is empty"), | ||
}) | ||
} | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func analyzeMatchValueNegation( | ||
value *match_parser.MatchValue, | ||
) []common.LSPError { | ||
errs := make([]common.LSPError, 0) | ||
|
||
positionsAsList := utils.AllIndexes(value.Value, "!") | ||
positions := utils.SliceToMap(positionsAsList, struct{}{}) | ||
|
||
delete(positions, 0) | ||
|
||
for position := range positions { | ||
errs = append(errs, common.LSPError{ | ||
Range: common.LocationRange{ | ||
Start: common.Location{ | ||
Line: value.Start.Line, | ||
Character: uint32(position) + value.Start.Character, | ||
}, | ||
End: common.Location{ | ||
Line: value.End.Line, | ||
Character: uint32(position) + value.End.Character, | ||
}, | ||
}, | ||
Err: errors.New("The negation operator (!) may only occur at the beginning of a value"), | ||
}) | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func analyzeMatchValuesContainsPositiveValue( | ||
values *match_parser.MatchValues, | ||
) []common.LSPError { | ||
if len(values.Values) == 0 { | ||
return nil | ||
} | ||
|
||
containsPositive := false | ||
|
||
for _, value := range values.Values { | ||
if !strings.HasPrefix(value.Value, "!") { | ||
containsPositive = true | ||
break | ||
} | ||
} | ||
|
||
if !containsPositive { | ||
return []common.LSPError{ | ||
{ | ||
Range: values.LocationRange, | ||
Err: errors.New("At least one positive value is required. A negated match will never produce a positive result by itself"), | ||
}, | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,63 @@ | ||
package analyzer | ||
|
||
import ( | ||
sshdconfig "config-lsp/handlers/sshd_config" | ||
"config-lsp/handlers/sshd_config/ast" | ||
"config-lsp/handlers/sshd_config/indexes" | ||
"config-lsp/utils" | ||
"testing" | ||
) | ||
|
||
func TestEmptyMatchBlocksMakesErrors( | ||
t *testing.T, | ||
) { | ||
input := utils.Dedent(` | ||
PermitRootLogin yes | ||
Match User root | ||
`) | ||
c := ast.NewSSHConfig() | ||
errors := c.Parse(input) | ||
|
||
if len(errors) > 0 { | ||
t.Fatalf("Parse error: %v", errors) | ||
} | ||
|
||
indexes, errors := indexes.CreateIndexes(*c) | ||
|
||
if len(errors) > 0 { | ||
t.Fatalf("Index error: %v", errors) | ||
} | ||
|
||
d := &sshdconfig.SSHDocument{ | ||
Config: c, | ||
Indexes: indexes, | ||
} | ||
|
||
errors = analyzeMatchBlocks(d) | ||
|
||
if !(len(errors) == 1) { | ||
t.Errorf("Expected 1 error, got %v", len(errors)) | ||
} | ||
} | ||
|
||
func TestContainsOnlyNegativeValues( | ||
t *testing.T, | ||
) { | ||
input := utils.Dedent(` | ||
PermitRootLogin yes | ||
Match User !root,!admin | ||
`) | ||
c := ast.NewSSHConfig() | ||
errors := c.Parse(input) | ||
|
||
if len(errors) > 0 { | ||
t.Fatalf("Parse error: %v", errors) | ||
} | ||
|
||
_, matchBlock := c.FindOption(uint32(1)) | ||
errors = analyzeMatchValuesContainsPositiveValue(matchBlock.MatchValue.Entries[0].Values) | ||
|
||
if !(len(errors) == 1) { | ||
t.Errorf("Expected 1 error, got %v", len(errors)) | ||
} | ||
} |
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
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
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
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
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
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