-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
262 lines (218 loc) · 5.7 KB
/
Copy pathapi.go
File metadata and controls
262 lines (218 loc) · 5.7 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2024 Factorial GmbH. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"log/slog"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"github.com/google/uuid"
)
const (
AuthMethodBasic = "basic"
)
type AuthConfig struct {
Host *Host `json:"host"`
Method string `json:"method"`
// If method is "basic"
Username string `json:"username"`
Password string `json:"password"`
}
// GetHeader returns the value of the Authorization header for the given
// authentication configuration.
func (ac *AuthConfig) GetHeader() (string, bool) {
switch ac.Method {
case AuthMethodBasic:
token := fmt.Sprintf("%s:%s", ac.Username, ac.Password)
token = base64.StdEncoding.EncodeToString([]byte(token))
return fmt.Sprintf("Basic %s", token), true
default:
slog.Warn("Unknown auth method.", "method", ac.Method)
return "", false
}
}
// Hash returns a hash of the authentication configuration. This is used to
// uniquely identify the configuration.
func (ac *AuthConfig) Hash() []byte {
return sha256.New().Sum([]byte(fmt.Sprintf("%#v", ac)))
}
// Matches checks if this AuthConfig should be used for the given Host.
func (ac *AuthConfig) Matches(h *Host) bool {
return h.Name == ac.Host.Name && h.Port == ac.Host.Port
}
type Request struct {
// We accept either a valid UUID as a string, or as an integer. If left
// empty, we'll generate one.
Run string `json:"run"`
URL string `json:"url"`
URLs []string `json:"urls"`
AliasedDomains []string `json:"aliases"`
IgnorePaths []string `json:"ignores"`
UserAgent string `json:"ua"`
// A list of authentication configurations, that are used in the run.
AuthConfigs []*AuthConfig `json:"auth"`
}
// GetRun returns the run's ID, if provided in the request. Please call req.Validate
// to make sure a provided ID is correctly formed.
func (req *Request) GetRun() string {
if req.Run != "" {
return uuid.MustParse(req.Run).String()
}
return uuid.New().String()
}
func (req *Request) GetURLs(clean bool) []string {
var urls []string
if req.URL != "" {
urls = append(urls, req.URL)
}
if req.URLs != nil {
urls = append(urls, req.URLs...)
}
if clean {
// Remove basic auth information from each URL. Otherwise net/http will
// use the information to authenticate the request. But we want to do this
// explicitly.
for i, u := range urls {
p, err := url.Parse(u)
if err != nil {
slog.Warn("Failed to parse URL while cleaning, skipping.", "url", u, "error", err)
urls = slices.Delete(urls, i, i+1)
continue
}
p.User = nil
urls[i] = p.String()
}
}
return urls
}
func (req *Request) GetAllowDomains() []string {
var domains []string
// The domains of the targets are allways allowed.
for _, u := range req.GetURLs(false) {
p, err := url.Parse(u)
if err != nil {
slog.Error("Failed to parse URL from request, not allowing that domain.", "url", u, "error", err)
continue
}
domains = append(domains, p.Hostname())
}
if req.AliasedDomains != nil {
domains = append(domains, req.AliasedDomains...)
}
return domains
}
func (req *Request) GetIgnorePaths() []string {
if req.IgnorePaths == nil {
return make([]string, 0)
}
return req.IgnorePaths
}
func (req *Request) GetAuthConfigs() []*AuthConfig {
var configs []*AuthConfig
if req.AuthConfigs != nil {
configs = req.AuthConfigs
}
for _, u := range req.GetURLs(false) {
p, err := url.Parse(u)
if err != nil {
slog.Warn("Failed to parse URL while building auth config, skipping.", "url", u, "error", err)
continue
}
if p.User != nil {
pass, _ := p.User.Password()
config := &AuthConfig{
Method: "basic",
Host: NewHostFromURL(p),
Username: p.User.Username(),
Password: pass,
}
configs = append(configs, config)
}
}
return configs
}
func (req *Request) GetUserAgent() string {
if req.UserAgent != "" {
return req.UserAgent
}
return UserAgent
}
func (req *Request) Validate() (bool, []error) {
var violations []error
if req.Run != "" {
_, err := uuid.Parse(req.Run)
if err != nil {
return false, append(violations, err)
}
}
if req.URL != "" {
_, err := url.ParseRequestURI(req.URL)
if err != nil {
return false, append(violations, err)
}
}
if req.URLs != nil {
for _, u := range req.URLs {
_, err := url.ParseRequestURI(u)
if err != nil {
return false, append(violations, err)
}
}
}
return true, violations
}
type APIRequest struct {
Request
// Metadata associated with this run that will be included in all results
RunMetadata interface{} `json:"run_metadata,omitempty"`
// The DSN of the result reporter to use as dynamic configuration.
ResultReporterDSN string `json:"result_reporter_dsn"`
}
type APIResponse struct {
Run string `json:"run"`
}
type APIError struct {
Message string `json:"message"`
}
type ConsoleRequest struct {
Request
OutputDir string
OutputContentOnly bool
}
func (req *ConsoleRequest) Validate() (bool, []error) {
if ok, violations := req.Request.Validate(); !ok {
return ok, violations
}
var violations []error
if req.OutputDir != "" {
abs, err := filepath.Abs(req.OutputDir)
if err != nil {
return false, append(violations, err)
}
wd, err := os.Getwd()
if err != nil {
return false, append(violations, err)
}
if !strings.HasPrefix(abs, wd) {
return false, append(violations, fmt.Errorf("output directory (%s) must be below the current working directory (%s)", abs, wd))
}
}
return true, nil
}
func (req *ConsoleRequest) GetOutputDir() string {
var p string
if req.OutputDir != "" {
p = req.OutputDir
} else {
p = "."
}
abs, _ := filepath.Abs(p)
return abs
}