forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Default path validation on push (oras-project#66)
Validate name by default
- Loading branch information
Showing
6 changed files
with
160 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package oras | ||
|
||
import ( | ||
"testing" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type PushOptsSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *PushOptsSuite) TestValidateNameAsPath() { | ||
var err error | ||
|
||
// valid path | ||
err = ValidateNameAsPath(descFromName("hello.txt")) | ||
suite.NoError(err, "valid path") | ||
err = ValidateNameAsPath(descFromName("foo/bar")) | ||
suite.NoError(err, "valid path with multiple sub-directories") | ||
|
||
// no empty name | ||
err = ValidateNameAsPath(descFromName("")) | ||
suite.Error(err, "empty path") | ||
|
||
// path should be clean | ||
err = ValidateNameAsPath(descFromName("./hello.txt")) | ||
suite.Error(err, "dirty path") | ||
err = ValidateNameAsPath(descFromName("foo/../bar")) | ||
suite.Error(err, "dirty path") | ||
|
||
// path should be slash-separated | ||
err = ValidateNameAsPath(descFromName("foo\\bar")) | ||
suite.Error(err, "path not slash separated") | ||
|
||
// disallow absolute path | ||
err = ValidateNameAsPath(descFromName("/foo/bar")) | ||
suite.Error(err, "unix: absolute path disallowed") | ||
err = ValidateNameAsPath(descFromName("C:\\foo\\bar")) | ||
suite.Error(err, "windows: absolute path disallowed") | ||
err = ValidateNameAsPath(descFromName("C:/foo/bar")) | ||
suite.Error(err, "windows: absolute path disallowed") | ||
|
||
// disallow path traversal | ||
err = ValidateNameAsPath(descFromName("..")) | ||
suite.Error(err, "path traversal disallowed") | ||
err = ValidateNameAsPath(descFromName("../bar")) | ||
suite.Error(err, "path traversal disallowed") | ||
err = ValidateNameAsPath(descFromName("foo/../../bar")) | ||
suite.Error(err, "path traversal disallowed") | ||
} | ||
|
||
func TestPushOptsSuite(t *testing.T) { | ||
suite.Run(t, new(PushOptsSuite)) | ||
} | ||
|
||
func descFromName(name string) ocispec.Descriptor { | ||
return ocispec.Descriptor{ | ||
Annotations: map[string]string{ | ||
ocispec.AnnotationTitle: name, | ||
}, | ||
} | ||
} |