-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecurity.go
168 lines (128 loc) · 3.96 KB
/
security.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/FriendsOfShopware/shopware-cli/version"
"github.com/google/go-github/v53/github"
"io"
"net/http"
"os"
)
type packagistSecurityResponse struct {
Advisories struct {
ShopwarePlatform []struct {
AdvisoryId string `json:"advisoryId"`
PackageName string `json:"packageName"`
RemoteId string `json:"remoteId"`
Title string `json:"title"`
Link string `json:"link"`
Cve string `json:"cve"`
AffectedVersions string `json:"affectedVersions"`
Source string `json:"source"`
ReportedAt string `json:"reportedAt"`
ComposerRepository string `json:"composerRepository"`
Sources []struct {
Name string `json:"name"`
RemoteId string `json:"remoteId"`
} `json:"sources"`
} `json:"shopware/platform"`
} `json:"advisories"`
}
func generateSecurityAdvisories(ctx context.Context, tags []*github.RepositoryTag) error {
packagistAdvisories, err := getAllSecurityAdvisories(ctx)
if err != nil {
return err
}
latestVersion, err := getSecurityPluginLatestVersion(ctx)
if err != nil {
return err
}
fileStruct := securityFile{
LatestPluginVersion: latestVersion,
Advisories: make(map[string]advisories),
VersionToAdvisories: make(map[string][]string),
}
for _, advisory := range packagistAdvisories.Advisories.ShopwarePlatform {
fileStruct.Advisories[advisory.AdvisoryId] = advisories{
Title: advisory.Title,
Link: advisory.Link,
CVE: advisory.Cve,
Affected: advisory.AffectedVersions,
Source: advisory.Source,
ReportedAt: advisory.ReportedAt,
}
}
for _, tag := range tags {
v := version.Must(version.NewVersion(tag.GetName()))
for _, advisory := range packagistAdvisories.Advisories.ShopwarePlatform {
constraint := version.MustConstraints(version.NewConstraint(advisory.AffectedVersions))
if constraint.Check(v) {
if fileStruct.VersionToAdvisories[tag.GetName()] == nil {
fileStruct.VersionToAdvisories[tag.GetName()] = []string{}
}
fileStruct.VersionToAdvisories[tag.GetName()] = append(fileStruct.VersionToAdvisories[tag.GetName()], advisory.AdvisoryId)
}
}
}
data, err := json.MarshalIndent(fileStruct, "", " ")
if err != nil {
return err
}
if err = os.WriteFile("data/security.json", data, os.ModePerm); err != nil {
return err
}
return nil
}
func getAllSecurityAdvisories(ctx context.Context) (*packagistSecurityResponse, error) {
r, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://packagist.org/api/security-advisories/?packages[]=shopware/platform", nil)
if err != nil {
return nil, err
}
r.Header.Set("User-Agent", "Shopware Security Checker")
resp, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.Body.Close() != nil {
return nil, err
}
var apiResponse packagistSecurityResponse
if err = json.Unmarshal(data, &apiResponse); err != nil {
return nil, err
}
return &apiResponse, nil
}
type shopwareApiResponse []struct {
Version string `json:"version"`
}
func getSecurityPluginLatestVersion(ctx context.Context) (string, error) {
r, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.shopware.com/pluginStore/pluginsByName?locale=en-GB&shopwareVersion=6.4.14.0&technicalNames%5B0%5D=SwagPlatformSecurity", nil)
if err != nil {
return "", err
}
r.Header.Set("User-Agent", "Shopware Security Checker")
resp, err := http.DefaultClient.Do(r)
if err != nil {
return "", err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.Body.Close() != nil {
return "", err
}
var apiResponse shopwareApiResponse
if err = json.Unmarshal(data, &apiResponse); err != nil {
return "", err
}
if len(apiResponse) == 0 {
return "", fmt.Errorf("no plugin found")
}
return apiResponse[0].Version, nil
}