@@ -33,6 +33,10 @@ type ErrInvalidPath struct {
33
33
errors.WrappableError
34
34
}
35
35
36
+ // PathIsUnderCurrentDirectory checks whether the `path`
37
+ // is under the current working directory. Examples:
38
+ // ./file, ./some/path, ../<cwd>.file would return `nil`.
39
+ // `../etc/password` would return an error.
36
40
func PathIsUnderCurrentDirectory (path string ) error {
37
41
wd , err := os .Getwd ()
38
42
if err != nil {
@@ -51,6 +55,9 @@ func PathIsUnderCurrentDirectory(path string) error {
51
55
return nil
52
56
}
53
57
58
+ // VerifyAttestationPath verifies that the path of an attestation
59
+ // is valid. It checks that the path is under the current working directory
60
+ // and that the extension of the file is `intoto.jsonl`.
54
61
func VerifyAttestationPath (path string ) error {
55
62
if ! strings .HasSuffix (path , "intoto.jsonl" ) {
56
63
return errors .Errorf (& ErrInvalidPath {}, "invalid suffix: %q. Must be .intoto.jsonl" , path )
@@ -61,6 +68,9 @@ func VerifyAttestationPath(path string) error {
61
68
return nil
62
69
}
63
70
71
+ // CreateNewFileUnderCurrentDirectory create a new file under the current directory
72
+ // and fails if the file already exists. The file is always created with the pemisisons
73
+ // `0o600`.
64
74
func CreateNewFileUnderCurrentDirectory (path string , flag int ) (io.Writer , error ) {
65
75
if path == "-" {
66
76
return os .Stdout , nil
@@ -71,5 +81,10 @@ func CreateNewFileUnderCurrentDirectory(path string, flag int) (io.Writer, error
71
81
}
72
82
73
83
// Ensure we never overwrite an existing file.
74
- return os .OpenFile (filepath .Clean (path ), flag | os .O_CREATE | os .O_EXCL , 0o600 )
84
+ fp , err := os .OpenFile (filepath .Clean (path ), flag | os .O_CREATE | os .O_EXCL , 0o600 )
85
+ if err != nil {
86
+ return nil , errors .Errorf (& ErrInternal {}, "os.OpenFile(): %v" , err )
87
+ }
88
+
89
+ return fp , nil
75
90
}
0 commit comments