-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifact.go
More file actions
112 lines (101 loc) · 2.63 KB
/
Copy pathartifact.go
File metadata and controls
112 lines (101 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package artifacts
import (
_ "crypto/sha256"
_ "crypto/sha512"
"fmt"
"net/url"
"strings"
"github.com/git-pkgs/purl"
"github.com/opencontainers/go-digest"
)
// Artifact describes a completed package file.
type Artifact struct {
PURL string
Digest digest.Digest
Size int64
Filename string
MediaType string
}
// New constructs a validated Artifact and stores the canonical package URL.
func New(packageURL string, contentDigest digest.Digest, size int64, filename, mediaType string) (Artifact, error) {
canonicalPURL, err := validate(packageURL, contentDigest, size)
if err != nil {
return Artifact{}, err
}
return Artifact{
PURL: canonicalPURL,
Digest: contentDigest,
Size: size,
Filename: filename,
MediaType: mediaType,
}, nil
}
// Validate checks that an Artifact has valid package coordinates, digest, and size.
func (artifact Artifact) Validate() error {
canonicalPURL, err := validate(artifact.PURL, artifact.Digest, artifact.Size)
if err != nil {
return err
}
if artifact.PURL != canonicalPURL {
return fmt.Errorf("PURL: is not canonical, want %q", canonicalPURL)
}
return nil
}
func validate(packageURL string, contentDigest digest.Digest, size int64) (string, error) {
parsed, err := purl.Parse(packageURL)
if err != nil {
return "", fmt.Errorf("PURL: %w", err)
}
if parsed.Subpath != "" {
parsed.Subpath, err = canonicalSubpath(parsed.Subpath)
if err != nil {
return "", err
}
}
canonicalPURL := parsed.String()
if err := contentDigest.Validate(); err != nil {
return "", fmt.Errorf("digest: %w", err)
}
if size < 0 {
return "", fmt.Errorf("size: must be zero or greater")
}
return canonicalPURL, nil
}
func canonicalSubpath(subpath string) (string, error) {
decode := strings.Contains(subpath, "%")
remainder := subpath
var canonical strings.Builder
if decode {
canonical.Grow(len(subpath))
}
for first := true; ; first = false {
rawSegment, next, found := strings.Cut(remainder, "/")
segment, err := url.PathUnescape(rawSegment)
if err != nil {
return "", fmt.Errorf("PURL subpath: %w", err)
}
if segment == "" {
return "", fmt.Errorf("PURL subpath: segment %q is empty", rawSegment)
}
if segment == "." || segment == ".." {
return "", fmt.Errorf("PURL subpath: segment %q is not allowed", segment)
}
if strings.Contains(segment, "/") {
return "", fmt.Errorf("PURL subpath: segment %q contains a slash", segment)
}
if decode {
if !first {
canonical.WriteByte('/')
}
canonical.WriteString(segment)
}
if !found {
break
}
remainder = next
}
if decode {
return canonical.String(), nil
}
return subpath, nil
}