Repository navigation
Expand file tree
/
Copy pathnode_artifacts.go
More file actions
69 lines (63 loc) · 2.18 KB
/
Copy pathnode_artifacts.go
File metadata and controls
69 lines (63 loc) · 2.18 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
package main
import (
"encoding/json"
"errors"
"io"
"net/url"
"regexp"
"strings"
"unicode"
)
type nodeArtifact struct {
Filename string `json:"filename"`
Size int64 `json:"size"`
SHA256 string `json:"sha256"`
}
type nodeManifest struct {
SourceCommit string `json:"source_commit"`
ArtifactBaseURL string `json:"artifact_base_url"`
Artifacts map[string]nodeArtifact `json:"artifacts"`
}
func (h *console) readNodeManifest() (nodeManifest, error) {
var manifest nodeManifest
f, err := h.nodePayload.Open("manifest.json")
if err != nil {
return manifest, err
}
defer f.Close()
raw, err := io.ReadAll(io.LimitReader(f, 1024*1024+1))
if err != nil || len(raw) > 1024*1024 || json.Unmarshal(raw, &manifest) != nil {
return manifest, errors.New("invalid node manifest")
}
return manifest, nil
}
var payloadRevision = regexp.MustCompile(`^[0-9a-f]{40}$`)
var nodeArtifactName = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
var nodeArtifactDigest = regexp.MustCompile(`^[0-9a-f]{64}$`)
// Release locations come only from the installed, verified distribution manifest.
// Web redirects missing artifacts instead of downloading or caching them itself.
func (m nodeManifest) artifactURL(filename string) string {
if !payloadRevision.MatchString(m.SourceCommit) || !nodeArtifactName.MatchString(filename) || !strings.Contains(filename, m.SourceCommit) {
return ""
}
allowed := false
for logical, entry := range m.Artifacts {
if optionalPayloadFiles[logical] && entry.Filename == filename && entry.Size > 0 && nodeArtifactDigest.MatchString(entry.SHA256) {
allowed = true
break
}
}
if !allowed {
return ""
}
base, err := url.Parse(m.ArtifactBaseURL)
if err != nil || base.Scheme != "https" || base.Hostname() == "" || base.User != nil || base.RawQuery != "" || base.ForceQuery || base.Fragment != "" || strings.Trim(base.Path, "/") == "" || strings.Contains(m.ArtifactBaseURL, "\\") || strings.IndexFunc(m.ArtifactBaseURL, unicode.IsSpace) >= 0 {
return ""
}
for _, part := range strings.Split(strings.ToLower(base.Path), "/") {
if part == "latest" {
return ""
}
}
return strings.TrimRight(base.String(), "/") + "/" + filename
}