This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add legal path tests to other asset types
docker, dockerlayer, github, helm, inline, local and web terraform, amazoneks, azureaks and googlegke assets all rely on the inline type internally
- Loading branch information
Showing
11 changed files
with
222 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// IsLegalPath checks if the provided path is a relative path within the current working directory. If it is not, it returns an error. | ||
func IsLegalPath(path string) error { | ||
|
||
if filepath.IsAbs(path) { | ||
return fmt.Errorf("cannot write to an absolute path: %s", path) | ||
} | ||
|
||
relPath, err := filepath.Rel(".", path) | ||
if err != nil { | ||
return errors.Wrap(err, "find relative path to dest") | ||
} | ||
|
||
if strings.Contains(relPath, "..") { | ||
return fmt.Errorf("cannot write to a path that is a parent of the working dir: %s", relPath) | ||
} | ||
|
||
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,39 @@ | ||
package util | ||
|
||
import "testing" | ||
|
||
func TestIsLegalPath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "relative path", | ||
path: "./happy/path", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "absolute path", | ||
path: "/unhappy/path", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "relative parent path", | ||
path: "../../unhappy/path", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "embedded relative parent path", | ||
path: "./happy/../../../unhappy/path", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := IsLegalPath(tt.path); (err != nil) != tt.wantErr { | ||
t.Errorf("IsLegalPath() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |