Repository navigation
Expand file tree
/
Copy pathurl_replacement.go
More file actions
329 lines (308 loc) · 11.3 KB
/
Copy pathurl_replacement.go
File metadata and controls
329 lines (308 loc) · 11.3 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2026 Columnar Technologies Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbc
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync/atomic"
)
// URLReplacement sends requests for URLs under Prefix to Replacement instead.
//
// Prefix and Replacement are absolute http or https URLs without userinfo,
// query or fragment. A request URL matches when its origin (scheme, host and
// port) equals the prefix's and the prefix path is a prefix of the request
// path at a path segment boundary. Percent-encoded unreserved ASCII
// characters compare as their literal characters; reserved escapes such as
// %2F stay escaped and do not become path separators. The matching part of
// the URL is then replaced by Replacement. Requests that were replaced are always anonymous:
// no credentials, no telemetry identifiers and no fallback to the original
// URL.
type URLReplacement struct {
Prefix string `toml:"prefix"`
Replacement string `toml:"replacement"`
}
// NetworkConfig holds network settings of the user's config.toml.
type NetworkConfig struct {
// URLReplacements is an ordered list of rules; the first matching rule
// wins and its result is never matched again.
URLReplacements []URLReplacement `toml:"url_replacements,omitempty"`
}
// ErrInvalidURLReplacement is wrapped by the errors returned for invalid URL
// replacement rules.
var ErrInvalidURLReplacement = errors.New("invalid URL replacement")
// ErrURLReplacementUnauthorized is wrapped by the error returned when the
// target of a URL replacement answers 401 or 403. Replaced requests never send
// credentials, so logging in to the original registry does not help. It does
// not wrap ErrUnauthorized.
var ErrURLReplacementUnauthorized = errors.New("URL replacement target refused access; dbc does not send credentials to URL replacement targets")
// urlRule is a validated URLReplacement.
type urlRule struct {
origin redirectOrigin
// path is the escaped prefix path with unreserved escapes normalized and
// without a trailing slash.
path string
// scheme, host and replPath describe the replacement; replPath is the
// escaped path without a trailing slash.
scheme string
host string
replPath string
}
// urlRouter applies URL replacement rules. It performs no I/O.
type urlRouter struct {
rules []urlRule
}
func parseReplacementURL(raw string) (*url.URL, error) {
if strings.ContainsAny(raw, "?#") {
return nil, errors.New("must not contain a query or fragment")
}
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
scheme := strings.ToLower(u.Scheme)
if scheme != "http" && scheme != "https" {
return nil, errors.New("must be an absolute http or https URL")
}
if u.Hostname() == "" {
return nil, errors.New("must include a host")
}
if u.User != nil {
return nil, errors.New("must not contain userinfo")
}
return u, nil
}
// newURLRouter validates the rules as a whole and returns a router. It returns
// a nil router when there are no rules.
func newURLRouter(rules []URLReplacement) (*urlRouter, error) {
if len(rules) == 0 {
return nil, nil
}
r := &urlRouter{rules: make([]urlRule, 0, len(rules))}
for i, rule := range rules {
p, err := parseReplacementURL(rule.Prefix)
if err != nil {
return nil, fmt.Errorf("%w: network.url_replacements[%d]: invalid prefix %q: %w", ErrInvalidURLReplacement, i, rule.Prefix, err)
}
repl, err := parseReplacementURL(rule.Replacement)
if err != nil {
return nil, fmt.Errorf("%w: network.url_replacements[%d]: invalid replacement %q: %w", ErrInvalidURLReplacement, i, rule.Replacement, err)
}
if strings.EqualFold(p.Scheme, "https") && strings.EqualFold(repl.Scheme, "http") {
return nil, fmt.Errorf("%w: network.url_replacements[%d]: an https prefix must not be replaced by an http URL", ErrInvalidURLReplacement, i)
}
r.rules = append(r.rules, urlRule{
origin: originOf(p),
path: strings.TrimRight(normalizeEscapedPath(p.EscapedPath()).path, "/"),
scheme: strings.ToLower(repl.Scheme),
host: repl.Host,
replPath: strings.TrimRight(repl.EscapedPath(), "/"),
})
}
return r, nil
}
// route returns the effective URL for u. replaced is false when no rule
// matches (or r is nil); the first matching rule wins and its result is not
// matched again. A matching URL that carries a query string is an error,
// because queries are never sent to a replacement.
func (r *urlRouter) route(u *url.URL) (eff *url.URL, replaced bool, err error) {
if r == nil {
return nil, false, nil
}
o := originOf(u)
escapedPath := u.EscapedPath()
normalized := normalizeEscapedPath(escapedPath)
p := normalized.path
for _, rule := range r.rules {
if rule.origin != o {
continue
}
if p != rule.path && !strings.HasPrefix(p, rule.path+"/") {
continue
}
if u.RawQuery != "" || u.ForceQuery {
return nil, false, fmt.Errorf("refusing to send %s%s to a URL replacement: the URL has a query string, which is never sent to a replacement", u.Host, p)
}
suffixOffset := normalized.sourceOffset(len(rule.path))
eff, err = url.Parse(rule.scheme + "://" + rule.host + rule.replPath + escapedPath[suffixOffset:])
if err != nil {
return nil, false, fmt.Errorf("failed to build replacement URL for %s%s: %w", u.Host, p, err)
}
return eff, true, nil
}
return nil, false, nil
}
// anonymousGet performs a GET for a replaced request. It sends no credentials
// and no query, never falls back to the original URL, and reports every
// failure (including non-200 statuses) naming the effective host and path.
// canonicalPath decides the Accept header, as for unreplaced requests. A
// redirect that leads back to a URL some rule replaces is refused, so a mirror
// cannot send the request to the site it stands in for.
func anonymousGet(ctx context.Context, hc *http.Client, r *urlRouter, eff *url.URL, canonicalPath string) (*http.Response, error) {
where := eff.Host + eff.EscapedPath()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, eff.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to build request for %s: %w", where, err)
}
if canonicalPath == "/index.yaml" {
req.Header.Set("Accept", "application/yaml")
}
resp, err := r.guardRedirects(hc, true).Do(req)
if err != nil {
return nil, fmt.Errorf("request to %s (URL replacement): %w", where, err)
}
switch resp.StatusCode {
case http.StatusOK:
return resp, nil
case http.StatusUnauthorized, http.StatusForbidden:
resp.Body.Close()
return nil, fmt.Errorf("request to %s (URL replacement): %s: %w", where, resp.Status, ErrURLReplacementUnauthorized)
}
resp.Body.Close()
return nil, fmt.Errorf("request to %s (URL replacement): %s", where, resp.Status)
}
// guardRedirects returns hc itself when r is nil. Otherwise it returns a copy
// of hc that keeps hc's redirect policy and additionally refuses redirects to
// URLs that a rule replaces, so that neither a mirror nor a site the user did
// not replace can send a request to an origin the user replaced. For replaced
// requests (replaced is true) it also refuses HTTPS to HTTP redirects. hc is
// not modified.
func (r *urlRouter) guardRedirects(hc *http.Client, replaced bool) *http.Client {
if r == nil {
return hc
}
check := func(req *http.Request, via []*http.Request) error {
if replaced && len(via) > 0 && strings.EqualFold(via[len(via)-1].URL.Scheme, "https") && strings.EqualFold(req.URL.Scheme, "http") {
return errors.New("refusing HTTPS to HTTP redirect for URL replacement request")
}
if _, isReplaced, err := r.route(req.URL); isReplaced || err != nil {
return fmt.Errorf("refusing redirect to %s, which a URL replacement rule replaces", req.URL.Host)
}
return nil
}
guarded := *hc
guarded.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if err := check(req, via); err != nil {
return err
}
if hc.CheckRedirect == nil {
// net/http's default policy.
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
return nil
}
if err := hc.CheckRedirect(req, via); err != nil {
// Includes http.ErrUseLastResponse, which must reach net/http
// unwrapped.
return err
}
// The callback may have changed the URL.
return check(req, via)
}
return &guarded
}
type normalizedEscapedPath struct {
path string
offsets []int
}
// normalizeEscapedPath decodes percent-encoded unreserved ASCII characters
// and uppercases retained escapes. offsets maps normalized byte boundaries
// back to the corresponding boundary in the original escaped path.
func normalizeEscapedPath(path string) normalizedEscapedPath {
var normalized strings.Builder
normalized.Grow(len(path))
offsets := make([]int, 1, len(path)+1)
offsets[0] = 0
for i := 0; i < len(path); {
start := i
var token string
if path[i] == '%' && i+2 < len(path) {
if high, ok := fromHex(path[i+1]); ok {
if low, ok := fromHex(path[i+2]); ok {
value := high<<4 | low
if isUnreserved(value) {
token = string([]byte{value})
} else {
token = strings.ToUpper(path[i : i+3])
}
i += 3
}
}
}
if token == "" {
token = path[i : i+1]
i++
}
normalized.WriteString(token)
for j := 1; j <= len(token); j++ {
if j == len(token) {
offsets = append(offsets, i)
} else {
offsets = append(offsets, start)
}
}
}
return normalizedEscapedPath{path: normalized.String(), offsets: offsets}
}
func (p normalizedEscapedPath) sourceOffset(normalizedOffset int) int {
if normalizedOffset <= 0 {
return 0
}
if normalizedOffset >= len(p.offsets) {
return p.offsets[len(p.offsets)-1]
}
return p.offsets[normalizedOffset]
}
func fromHex(b byte) (byte, bool) {
switch {
case b >= '0' && b <= '9':
return b - '0', true
case b >= 'a' && b <= 'f':
return b - 'a' + 10, true
case b >= 'A' && b <= 'F':
return b - 'A' + 10, true
default:
return 0, false
}
}
func isUnreserved(b byte) bool {
return b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || b == '-' || b == '.' || b == '_' || b == '~'
}
var defaultRouter atomic.Pointer[urlRouter]
// SetDefaultURLReplacements sets the URL replacement rules used by the
// deprecated package-level functions (GetDriverList and
// PkgInfo.DownloadPackage). Passing no rules clears them. It returns an error,
// leaving the previous rules in place, if a rule is invalid.
func SetDefaultURLReplacements(rules []URLReplacement) error {
r, err := newURLRouter(rules)
if err != nil {
return err
}
defaultRouter.Store(r)
return nil
}
// WithURLReplacements sets the URL replacement rules for the client. They
// override the rules of a global config passed to WithGlobalConfig. They
// apply independently of WithBaseURL and WithRegistries.
func WithURLReplacements(rules []URLReplacement) Option {
rulesCopy := append([]URLReplacement(nil), rules...)
return func(c *clientConfig) {
c.urlReplacements = rulesCopy
c.explicitURLReplacements = true
}
}