-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ lint error when query is used both as check and data query #1380
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -34,6 +34,7 @@ const ( | |
queryTitle = "query-name" | ||
queryUidUnique = "query-uid-unique" | ||
queryUnassigned = "query-unassigned" | ||
queryUsedAsDifferentTypes = "query-used-as-different-types" | ||
) | ||
|
||
type Rule struct { | ||
|
@@ -118,6 +119,11 @@ var LinterRules = []Rule{ | |
Name: "Unassigned query", | ||
Description: "The query is not assigned to any policy", | ||
}, | ||
{ | ||
ID: queryUsedAsDifferentTypes, | ||
Name: "Query used as a check and data query", | ||
Description: "The query is used both as a check and a data query", | ||
}, | ||
} | ||
|
||
type Results struct { | ||
|
@@ -229,6 +235,8 @@ func lintFile(file string) (*Results, error) { | |
// index global queries that are not embedded | ||
globalQueriesUids := map[string]int{} | ||
globalQueriesByUid := map[string]*Mquery{} | ||
checks := map[string]struct{}{} | ||
dataQueries := map[string]struct{}{} | ||
// child to parent mapping | ||
variantMapping := map[string]string{} | ||
for i := range policyBundle.Queries { | ||
|
@@ -414,6 +422,20 @@ func lintFile(file string) (*Results, error) { | |
uid := check.Uid | ||
updateAssignedQueries(check, assignedQueries, globalQueriesByUid) | ||
|
||
checks[check.Uid] = struct{}{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we only check by uid since this is under user control. It is still possible to cause weirdness when the MQL is identical for a query and a check but that's a bug in the resolved policy. It's not something to be caught by the linter |
||
if _, ok := dataQueries[check.Uid]; ok { | ||
res.Entries = append(res.Entries, Entry{ | ||
RuleID: queryUsedAsDifferentTypes, | ||
Message: fmt.Sprintf("query %s is used as a check and data query", uid), | ||
Level: levelError, | ||
Location: []Location{{ | ||
File: file, | ||
Line: group.FileContext.Line, | ||
Column: group.FileContext.Column, | ||
}}, | ||
}) | ||
} | ||
|
||
// check if the query is embedded | ||
if isEmbeddedQuery(check) { | ||
// NOTE: embedded queries do not need a uid | ||
|
@@ -442,6 +464,20 @@ func lintFile(file string) (*Results, error) { | |
uid := query.Uid | ||
updateAssignedQueries(query, assignedQueries, globalQueriesByUid) | ||
|
||
dataQueries[query.Uid] = struct{}{} | ||
if _, ok := checks[query.Uid]; ok { | ||
res.Entries = append(res.Entries, Entry{ | ||
RuleID: queryUsedAsDifferentTypes, | ||
Message: fmt.Sprintf("query %s is used as a check and data query", uid), | ||
Level: levelError, | ||
Location: []Location{{ | ||
File: file, | ||
Line: group.FileContext.Line, | ||
Column: group.FileContext.Column, | ||
}}, | ||
}) | ||
} | ||
|
||
// check if the query is embedded | ||
if isEmbeddedQuery(query) { | ||
// NOTE: embedded queries do not need a uid | ||
|
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,22 @@ | ||
policies: | ||
- uid: data-queries-mix | ||
name: Test data SSH Policy | ||
version: "1.0.0" | ||
owner_mrn: "" | ||
is_public: true | ||
authors: | ||
- name: Mondoo, Inc. | ||
email: [email protected] | ||
groups: | ||
- title: group 01 | ||
checks: | ||
- uid: sshd-sshd-01 | ||
queries: | ||
- uid: sshd-sshd-01 | ||
filters: | | ||
asset.family.contains(_ == 'unix') | ||
|
||
queries: | ||
- uid: sshd-sshd-01 | ||
title: Asset name is "test" | ||
query: asset.name == "test" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you need to update the
LinterRules
var with this entry too so that it shows in the sarif reportThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point! I missed that