-
Notifications
You must be signed in to change notification settings - Fork 273
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
Use shell environment variables in migrations using Go's text/template parsing #439
Open
davidecavestro
wants to merge
5
commits into
amacneil:main
Choose a base branch
from
davidecavestro:env-vars
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
5 commits
Select commit
Hold shift + click to select a range
2d90e1b
Use system variables in migrations
davidecavestro e29b24c
Use system variables in migrations
davidecavestro 7a927ae
Use system variables in migrations
davidecavestro 4b585bc
Use system variables in migrations
davidecavestro 6def5ab
Use system variables in migrations
davidecavestro 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
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,42 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func GetEnvMap() map[string]string { | ||
envMap := make(map[string]string) | ||
|
||
for _, envVar := range os.Environ() { | ||
entry := strings.SplitN(envVar, "=", 2) | ||
envMap[entry[0]] = entry[1] | ||
} | ||
|
||
return envMap | ||
} | ||
|
||
func ResolveRefs(snippet string, envVars []string, envMap map[string]string) (string, error) { | ||
if envVars == nil { | ||
return snippet, nil | ||
} | ||
|
||
model := make(map[string]string, len(envVars)) | ||
for _, envVar := range envVars { | ||
varValue, exists := envMap[envVar] | ||
if exists { | ||
model[envVar] = varValue | ||
} | ||
} | ||
|
||
template := template.Must(template.New("tmpl").Option("missingkey=error").Parse(snippet)) | ||
|
||
var buffer bytes.Buffer | ||
if err := template.Execute(&buffer, model); err != nil { | ||
return "", err | ||
} | ||
|
||
return buffer.String(), 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,112 @@ | ||
package internal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/amacneil/dbmate/v2/pkg/dbmate/internal" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestResolveRefs(t *testing.T) { | ||
parseUp := `create role '{{ .THE_ROLE }}' login password '{{ .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
"THE_ROLE": "Barney", | ||
"THE_PASSWORD": "Betty", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "create role 'Barney' login password 'Betty';", resolved) | ||
} | ||
|
||
func TestResolveRefsUsingDefaults(t *testing.T) { | ||
parseUp := `create role '{{ or (index . "THE_ROLE") "Fred" }}' login password '{{ .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
"THE_PASSWORD": "Wilma", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "create role 'Fred' login password 'Wilma';", resolved) | ||
} | ||
|
||
func TestResolveRefsIgnoringDefaults(t *testing.T) { | ||
parseUp := `create role '{{ or (index . "THE_ROLE") "Fred" }}' login password '{{ .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
"THE_ROLE": "Dino", | ||
"THE_PASSWORD": "Wilma", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, "create role 'Dino' login password 'Wilma';", resolved) | ||
} | ||
|
||
func TestResolveRefsErroringOnMissingVar(t *testing.T) { | ||
parseUp := `create role '{{ or (index . "THE_ROLE") "Fred" }}' login password '{{ .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
"THE_ROLE": "Dino", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.Error(t, err) | ||
require.Equal(t, "", resolved) | ||
} | ||
|
||
func TestResolveWithSqlInjection(t *testing.T) { | ||
parseUp := `create role '{{ .THE_ROLE }}' login password '{{ .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
"THE_ROLE": "Slate'; drop table SALARY; create role 'Barney", | ||
"THE_PASSWORD": "Betty", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.NoError(t, err) | ||
// sql injection is not prevented here | ||
require.Equal(t, "create role 'Slate'; drop table SALARY; create role 'Barney' login password 'Betty';", resolved) | ||
} | ||
|
||
func TestResolveMitigatingSqlInjection(t *testing.T) { | ||
parseUp := `create role '{{ js .THE_ROLE }}' login password '{{ js .THE_PASSWORD }}';` | ||
parsedUpOptsEnvVars := []string{ | ||
"THE_ROLE", | ||
"THE_PASSWORD", | ||
} | ||
envMap := map[string]string{ | ||
// simulating naive SQL injection attempt | ||
"THE_ROLE": "Slate'; drop table SALARY; create role 'Barney", | ||
"THE_PASSWORD": "Betty", | ||
} | ||
|
||
resolved, err := internal.ResolveRefs(parseUp, parsedUpOptsEnvVars, envMap) | ||
|
||
require.NoError(t, err) | ||
// sql injection mitigated | ||
require.Equal(t, "create role 'Slate\\'; drop table SALARY; create role \\'Barney' login password 'Betty';", resolved) | ||
} |
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
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.
sorry if I missed the discussion- how does this mitigate SQL injections risks? Can't I insert a string with a single quote still?
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.
The go/template functions includes
js
which is described as:While this may not necessarily be sufficient to prevent all possible SQL injection attacks (see: Unicode homoglyph injection attacks aka Unicode smuggling [1] [2], character set transcoding SQL injection attacks [3]), it prevents the most trivial kind where an unescaped single quote
'
is in a string that will be interpolated into the SQL query, by escaping it with a backslash, thus turning'
into\'
.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.
The trivial case of unescaped single quote is covered by TestResolveMitigatingSqlInjection.
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.
nice test case!
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 added a comment in the main PR thread - I'm not a fan of introducing functions to the template syntax, or encouraging use of js escaping functions for sql escaping.