-
Notifications
You must be signed in to change notification settings - Fork 505
fix: Limit TOML file size #3432
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4d76cb
Limit TOML file size
sfc-gh-jmichalak fa5a7e0
Move code to internal/os
sfc-gh-jmichalak 2fe0170
Fix
sfc-gh-jmichalak 3bb0f46
Merge remote-tracking branch 'origin/dev' into file-size
sfc-gh-jmichalak 6f6ab7a
Merge remote-tracking branch 'origin/dev' into file-size
sfc-gh-jmichalak 3fd2d3a
Review
sfc-gh-jmichalak 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 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,67 @@ | ||
| // Package oswrapper is a wrapper around the standard os package that allows more secure interactions with the operating system. | ||
| // It should be used as a replacement in production code of the standard os package. | ||
| package oswrapper | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "runtime" | ||
| ) | ||
|
|
||
| const ( | ||
| maxFileSizeInMb = 10 | ||
| ) | ||
|
|
||
| // IsRunningOnWindows returns true if the code is running on Windows. | ||
| func IsRunningOnWindows() bool { | ||
| return runtime.GOOS == "windows" | ||
| } | ||
|
|
||
| // Stat is an os.Stat wrapper. | ||
| func Stat(path string) (os.FileInfo, error) { | ||
| log.Printf("[DEBUG] reading the %s file info", path) | ||
| return os.Stat(path) | ||
| } | ||
|
|
||
| // Getenv is an os.Getenv wrapper. | ||
| func Getenv(name string) string { | ||
| log.Printf("[DEBUG] reading the %s environmental variable", name) | ||
| return os.Getenv(name) | ||
| } | ||
|
|
||
| // LookupEnv is an os.LookupEnv wrapper. | ||
| func LookupEnv(name string) (string, bool) { | ||
| log.Printf("[DEBUG] reading the %s environmental variable", name) | ||
| return os.LookupEnv(name) | ||
| } | ||
|
|
||
| // ReadFileSafe checks if a file is safe to read, and then reads it. | ||
| func ReadFileSafe(path string) ([]byte, error) { | ||
| if err := fileIsSafeToRead(path); err != nil { | ||
| return nil, err | ||
| } | ||
| return readFile(path) | ||
| } | ||
|
|
||
| func readFile(path string) ([]byte, error) { | ||
| log.Printf("[DEBUG] reading the %s file", path) | ||
| return os.ReadFile(path) | ||
| } | ||
|
|
||
| func fileIsSafeToRead(path string) error { | ||
| fileinfo, err := Stat(path) | ||
| if err != nil { | ||
| return fmt.Errorf("reading information about the config file: %w", err) | ||
| } | ||
| if fileinfo.Size() > maxFileSizeInMb*1024*1024 { | ||
| return fmt.Errorf("config file %s is too big - maximum allowed size is %dMB", path, maxFileSizeInMb) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // UserHomeDir is an os.UserHomeDir wrapper. | ||
| func UserHomeDir() (string, error) { | ||
| log.Printf("[DEBUG] reading the user home directory location from the operating system") | ||
| return os.UserHomeDir() | ||
| } |
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,27 @@ | ||
| package oswrapper_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/oswrapper" | ||
| "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/testhelpers" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestLoadConfigFileThatIsTooBig(t *testing.T) { | ||
| if oswrapper.IsRunningOnWindows() { | ||
| t.Skip("checking file sizes on Windows is currently done in manual tests package") | ||
| } | ||
| c := make([]byte, 11*1024*1024) | ||
| configPath := testhelpers.TestFile(t, "config", c) | ||
|
|
||
| _, err := oswrapper.ReadFileSafe(configPath) | ||
| require.ErrorContains(t, err, fmt.Sprintf("config file %s is too big - maximum allowed size is 10MB", configPath)) | ||
| } | ||
|
|
||
| func TestLoadConfigFileThatDoesNotExist(t *testing.T) { | ||
| configPath := "non-existing" | ||
| _, err := oswrapper.ReadFileSafe(configPath) | ||
| require.ErrorContains(t, err, fmt.Sprintf("reading information about the config file: stat %s: no such file or directory", configPath)) | ||
| } |
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,22 @@ | ||
| package windows_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/oswrapper" | ||
| "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
| "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/testhelpers" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestLoadConfigFileThatIsTooBig(t *testing.T) { | ||
| if !oswrapper.IsRunningOnWindows() { | ||
| t.Skip("checking file sizes on other platforms is currently done in the sdk package") | ||
| } | ||
| c := make([]byte, 11*1024*1024) | ||
| configPath := testhelpers.TestFile(t, "config", c) | ||
|
|
||
| _, err := sdk.LoadConfigFile(configPath) | ||
| require.ErrorContains(t, err, fmt.Sprintf("config file %s is too big - maximum allowed size is 10MB", configPath)) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.