| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package remote |
| 17 |
|
| 18 |
import ( |
| 19 |
"encoding/json" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/url" |
| 25 |
"strings" |
| 26 |
|
| 27 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 |
"oras.land/oras-go/v2/content" |
| 29 |
"oras.land/oras-go/v2/errdef" |
| 30 |
) |
| 31 |
|
| 32 |
// defaultMaxMetadataBytes specifies the default limit on how many response |
| 33 |
// bytes are allowed in the server's response to the metadata APIs. |
| 34 |
// See also: Repository.MaxMetadataBytes |
| 35 |
var defaultMaxMetadataBytes int64 = 4 * 1024 * 1024 // 4 MiB |
| 36 |
|
| 37 |
// errNoLink is returned by parseLink() when no Link header is present. |
| 38 |
var errNoLink = errors.New("no Link header in response") |
| 39 |
|
| 40 |
// parseLink returns the URL of the response's "Link" header, if present. |
| 41 |
func parseLink(resp *http.Response) (string, error) { |
| 42 |
link := resp.Header.Get("Link") |
| 43 |
if link == "" { |
| 44 |
return "", errNoLink |
| 45 |
} |
| 46 |
if link[0] != '<' { |
| 47 |
return "", fmt.Errorf("invalid next link %q: missing '<'", link) |
| 48 |
} |
| 49 |
if i := strings.IndexByte(link, '>'); i == -1 { |
| 50 |
return "", fmt.Errorf("invalid next link %q: missing '>'", link) |
| 51 |
} else { |
| 52 |
link = link[1:i] |
| 53 |
} |
| 54 |
|
| 55 |
linkURL, err := resp.Request.URL.Parse(link) |
| 56 |
if err != nil { |
| 57 |
return "", err |
| 58 |
} |
| 59 |
// The Link header value is controlled by the (potentially malicious) |
| 60 |
// registry. Restrict pagination to the same origin as the originating |
| 61 |
// request so that a registry cannot redirect pagination to an arbitrary |
| 62 |
// host and turn a listing call into a server-side request forgery. |
| 63 |
if !isSameOrigin(resp.Request.URL, linkURL) { |
| 64 |
return "", fmt.Errorf("invalid next link %q: not the same origin as %q", link, resp.Request.URL) |
| 65 |
} |
| 66 |
return linkURL.String(), nil |
| 67 |
} |
| 68 |
|
| 69 |
// isSameOrigin reports whether the two URLs share the same origin, that is the |
| 70 |
// same scheme, host, and port (with the default port applied for http/https). |
| 71 |
func isSameOrigin(a, b *url.URL) bool { |
| 72 |
if !strings.EqualFold(a.Scheme, b.Scheme) { |
| 73 |
return false |
| 74 |
} |
| 75 |
return canonicalHostPort(a) == canonicalHostPort(b) |
| 76 |
} |
| 77 |
|
| 78 |
// canonicalHostPort returns the lower-cased "host:port" of u, filling in the |
| 79 |
// default port for the http and https schemes when none is present. |
| 80 |
func canonicalHostPort(u *url.URL) string { |
| 81 |
port := u.Port() |
| 82 |
if port == "" { |
| 83 |
switch strings.ToLower(u.Scheme) { |
| 84 |
case "https": |
| 85 |
port = "443" |
| 86 |
case "http": |
| 87 |
port = "80" |
| 88 |
} |
| 89 |
} |
| 90 |
return strings.ToLower(u.Hostname()) + ":" + port |
| 91 |
} |
| 92 |
|
| 93 |
// limitReader returns a Reader that reads from r but stops with EOF after n |
| 94 |
// bytes. If n is less than or equal to zero, defaultMaxMetadataBytes is used. |
| 95 |
func limitReader(r io.Reader, n int64) io.Reader { |
| 96 |
if n <= 0 { |
| 97 |
n = defaultMaxMetadataBytes |
| 98 |
} |
| 99 |
return io.LimitReader(r, n) |
| 100 |
} |
| 101 |
|
| 102 |
// limitSize returns ErrSizeExceedsLimit if the size of desc exceeds the limit n. |
| 103 |
// If n is less than or equal to zero, defaultMaxMetadataBytes is used. |
| 104 |
func limitSize(desc ocispec.Descriptor, n int64) error { |
| 105 |
if n <= 0 { |
| 106 |
n = defaultMaxMetadataBytes |
| 107 |
} |
| 108 |
if desc.Size > n { |
| 109 |
return fmt.Errorf( |
| 110 |
"content size %v exceeds MaxMetadataBytes %v: %w", |
| 111 |
desc.Size, |
| 112 |
n, |
| 113 |
errdef.ErrSizeExceedsLimit) |
| 114 |
} |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
// decodeJSON safely reads the JSON content described by desc, and |
| 119 |
// decodes it into v. |
| 120 |
func decodeJSON(r io.Reader, desc ocispec.Descriptor, v any) error { |
| 121 |
jsonBytes, err := content.ReadAll(r, desc) |
| 122 |
if err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
return json.Unmarshal(jsonBytes, v) |
| 126 |
} |