Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · internal/artifactpath/artifactpath.go

Line attribution on main · 54 line(s)

5f2a0b0a
Keenan Nemetz
20d ago
1 // Package artifactpath is the path rule shared by release attachments and run
2 // outputs. Both are slash-separated paths relative to a root: a flat file is one
3 // segment, and a package layout is a small tree.
4 //
5 // The rule is one place because the pointer writer, the metadata validator, the
6 // runner's artifact report, and the download routes must agree. A path that one
7 // layer accepts and another refuses is a release that cannot be published, or a
8 // link that cannot be served.
9 package artifactpath
10
11 import (
12 "fmt"
13 "strings"
14 )
15
16 const (
17 // MaxSegment is the old flat-name bound. A segment is a filename.
18 MaxSegment = 200
19 // MaxDepth keeps a pathological tree from turning one release into thousands
20 // of metadata paths.
21 MaxDepth = 32
22 // MaxPath bounds the whole slash-separated path.
23 MaxPath = 1024
24 )
25
26 // Valid checks one artifact path. Empty segments, '.' and '..', backslashes, NUL,
27 // and control characters are refused. A leading or trailing slash is an empty
28 // segment, so it is refused the same way.
29 func Valid(path string) error {
30 if path == "" {
31 return fmt.Errorf("an artifact needs a path")
32 }
33 if len(path) > MaxPath {
34 return fmt.Errorf("artifact path is %d bytes, limit %d", len(path), MaxPath)
35 }
36 if strings.ContainsAny(path, "\\\x00\n\r\t") {
37 return fmt.Errorf("artifact path %q contains a backslash or control character", path)
38 }
39 segments := strings.Split(path, "/")
40 if len(segments) > MaxDepth {
41 return fmt.Errorf("artifact path has %d segments, limit %d", len(segments), MaxDepth)
42 }
43 for _, segment := range segments {
44 switch {
45 case segment == "":
46 return fmt.Errorf("artifact path %q has an empty segment", path)
47 case segment == "." || segment == "..":
48 return fmt.Errorf("artifact path %q must not contain %q", path, segment)
49 case len(segment) > MaxSegment:
50 return fmt.Errorf("artifact path segment is %d bytes, limit %d", len(segment), MaxSegment)
51 }
52 }
53 return nil
54 }