-
Notifications
You must be signed in to change notification settings - Fork 1
Add validation for queries #98
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
Open
MarcusGoldschmidt
wants to merge
7
commits into
main
Choose a base branch
from
goldschmidt/validate-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8dedbc9
add validation for queries
MarcusGoldschmidt b5d5860
add validation for credentials
MarcusGoldschmidt 3b04ca4
sanitize auto injection
MarcusGoldschmidt 6597e7f
fix linter
MarcusGoldschmidt 282b6f7
fix postgres test
MarcusGoldschmidt dbc7a21
remove required and fix validation for entitlements
MarcusGoldschmidt f5aea0f
fix validation format err
MarcusGoldschmidt 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,169 @@ | ||
| package bsql | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| func validateVarsInQuery(s *SQLSyncer, query string, vars map[string]string) error { | ||
| if query == "" { | ||
| return fmt.Errorf("query is required") | ||
| } | ||
|
|
||
| usedVars, err := s.queryVars(query) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse query for vars: %w", err) | ||
| } | ||
|
|
||
| if vars == nil { | ||
| vars = make(map[string]string) | ||
| } | ||
|
|
||
| for _, v := range usedVars { | ||
| if _, ok := vars[v]; !ok { | ||
| if v == limitKey || v == offsetKey || v == cursorKey { | ||
| continue | ||
| } | ||
| return fmt.Errorf("query uses variable '%s' which is not defined in vars", v) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *ListQuery) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| return validateVarsInQuery(s, l.Query, l.Vars) | ||
| } | ||
|
|
||
| func (l *EntitlementsQuery) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| for _, mapping := range l.Map { | ||
| if mapping.Provisioning == nil { | ||
| continue | ||
| } | ||
|
|
||
| if mapping.Provisioning.Grant != nil { | ||
| for _, query := range mapping.Provisioning.Grant.Queries { | ||
| err := validateVarsInQuery(s, query, mapping.Provisioning.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if mapping.Provisioning.Revoke != nil { | ||
| for _, query := range mapping.Provisioning.Revoke.Queries { | ||
| err := validateVarsInQuery(s, query, mapping.Provisioning.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return validateVarsInQuery(s, l.Query, l.Vars) | ||
| } | ||
|
|
||
| func (l *EntitlementMapping) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| if l.Provisioning == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if l.Provisioning.Grant != nil { | ||
| for _, query := range l.Provisioning.Grant.Queries { | ||
| err := validateVarsInQuery(s, query, l.Provisioning.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if l.Provisioning.Revoke != nil { | ||
| for _, query := range l.Provisioning.Revoke.Queries { | ||
| err := validateVarsInQuery(s, query, l.Provisioning.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *GrantsQuery) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| return validateVarsInQuery(s, l.Query, l.Vars) | ||
| } | ||
|
|
||
| func (l *AccountProvisioning) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| if l.Credentials == nil { | ||
| return errors.New("no credentials defined") | ||
| } | ||
|
|
||
| if l.Credentials.EncryptedPassword == nil && | ||
| l.Credentials.RandomPassword == nil && | ||
| l.Credentials.NoPassword == nil { | ||
| return errors.New("no credential method defined") | ||
| } | ||
|
|
||
| if l.Credentials.RandomPassword != nil { | ||
| if l.Credentials.RandomPassword.MaxLength <= 0 { | ||
| return errors.New("random password max_length must be greater than zero") | ||
| } | ||
|
|
||
| if l.Credentials.RandomPassword.MinLength <= 0 { | ||
| return errors.New("random password min_length must be greater than zero") | ||
| } | ||
|
|
||
| if l.Credentials.RandomPassword.MinLength > l.Credentials.RandomPassword.MaxLength { | ||
| return errors.New("random password min_length cannot be greater than max_length") | ||
| } | ||
| } | ||
|
|
||
| if l.Create == nil { | ||
| return errors.New("no create functions defined") | ||
| } | ||
|
|
||
| for _, query := range l.Create.Queries { | ||
| err := validateVarsInQuery(s, query, l.Create.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if l.Validate == nil { | ||
| return errors.New("no validate functions defined") | ||
| } | ||
|
|
||
| err := validateVarsInQuery(s, l.Validate.Query, l.Validate.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *CredentialRotation) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| if l.Update != nil { | ||
| for _, query := range l.Update.Queries { | ||
| err := validateVarsInQuery(s, query, l.Update.Vars) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *ActionConfig) staticValidate(ctx context.Context, s *SQLSyncer) error { | ||
| availableVars := make(map[string]string) | ||
| for k, v := range l.Vars { | ||
| availableVars[k] = v | ||
| } | ||
|
|
||
| for k, config := range l.Arguments { | ||
| availableVars[k] = config.Type | ||
| } | ||
|
|
||
| return validateVarsInQuery(s, l.Query, availableVars) | ||
| } | ||
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.
Missing validation of
EntitlementsQuery.Mapfield. TheEntitlementsQuery.StaticValidatemethod only validates the query variables but doesn't validate theMapfield, which is a slice ofEntitlementMappingobjects. EachEntitlementMappinghas provisioning queries that should also be validated.Consider adding validation for the Map field: